--with-gnu-ld uses different x- fiile under aix 4.1
[official-gcc.git] / gcc / cpplib.c
blobaa343f51d043ede49f0270d1676f0ba77da8500a
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
24 #ifndef STDC_VALUE
25 #define STDC_VALUE 1
26 #endif
28 #include <signal.h>
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
32 #endif
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
36 #endif
38 #include "cpplib.h"
39 #include "cpphash.h"
40 #include "output.h"
41 #include "prefix.h"
43 #ifndef GET_ENV_PATH_LIST
44 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
45 #endif
47 /* By default, colon separates directories in a path. */
48 #ifndef PATH_SEPARATOR
49 #define PATH_SEPARATOR ':'
50 #endif
52 #ifndef STANDARD_INCLUDE_DIR
53 #define STANDARD_INCLUDE_DIR "/usr/include"
54 #endif
56 /* Symbols to predefine. */
58 #ifdef CPP_PREDEFINES
59 static char *predefs = CPP_PREDEFINES;
60 #else
61 static char *predefs = "";
62 #endif
64 /* We let tm.h override the types used here, to handle trivial differences
65 such as the choice of unsigned int or long unsigned int for size_t.
66 When machines start needing nontrivial differences in the size type,
67 it would be best to do something here to figure out automatically
68 from other information what type to use. */
70 /* The string value for __SIZE_TYPE__. */
72 #ifndef SIZE_TYPE
73 #define SIZE_TYPE "long unsigned int"
74 #endif
76 /* The string value for __PTRDIFF_TYPE__. */
78 #ifndef PTRDIFF_TYPE
79 #define PTRDIFF_TYPE "long int"
80 #endif
82 /* The string value for __WCHAR_TYPE__. */
84 #ifndef WCHAR_TYPE
85 #define WCHAR_TYPE "int"
86 #endif
87 #define CPP_WCHAR_TYPE(PFILE) \
88 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
90 /* The string value for __USER_LABEL_PREFIX__ */
92 #ifndef USER_LABEL_PREFIX
93 #define USER_LABEL_PREFIX ""
94 #endif
96 /* The string value for __REGISTER_PREFIX__ */
98 #ifndef REGISTER_PREFIX
99 #define REGISTER_PREFIX ""
100 #endif
102 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
103 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
105 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
106 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
107 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
108 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
109 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
110 (Note that it is false while we're expanding marco *arguments*.) */
111 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
113 /* Move all backslash-newline pairs out of embarrassing places.
114 Exchange all such pairs following BP
115 with any potentially-embarrassing characters that follow them.
116 Potentially-embarrassing characters are / and *
117 (because a backslash-newline inside a comment delimiter
118 would cause it not to be recognized). */
120 #define NEWLINE_FIX \
121 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
123 /* Same, but assume we've already read the potential '\\' into C. */
124 #define NEWLINE_FIX1(C) do { \
125 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
126 } while(0)
128 struct cpp_pending {
129 struct cpp_pending *next;
130 char *cmd;
131 char *arg;
134 /* Forward declarations. */
136 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
138 static char *my_strerror PROTO ((int));
139 static void make_assertion PROTO ((cpp_reader *, char *, U_CHAR *));
140 static void path_include PROTO ((cpp_reader *, char *));
141 static void initialize_builtins PROTO ((cpp_reader *));
142 static void initialize_char_syntax PROTO ((void));
143 #if 0
144 static void trigraph_pcp ();
145 #endif
146 static void validate_else PROTO ((cpp_reader *, char *));
147 static int comp_def_part PROTO ((int, U_CHAR *, int, U_CHAR *,
148 int, int));
149 #ifdef abort
150 extern void fancy_abort ();
151 #endif
152 static int check_macro_name PROTO ((cpp_reader *, U_CHAR *, char *));
153 static int compare_defs PROTO ((cpp_reader *,
154 DEFINITION *, DEFINITION *));
155 static HOST_WIDE_INT eval_if_expression PROTO ((cpp_reader *));
156 static int change_newlines PROTO ((U_CHAR *, int));
157 static void push_macro_expansion PARAMS ((cpp_reader *,
158 U_CHAR *, int, HASHNODE *));
159 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
161 static void conditional_skip PROTO ((cpp_reader *, int,
162 enum node_type, U_CHAR *));
163 static void skip_if_group PROTO ((cpp_reader *));
164 static int parse_name PARAMS ((cpp_reader *, int));
165 static void print_help PROTO ((void));
167 /* Last arg to output_line_command. */
168 enum file_change_code {same_file, enter_file, leave_file};
170 /* External declarations. */
172 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
174 extern char *version_string;
175 extern struct tm *localtime ();
178 /* #include "file" looks in source file dir, then stack. */
179 /* #include <file> just looks in the stack. */
180 /* -I directories are added to the end, then the defaults are added. */
181 /* The */
182 static struct default_include {
183 char *fname; /* The name of the directory. */
184 char *component; /* The component containing the directory */
185 int cplusplus; /* Only look here if we're compiling C++. */
186 int cxx_aware; /* Includes in this directory don't need to
187 be wrapped in extern "C" when compiling
188 C++. */
189 } include_defaults_array[]
190 #ifdef INCLUDE_DEFAULTS
191 = INCLUDE_DEFAULTS;
192 #else
194 /* Pick up GNU C++ specific include files. */
195 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
196 #ifdef CROSS_COMPILE
197 /* This is the dir for fixincludes. Put it just before
198 the files that we fix. */
199 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
200 /* For cross-compilation, this dir name is generated
201 automatically in Makefile.in. */
202 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
203 #ifdef TOOL_INCLUDE_DIR
204 /* This is another place that the target system's headers might be. */
205 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
206 #endif
207 #else /* not CROSS_COMPILE */
208 #ifdef LOCAL_INCLUDE_DIR
209 /* This should be /usr/local/include and should come before
210 the fixincludes-fixed header files. */
211 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
212 #endif
213 #ifdef TOOL_INCLUDE_DIR
214 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
215 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
216 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
217 #endif
218 /* This is the dir for fixincludes. Put it just before
219 the files that we fix. */
220 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
221 /* Some systems have an extra dir of include files. */
222 #ifdef SYSTEM_INCLUDE_DIR
223 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
224 #endif
225 #ifndef STANDARD_INCLUDE_COMPONENT
226 #define STANDARD_INCLUDE_COMPONENT 0
227 #endif
228 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
229 #endif /* not CROSS_COMPILE */
230 { 0, 0, 0, 0 }
232 #endif /* no INCLUDE_DEFAULTS */
234 /* `struct directive' defines one #-directive, including how to handle it. */
236 struct directive {
237 int length; /* Length of name */
238 int (*func) /* Function to handle directive */
239 PARAMS ((cpp_reader *, struct directive *));
240 char *name; /* Name of directive */
241 enum node_type type; /* Code which describes which directive. */
244 /* These functions are declared to return int instead of void since they
245 are going to be placed in a table and some old compilers have trouble with
246 pointers to functions returning void. */
248 static int do_define PARAMS ((cpp_reader *, struct directive *));
249 static int do_line PARAMS ((cpp_reader *, struct directive *));
250 static int do_include PARAMS ((cpp_reader *, struct directive *));
251 static int do_undef PARAMS ((cpp_reader *, struct directive *));
252 static int do_error PARAMS ((cpp_reader *, struct directive *));
253 static int do_pragma PARAMS ((cpp_reader *, struct directive *));
254 static int do_ident PARAMS ((cpp_reader *, struct directive *));
255 static int do_if PARAMS ((cpp_reader *, struct directive *));
256 static int do_xifdef PARAMS ((cpp_reader *, struct directive *));
257 static int do_else PARAMS ((cpp_reader *, struct directive *));
258 static int do_elif PARAMS ((cpp_reader *, struct directive *));
259 static int do_endif PARAMS ((cpp_reader *, struct directive *));
260 #ifdef SCCS_DIRECTIVE
261 static int do_sccs PARAMS ((cpp_reader *, struct directive *));
262 #endif
263 static int do_assert PARAMS ((cpp_reader *, struct directive *));
264 static int do_unassert PARAMS ((cpp_reader *, struct directive *));
265 static int do_warning PARAMS ((cpp_reader *, struct directive *));
267 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
268 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
270 /* Here is the actual list of #-directives, most-often-used first.
271 The initialize_builtins function assumes #define is the very first. */
273 static struct directive directive_table[] = {
274 { 6, do_define, "define", T_DEFINE },
275 { 5, do_xifdef, "ifdef", T_IFDEF },
276 { 6, do_xifdef, "ifndef", T_IFNDEF },
277 { 7, do_include, "include", T_INCLUDE },
278 { 12, do_include, "include_next", T_INCLUDE_NEXT },
279 { 6, do_include, "import", T_IMPORT },
280 { 5, do_endif, "endif", T_ENDIF },
281 { 4, do_else, "else", T_ELSE },
282 { 2, do_if, "if", T_IF },
283 { 4, do_elif, "elif", T_ELIF },
284 { 5, do_undef, "undef", T_UNDEF },
285 { 5, do_error, "error", T_ERROR },
286 { 7, do_warning, "warning", T_WARNING },
287 { 6, do_pragma, "pragma", T_PRAGMA },
288 { 4, do_line, "line", T_LINE },
289 { 5, do_ident, "ident", T_IDENT },
290 #ifdef SCCS_DIRECTIVE
291 { 4, do_sccs, "sccs", T_SCCS },
292 #endif
293 { 6, do_assert, "assert", T_ASSERT },
294 { 8, do_unassert, "unassert", T_UNASSERT },
295 { -1, 0, "", T_UNUSED }
298 /* table to tell if char can be part of a C identifier. */
299 U_CHAR is_idchar[256] = { 0 };
300 /* table to tell if char can be first char of a c identifier. */
301 U_CHAR is_idstart[256] = { 0 };
302 /* table to tell if c is horizontal space. */
303 U_CHAR is_hor_space[256] = { 0 };
304 /* table to tell if c is horizontal or vertical space. */
305 U_CHAR is_space[256] = { 0 };
307 /* Initialize syntactic classifications of characters. */
308 static void
309 initialize_char_syntax ()
311 register int i;
314 * Set up is_idchar and is_idstart tables. These should be
315 * faster than saying (is_alpha (c) || c == '_'), etc.
316 * Set up these things before calling any routines tthat
317 * refer to them.
318 * XXX We should setlocale(LC_CTYPE, "C") here for safety.
320 for (i = 0; i < 256; i++)
322 is_idchar[i] = ISALNUM (i);
323 is_idstart[i] = ISALPHA (i);
326 is_idchar['_'] = 1;
327 is_idstart['_'] = 1;
329 /* These will be reset later if -$ is in effect. */
330 is_idchar['$'] = 1;
331 is_idstart['$'] = 1;
333 /* horizontal space table */
334 is_hor_space[' '] = 1;
335 is_hor_space['\t'] = 1;
336 is_hor_space['\v'] = 1;
337 is_hor_space['\f'] = 1;
338 is_hor_space['\r'] = 1;
340 is_space[' '] = 1;
341 is_space['\t'] = 1;
342 is_space['\v'] = 1;
343 is_space['\f'] = 1;
344 is_space['\n'] = 1;
345 is_space['\r'] = 1;
349 /* Place into PFILE a quoted string representing the string SRC.
350 Caller must reserve enough space in pfile->token_buffer. */
352 static void
353 quote_string (pfile, src)
354 cpp_reader *pfile;
355 char *src;
357 U_CHAR c;
359 CPP_PUTC_Q (pfile, '\"');
360 for (;;)
361 switch ((c = *src++))
363 default:
364 if (ISPRINT (c))
365 CPP_PUTC_Q (pfile, c);
366 else
368 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
369 CPP_ADJUST_WRITTEN (pfile, 4);
371 break;
373 case '\"':
374 case '\\':
375 CPP_PUTC_Q (pfile, '\\');
376 CPP_PUTC_Q (pfile, c);
377 break;
379 case '\0':
380 CPP_PUTC_Q (pfile, '\"');
381 CPP_NUL_TERMINATE_Q (pfile);
382 return;
386 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
388 void
389 cpp_grow_buffer (pfile, n)
390 cpp_reader *pfile;
391 long n;
393 long old_written = CPP_WRITTEN (pfile);
394 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
395 pfile->token_buffer = (U_CHAR *)
396 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
397 CPP_SET_WRITTEN (pfile, old_written);
402 * process a given definition string, for initialization
403 * If STR is just an identifier, define it with value 1.
404 * If STR has anything after the identifier, then it should
405 * be identifier=definition.
408 void
409 cpp_define (pfile, str)
410 cpp_reader *pfile;
411 U_CHAR *str;
413 U_CHAR *buf, *p;
415 buf = str;
416 p = str;
417 if (!is_idstart[*p])
419 cpp_error (pfile, "malformed option `-D %s'", str);
420 return;
422 while (is_idchar[*++p])
424 if (*p == '(') {
425 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
427 if (*p++ != ')')
428 p = (U_CHAR *) str; /* Error */
430 if (*p == 0)
432 buf = (U_CHAR *) alloca (p - buf + 4);
433 strcpy ((char *)buf, str);
434 strcat ((char *)buf, " 1");
436 else if (*p != '=')
438 cpp_error (pfile, "malformed option `-D %s'", str);
439 return;
441 else
443 U_CHAR *q;
444 /* Copy the entire option so we can modify it. */
445 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
446 strncpy (buf, str, p - str);
447 /* Change the = to a space. */
448 buf[p - str] = ' ';
449 /* Scan for any backslash-newline and remove it. */
450 p++;
451 q = &buf[p - str];
452 while (*p)
454 if (*p == '\\' && p[1] == '\n')
455 p += 2;
456 else
457 *q++ = *p++;
459 *q = 0;
462 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
464 do_define (pfile, NULL);
465 cpp_pop_buffer (pfile);
469 /* Process the string STR as if it appeared as the body of a #assert.
470 OPTION is the option name for which STR was the argument. */
472 static void
473 make_assertion (pfile, option, str)
474 cpp_reader *pfile;
475 char *option;
476 U_CHAR *str;
478 U_CHAR *buf, *p, *q;
480 /* Copy the entire option so we can modify it. */
481 buf = (U_CHAR *) alloca (strlen (str) + 1);
482 strcpy ((char *) buf, str);
483 /* Scan for any backslash-newline and remove it. */
484 p = q = buf;
485 while (*p) {
486 #if 0
487 if (*p == '\\' && p[1] == '\n')
488 p += 2;
489 else
490 #endif
491 *q++ = *p++;
493 *q = 0;
495 p = buf;
496 if (!is_idstart[*p]) {
497 cpp_error (pfile, "malformed option `%s %s'", option, str);
498 return;
500 while (is_idchar[*++p])
502 while (*p == ' ' || *p == '\t') p++;
503 if (! (*p == 0 || *p == '(')) {
504 cpp_error (pfile, "malformed option `%s %s'", option, str);
505 return;
508 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
510 do_assert (pfile, NULL);
511 cpp_pop_buffer (pfile);
515 /* Given a colon-separated list of file names PATH,
516 add all the names to the search path for include files. */
518 static void
519 path_include (pfile, path)
520 cpp_reader *pfile;
521 char *path;
523 char *p;
525 p = path;
527 if (*p)
528 while (1) {
529 char *q = p;
530 char *name;
532 /* Find the end of this name. */
533 while (*q != 0 && *q != PATH_SEPARATOR) q++;
534 if (p == q) {
535 /* An empty name in the path stands for the current directory. */
536 name = (char *) xmalloc (2);
537 name[0] = '.';
538 name[1] = 0;
539 } else {
540 /* Otherwise use the directory that is named. */
541 name = (char *) xmalloc (q - p + 1);
542 bcopy (p, name, q - p);
543 name[q - p] = 0;
546 append_include_chain (pfile,
547 &(CPP_OPTIONS (pfile)->bracket_include), name, 0);
549 /* Advance past this name. */
550 p = q;
551 if (*p == 0)
552 break;
553 /* Skip the colon. */
554 p++;
558 void
559 cpp_options_init (opts)
560 cpp_options *opts;
562 bzero ((char *) opts, sizeof *opts);
563 opts->in_fname = NULL;
564 opts->out_fname = NULL;
566 opts->dollars_in_ident = 1;
567 initialize_char_syntax ();
569 opts->no_line_commands = 0;
570 opts->no_trigraphs = 1;
571 opts->put_out_comments = 0;
572 opts->print_include_names = 0;
573 opts->dump_macros = dump_none;
574 opts->no_output = 0;
575 opts->remap = 0;
576 opts->cplusplus = 0;
577 opts->cplusplus_comments = 1;
579 opts->verbose = 0;
580 opts->objc = 0;
581 opts->lang_asm = 0;
582 opts->for_lint = 0;
583 opts->chill = 0;
584 opts->pedantic_errors = 0;
585 opts->inhibit_warnings = 0;
586 opts->warn_comments = 0;
587 opts->warn_import = 1;
588 opts->warnings_are_errors = 0;
591 enum cpp_token
592 null_underflow (pfile)
593 cpp_reader *pfile ATTRIBUTE_UNUSED;
595 return CPP_EOF;
599 null_cleanup (pbuf, pfile)
600 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
601 cpp_reader *pfile ATTRIBUTE_UNUSED;
603 return 0;
607 macro_cleanup (pbuf, pfile)
608 cpp_buffer *pbuf;
609 cpp_reader *pfile ATTRIBUTE_UNUSED;
611 HASHNODE *macro = (HASHNODE *) pbuf->data;
612 if (macro->type == T_DISABLED)
613 macro->type = T_MACRO;
614 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
615 free (pbuf->buf);
616 return 0;
619 /* Assuming we have read '/'.
620 If this is the start of a comment (followed by '*' or '/'),
621 skip to the end of the comment, and return ' '.
622 Return EOF if we reached the end of file before the end of the comment.
623 If not the start of a comment, return '/'. */
625 static int
626 skip_comment (pfile, linep)
627 cpp_reader *pfile;
628 long *linep;
630 int c = 0;
631 while (PEEKC() == '\\' && PEEKN(1) == '\n')
633 if (linep)
634 (*linep)++;
635 FORWARD(2);
637 if (PEEKC() == '*')
639 FORWARD(1);
640 for (;;)
642 int prev_c = c;
643 c = GETC ();
644 if (c == EOF)
645 return EOF;
646 while (c == '\\' && PEEKC() == '\n')
648 if (linep)
649 (*linep)++;
650 FORWARD(1), c = GETC();
652 if (prev_c == '*' && c == '/')
653 return ' ';
654 if (c == '\n' && linep)
655 (*linep)++;
658 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
660 FORWARD(1);
661 for (;;)
663 c = GETC ();
664 if (c == EOF)
665 return ' '; /* Allow // to be terminated by EOF. */
666 while (c == '\\' && PEEKC() == '\n')
668 FORWARD(1);
669 c = GETC();
670 if (linep)
671 (*linep)++;
673 if (c == '\n')
675 /* Don't consider final '\n' to be part of comment. */
676 FORWARD(-1);
677 return ' ';
681 else
682 return '/';
685 /* Skip whitespace \-newline and comments. Does not macro-expand. */
687 void
688 cpp_skip_hspace (pfile)
689 cpp_reader *pfile;
691 while (1)
693 int c = PEEKC();
694 if (c == EOF)
695 return; /* FIXME */
696 if (is_hor_space[c])
698 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
699 cpp_pedwarn (pfile, "%s in preprocessing directive",
700 c == '\f' ? "formfeed" : "vertical tab");
701 FORWARD(1);
703 else if (c == '/')
705 FORWARD (1);
706 c = skip_comment (pfile, NULL);
707 if (c == '/')
708 FORWARD(-1);
709 if (c == EOF || c == '/')
710 return;
712 else if (c == '\\' && PEEKN(1) == '\n') {
713 FORWARD(2);
715 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
716 && is_hor_space[PEEKN(1)])
717 FORWARD(2);
718 else return;
722 /* Read the rest of the current line.
723 The line is appended to PFILE's output buffer. */
725 static void
726 copy_rest_of_line (pfile)
727 cpp_reader *pfile;
729 struct cpp_options *opts = CPP_OPTIONS (pfile);
730 for (;;)
732 int c = GETC();
733 int nextc;
734 switch (c)
736 case EOF:
737 goto end_directive;
738 case '\\':
739 if (PEEKC() == '\n')
741 FORWARD (1);
742 continue;
744 case '\'':
745 case '\"':
746 goto scan_directive_token;
747 break;
748 case '/':
749 nextc = PEEKC();
750 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
751 goto scan_directive_token;
752 break;
753 case '\f':
754 case '\v':
755 if (CPP_PEDANTIC (pfile))
756 cpp_pedwarn (pfile, "%s in preprocessing directive",
757 c == '\f' ? "formfeed" : "vertical tab");
758 break;
760 case '\n':
761 FORWARD(-1);
762 goto end_directive;
763 scan_directive_token:
764 FORWARD(-1);
765 cpp_get_token (pfile);
766 continue;
768 CPP_PUTC (pfile, c);
770 end_directive: ;
771 CPP_NUL_TERMINATE (pfile);
774 void
775 skip_rest_of_line (pfile)
776 cpp_reader *pfile;
778 long old = CPP_WRITTEN (pfile);
779 copy_rest_of_line (pfile);
780 CPP_SET_WRITTEN (pfile, old);
783 /* Handle a possible # directive.
784 '#' has already been read. */
787 handle_directive (pfile)
788 cpp_reader *pfile;
789 { int c;
790 register struct directive *kt;
791 int ident_length;
792 U_CHAR *ident;
793 long old_written = CPP_WRITTEN (pfile);
795 cpp_skip_hspace (pfile);
797 c = PEEKC ();
798 if (c >= '0' && c <= '9')
800 /* Handle # followed by a line number. */
801 if (CPP_PEDANTIC (pfile))
802 cpp_pedwarn (pfile, "`#' followed by integer");
803 do_line (pfile, NULL);
804 goto done_a_directive;
807 /* Now find the directive name. */
808 CPP_PUTC (pfile, '#');
809 parse_name (pfile, GETC());
810 ident = pfile->token_buffer + old_written + 1;
811 ident_length = CPP_PWRITTEN (pfile) - ident;
812 if (ident_length == 0 && PEEKC() == '\n')
814 /* A line of just `#' becomes blank. */
815 goto done_a_directive;
818 #if 0
819 if (ident_length == 0 || !is_idstart[*ident]) {
820 U_CHAR *p = ident;
821 while (is_idchar[*p]) {
822 if (*p < '0' || *p > '9')
823 break;
824 p++;
826 /* Avoid error for `###' and similar cases unless -pedantic. */
827 if (p == ident) {
828 while (*p == '#' || is_hor_space[*p]) p++;
829 if (*p == '\n') {
830 if (pedantic && !lang_asm)
831 cpp_warning (pfile, "invalid preprocessor directive");
832 return 0;
836 if (!lang_asm)
837 cpp_error (pfile, "invalid preprocessor directive name");
839 return 0;
841 #endif
843 * Decode the keyword and call the appropriate expansion
844 * routine, after moving the input pointer up to the next line.
846 for (kt = directive_table; ; kt++) {
847 if (kt->length <= 0)
848 goto not_a_directive;
849 if (kt->length == ident_length
850 && !strncmp (kt->name, ident, ident_length))
851 break;
854 /* We may want to pass through #define, #pragma, and #include.
855 Other directives may create output, but we don't want the directive
856 itself out, so we pop it now. For example conditionals may emit
857 #failed ... #endfailed stuff. */
859 if (! (kt->type == T_DEFINE
860 || kt->type == T_PRAGMA
861 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
862 && CPP_OPTIONS (pfile)->dump_includes)))
863 CPP_SET_WRITTEN (pfile, old_written);
865 (*kt->func) (pfile, kt);
867 if (kt->type == T_DEFINE)
869 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
871 /* Skip "#define". */
872 U_CHAR *p = pfile->token_buffer + old_written + 7;
874 SKIP_WHITE_SPACE (p);
875 while (is_idchar[*p]) p++;
876 pfile->limit = p;
877 CPP_PUTC (pfile, '\n');
879 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
880 CPP_SET_WRITTEN (pfile, old_written);
883 done_a_directive:
884 return 1;
886 not_a_directive:
887 return 0;
890 /* Pass a directive through to the output file.
891 BUF points to the contents of the directive, as a contiguous string.
892 m LIMIT points to the first character past the end of the directive.
893 KEYWORD is the keyword-table entry for the directive. */
895 static void
896 pass_thru_directive (buf, limit, pfile, keyword)
897 U_CHAR *buf, *limit;
898 cpp_reader *pfile;
899 struct directive *keyword;
901 register unsigned keyword_length = keyword->length;
903 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
904 CPP_PUTC_Q (pfile, '#');
905 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
906 if (limit != buf && buf[0] != ' ')
907 CPP_PUTC_Q (pfile, ' ');
908 CPP_PUTS_Q (pfile, buf, limit - buf);
909 #if 0
910 CPP_PUTS_Q (pfile, '\n');
911 /* Count the line we have just made in the output,
912 to get in sync properly. */
913 pfile->lineno++;
914 #endif
917 /* The arglist structure is built by do_define to tell
918 collect_definition where the argument names begin. That
919 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
920 would contain pointers to the strings x, y, and z.
921 Collect_definition would then build a DEFINITION node,
922 with reflist nodes pointing to the places x, y, and z had
923 appeared. So the arglist is just convenience data passed
924 between these two routines. It is not kept around after
925 the current #define has been processed and entered into the
926 hash table. */
928 struct arglist {
929 struct arglist *next;
930 U_CHAR *name;
931 int length;
932 int argno;
933 char rest_args;
936 /* Read a replacement list for a macro with parameters.
937 Build the DEFINITION structure.
938 Reads characters of text starting at BUF until END.
939 ARGLIST specifies the formal parameters to look for
940 in the text of the definition; NARGS is the number of args
941 in that list, or -1 for a macro name that wants no argument list.
942 MACRONAME is the macro name itself (so we can avoid recursive expansion)
943 and NAMELEN is its length in characters.
945 Note that comments, backslash-newlines, and leading white space
946 have already been deleted from the argument. */
948 static DEFINITION *
949 collect_expansion (pfile, buf, limit, nargs, arglist)
950 cpp_reader *pfile;
951 U_CHAR *buf, *limit;
952 int nargs;
953 struct arglist *arglist;
955 DEFINITION *defn;
956 register U_CHAR *p, *lastp, *exp_p;
957 struct reflist *endpat = NULL;
958 /* Pointer to first nonspace after last ## seen. */
959 U_CHAR *concat = 0;
960 /* Pointer to first nonspace after last single-# seen. */
961 U_CHAR *stringify = 0;
962 int maxsize;
963 int expected_delimiter = '\0';
965 /* Scan thru the replacement list, ignoring comments and quoted
966 strings, picking up on the macro calls. It does a linear search
967 thru the arg list on every potential symbol. Profiling might say
968 that something smarter should happen. */
970 if (limit < buf)
971 abort ();
973 /* Find the beginning of the trailing whitespace. */
974 p = buf;
975 while (p < limit && is_space[limit[-1]]) limit--;
977 /* Allocate space for the text in the macro definition.
978 Leading and trailing whitespace chars need 2 bytes each.
979 Each other input char may or may not need 1 byte,
980 so this is an upper bound. The extra 5 are for invented
981 leading and trailing newline-marker and final null. */
982 maxsize = (sizeof (DEFINITION)
983 + (limit - p) + 5);
984 /* Occurrences of '@' get doubled, so allocate extra space for them. */
985 while (p < limit)
986 if (*p++ == '@')
987 maxsize++;
988 defn = (DEFINITION *) xcalloc (1, maxsize);
990 defn->nargs = nargs;
991 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
992 lastp = exp_p;
994 p = buf;
996 /* Add one initial space escape-marker to prevent accidental
997 token-pasting (often removed by macroexpand). */
998 *exp_p++ = '@';
999 *exp_p++ = ' ';
1001 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1002 cpp_error (pfile, "`##' at start of macro definition");
1003 p += 2;
1006 /* Process the main body of the definition. */
1007 while (p < limit) {
1008 int skipped_arg = 0;
1009 register U_CHAR c = *p++;
1011 *exp_p++ = c;
1013 if (!CPP_TRADITIONAL (pfile)) {
1014 switch (c) {
1015 case '\'':
1016 case '\"':
1017 if (expected_delimiter != '\0') {
1018 if (c == expected_delimiter)
1019 expected_delimiter = '\0';
1020 } else
1021 expected_delimiter = c;
1022 break;
1024 case '\\':
1025 if (p < limit && expected_delimiter) {
1026 /* In a string, backslash goes through
1027 and makes next char ordinary. */
1028 *exp_p++ = *p++;
1030 break;
1032 case '@':
1033 /* An '@' in a string or character constant stands for itself,
1034 and does not need to be escaped. */
1035 if (!expected_delimiter)
1036 *exp_p++ = c;
1037 break;
1039 case '#':
1040 /* # is ordinary inside a string. */
1041 if (expected_delimiter)
1042 break;
1043 if (p < limit && *p == '#') {
1044 /* ##: concatenate preceding and following tokens. */
1045 /* Take out the first #, discard preceding whitespace. */
1046 exp_p--;
1047 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1048 --exp_p;
1049 /* Skip the second #. */
1050 p++;
1051 /* Discard following whitespace. */
1052 SKIP_WHITE_SPACE (p);
1053 concat = p;
1054 if (p == limit)
1055 cpp_error (pfile, "`##' at end of macro definition");
1056 } else if (nargs >= 0) {
1057 /* Single #: stringify following argument ref.
1058 Don't leave the # in the expansion. */
1059 exp_p--;
1060 SKIP_WHITE_SPACE (p);
1061 if (p == limit || ! is_idstart[*p]
1062 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1063 cpp_error (pfile,
1064 "`#' operator is not followed by a macro argument name");
1065 else
1066 stringify = p;
1068 break;
1070 } else {
1071 /* In -traditional mode, recognize arguments inside strings and
1072 character constants, and ignore special properties of #.
1073 Arguments inside strings are considered "stringified", but no
1074 extra quote marks are supplied. */
1075 switch (c) {
1076 case '\'':
1077 case '\"':
1078 if (expected_delimiter != '\0') {
1079 if (c == expected_delimiter)
1080 expected_delimiter = '\0';
1081 } else
1082 expected_delimiter = c;
1083 break;
1085 case '\\':
1086 /* Backslash quotes delimiters and itself, but not macro args. */
1087 if (expected_delimiter != 0 && p < limit
1088 && (*p == expected_delimiter || *p == '\\')) {
1089 *exp_p++ = *p++;
1090 continue;
1092 break;
1094 case '/':
1095 if (expected_delimiter != '\0') /* No comments inside strings. */
1096 break;
1097 if (*p == '*') {
1098 /* If we find a comment that wasn't removed by handle_directive,
1099 this must be -traditional. So replace the comment with
1100 nothing at all. */
1101 exp_p--;
1102 p += 1;
1103 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1104 p++;
1105 #if 0
1106 /* Mark this as a concatenation-point, as if it had been ##. */
1107 concat = p;
1108 #endif
1110 break;
1114 /* Handle the start of a symbol. */
1115 if (is_idchar[c] && nargs > 0) {
1116 U_CHAR *id_beg = p - 1;
1117 int id_len;
1119 --exp_p;
1120 while (p != limit && is_idchar[*p]) p++;
1121 id_len = p - id_beg;
1123 if (is_idstart[c]
1124 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1125 register struct arglist *arg;
1127 for (arg = arglist; arg != NULL; arg = arg->next) {
1128 struct reflist *tpat;
1130 if (arg->name[0] == c
1131 && arg->length == id_len
1132 && strncmp (arg->name, id_beg, id_len) == 0) {
1133 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1134 if (CPP_TRADITIONAL (pfile)) {
1135 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1136 id_len, arg->name);
1137 } else {
1138 cpp_warning (pfile,
1139 "macro arg `%.*s' would be stringified with -traditional.",
1140 id_len, arg->name);
1143 /* If ANSI, don't actually substitute inside a string. */
1144 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1145 break;
1146 /* make a pat node for this arg and append it to the end of
1147 the pat list */
1148 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1149 tpat->next = NULL;
1150 tpat->raw_before = concat == id_beg;
1151 tpat->raw_after = 0;
1152 tpat->rest_args = arg->rest_args;
1153 tpat->stringify = (CPP_TRADITIONAL (pfile)
1154 ? expected_delimiter != '\0'
1155 : stringify == id_beg);
1157 if (endpat == NULL)
1158 defn->pattern = tpat;
1159 else
1160 endpat->next = tpat;
1161 endpat = tpat;
1163 tpat->argno = arg->argno;
1164 tpat->nchars = exp_p - lastp;
1166 register U_CHAR *p1 = p;
1167 SKIP_WHITE_SPACE (p1);
1168 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1169 tpat->raw_after = 1;
1171 lastp = exp_p; /* place to start copying from next time */
1172 skipped_arg = 1;
1173 break;
1178 /* If this was not a macro arg, copy it into the expansion. */
1179 if (! skipped_arg) {
1180 register U_CHAR *lim1 = p;
1181 p = id_beg;
1182 while (p != lim1)
1183 *exp_p++ = *p++;
1184 if (stringify == id_beg)
1185 cpp_error (pfile,
1186 "`#' operator should be followed by a macro argument name");
1191 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1193 /* If ANSI, put in a "@ " marker to prevent token pasting.
1194 But not if "inside a string" (which in ANSI mode
1195 happens only for -D option). */
1196 *exp_p++ = '@';
1197 *exp_p++ = ' ';
1200 *exp_p = '\0';
1202 defn->length = exp_p - defn->expansion;
1204 /* Crash now if we overrun the allocated size. */
1205 if (defn->length + 1 > maxsize)
1206 abort ();
1208 #if 0
1209 /* This isn't worth the time it takes. */
1210 /* give back excess storage */
1211 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1212 #endif
1214 return defn;
1218 * special extension string that can be added to the last macro argument to
1219 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1220 * #define wow(a, b...) process (b, a, b)
1221 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1222 * { wow (one, two); } -> { process (two, one, two); }
1223 * if this "rest_arg" is used with the concat token '##' and if it is not
1224 * supplied then the token attached to with ## will not be outputted. Ex:
1225 * #define wow (a, b...) process (b ## , a, ## b)
1226 * { wow (1, 2); } -> { process (2, 1, 2); }
1227 * { wow (one); } -> { process (one); {
1229 static char rest_extension[] = "...";
1230 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1232 /* Create a DEFINITION node from a #define directive. Arguments are
1233 as for do_define. */
1235 static MACRODEF
1236 create_definition (buf, limit, pfile, predefinition)
1237 U_CHAR *buf, *limit;
1238 cpp_reader *pfile;
1239 int predefinition;
1241 U_CHAR *bp; /* temp ptr into input buffer */
1242 U_CHAR *symname; /* remember where symbol name starts */
1243 int sym_length; /* and how long it is */
1244 int rest_args = 0;
1245 long line, col;
1246 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1247 DEFINITION *defn;
1248 int arglengths = 0; /* Accumulate lengths of arg names
1249 plus number of args. */
1250 MACRODEF mdef;
1251 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1253 bp = buf;
1255 while (is_hor_space[*bp])
1256 bp++;
1258 symname = bp; /* remember where it starts */
1260 sym_length = check_macro_name (pfile, bp, "macro");
1261 bp += sym_length;
1263 /* Lossage will occur if identifiers or control keywords are broken
1264 across lines using backslash. This is not the right place to take
1265 care of that. */
1267 if (*bp == '(') {
1268 struct arglist *arg_ptrs = NULL;
1269 int argno = 0;
1271 bp++; /* skip '(' */
1272 SKIP_WHITE_SPACE (bp);
1274 /* Loop over macro argument names. */
1275 while (*bp != ')') {
1276 struct arglist *temp;
1278 temp = (struct arglist *) alloca (sizeof (struct arglist));
1279 temp->name = bp;
1280 temp->next = arg_ptrs;
1281 temp->argno = argno++;
1282 temp->rest_args = 0;
1283 arg_ptrs = temp;
1285 if (rest_args)
1286 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1288 if (!is_idstart[*bp])
1289 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1291 /* Find the end of the arg name. */
1292 while (is_idchar[*bp]) {
1293 bp++;
1294 /* do we have a "special" rest-args extension here? */
1295 if ((size_t)(limit - bp) > REST_EXTENSION_LENGTH
1296 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1297 rest_args = 1;
1298 temp->rest_args = 1;
1299 break;
1302 temp->length = bp - temp->name;
1303 if (rest_args == 1)
1304 bp += REST_EXTENSION_LENGTH;
1305 arglengths += temp->length + 2;
1306 SKIP_WHITE_SPACE (bp);
1307 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1308 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1309 goto nope;
1311 if (*bp == ',') {
1312 bp++;
1313 SKIP_WHITE_SPACE (bp);
1315 if (bp >= limit) {
1316 cpp_error (pfile, "unterminated parameter list in `#define'");
1317 goto nope;
1320 struct arglist *otemp;
1322 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1323 if (temp->length == otemp->length
1324 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1325 U_CHAR *name;
1327 name = (U_CHAR *) alloca (temp->length + 1);
1328 (void) strncpy (name, temp->name, temp->length);
1329 name[temp->length] = '\0';
1330 cpp_error (pfile,
1331 "duplicate argument name `%s' in `#define'", name);
1332 goto nope;
1337 ++bp; /* skip paren */
1338 SKIP_WHITE_SPACE (bp);
1339 /* now everything from bp before limit is the definition. */
1340 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1341 defn->rest_args = rest_args;
1343 /* Now set defn->args.argnames to the result of concatenating
1344 the argument names in reverse order
1345 with comma-space between them. */
1346 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1348 struct arglist *temp;
1349 int i = 0;
1350 for (temp = arg_ptrs; temp; temp = temp->next) {
1351 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1352 i += temp->length;
1353 if (temp->next != 0) {
1354 defn->args.argnames[i++] = ',';
1355 defn->args.argnames[i++] = ' ';
1358 defn->args.argnames[i] = 0;
1360 } else {
1361 /* Simple expansion or empty definition. */
1363 if (bp < limit)
1365 if (is_hor_space[*bp]) {
1366 bp++;
1367 SKIP_WHITE_SPACE (bp);
1368 } else {
1369 switch (*bp) {
1370 case '!': case '"': case '#': case '%': case '&': case '\'':
1371 case ')': case '*': case '+': case ',': case '-': case '.':
1372 case '/': case ':': case ';': case '<': case '=': case '>':
1373 case '?': case '[': case '\\': case ']': case '^': case '{':
1374 case '|': case '}': case '~':
1375 cpp_warning (pfile, "missing white space after `#define %.*s'",
1376 sym_length, symname);
1377 break;
1379 default:
1380 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1381 sym_length, symname);
1382 break;
1386 /* now everything from bp before limit is the definition. */
1387 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1388 defn->args.argnames = (U_CHAR *) "";
1391 defn->line = line;
1392 defn->file = file;
1394 /* OP is null if this is a predefinition */
1395 defn->predefined = predefinition;
1396 mdef.defn = defn;
1397 mdef.symnam = symname;
1398 mdef.symlen = sym_length;
1400 return mdef;
1402 nope:
1403 mdef.defn = 0;
1404 return mdef;
1407 /* Check a purported macro name SYMNAME, and yield its length.
1408 USAGE is the kind of name this is intended for. */
1410 static int
1411 check_macro_name (pfile, symname, usage)
1412 cpp_reader *pfile;
1413 U_CHAR *symname;
1414 char *usage;
1416 U_CHAR *p;
1417 int sym_length;
1419 for (p = symname; is_idchar[*p]; p++)
1421 sym_length = p - symname;
1422 if (sym_length == 0
1423 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1424 cpp_error (pfile, "invalid %s name", usage);
1425 else if (!is_idstart[*symname]) {
1426 U_CHAR *msg; /* what pain... */
1427 msg = (U_CHAR *) alloca (sym_length + 1);
1428 bcopy (symname, msg, sym_length);
1429 msg[sym_length] = 0;
1430 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1431 } else {
1432 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1433 cpp_error (pfile, "invalid %s name `defined'", usage);
1435 return sym_length;
1438 /* Return zero if two DEFINITIONs are isomorphic. */
1440 static int
1441 compare_defs (pfile, d1, d2)
1442 cpp_reader *pfile;
1443 DEFINITION *d1, *d2;
1445 register struct reflist *a1, *a2;
1446 register U_CHAR *p1 = d1->expansion;
1447 register U_CHAR *p2 = d2->expansion;
1448 int first = 1;
1450 if (d1->nargs != d2->nargs)
1451 return 1;
1452 if (CPP_PEDANTIC (pfile)
1453 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1454 return 1;
1455 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1456 a1 = a1->next, a2 = a2->next) {
1457 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1458 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1459 || a1->argno != a2->argno
1460 || a1->stringify != a2->stringify
1461 || a1->raw_before != a2->raw_before
1462 || a1->raw_after != a2->raw_after)
1463 return 1;
1464 first = 0;
1465 p1 += a1->nchars;
1466 p2 += a2->nchars;
1468 if (a1 != a2)
1469 return 1;
1470 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1471 p2, d2->length - (p2 - d2->expansion), 1))
1472 return 1;
1473 return 0;
1476 /* Return 1 if two parts of two macro definitions are effectively different.
1477 One of the parts starts at BEG1 and has LEN1 chars;
1478 the other has LEN2 chars at BEG2.
1479 Any sequence of whitespace matches any other sequence of whitespace.
1480 FIRST means these parts are the first of a macro definition;
1481 so ignore leading whitespace entirely.
1482 LAST means these parts are the last of a macro definition;
1483 so ignore trailing whitespace entirely. */
1485 static int
1486 comp_def_part (first, beg1, len1, beg2, len2, last)
1487 int first;
1488 U_CHAR *beg1, *beg2;
1489 int len1, len2;
1490 int last;
1492 register U_CHAR *end1 = beg1 + len1;
1493 register U_CHAR *end2 = beg2 + len2;
1494 if (first) {
1495 while (beg1 != end1 && is_space[*beg1]) beg1++;
1496 while (beg2 != end2 && is_space[*beg2]) beg2++;
1498 if (last) {
1499 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1500 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1502 while (beg1 != end1 && beg2 != end2) {
1503 if (is_space[*beg1] && is_space[*beg2]) {
1504 while (beg1 != end1 && is_space[*beg1]) beg1++;
1505 while (beg2 != end2 && is_space[*beg2]) beg2++;
1506 } else if (*beg1 == *beg2) {
1507 beg1++; beg2++;
1508 } else break;
1510 return (beg1 != end1) || (beg2 != end2);
1513 /* Process a #define command.
1514 KEYWORD is the keyword-table entry for #define,
1515 or NULL for a "predefined" macro. */
1517 static int
1518 do_define (pfile, keyword)
1519 cpp_reader *pfile;
1520 struct directive *keyword;
1522 int hashcode;
1523 MACRODEF mdef;
1524 HASHNODE *hp;
1525 int save_put_out_comments;
1526 long here;
1527 U_CHAR *macro, *buf, *end;
1529 here = CPP_WRITTEN (pfile);
1531 save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1532 CPP_OPTIONS (pfile)->put_out_comments = CPP_TRADITIONAL (pfile);
1533 copy_rest_of_line (pfile);
1534 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1536 /* Copy out the line so we can pop the token buffer. */
1537 buf = pfile->token_buffer + here;
1538 end = CPP_PWRITTEN (pfile);
1539 macro = alloca (end - buf + 1);
1540 bcopy (buf, macro, end - buf + 1);
1541 end = macro + (end - buf);
1543 CPP_SET_WRITTEN (pfile, here);
1545 #if 0
1546 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1547 if (pcp_outfile && keyword)
1548 pass_thru_directive (macro, end, pfile, keyword);
1549 #endif
1551 mdef = create_definition (macro, end, pfile, keyword == NULL);
1552 if (mdef.defn == 0)
1553 goto nope;
1555 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1557 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1559 int ok = 0;
1560 /* Redefining a precompiled key is ok. */
1561 if (hp->type == T_PCSTRING)
1562 ok = 1;
1563 /* Redefining a macro is ok if the definitions are the same. */
1564 else if (hp->type == T_MACRO)
1565 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1566 /* Redefining a constant is ok with -D. */
1567 else if (hp->type == T_CONST)
1568 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1569 /* Print the warning if it's not ok. */
1570 if (!ok)
1572 U_CHAR *msg; /* what pain... */
1574 /* If we are passing through #define and #undef directives, do
1575 that for this re-definition now. */
1576 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1577 pass_thru_directive (macro, end, pfile, keyword);
1579 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1580 *msg = '`';
1581 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1582 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1583 cpp_pedwarn (pfile, msg);
1584 if (hp->type == T_MACRO)
1585 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1586 "this is the location of the previous definition");
1588 /* Replace the old definition. */
1589 hp->type = T_MACRO;
1590 hp->value.defn = mdef.defn;
1592 else
1594 /* If we are passing through #define and #undef directives, do
1595 that for this new definition now. */
1596 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1597 pass_thru_directive (macro, end, pfile, keyword);
1598 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1599 (char *) mdef.defn, hashcode);
1602 return 0;
1604 nope:
1606 return 1;
1609 /* This structure represents one parsed argument in a macro call.
1610 `raw' points to the argument text as written (`raw_length' is its length).
1611 `expanded' points to the argument's macro-expansion
1612 (its length is `expand_length').
1613 `stringified_length' is the length the argument would have
1614 if stringified.
1615 `use_count' is the number of times this macro arg is substituted
1616 into the macro. If the actual use count exceeds 10,
1617 the value stored is 10. */
1619 /* raw and expanded are relative to ARG_BASE */
1620 #define ARG_BASE ((pfile)->token_buffer)
1622 struct argdata {
1623 /* Strings relative to pfile->token_buffer */
1624 long raw, expanded, stringified;
1625 int raw_length, expand_length;
1626 int stringified_length;
1627 char newlines;
1628 char use_count;
1631 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1632 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1633 as the new input buffer.
1634 Return the new buffer, or NULL on failure. */
1636 cpp_buffer *
1637 cpp_push_buffer (pfile, buffer, length)
1638 cpp_reader *pfile;
1639 U_CHAR *buffer;
1640 long length;
1642 register cpp_buffer *buf = CPP_BUFFER (pfile);
1643 if (buf == pfile->buffer_stack)
1645 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1646 buf->fname);
1647 return NULL;
1649 buf--;
1650 bzero ((char *) buf, sizeof (cpp_buffer));
1651 CPP_BUFFER (pfile) = buf;
1652 buf->if_stack = pfile->if_stack;
1653 buf->cleanup = null_cleanup;
1654 buf->underflow = null_underflow;
1655 buf->buf = buf->cur = buffer;
1656 buf->alimit = buf->rlimit = buffer + length;
1658 return buf;
1661 cpp_buffer *
1662 cpp_pop_buffer (pfile)
1663 cpp_reader *pfile;
1665 cpp_buffer *buf = CPP_BUFFER (pfile);
1666 (*buf->cleanup) (buf, pfile);
1667 return ++CPP_BUFFER (pfile);
1670 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1671 Pop the buffer when done. */
1673 void
1674 cpp_scan_buffer (pfile)
1675 cpp_reader *pfile;
1677 cpp_buffer *buffer = CPP_BUFFER (pfile);
1678 for (;;)
1680 enum cpp_token token = cpp_get_token (pfile);
1681 if (token == CPP_EOF) /* Should not happen ... */
1682 break;
1683 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1685 cpp_pop_buffer (pfile);
1686 break;
1692 * Rescan a string (which may have escape marks) into pfile's buffer.
1693 * Place the result in pfile->token_buffer.
1695 * The input is copied before it is scanned, so it is safe to pass
1696 * it something from the token_buffer that will get overwritten
1697 * (because it follows CPP_WRITTEN). This is used by do_include.
1700 static void
1701 cpp_expand_to_buffer (pfile, buf, length)
1702 cpp_reader *pfile;
1703 U_CHAR *buf;
1704 int length;
1706 register cpp_buffer *ip;
1707 #if 0
1708 cpp_buffer obuf;
1709 #endif
1710 U_CHAR *limit = buf + length;
1711 U_CHAR *buf1;
1712 #if 0
1713 int odepth = indepth;
1714 #endif
1716 if (length < 0)
1717 abort ();
1719 /* Set up the input on the input stack. */
1721 buf1 = (U_CHAR *) alloca (length + 1);
1723 register U_CHAR *p1 = buf;
1724 register U_CHAR *p2 = buf1;
1726 while (p1 != limit)
1727 *p2++ = *p1++;
1729 buf1[length] = 0;
1731 ip = cpp_push_buffer (pfile, buf1, length);
1732 if (ip == NULL)
1733 return;
1734 ip->has_escapes = 1;
1735 #if 0
1736 ip->lineno = obuf.lineno = 1;
1737 #endif
1739 /* Scan the input, create the output. */
1740 cpp_scan_buffer (pfile);
1742 #if 0
1743 if (indepth != odepth)
1744 abort ();
1745 #endif
1747 CPP_NUL_TERMINATE (pfile);
1751 static void
1752 adjust_position (buf, limit, linep, colp)
1753 U_CHAR *buf;
1754 U_CHAR *limit;
1755 long *linep;
1756 long *colp;
1758 while (buf < limit)
1760 U_CHAR ch = *buf++;
1761 if (ch == '\n')
1762 (*linep)++, (*colp) = 1;
1763 else
1764 (*colp)++;
1768 /* Move line_base forward, updating lineno and colno. */
1770 static void
1771 update_position (pbuf)
1772 register cpp_buffer *pbuf;
1774 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1775 unsigned char *new_pos = pbuf->cur;
1776 register struct parse_marker *mark;
1777 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
1779 if (pbuf->buf + mark->position < new_pos)
1780 new_pos = pbuf->buf + mark->position;
1782 pbuf->line_base += new_pos - old_pos;
1783 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1786 void
1787 cpp_buf_line_and_col (pbuf, linep, colp)
1788 register cpp_buffer *pbuf;
1789 long *linep, *colp;
1791 long dummy;
1792 if (colp == NULL)
1793 colp = &dummy;
1794 if (pbuf)
1796 *linep = pbuf->lineno;
1797 *colp = pbuf->colno;
1798 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
1800 else
1802 *linep = 0;
1803 *colp = 0;
1807 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1809 cpp_buffer *
1810 cpp_file_buffer (pfile)
1811 cpp_reader *pfile;
1813 cpp_buffer *ip = CPP_BUFFER (pfile);
1815 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
1816 if (ip->fname != NULL)
1817 return ip;
1818 return NULL;
1821 static long
1822 count_newlines (buf, limit)
1823 register U_CHAR *buf;
1824 register U_CHAR *limit;
1826 register long count = 0;
1827 while (buf < limit)
1829 U_CHAR ch = *buf++;
1830 if (ch == '\n')
1831 count++;
1833 return count;
1837 * write out a #line command, for instance, after an #include file.
1838 * If CONDITIONAL is nonzero, we can omit the #line if it would
1839 * appear to be a no-op, and we can output a few newlines instead
1840 * if we want to increase the line number by a small amount.
1841 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
1844 static void
1845 output_line_command (pfile, conditional, file_change)
1846 cpp_reader *pfile;
1847 int conditional;
1848 enum file_change_code file_change;
1850 long line, col;
1851 cpp_buffer *ip = CPP_BUFFER (pfile);
1853 if (ip->fname == NULL)
1854 return;
1856 update_position (ip);
1858 if (CPP_OPTIONS (pfile)->no_line_commands
1859 || CPP_OPTIONS (pfile)->no_output)
1860 return;
1862 line = CPP_BUFFER (pfile)->lineno;
1863 col = CPP_BUFFER (pfile)->colno;
1864 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
1866 if (CPP_OPTIONS (pfile)->no_line_commands)
1867 return;
1869 if (conditional) {
1870 if (line == pfile->lineno)
1871 return;
1873 /* If the inherited line number is a little too small,
1874 output some newlines instead of a #line command. */
1875 if (line > pfile->lineno && line < pfile->lineno + 8) {
1876 CPP_RESERVE (pfile, 20);
1877 while (line > pfile->lineno) {
1878 CPP_PUTC_Q (pfile, '\n');
1879 pfile->lineno++;
1881 return;
1885 #if 0
1886 /* Don't output a line number of 0 if we can help it. */
1887 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
1888 && *ip->bufp == '\n') {
1889 ip->lineno++;
1890 ip->bufp++;
1892 #endif
1894 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
1896 #ifdef OUTPUT_LINE_COMMANDS
1897 static char sharp_line[] = "#line ";
1898 #else
1899 static char sharp_line[] = "# ";
1900 #endif
1901 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
1904 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
1905 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
1907 quote_string (pfile, ip->nominal_fname);
1908 if (file_change != same_file) {
1909 CPP_PUTC_Q (pfile, ' ');
1910 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
1912 /* Tell cc1 if following text comes from a system header file. */
1913 if (ip->system_header_p) {
1914 CPP_PUTC_Q (pfile, ' ');
1915 CPP_PUTC_Q (pfile, '3');
1917 #ifndef NO_IMPLICIT_EXTERN_C
1918 /* Tell cc1plus if following text should be treated as C. */
1919 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
1920 CPP_PUTC_Q (pfile, ' ');
1921 CPP_PUTC_Q (pfile, '4');
1923 #endif
1924 CPP_PUTC_Q (pfile, '\n');
1925 pfile->lineno = line;
1929 * Parse a macro argument and append the info on PFILE's token_buffer.
1930 * REST_ARGS means to absorb the rest of the args.
1931 * Return nonzero to indicate a syntax error.
1934 static enum cpp_token
1935 macarg (pfile, rest_args)
1936 cpp_reader *pfile;
1937 int rest_args;
1939 int paren = 0;
1940 enum cpp_token token;
1941 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1942 CPP_OPTIONS (pfile)->put_out_comments = 0;
1944 /* Try to parse as much of the argument as exists at this
1945 input stack level. */
1946 pfile->no_macro_expand++;
1947 for (;;)
1949 token = cpp_get_token (pfile);
1950 switch (token)
1952 case CPP_EOF:
1953 goto done;
1954 case CPP_POP:
1955 /* If we've hit end of file, it's an error (reported by caller).
1956 Ditto if it's the end of cpp_expand_to_buffer text.
1957 If we've hit end of macro, just continue. */
1958 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1959 goto done;
1960 break;
1961 case CPP_LPAREN:
1962 paren++;
1963 break;
1964 case CPP_RPAREN:
1965 if (--paren < 0)
1966 goto found;
1967 break;
1968 case CPP_COMMA:
1969 /* if we've returned to lowest level and
1970 we aren't absorbing all args */
1971 if (paren == 0 && rest_args == 0)
1972 goto found;
1973 break;
1974 found:
1975 /* Remove ',' or ')' from argument buffer. */
1976 CPP_ADJUST_WRITTEN (pfile, -1);
1977 goto done;
1978 default: ;
1982 done:
1983 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1984 pfile->no_macro_expand--;
1986 return token;
1989 /* Turn newlines to spaces in the string of length LENGTH at START,
1990 except inside of string constants.
1991 The string is copied into itself with its beginning staying fixed. */
1993 static int
1994 change_newlines (start, length)
1995 U_CHAR *start;
1996 int length;
1998 register U_CHAR *ibp;
1999 register U_CHAR *obp;
2000 register U_CHAR *limit;
2001 register int c;
2003 ibp = start;
2004 limit = start + length;
2005 obp = start;
2007 while (ibp < limit) {
2008 *obp++ = c = *ibp++;
2009 switch (c) {
2011 case '\'':
2012 case '\"':
2013 /* Notice and skip strings, so that we don't delete newlines in them. */
2015 int quotec = c;
2016 while (ibp < limit) {
2017 *obp++ = c = *ibp++;
2018 if (c == quotec)
2019 break;
2020 if (c == '\n' && quotec == '\'')
2021 break;
2024 break;
2028 return obp - start;
2032 static struct tm *
2033 timestamp (pfile)
2034 cpp_reader *pfile;
2036 if (!pfile->timebuf) {
2037 time_t t = time ((time_t *) 0);
2038 pfile->timebuf = localtime (&t);
2040 return pfile->timebuf;
2043 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2044 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2048 * expand things like __FILE__. Place the expansion into the output
2049 * buffer *without* rescanning.
2052 static void
2053 special_symbol (hp, pfile)
2054 HASHNODE *hp;
2055 cpp_reader *pfile;
2057 const char *buf;
2058 char *wbuf;
2059 int len;
2060 int true_indepth;
2061 cpp_buffer *ip = NULL;
2062 struct tm *timebuf;
2064 int paren = 0; /* For special `defined' keyword */
2066 #if 0
2067 if (pcp_outfile && pcp_inside_if
2068 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2069 cpp_error (pfile,
2070 "Predefined macro `%s' used inside `#if' during precompilation",
2071 hp->name);
2072 #endif
2074 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2076 if (ip == CPP_NULL_BUFFER (pfile))
2078 cpp_error (pfile, "cccp error: not in any file?!");
2079 return; /* the show must go on */
2081 if (ip->fname != NULL)
2082 break;
2085 switch (hp->type)
2087 case T_FILE:
2088 case T_BASE_FILE:
2090 char *string;
2091 if (hp->type == T_BASE_FILE)
2093 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2094 ip = CPP_PREV_BUFFER (ip);
2096 string = ip->nominal_fname;
2098 if (!string)
2099 string = "";
2100 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2101 quote_string (pfile, string);
2102 return;
2105 case T_INCLUDE_LEVEL:
2106 true_indepth = 0;
2107 ip = CPP_BUFFER (pfile);
2108 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2109 if (ip->fname != NULL)
2110 true_indepth++;
2112 wbuf = (char *) alloca (8); /* Eight bytes ought to be more than enough*/
2113 sprintf (wbuf, "%d", true_indepth - 1);
2114 buf = wbuf;
2115 break;
2117 case T_VERSION:
2118 wbuf = (char *) alloca (3 + strlen (version_string));
2119 sprintf (wbuf, "\"%s\"", version_string);
2120 buf = wbuf;
2121 break;
2123 #ifndef NO_BUILTIN_SIZE_TYPE
2124 case T_SIZE_TYPE:
2125 buf = SIZE_TYPE;
2126 break;
2127 #endif
2129 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2130 case T_PTRDIFF_TYPE:
2131 buf = PTRDIFF_TYPE;
2132 break;
2133 #endif
2135 case T_WCHAR_TYPE:
2136 buf = CPP_WCHAR_TYPE (pfile);
2137 break;
2139 case T_USER_LABEL_PREFIX_TYPE:
2140 buf = user_label_prefix;
2141 break;
2143 case T_REGISTER_PREFIX_TYPE:
2144 buf = REGISTER_PREFIX;
2145 break;
2147 case T_CONST:
2148 wbuf = (char *) alloca (4 * sizeof (int));
2149 sprintf (wbuf, "%d", hp->value.ival);
2150 #ifdef STDC_0_IN_SYSTEM_HEADERS
2151 if (ip->system_header_p
2152 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2153 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2154 strcpy (wbuf, "0");
2155 #endif
2156 #if 0
2157 if (pcp_inside_if && pcp_outfile)
2158 /* Output a precondition for this macro use */
2159 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2160 #endif
2161 buf = wbuf;
2162 break;
2164 case T_SPECLINE:
2166 long line = ip->lineno;
2167 long col = ip->colno;
2168 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2170 wbuf = (char *) alloca (10);
2171 sprintf (wbuf, "%ld", line);
2172 buf = wbuf;
2174 break;
2176 case T_DATE:
2177 case T_TIME:
2178 wbuf = (char *) alloca (20);
2179 timebuf = timestamp (pfile);
2180 if (hp->type == T_DATE)
2181 sprintf (wbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2182 timebuf->tm_mday, timebuf->tm_year + 1900);
2183 else
2184 sprintf (wbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2185 timebuf->tm_sec);
2186 buf = wbuf;
2187 break;
2189 case T_SPEC_DEFINED:
2190 buf = " 0 "; /* Assume symbol is not defined */
2191 ip = CPP_BUFFER (pfile);
2192 SKIP_WHITE_SPACE (ip->cur);
2193 if (*ip->cur == '(')
2195 paren++;
2196 ip->cur++; /* Skip over the paren */
2197 SKIP_WHITE_SPACE (ip->cur);
2200 if (!is_idstart[*ip->cur])
2201 goto oops;
2202 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2203 goto oops;
2204 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2206 #if 0
2207 if (pcp_outfile && pcp_inside_if
2208 && (hp->type == T_CONST
2209 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2210 /* Output a precondition for this macro use. */
2211 fprintf (pcp_outfile, "#define %s\n", hp->name);
2212 #endif
2213 buf = " 1 ";
2215 #if 0
2216 else
2217 if (pcp_outfile && pcp_inside_if)
2219 /* Output a precondition for this macro use */
2220 U_CHAR *cp = ip->bufp;
2221 fprintf (pcp_outfile, "#undef ");
2222 while (is_idchar[*cp]) /* Ick! */
2223 fputc (*cp++, pcp_outfile);
2224 putc ('\n', pcp_outfile);
2226 #endif
2227 while (is_idchar[*ip->cur])
2228 ++ip->cur;
2229 SKIP_WHITE_SPACE (ip->cur);
2230 if (paren)
2232 if (*ip->cur != ')')
2233 goto oops;
2234 ++ip->cur;
2236 break;
2238 oops:
2240 cpp_error (pfile, "`defined' without an identifier");
2241 break;
2243 default:
2244 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2245 abort ();
2247 len = strlen (buf);
2248 CPP_RESERVE (pfile, len + 1);
2249 CPP_PUTS_Q (pfile, buf, len);
2250 CPP_NUL_TERMINATE_Q (pfile);
2252 return;
2255 /* Write out a #define command for the special named MACRO_NAME
2256 to PFILE's token_buffer. */
2258 static void
2259 dump_special_to_buffer (pfile, macro_name)
2260 cpp_reader *pfile;
2261 char *macro_name;
2263 static char define_directive[] = "#define ";
2264 int macro_name_length = strlen (macro_name);
2265 output_line_command (pfile, 0, same_file);
2266 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2267 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2268 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2269 CPP_PUTC_Q (pfile, ' ');
2270 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2271 CPP_PUTC (pfile, '\n');
2274 /* Initialize the built-in macros. */
2276 static void
2277 initialize_builtins (pfile)
2278 cpp_reader *pfile;
2280 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2281 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2282 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2283 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2284 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2285 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2286 #ifndef NO_BUILTIN_SIZE_TYPE
2287 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2288 #endif
2289 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2290 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2291 #endif
2292 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2293 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2294 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2295 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2296 if (!CPP_TRADITIONAL (pfile))
2297 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2298 if (CPP_OPTIONS (pfile)->objc)
2299 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2300 /* This is supplied using a -D by the compiler driver
2301 so that it is present only when truly compiling with GNU C. */
2302 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2304 if (CPP_OPTIONS (pfile)->debug_output)
2306 dump_special_to_buffer (pfile, "__BASE_FILE__");
2307 dump_special_to_buffer (pfile, "__VERSION__");
2308 #ifndef NO_BUILTIN_SIZE_TYPE
2309 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2310 #endif
2311 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2312 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2313 #endif
2314 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2315 dump_special_to_buffer (pfile, "__DATE__");
2316 dump_special_to_buffer (pfile, "__TIME__");
2317 if (!CPP_TRADITIONAL (pfile))
2318 dump_special_to_buffer (pfile, "__STDC__");
2319 if (CPP_OPTIONS (pfile)->objc)
2320 dump_special_to_buffer (pfile, "__OBJC__");
2324 /* Return 1 iff a token ending in C1 followed directly by a token C2
2325 could cause mis-tokenization. */
2327 static int
2328 unsafe_chars (c1, c2)
2329 int c1, c2;
2331 switch (c1)
2333 case '+': case '-':
2334 if (c2 == c1 || c2 == '=')
2335 return 1;
2336 goto letter;
2337 case '.':
2338 case '0': case '1': case '2': case '3': case '4':
2339 case '5': case '6': case '7': case '8': case '9':
2340 case 'e': case 'E': case 'p': case 'P':
2341 if (c2 == '-' || c2 == '+')
2342 return 1; /* could extend a pre-processing number */
2343 goto letter;
2344 case 'L':
2345 if (c2 == '\'' || c2 == '\"')
2346 return 1; /* Could turn into L"xxx" or L'xxx'. */
2347 goto letter;
2348 letter:
2349 case '_':
2350 case 'a': case 'b': case 'c': case 'd': case 'f':
2351 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2352 case 'm': case 'n': case 'o': case 'q': case 'r':
2353 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2354 case 'y': case 'z':
2355 case 'A': case 'B': case 'C': case 'D': case 'F':
2356 case 'G': case 'H': case 'I': case 'J': case 'K':
2357 case 'M': case 'N': case 'O': case 'Q': case 'R':
2358 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2359 case 'Y': case 'Z':
2360 /* We're in the middle of either a name or a pre-processing number. */
2361 return (is_idchar[c2] || c2 == '.');
2362 case '<': case '>': case '!': case '%': case '#': case ':':
2363 case '^': case '&': case '|': case '*': case '/': case '=':
2364 return (c2 == c1 || c2 == '=');
2366 return 0;
2369 /* Expand a macro call.
2370 HP points to the symbol that is the macro being called.
2371 Put the result of expansion onto the input stack
2372 so that subsequent input by our caller will use it.
2374 If macro wants arguments, caller has already verified that
2375 an argument list follows; arguments come from the input stack. */
2377 static void
2378 macroexpand (pfile, hp)
2379 cpp_reader *pfile;
2380 HASHNODE *hp;
2382 int nargs;
2383 DEFINITION *defn = hp->value.defn;
2384 register U_CHAR *xbuf;
2385 long start_line, start_column;
2386 int xbuf_len;
2387 struct argdata *args;
2388 long old_written = CPP_WRITTEN (pfile);
2389 #if 0
2390 int start_line = instack[indepth].lineno;
2391 #endif
2392 int rest_args, rest_zero;
2393 register int i;
2395 #if 0
2396 CHECK_DEPTH (return;);
2397 #endif
2399 #if 0
2400 /* This macro is being used inside a #if, which means it must be */
2401 /* recorded as a precondition. */
2402 if (pcp_inside_if && pcp_outfile && defn->predefined)
2403 dump_single_macro (hp, pcp_outfile);
2404 #endif
2406 pfile->output_escapes++;
2407 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2409 nargs = defn->nargs;
2411 if (nargs >= 0)
2413 enum cpp_token token;
2415 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2417 for (i = 0; i < nargs; i++)
2419 args[i].raw = args[i].expanded = 0;
2420 args[i].raw_length = 0;
2421 args[i].expand_length = args[i].stringified_length = -1;
2422 args[i].use_count = 0;
2425 /* Parse all the macro args that are supplied. I counts them.
2426 The first NARGS args are stored in ARGS.
2427 The rest are discarded. If rest_args is set then we assume
2428 macarg absorbed the rest of the args. */
2429 i = 0;
2430 rest_args = 0;
2431 rest_args = 0;
2432 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2435 if (rest_args)
2436 continue;
2437 if (i < nargs || (nargs == 0 && i == 0))
2439 /* if we are working on last arg which absorbs rest of args... */
2440 if (i == nargs - 1 && defn->rest_args)
2441 rest_args = 1;
2442 args[i].raw = CPP_WRITTEN (pfile);
2443 token = macarg (pfile, rest_args);
2444 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2445 args[i].newlines = 0; /* FIXME */
2447 else
2448 token = macarg (pfile, 0);
2449 if (token == CPP_EOF || token == CPP_POP)
2451 cpp_error_with_line (pfile, start_line, start_column,
2452 "unterminated macro call");
2453 return;
2455 i++;
2456 } while (token == CPP_COMMA);
2458 /* If we got one arg but it was just whitespace, call that 0 args. */
2459 if (i == 1)
2461 register U_CHAR *bp = ARG_BASE + args[0].raw;
2462 register U_CHAR *lim = bp + args[0].raw_length;
2463 /* cpp.texi says for foo ( ) we provide one argument.
2464 However, if foo wants just 0 arguments, treat this as 0. */
2465 if (nargs == 0)
2466 while (bp != lim && is_space[*bp]) bp++;
2467 if (bp == lim)
2468 i = 0;
2471 /* Don't output an error message if we have already output one for
2472 a parse error above. */
2473 rest_zero = 0;
2474 if (nargs == 0 && i > 0)
2476 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2478 else if (i < nargs)
2480 /* traditional C allows foo() if foo wants one argument. */
2481 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2483 /* the rest args token is allowed to absorb 0 tokens */
2484 else if (i == nargs - 1 && defn->rest_args)
2485 rest_zero = 1;
2486 else if (i == 0)
2487 cpp_error (pfile, "macro `%s' used without args", hp->name);
2488 else if (i == 1)
2489 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2490 else
2491 cpp_error (pfile, "macro `%s' used with only %d args",
2492 hp->name, i);
2494 else if (i > nargs)
2496 cpp_error (pfile,
2497 "macro `%s' used with too many (%d) args", hp->name, i);
2501 /* If macro wants zero args, we parsed the arglist for checking only.
2502 Read directly from the macro definition. */
2503 if (nargs <= 0)
2505 xbuf = defn->expansion;
2506 xbuf_len = defn->length;
2508 else
2510 register U_CHAR *exp = defn->expansion;
2511 register int offset; /* offset in expansion,
2512 copied a piece at a time */
2513 register int totlen; /* total amount of exp buffer filled so far */
2515 register struct reflist *ap, *last_ap;
2517 /* Macro really takes args. Compute the expansion of this call. */
2519 /* Compute length in characters of the macro's expansion.
2520 Also count number of times each arg is used. */
2521 xbuf_len = defn->length;
2522 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2524 if (ap->stringify)
2526 register struct argdata *arg = &args[ap->argno];
2527 /* Stringify if it hasn't already been */
2528 if (arg->stringified_length < 0)
2530 int arglen = arg->raw_length;
2531 int escaped = 0;
2532 int in_string = 0;
2533 int c;
2534 /* Initially need_space is -1. Otherwise, 1 means the
2535 previous character was a space, but we suppressed it;
2536 0 means the previous character was a non-space. */
2537 int need_space = -1;
2538 i = 0;
2539 arg->stringified = CPP_WRITTEN (pfile);
2540 if (!CPP_TRADITIONAL (pfile))
2541 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2542 for (; i < arglen; i++)
2544 c = (ARG_BASE + arg->raw)[i];
2546 if (! in_string)
2548 /* Internal sequences of whitespace are replaced by
2549 one space except within an string or char token.*/
2550 if (is_space[c])
2552 if (CPP_WRITTEN (pfile) > (unsigned)arg->stringified
2553 && (CPP_PWRITTEN (pfile))[-1] == '@')
2555 /* "@ " escape markers are removed */
2556 CPP_ADJUST_WRITTEN (pfile, -1);
2557 continue;
2559 if (need_space == 0)
2560 need_space = 1;
2561 continue;
2563 else if (need_space > 0)
2564 CPP_PUTC (pfile, ' ');
2565 need_space = 0;
2568 if (escaped)
2569 escaped = 0;
2570 else
2572 if (c == '\\')
2573 escaped = 1;
2574 if (in_string)
2576 if (c == in_string)
2577 in_string = 0;
2579 else if (c == '\"' || c == '\'')
2580 in_string = c;
2583 /* Escape these chars */
2584 if (c == '\"' || (in_string && c == '\\'))
2585 CPP_PUTC (pfile, '\\');
2586 if (ISPRINT (c))
2587 CPP_PUTC (pfile, c);
2588 else
2590 CPP_RESERVE (pfile, 4);
2591 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2592 (unsigned int) c);
2593 CPP_ADJUST_WRITTEN (pfile, 4);
2596 if (!CPP_TRADITIONAL (pfile))
2597 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2598 arg->stringified_length
2599 = CPP_WRITTEN (pfile) - arg->stringified;
2601 xbuf_len += args[ap->argno].stringified_length;
2603 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2604 /* Add 4 for two newline-space markers to prevent
2605 token concatenation. */
2606 xbuf_len += args[ap->argno].raw_length + 4;
2607 else
2609 /* We have an ordinary (expanded) occurrence of the arg.
2610 So compute its expansion, if we have not already. */
2611 if (args[ap->argno].expand_length < 0)
2613 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2614 cpp_expand_to_buffer (pfile,
2615 ARG_BASE + args[ap->argno].raw,
2616 args[ap->argno].raw_length);
2618 args[ap->argno].expand_length
2619 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2622 /* Add 4 for two newline-space markers to prevent
2623 token concatenation. */
2624 xbuf_len += args[ap->argno].expand_length + 4;
2626 if (args[ap->argno].use_count < 10)
2627 args[ap->argno].use_count++;
2630 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2632 /* Generate in XBUF the complete expansion
2633 with arguments substituted in.
2634 TOTLEN is the total size generated so far.
2635 OFFSET is the index in the definition
2636 of where we are copying from. */
2637 offset = totlen = 0;
2638 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2639 last_ap = ap, ap = ap->next)
2641 register struct argdata *arg = &args[ap->argno];
2642 int count_before = totlen;
2644 /* Add chars to XBUF. */
2645 for (i = 0; i < ap->nchars; i++, offset++)
2646 xbuf[totlen++] = exp[offset];
2648 /* If followed by an empty rest arg with concatenation,
2649 delete the last run of nonwhite chars. */
2650 if (rest_zero && totlen > count_before
2651 && ((ap->rest_args && ap->raw_before)
2652 || (last_ap != NULL && last_ap->rest_args
2653 && last_ap->raw_after)))
2655 /* Delete final whitespace. */
2656 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2657 totlen--;
2659 /* Delete the nonwhites before them. */
2660 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2661 totlen--;
2664 if (ap->stringify != 0)
2666 bcopy (ARG_BASE + arg->stringified,
2667 xbuf + totlen, arg->stringified_length);
2668 totlen += arg->stringified_length;
2670 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2672 U_CHAR *p1 = ARG_BASE + arg->raw;
2673 U_CHAR *l1 = p1 + arg->raw_length;
2674 if (ap->raw_before)
2676 while (p1 != l1 && is_space[*p1]) p1++;
2677 while (p1 != l1 && is_idchar[*p1])
2678 xbuf[totlen++] = *p1++;
2680 if (ap->raw_after)
2682 /* Arg is concatenated after: delete trailing whitespace,
2683 whitespace markers, and no-reexpansion markers. */
2684 while (p1 != l1)
2686 if (is_space[l1[-1]]) l1--;
2687 else if (l1[-1] == '@')
2689 U_CHAR *p2 = l1 - 1;
2690 /* If whitespace is preceded by an odd number
2691 of `@' signs, the last `@' was a whitespace
2692 marker; drop it too. */
2693 while (p2 != p1 && p2[0] == '@') p2--;
2694 if ((l1 - p2) & 1)
2695 l1--;
2696 break;
2698 else if (l1[-1] == '-')
2700 U_CHAR *p2 = l1 - 1;
2701 /* If a `-' is preceded by an odd number of
2702 `@' signs then it and the last `@' are
2703 a no-reexpansion marker. */
2704 while (p2 != p1 && p2[0] == '@') p2--;
2705 if ((l1 - p2) & 1)
2706 l1 -= 2;
2707 else
2708 break;
2710 else break;
2714 /* Delete any no-reexpansion marker that precedes
2715 an identifier at the beginning of the argument. */
2716 if (p1[0] == '@' && p1[1] == '-')
2717 p1 += 2;
2719 bcopy (p1, xbuf + totlen, l1 - p1);
2720 totlen += l1 - p1;
2722 else
2724 U_CHAR *expanded = ARG_BASE + arg->expanded;
2725 if (!ap->raw_before && totlen > 0 && arg->expand_length
2726 && !CPP_TRADITIONAL(pfile)
2727 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2729 xbuf[totlen++] = '@';
2730 xbuf[totlen++] = ' ';
2733 bcopy (expanded, xbuf + totlen, arg->expand_length);
2734 totlen += arg->expand_length;
2736 if (!ap->raw_after && totlen > 0 && offset < defn->length
2737 && !CPP_TRADITIONAL(pfile)
2738 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2740 xbuf[totlen++] = '@';
2741 xbuf[totlen++] = ' ';
2744 /* If a macro argument with newlines is used multiple times,
2745 then only expand the newlines once. This avoids creating
2746 output lines which don't correspond to any input line,
2747 which confuses gdb and gcov. */
2748 if (arg->use_count > 1 && arg->newlines > 0)
2750 /* Don't bother doing change_newlines for subsequent
2751 uses of arg. */
2752 arg->use_count = 1;
2753 arg->expand_length
2754 = change_newlines (expanded, arg->expand_length);
2758 if (totlen > xbuf_len)
2759 abort ();
2762 /* if there is anything left of the definition
2763 after handling the arg list, copy that in too. */
2765 for (i = offset; i < defn->length; i++)
2767 /* if we've reached the end of the macro */
2768 if (exp[i] == ')')
2769 rest_zero = 0;
2770 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2771 && last_ap->raw_after))
2772 xbuf[totlen++] = exp[i];
2775 xbuf[totlen] = 0;
2776 xbuf_len = totlen;
2780 pfile->output_escapes--;
2782 /* Now put the expansion on the input stack
2783 so our caller will commence reading from it. */
2784 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2785 CPP_BUFFER (pfile)->has_escapes = 1;
2787 /* Pop the space we've used in the token_buffer for argument expansion. */
2788 CPP_SET_WRITTEN (pfile, old_written);
2790 /* Recursive macro use sometimes works traditionally.
2791 #define foo(x,y) bar (x (y,0), y)
2792 foo (foo, baz) */
2794 if (!CPP_TRADITIONAL (pfile))
2795 hp->type = T_DISABLED;
2798 static void
2799 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2800 cpp_reader *pfile;
2801 register U_CHAR *xbuf;
2802 int xbuf_len;
2803 HASHNODE *hp;
2805 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2806 if (mbuf == NULL)
2807 return;
2808 mbuf->cleanup = macro_cleanup;
2809 mbuf->data = hp;
2811 /* The first chars of the expansion should be a "@ " added by
2812 collect_expansion. This is to prevent accidental token-pasting
2813 between the text preceding the macro invocation, and the macro
2814 expansion text.
2816 We would like to avoid adding unneeded spaces (for the sake of
2817 tools that use cpp, such as imake). In some common cases we can
2818 tell that it is safe to omit the space.
2820 The character before the macro invocation cannot have been an
2821 idchar (or else it would have been pasted with the idchars of
2822 the macro name). Therefore, if the first non-space character
2823 of the expansion is an idchar, we do not need the extra space
2824 to prevent token pasting.
2826 Also, we don't need the extra space if the first char is '(',
2827 or some other (less common) characters. */
2829 if (xbuf[0] == '@' && xbuf[1] == ' '
2830 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
2831 || xbuf[2] == '\"'))
2832 mbuf->cur += 2;
2835 /* Like cpp_get_token, except that it does not read past end-of-line.
2836 Also, horizontal space is skipped, and macros are popped. */
2838 static enum cpp_token
2839 get_directive_token (pfile)
2840 cpp_reader *pfile;
2842 for (;;)
2844 long old_written = CPP_WRITTEN (pfile);
2845 enum cpp_token token;
2846 cpp_skip_hspace (pfile);
2847 if (PEEKC () == '\n')
2848 return CPP_VSPACE;
2849 token = cpp_get_token (pfile);
2850 switch (token)
2852 case CPP_POP:
2853 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2854 return token;
2855 /* ... else fall though ... */
2856 case CPP_HSPACE: case CPP_COMMENT:
2857 CPP_SET_WRITTEN (pfile, old_written);
2858 break;
2859 default:
2860 return token;
2865 /* Handle #include and #import.
2866 This function expects to see "fname" or <fname> on the input.
2868 The input is normally in part of the output_buffer following
2869 CPP_WRITTEN, and will get overwritten by output_line_command.
2870 I.e. in input file specification has been popped by handle_directive.
2871 This is safe. */
2873 static int
2874 do_include (pfile, keyword)
2875 cpp_reader *pfile;
2876 struct directive *keyword;
2878 int importing = (keyword->type == T_IMPORT);
2879 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
2880 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
2881 int before; /* included before? */
2882 long flen;
2883 char *fbeg, *fend;
2884 cpp_buffer *fp;
2886 enum cpp_token token;
2888 /* Chain of dirs to search */
2889 struct include_hash *ihash;
2890 struct file_name_list *search_start;
2892 long old_written = CPP_WRITTEN (pfile);
2894 int fd;
2896 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
2898 if (importing)
2899 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
2900 if (skip_dirs)
2901 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
2904 if (importing && CPP_OPTIONS (pfile)->warn_import
2905 && !CPP_OPTIONS (pfile)->inhibit_warnings
2906 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
2908 pfile->import_warning = 1;
2909 cpp_warning (pfile, "`#import' is obsolete, use an #ifdef wrapper in the header file");
2912 pfile->parsing_include_directive++;
2913 token = get_directive_token (pfile);
2914 pfile->parsing_include_directive--;
2916 if (token == CPP_STRING)
2918 fbeg = pfile->token_buffer + old_written + 1;
2919 fend = CPP_PWRITTEN (pfile) - 1;
2920 *fend = '\0';
2921 if (fbeg[-1] == '<')
2922 angle_brackets = 1;
2924 #ifdef VMS
2925 else if (token == CPP_NAME)
2927 /* Support '#include xyz' like VAX-C to allow for easy use of
2928 * all the decwindow include files. It defaults to '#include
2929 * <xyz.h>' and generates a warning. */
2930 cpp_warning (pfile,
2931 "VAX-C-style include specification found, use '#include <filename.h>' !");
2932 angle_brackets = 1;
2934 /* Append the missing `.h' to the name. */
2935 CPP_PUTS (pfile, ".h", 3)
2936 CPP_NUL_TERMINATE_Q (pfile);
2938 fbeg = pfile->token_buffer + old_written;
2939 fend = CPP_PWRITTEN (pfile);
2941 #endif
2942 else
2944 cpp_error (pfile,
2945 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
2946 CPP_SET_WRITTEN (pfile, old_written);
2947 skip_rest_of_line (pfile);
2948 return 0;
2951 token = get_directive_token (pfile);
2952 if (token != CPP_VSPACE)
2954 cpp_error (pfile, "junk at end of `#include'");
2955 skip_rest_of_line (pfile);
2958 CPP_SET_WRITTEN (pfile, old_written);
2960 flen = fend - fbeg;
2962 if (flen == 0)
2964 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
2965 return 0;
2968 search_start = 0;
2970 for (fp = CPP_BUFFER (pfile);
2971 fp != CPP_NULL_BUFFER (pfile);
2972 fp = CPP_PREV_BUFFER (fp))
2973 if (fp->fname != NULL)
2974 break;
2976 if (fp == CPP_NULL_BUFFER (pfile))
2978 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
2979 return 1;
2982 /* For #include_next, skip in the search path past the dir in which the
2983 containing file was found. Treat files specified using an absolute path
2984 as if there are no more directories to search. Treat the primary source
2985 file like any other included source, but generate a warning. */
2986 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
2988 if (fp->ihash->foundhere != ABSOLUTE_PATH)
2989 search_start = fp->ihash->foundhere->next;
2991 else
2993 if (skip_dirs)
2994 cpp_warning (pfile, "#include_next in primary source file");
2996 if (angle_brackets)
2997 search_start = CPP_OPTIONS (pfile)->bracket_include;
2998 else
3000 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
3002 if (fp)
3003 search_start = fp->actual_dir;
3005 else
3006 search_start = CPP_OPTIONS (pfile)->quote_include;
3010 if (!search_start)
3012 cpp_error (pfile, "No include path in which to find %s", fbeg);
3013 return 0;
3016 fd = find_include_file (pfile, fbeg, search_start, &ihash, &before);
3018 if (fd == -2)
3019 return 0;
3021 if (fd == -1)
3023 if (CPP_OPTIONS (pfile)->print_deps_missing_files
3024 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
3025 (pfile->system_include_depth > 0)))
3027 if (!angle_brackets)
3028 deps_output (pfile, fbeg, ' ');
3029 else
3031 char *p;
3032 struct file_name_list *ptr;
3033 /* If requested as a system header, assume it belongs in
3034 the first system header directory. */
3035 if (CPP_OPTIONS (pfile)->bracket_include)
3036 ptr = CPP_OPTIONS (pfile)->bracket_include;
3037 else
3038 ptr = CPP_OPTIONS (pfile)->quote_include;
3040 p = (char *) alloca (strlen (ptr->name)
3041 + strlen (fbeg) + 2);
3042 if (*ptr->name != '\0')
3044 strcpy (p, ptr->name);
3045 strcat (p, "/");
3047 strcat (p, fbeg);
3048 deps_output (pfile, p, ' ');
3051 /* If -M was specified, and this header file won't be added to
3052 the dependency list, then don't count this as an error,
3053 because we can still produce correct output. Otherwise, we
3054 can't produce correct output, because there may be
3055 dependencies we need inside the missing file, and we don't
3056 know what directory this missing file exists in. */
3057 else if (CPP_PRINT_DEPS (pfile)
3058 && (CPP_PRINT_DEPS (pfile)
3059 <= (angle_brackets || (pfile->system_include_depth > 0))))
3060 cpp_warning (pfile, "No include path in which to find %s", fbeg);
3061 else
3062 cpp_error_from_errno (pfile, fbeg);
3064 return 0;
3067 /* For -M, add the file to the dependencies on its first inclusion. */
3068 if (!before && (CPP_PRINT_DEPS (pfile)
3069 > (angle_brackets || (pfile->system_include_depth > 0))))
3070 deps_output (pfile, ihash->name, ' ');
3072 /* Handle -H option. */
3073 if (CPP_OPTIONS(pfile)->print_include_names)
3075 fp = CPP_BUFFER (pfile);
3076 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
3077 putc ('.', stderr);
3078 fprintf (stderr, " %s\n", ihash->name);
3081 /* Actually process the file */
3083 if (importing)
3084 ihash->control_macro = "";
3086 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3088 close (fd);
3089 return 0;
3092 if (angle_brackets)
3093 pfile->system_include_depth++; /* Decremented in file_cleanup. */
3095 if (finclude (pfile, fd, ihash))
3097 output_line_command (pfile, 0, enter_file);
3098 pfile->only_seen_white = 2;
3101 return 0;
3105 /* Convert a character string literal into a nul-terminated string.
3106 The input string is [IN ... LIMIT).
3107 The result is placed in RESULT. RESULT can be the same as IN.
3108 The value returned in the end of the string written to RESULT,
3109 or NULL on error. */
3111 static U_CHAR *
3112 convert_string (pfile, result, in, limit, handle_escapes)
3113 cpp_reader *pfile;
3114 register U_CHAR *result, *in, *limit;
3115 int handle_escapes;
3117 U_CHAR c;
3118 c = *in++;
3119 if (c != '\"')
3120 return NULL;
3121 while (in < limit)
3123 U_CHAR c = *in++;
3124 switch (c)
3126 case '\0':
3127 return NULL;
3128 case '\"':
3129 limit = in;
3130 break;
3131 case '\\':
3132 if (handle_escapes)
3134 char *bpc = (char *) in;
3135 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc, 0x00ff);
3136 in = (U_CHAR *) bpc;
3137 if (i >= 0)
3138 *result++ = (U_CHAR)c;
3139 break;
3141 /* else fall through */
3142 default:
3143 *result++ = c;
3146 *result = 0;
3147 return result;
3151 * interpret #line command. Remembers previously seen fnames
3152 * in its very own hash table.
3154 #define FNAME_HASHSIZE 37
3156 static int
3157 do_line (pfile, keyword)
3158 cpp_reader *pfile;
3159 struct directive *keyword ATTRIBUTE_UNUSED;
3161 cpp_buffer *ip = CPP_BUFFER (pfile);
3162 int new_lineno;
3163 long old_written = CPP_WRITTEN (pfile);
3164 enum file_change_code file_change = same_file;
3165 enum cpp_token token;
3167 token = get_directive_token (pfile);
3169 if (token != CPP_NUMBER
3170 || !ISDIGIT(pfile->token_buffer[old_written]))
3172 cpp_error (pfile, "invalid format `#line' command");
3173 goto bad_line_directive;
3176 /* The Newline at the end of this line remains to be processed.
3177 To put the next line at the specified line number,
3178 we must store a line number now that is one less. */
3179 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3180 CPP_SET_WRITTEN (pfile, old_written);
3182 /* NEW_LINENO is one less than the actual line number here. */
3183 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3184 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3186 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3187 if (PEEKC() && !is_space[PEEKC()]) {
3188 cpp_error (pfile, "invalid format `#line' command");
3189 goto bad_line_directive;
3191 #endif
3193 token = get_directive_token (pfile);
3195 if (token == CPP_STRING) {
3196 U_CHAR *fname = pfile->token_buffer + old_written;
3197 U_CHAR *end_name;
3198 static HASHNODE *fname_table[FNAME_HASHSIZE];
3199 HASHNODE *hp, **hash_bucket;
3200 U_CHAR *p;
3201 long num_start;
3202 int fname_length;
3204 /* Turn the file name, which is a character string literal,
3205 into a null-terminated string. Do this in place. */
3206 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3207 if (end_name == NULL)
3209 cpp_error (pfile, "invalid format `#line' command");
3210 goto bad_line_directive;
3213 fname_length = end_name - fname;
3215 num_start = CPP_WRITTEN (pfile);
3216 token = get_directive_token (pfile);
3217 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3218 p = pfile->token_buffer + num_start;
3219 if (CPP_PEDANTIC (pfile))
3220 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3222 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3224 cpp_error (pfile, "invalid format `#line' command");
3225 goto bad_line_directive;
3227 if (*p == '1')
3228 file_change = enter_file;
3229 else if (*p == '2')
3230 file_change = leave_file;
3231 else if (*p == '3')
3232 ip->system_header_p = 1;
3233 else /* if (*p == '4') */
3234 ip->system_header_p = 2;
3236 CPP_SET_WRITTEN (pfile, num_start);
3237 token = get_directive_token (pfile);
3238 p = pfile->token_buffer + num_start;
3239 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3240 ip->system_header_p = *p == '3' ? 1 : 2;
3241 token = get_directive_token (pfile);
3243 if (token != CPP_VSPACE) {
3244 cpp_error (pfile, "invalid format `#line' command");
3245 goto bad_line_directive;
3249 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3250 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3251 if (hp->length == fname_length
3252 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3253 ip->nominal_fname = hp->value.cpval;
3254 break;
3256 if (hp == 0) {
3257 /* Didn't find it; cons up a new one. */
3258 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3259 hp->next = *hash_bucket;
3260 *hash_bucket = hp;
3262 hp->length = fname_length;
3263 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3264 bcopy (fname, hp->value.cpval, fname_length);
3267 else if (token != CPP_VSPACE && token != CPP_EOF) {
3268 cpp_error (pfile, "invalid format `#line' command");
3269 goto bad_line_directive;
3272 ip->lineno = new_lineno;
3273 bad_line_directive:
3274 skip_rest_of_line (pfile);
3275 CPP_SET_WRITTEN (pfile, old_written);
3276 output_line_command (pfile, 0, file_change);
3277 return 0;
3281 * remove the definition of a symbol from the symbol table.
3282 * according to un*x /lib/cpp, it is not an error to undef
3283 * something that has no definitions, so it isn't one here either.
3286 static int
3287 do_undef (pfile, keyword)
3288 cpp_reader *pfile;
3289 struct directive *keyword;
3291 int sym_length;
3292 HASHNODE *hp;
3293 U_CHAR *buf, *name, *limit;
3294 int c;
3295 long here = CPP_WRITTEN (pfile);
3296 enum cpp_token token;
3298 cpp_skip_hspace (pfile);
3299 c = GETC();
3300 if (! is_idstart[c])
3302 cpp_error (pfile, "token after #undef is not an identifier");
3303 skip_rest_of_line (pfile);
3304 return 1;
3307 parse_name (pfile, c);
3308 buf = pfile->token_buffer + here;
3309 limit = CPP_PWRITTEN(pfile);
3311 /* Copy out the token so we can pop the token buffer. */
3312 name = alloca (limit - buf + 1);
3313 bcopy(buf, name, limit - buf);
3314 name[limit - buf] = '\0';
3316 token = get_directive_token (pfile);
3317 if (token != CPP_VSPACE && token != CPP_POP)
3319 cpp_pedwarn (pfile, "junk on line after #undef");
3320 skip_rest_of_line (pfile);
3323 CPP_SET_WRITTEN (pfile, here);
3325 #if 0
3326 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3327 if (pcp_outfile && keyword)
3328 pass_thru_directive (buf, limit, pfile, keyword);
3329 #endif
3331 sym_length = check_macro_name (pfile, name, "macro");
3333 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
3335 /* If we are generating additional info for debugging (with -g) we
3336 need to pass through all effective #undef commands. */
3337 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3338 pass_thru_directive (name, name+sym_length, pfile, keyword);
3339 if (hp->type != T_MACRO)
3340 cpp_warning (pfile, "undefining `%s'", hp->name);
3341 delete_macro (hp);
3344 return 0;
3347 /* Wrap do_undef for -U processing. */
3348 static void
3349 cpp_undef (pfile, macro)
3350 cpp_reader *pfile;
3351 U_CHAR *macro;
3353 if (cpp_push_buffer (pfile, macro, strlen(macro)))
3355 do_undef (pfile, NULL);
3356 cpp_pop_buffer (pfile);
3362 * Report an error detected by the program we are processing.
3363 * Use the text of the line in the error message.
3364 * (We use error because it prints the filename & line#.)
3367 static int
3368 do_error (pfile, keyword)
3369 cpp_reader *pfile;
3370 struct directive *keyword ATTRIBUTE_UNUSED;
3372 long here = CPP_WRITTEN (pfile);
3373 U_CHAR *text;
3374 copy_rest_of_line (pfile);
3375 text = pfile->token_buffer + here;
3376 SKIP_WHITE_SPACE(text);
3378 cpp_error (pfile, "#error %s", text);
3379 CPP_SET_WRITTEN (pfile, here);
3380 return 0;
3384 * Report a warning detected by the program we are processing.
3385 * Use the text of the line in the warning message, then continue.
3388 static int
3389 do_warning (pfile, keyword)
3390 cpp_reader *pfile;
3391 struct directive *keyword ATTRIBUTE_UNUSED;
3393 U_CHAR *text;
3394 long here = CPP_WRITTEN(pfile);
3395 copy_rest_of_line (pfile);
3396 text = pfile->token_buffer + here;
3397 SKIP_WHITE_SPACE(text);
3399 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3400 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3402 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3403 if -pedantic-errors is given, #warning should cause an error. */
3404 cpp_pedwarn (pfile, "#warning %s", text);
3405 CPP_SET_WRITTEN (pfile, here);
3406 return 0;
3409 /* Report program identification. */
3411 static int
3412 do_ident (pfile, keyword)
3413 cpp_reader *pfile;
3414 struct directive *keyword ATTRIBUTE_UNUSED;
3416 /* Allow #ident in system headers, since that's not user's fault. */
3417 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3418 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3420 skip_rest_of_line (pfile); /* Correct? Appears to match cccp. */
3422 return 0;
3425 /* Just check for some recognized pragmas that need validation here,
3426 and leave the text in the token buffer to be output. */
3428 static int
3429 do_pragma (pfile, keyword)
3430 cpp_reader *pfile;
3431 struct directive *keyword ATTRIBUTE_UNUSED;
3433 long here = CPP_WRITTEN (pfile);
3434 U_CHAR *buf;
3436 copy_rest_of_line (pfile);
3437 buf = pfile->token_buffer + here;
3438 SKIP_WHITE_SPACE (buf);
3440 if (!strncmp (buf, "once", 4))
3442 cpp_buffer *ip = NULL;
3444 /* Allow #pragma once in system headers, since that's not the user's
3445 fault. */
3446 if (!CPP_BUFFER (pfile)->system_header_p)
3447 cpp_warning (pfile, "`#pragma once' is obsolete");
3449 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3451 if (ip == CPP_NULL_BUFFER (pfile))
3452 return 0;
3453 if (ip->fname != NULL)
3454 break;
3457 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
3458 cpp_warning (pfile, "`#pragma once' outside include file");
3459 else
3460 ip->ihash->control_macro = ""; /* never repeat */
3463 if (!strncmp (buf, "implementation", 14))
3465 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3466 been included yet. */
3467 struct include_hash *ptr;
3468 U_CHAR *p = buf + 14, *fname, *fcopy;
3469 SKIP_WHITE_SPACE (p);
3470 if (*p == '\n' || *p != '\"')
3471 return 0;
3473 fname = p + 1;
3474 p = (U_CHAR *) index (fname, '\"');
3476 fcopy = alloca (p - fname + 1);
3477 bcopy (fname, fcopy, p - fname);
3478 fcopy[p-fname] = '\0';
3480 ptr = include_hash (pfile, fcopy, 0);
3481 if (ptr)
3482 cpp_warning (pfile,
3483 "`#pragma implementation' for `%s' appears after file is included",
3484 fcopy);
3487 return 0;
3490 #ifdef SCCS_DIRECTIVE
3491 /* Just ignore #sccs, on systems where we define it at all. */
3493 static int
3494 do_sccs (pfile, keyword)
3495 cpp_reader *pfile;
3496 struct directive *keyword ATTRIBUTE_UNUSED;
3498 if (CPP_PEDANTIC (pfile))
3499 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
3500 skip_rest_of_line (pfile);
3501 return 0;
3503 #endif
3506 * handle #if command by
3507 * 1) inserting special `defined' keyword into the hash table
3508 * that gets turned into 0 or 1 by special_symbol (thus,
3509 * if the luser has a symbol called `defined' already, it won't
3510 * work inside the #if command)
3511 * 2) rescan the input into a temporary output buffer
3512 * 3) pass the output buffer to the yacc parser and collect a value
3513 * 4) clean up the mess left from steps 1 and 2.
3514 * 5) call conditional_skip to skip til the next #endif (etc.),
3515 * or not, depending on the value from step 3.
3518 static int
3519 do_if (pfile, keyword)
3520 cpp_reader *pfile;
3521 struct directive *keyword ATTRIBUTE_UNUSED;
3523 HOST_WIDE_INT value = eval_if_expression (pfile);
3524 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
3525 return 0;
3529 * handle a #elif directive by not changing if_stack either.
3530 * see the comment above do_else.
3533 static int
3534 do_elif (pfile, keyword)
3535 cpp_reader *pfile;
3536 struct directive *keyword ATTRIBUTE_UNUSED;
3538 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3539 cpp_error (pfile, "`#elif' not within a conditional");
3540 return 0;
3541 } else {
3542 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3543 cpp_error (pfile, "`#elif' after `#else'");
3544 #if 0
3545 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3546 #endif
3547 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
3548 && strcmp (pfile->if_stack->fname,
3549 CPP_BUFFER (pfile)->nominal_fname) != 0)
3550 fprintf (stderr, ", file %s", pfile->if_stack->fname);
3551 fprintf (stderr, ")\n");
3553 pfile->if_stack->type = T_ELIF;
3556 if (pfile->if_stack->if_succeeded)
3557 skip_if_group (pfile);
3558 else {
3559 HOST_WIDE_INT value = eval_if_expression (pfile);
3560 if (value == 0)
3561 skip_if_group (pfile);
3562 else {
3563 ++pfile->if_stack->if_succeeded; /* continue processing input */
3564 output_line_command (pfile, 1, same_file);
3567 return 0;
3571 * evaluate a #if expression in BUF, of length LENGTH,
3572 * then parse the result as a C expression and return the value as an int.
3575 static HOST_WIDE_INT
3576 eval_if_expression (pfile)
3577 cpp_reader *pfile;
3579 HASHNODE *save_defined;
3580 HOST_WIDE_INT value;
3581 long old_written = CPP_WRITTEN (pfile);
3583 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
3584 pfile->pcp_inside_if = 1;
3586 value = cpp_parse_expr (pfile);
3587 pfile->pcp_inside_if = 0;
3588 delete_macro (save_defined); /* clean up special symbol */
3590 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3592 return value;
3596 * routine to handle ifdef/ifndef. Try to look up the symbol,
3597 * then do or don't skip to the #endif/#else/#elif depending
3598 * on what directive is actually being processed.
3601 static int
3602 do_xifdef (pfile, keyword)
3603 cpp_reader *pfile;
3604 struct directive *keyword;
3606 int skip;
3607 cpp_buffer *ip = CPP_BUFFER (pfile);
3608 U_CHAR *ident;
3609 int ident_length;
3610 enum cpp_token token;
3611 int start_of_file = 0;
3612 U_CHAR *control_macro = 0;
3613 int old_written = CPP_WRITTEN (pfile);
3615 /* Detect a #ifndef at start of file (not counting comments). */
3616 if (ip->fname != 0 && keyword->type == T_IFNDEF)
3617 start_of_file = pfile->only_seen_white == 2;
3619 pfile->no_macro_expand++;
3620 token = get_directive_token (pfile);
3621 pfile->no_macro_expand--;
3623 ident = pfile->token_buffer + old_written;
3624 ident_length = CPP_WRITTEN (pfile) - old_written;
3625 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3627 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
3629 skip = (keyword->type == T_IFDEF);
3630 if (! CPP_TRADITIONAL (pfile))
3631 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
3633 else if (token == CPP_NAME)
3635 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
3636 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
3637 if (start_of_file && !skip)
3639 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
3640 bcopy (ident, control_macro, ident_length + 1);
3643 else
3645 skip = (keyword->type == T_IFDEF);
3646 if (! CPP_TRADITIONAL (pfile))
3647 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
3650 if (!CPP_TRADITIONAL (pfile))
3651 { int c;
3652 cpp_skip_hspace (pfile);
3653 c = PEEKC ();
3654 if (c != EOF && c != '\n')
3655 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
3657 skip_rest_of_line (pfile);
3659 #if 0
3660 if (pcp_outfile) {
3661 /* Output a precondition for this macro. */
3662 if (hp && hp->value.defn->predefined)
3663 fprintf (pcp_outfile, "#define %s\n", hp->name);
3664 else {
3665 U_CHAR *cp = buf;
3666 fprintf (pcp_outfile, "#undef ");
3667 while (is_idchar[*cp]) /* Ick! */
3668 fputc (*cp++, pcp_outfile);
3669 putc ('\n', pcp_outfile);
3671 #endif
3673 conditional_skip (pfile, skip, T_IF, control_macro);
3674 return 0;
3677 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3678 If this is a #ifndef starting at the beginning of a file,
3679 CONTROL_MACRO is the macro name tested by the #ifndef.
3680 Otherwise, CONTROL_MACRO is 0. */
3682 static void
3683 conditional_skip (pfile, skip, type, control_macro)
3684 cpp_reader *pfile;
3685 int skip;
3686 enum node_type type;
3687 U_CHAR *control_macro;
3689 IF_STACK_FRAME *temp;
3691 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3692 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
3693 #if 0
3694 temp->lineno = CPP_BUFFER (pfile)->lineno;
3695 #endif
3696 temp->next = pfile->if_stack;
3697 temp->control_macro = control_macro;
3698 pfile->if_stack = temp;
3700 pfile->if_stack->type = type;
3702 if (skip != 0) {
3703 skip_if_group (pfile);
3704 return;
3705 } else {
3706 ++pfile->if_stack->if_succeeded;
3707 output_line_command (pfile, 1, same_file);
3711 /* Subroutine of skip_if_group. Examine one preprocessing directive and
3712 return 0 if skipping should continue, 1 if it should halt. Also
3713 adjusts the if_stack as appropriate.
3714 The `#' has been read, but not the identifier. */
3716 static int
3717 consider_directive_while_skipping (pfile, stack)
3718 cpp_reader *pfile;
3719 IF_STACK_FRAME *stack;
3721 long ident_len, ident;
3722 struct directive *kt;
3723 IF_STACK_FRAME *temp;
3725 cpp_skip_hspace (pfile);
3727 ident = CPP_WRITTEN (pfile);
3728 parse_name (pfile, GETC());
3729 ident_len = CPP_WRITTEN (pfile) - ident;
3731 CPP_SET_WRITTEN (pfile, ident);
3733 for (kt = directive_table; kt->length >= 0; kt++)
3734 if (kt->length == ident_len
3735 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
3736 switch (kt->type)
3738 case T_IF:
3739 case T_IFDEF:
3740 case T_IFNDEF:
3741 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
3742 temp->next = pfile->if_stack;
3743 pfile->if_stack = temp;
3744 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
3745 temp->type = kt->type;
3746 return 0;
3748 case T_ELSE:
3749 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
3750 validate_else (pfile, "#else");
3751 /* fall through */
3752 case T_ELIF:
3753 if (pfile->if_stack->type == T_ELSE)
3754 cpp_error (pfile, "`%s' after `#else'", kt->name);
3756 if (pfile->if_stack == stack)
3757 return 1;
3758 else
3760 pfile->if_stack->type = kt->type;
3761 return 0;
3764 case T_ENDIF:
3765 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
3766 validate_else (pfile, "#endif");
3768 if (pfile->if_stack == stack)
3769 return 1;
3771 temp = pfile->if_stack;
3772 pfile->if_stack = temp->next;
3773 free (temp);
3774 return 0;
3776 default:
3777 return 0;
3780 /* Don't let erroneous code go by. */
3781 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
3782 cpp_pedwarn (pfile, "invalid preprocessor directive name");
3783 return 0;
3786 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
3787 * leaves input ptr at the sharp sign found.
3789 static void
3790 skip_if_group (pfile)
3791 cpp_reader *pfile;
3793 int c;
3794 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
3795 U_CHAR *beg_of_line;
3796 long old_written;
3798 if (CPP_OPTIONS (pfile)->output_conditionals)
3800 CPP_PUTS (pfile, "#failed\n", 8);
3801 pfile->lineno++;
3802 output_line_command (pfile, 1, same_file);
3805 old_written = CPP_WRITTEN (pfile);
3807 for (;;)
3809 beg_of_line = CPP_BUFFER (pfile)->cur;
3811 if (! CPP_TRADITIONAL (pfile))
3812 cpp_skip_hspace (pfile);
3813 c = GETC();
3814 if (c == '\n')
3816 if (CPP_OPTIONS (pfile)->output_conditionals)
3817 CPP_PUTC (pfile, c);
3818 continue;
3820 else if (c == '#')
3822 if (consider_directive_while_skipping (pfile, save_if_stack))
3823 break;
3825 else if (c == EOF)
3826 return; /* Caller will issue error. */
3828 FORWARD(-1);
3829 if (CPP_OPTIONS (pfile)->output_conditionals)
3831 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
3832 copy_rest_of_line (pfile);
3834 else
3836 copy_rest_of_line (pfile);
3837 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
3840 c = GETC();
3841 if (c == EOF)
3842 return; /* Caller will issue error. */
3843 else
3845 /* \n */
3846 if (CPP_OPTIONS (pfile)->output_conditionals)
3847 CPP_PUTC (pfile, c);
3851 /* Back up to the beginning of this line. Caller will process the
3852 directive. */
3853 CPP_BUFFER (pfile)->cur = beg_of_line;
3854 pfile->only_seen_white = 1;
3855 if (CPP_OPTIONS (pfile)->output_conditionals)
3857 CPP_PUTS (pfile, "#endfailed\n", 11);
3858 pfile->lineno++;
3863 * handle a #else directive. Do this by just continuing processing
3864 * without changing if_stack ; this is so that the error message
3865 * for missing #endif's etc. will point to the original #if. It
3866 * is possible that something different would be better.
3869 static int
3870 do_else (pfile, keyword)
3871 cpp_reader *pfile;
3872 struct directive *keyword ATTRIBUTE_UNUSED;
3874 cpp_buffer *ip = CPP_BUFFER (pfile);
3876 if (CPP_PEDANTIC (pfile))
3877 validate_else (pfile, "#else");
3878 skip_rest_of_line (pfile);
3880 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3881 cpp_error (pfile, "`#else' not within a conditional");
3882 return 0;
3883 } else {
3884 /* #ifndef can't have its special treatment for containing the whole file
3885 if it has a #else clause. */
3886 pfile->if_stack->control_macro = 0;
3888 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3889 cpp_error (pfile, "`#else' after `#else'");
3890 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3891 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
3892 fprintf (stderr, ", file %s", pfile->if_stack->fname);
3893 fprintf (stderr, ")\n");
3895 pfile->if_stack->type = T_ELSE;
3898 if (pfile->if_stack->if_succeeded)
3899 skip_if_group (pfile);
3900 else {
3901 ++pfile->if_stack->if_succeeded; /* continue processing input */
3902 output_line_command (pfile, 1, same_file);
3904 return 0;
3908 * unstack after #endif command
3911 static int
3912 do_endif (pfile, keyword)
3913 cpp_reader *pfile;
3914 struct directive *keyword ATTRIBUTE_UNUSED;
3916 if (CPP_PEDANTIC (pfile))
3917 validate_else (pfile, "#endif");
3918 skip_rest_of_line (pfile);
3920 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
3921 cpp_error (pfile, "unbalanced `#endif'");
3922 else
3924 IF_STACK_FRAME *temp = pfile->if_stack;
3925 pfile->if_stack = temp->next;
3926 if (temp->control_macro != 0)
3928 /* This #endif matched a #ifndef at the start of the file.
3929 See if it is at the end of the file. */
3930 struct parse_marker start_mark;
3931 int c;
3933 parse_set_mark (&start_mark, pfile);
3935 for (;;)
3937 cpp_skip_hspace (pfile);
3938 c = GETC ();
3939 if (c != '\n')
3940 break;
3942 parse_goto_mark (&start_mark, pfile);
3943 parse_clear_mark (&start_mark);
3945 if (c == EOF)
3947 /* This #endif ends a #ifndef
3948 that contains all of the file (aside from whitespace).
3949 Arrange not to include the file again
3950 if the macro that was tested is defined. */
3951 struct cpp_buffer *ip;
3952 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3953 if (ip->fname != NULL)
3954 break;
3955 ip->ihash->control_macro = temp->control_macro;
3958 free (temp);
3959 output_line_command (pfile, 1, same_file);
3961 return 0;
3964 /* When an #else or #endif is found while skipping failed conditional,
3965 if -pedantic was specified, this is called to warn about text after
3966 the command name. P points to the first char after the command name. */
3968 static void
3969 validate_else (pfile, directive)
3970 cpp_reader *pfile;
3971 char *directive;
3973 int c;
3974 cpp_skip_hspace (pfile);
3975 c = PEEKC ();
3976 if (c != EOF && c != '\n')
3977 cpp_pedwarn (pfile,
3978 "text following `%s' violates ANSI standard", directive);
3981 /* Get the next token, and add it to the text in pfile->token_buffer.
3982 Return the kind of token we got. */
3984 enum cpp_token
3985 cpp_get_token (pfile)
3986 cpp_reader *pfile;
3988 register int c, c2, c3;
3989 long old_written;
3990 long start_line, start_column;
3991 enum cpp_token token;
3992 struct cpp_options *opts = CPP_OPTIONS (pfile);
3993 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
3994 get_next:
3995 c = GETC();
3996 if (c == EOF)
3998 handle_eof:
3999 if (CPP_BUFFER (pfile)->seen_eof)
4001 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4002 goto get_next;
4003 else
4004 return CPP_EOF;
4006 else
4008 cpp_buffer *next_buf
4009 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4010 CPP_BUFFER (pfile)->seen_eof = 1;
4011 if (CPP_BUFFER (pfile)->nominal_fname
4012 && next_buf != CPP_NULL_BUFFER (pfile))
4014 /* We're about to return from an #include file.
4015 Emit #line information now (as part of the CPP_POP) result.
4016 But the #line refers to the file we will pop to. */
4017 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4018 CPP_BUFFER (pfile) = next_buf;
4019 pfile->input_stack_listing_current = 0;
4020 output_line_command (pfile, 0, leave_file);
4021 CPP_BUFFER (pfile) = cur_buffer;
4023 return CPP_POP;
4026 else
4028 switch (c)
4030 long newlines;
4031 struct parse_marker start_mark;
4032 case '/':
4033 if (PEEKC () == '=')
4034 goto op2;
4035 if (opts->put_out_comments)
4036 parse_set_mark (&start_mark, pfile);
4037 newlines = 0;
4038 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4039 &start_line, &start_column);
4040 c = skip_comment (pfile, &newlines);
4041 if (opts->put_out_comments && (c == '/' || c == EOF))
4042 parse_clear_mark (&start_mark);
4043 if (c == '/')
4044 goto randomchar;
4045 if (c == EOF)
4047 cpp_error_with_line (pfile, start_line, start_column,
4048 "unterminated comment");
4049 goto handle_eof;
4051 c = '/'; /* Initial letter of comment. */
4052 return_comment:
4053 /* Comments are equivalent to spaces.
4054 For -traditional, a comment is equivalent to nothing. */
4055 if (opts->put_out_comments)
4057 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4058 U_CHAR *start = pbuf->buf + start_mark.position;
4059 int len = pbuf->cur - start;
4060 CPP_RESERVE(pfile, 1 + len);
4061 CPP_PUTC_Q (pfile, c);
4062 CPP_PUTS_Q (pfile, start, len);
4063 pfile->lineno += newlines;
4064 parse_clear_mark (&start_mark);
4065 return CPP_COMMENT;
4067 else if (CPP_TRADITIONAL (pfile))
4069 return CPP_COMMENT;
4071 else
4073 #if 0
4074 /* This may not work if cpp_get_token is called recursively,
4075 since many places look for horizontal space. */
4076 if (newlines)
4078 /* Copy the newlines into the output buffer, in order to
4079 avoid the pain of a #line every time a multiline comment
4080 is seen. */
4081 CPP_RESERVE(pfile, newlines);
4082 while (--newlines >= 0)
4084 CPP_PUTC_Q (pfile, '\n');
4085 pfile->lineno++;
4087 return CPP_VSPACE;
4089 #endif
4090 CPP_RESERVE(pfile, 1);
4091 CPP_PUTC_Q (pfile, ' ');
4092 return CPP_HSPACE;
4094 #if 0
4095 if (opts->for_lint) {
4096 U_CHAR *argbp;
4097 int cmdlen, arglen;
4098 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4100 if (lintcmd != NULL) {
4101 /* I believe it is always safe to emit this newline: */
4102 obp[-1] = '\n';
4103 bcopy ("#pragma lint ", (char *) obp, 13);
4104 obp += 13;
4105 bcopy (lintcmd, (char *) obp, cmdlen);
4106 obp += cmdlen;
4108 if (arglen != 0) {
4109 *(obp++) = ' ';
4110 bcopy (argbp, (char *) obp, arglen);
4111 obp += arglen;
4114 /* OK, now bring us back to the state we were in before we entered
4115 this branch. We need #line because the newline for the pragma
4116 could mess things up. */
4117 output_line_command (pfile, 0, same_file);
4118 *(obp++) = ' '; /* just in case, if comments are copied thru */
4119 *(obp++) = '/';
4122 #endif
4124 case '#':
4125 #if 0
4126 /* If this is expanding a macro definition, don't recognize
4127 preprocessor directives. */
4128 if (ip->macro != 0)
4129 goto randomchar;
4130 /* If this is expand_into_temp_buffer, recognize them
4131 only after an actual newline at this level,
4132 not at the beginning of the input level. */
4133 if (ip->fname == 0 && beg_of_line == ip->buf)
4134 goto randomchar;
4135 if (ident_length)
4136 goto specialchar;
4137 #endif
4139 if (!pfile->only_seen_white)
4140 goto randomchar;
4141 if (handle_directive (pfile))
4142 return CPP_DIRECTIVE;
4143 pfile->only_seen_white = 0;
4144 return CPP_OTHER;
4146 case '\"':
4147 case '\'':
4148 /* A single quoted string is treated like a double -- some
4149 programs (e.g., troff) are perverse this way */
4150 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4151 &start_line, &start_column);
4152 old_written = CPP_WRITTEN (pfile);
4153 string:
4154 CPP_PUTC (pfile, c);
4155 while (1)
4157 int cc = GETC();
4158 if (cc == EOF)
4160 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4162 /* try harder: this string crosses a macro expansion
4163 boundary. This can happen naturally if -traditional.
4164 Otherwise, only -D can make a macro with an unmatched
4165 quote. */
4166 cpp_buffer *next_buf
4167 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4168 (*CPP_BUFFER (pfile)->cleanup)
4169 (CPP_BUFFER (pfile), pfile);
4170 CPP_BUFFER (pfile) = next_buf;
4171 continue;
4173 if (!CPP_TRADITIONAL (pfile))
4175 cpp_error_with_line (pfile, start_line, start_column,
4176 "unterminated string or character constant");
4177 if (pfile->multiline_string_line != start_line
4178 && pfile->multiline_string_line != 0)
4179 cpp_error_with_line (pfile,
4180 pfile->multiline_string_line, -1,
4181 "possible real start of unterminated constant");
4182 pfile->multiline_string_line = 0;
4184 break;
4186 CPP_PUTC (pfile, cc);
4187 switch (cc)
4189 case '\n':
4190 /* Traditionally, end of line ends a string constant with
4191 no error. So exit the loop and record the new line. */
4192 if (CPP_TRADITIONAL (pfile))
4193 goto while2end;
4194 if (c == '\'')
4196 cpp_error_with_line (pfile, start_line, start_column,
4197 "unterminated character constant");
4198 goto while2end;
4200 if (CPP_PEDANTIC (pfile)
4201 && pfile->multiline_string_line == 0)
4203 cpp_pedwarn_with_line (pfile, start_line, start_column,
4204 "string constant runs past end of line");
4206 if (pfile->multiline_string_line == 0)
4207 pfile->multiline_string_line = start_line;
4208 break;
4210 case '\\':
4211 cc = GETC();
4212 if (cc == '\n')
4214 /* Backslash newline is replaced by nothing at all. */
4215 CPP_ADJUST_WRITTEN (pfile, -1);
4216 pfile->lineno++;
4218 else
4220 /* ANSI stupidly requires that in \\ the second \
4221 is *not* prevented from combining with a newline. */
4222 NEWLINE_FIX1(cc);
4223 if (cc != EOF)
4224 CPP_PUTC (pfile, cc);
4226 break;
4228 case '\"':
4229 case '\'':
4230 if (cc == c)
4231 goto while2end;
4232 break;
4235 while2end:
4236 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4237 CPP_PWRITTEN (pfile));
4238 pfile->only_seen_white = 0;
4239 return c == '\'' ? CPP_CHAR : CPP_STRING;
4241 case '$':
4242 if (!opts->dollars_in_ident)
4243 goto randomchar;
4244 goto letter;
4246 case ':':
4247 if (opts->cplusplus && PEEKC () == ':')
4248 goto op2;
4249 goto randomchar;
4251 case '&':
4252 case '+':
4253 case '|':
4254 NEWLINE_FIX;
4255 c2 = PEEKC ();
4256 if (c2 == c || c2 == '=')
4257 goto op2;
4258 goto randomchar;
4260 case '*':
4261 case '!':
4262 case '%':
4263 case '=':
4264 case '^':
4265 NEWLINE_FIX;
4266 if (PEEKC () == '=')
4267 goto op2;
4268 goto randomchar;
4270 case '-':
4271 NEWLINE_FIX;
4272 c2 = PEEKC ();
4273 if (c2 == '-' && opts->chill)
4275 /* Chill style comment */
4276 if (opts->put_out_comments)
4277 parse_set_mark (&start_mark, pfile);
4278 FORWARD(1); /* Skip second '-'. */
4279 for (;;)
4281 c = GETC ();
4282 if (c == EOF)
4283 break;
4284 if (c == '\n')
4286 /* Don't consider final '\n' to be part of comment. */
4287 FORWARD(-1);
4288 break;
4291 c = '-';
4292 goto return_comment;
4294 if (c2 == '-' || c2 == '=' || c2 == '>')
4295 goto op2;
4296 goto randomchar;
4298 case '<':
4299 if (pfile->parsing_include_directive)
4301 for (;;)
4303 CPP_PUTC (pfile, c);
4304 if (c == '>')
4305 break;
4306 c = GETC ();
4307 NEWLINE_FIX1 (c);
4308 if (c == '\n' || c == EOF)
4310 cpp_error (pfile,
4311 "missing '>' in `#include <FILENAME>'");
4312 break;
4315 return CPP_STRING;
4317 /* else fall through */
4318 case '>':
4319 NEWLINE_FIX;
4320 c2 = PEEKC ();
4321 if (c2 == '=')
4322 goto op2;
4323 if (c2 != c)
4324 goto randomchar;
4325 FORWARD(1);
4326 CPP_RESERVE (pfile, 4);
4327 CPP_PUTC (pfile, c);
4328 CPP_PUTC (pfile, c2);
4329 NEWLINE_FIX;
4330 c3 = PEEKC ();
4331 if (c3 == '=')
4332 CPP_PUTC_Q (pfile, GETC ());
4333 CPP_NUL_TERMINATE_Q (pfile);
4334 pfile->only_seen_white = 0;
4335 return CPP_OTHER;
4337 case '@':
4338 if (CPP_BUFFER (pfile)->has_escapes)
4340 c = GETC ();
4341 if (c == '-')
4343 if (pfile->output_escapes)
4344 CPP_PUTS (pfile, "@-", 2);
4345 parse_name (pfile, GETC ());
4346 return CPP_NAME;
4348 else if (is_space [c])
4350 CPP_RESERVE (pfile, 2);
4351 if (pfile->output_escapes)
4352 CPP_PUTC_Q (pfile, '@');
4353 CPP_PUTC_Q (pfile, c);
4354 return CPP_HSPACE;
4357 if (pfile->output_escapes)
4359 CPP_PUTS (pfile, "@@", 2);
4360 return CPP_OTHER;
4362 goto randomchar;
4364 case '.':
4365 NEWLINE_FIX;
4366 c2 = PEEKC ();
4367 if (ISDIGIT(c2))
4369 CPP_RESERVE(pfile, 2);
4370 CPP_PUTC_Q (pfile, '.');
4371 c = GETC ();
4372 goto number;
4374 /* FIXME - misses the case "..\\\n." */
4375 if (c2 == '.' && PEEKN(1) == '.')
4377 CPP_RESERVE(pfile, 4);
4378 CPP_PUTC_Q (pfile, '.');
4379 CPP_PUTC_Q (pfile, '.');
4380 CPP_PUTC_Q (pfile, '.');
4381 FORWARD (2);
4382 CPP_NUL_TERMINATE_Q (pfile);
4383 pfile->only_seen_white = 0;
4384 return CPP_3DOTS;
4386 goto randomchar;
4388 op2:
4389 token = CPP_OTHER;
4390 pfile->only_seen_white = 0;
4391 op2any:
4392 CPP_RESERVE(pfile, 3);
4393 CPP_PUTC_Q (pfile, c);
4394 CPP_PUTC_Q (pfile, GETC ());
4395 CPP_NUL_TERMINATE_Q (pfile);
4396 return token;
4398 case 'L':
4399 NEWLINE_FIX;
4400 c2 = PEEKC ();
4401 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4403 CPP_PUTC (pfile, c);
4404 c = GETC ();
4405 goto string;
4407 goto letter;
4409 case '0': case '1': case '2': case '3': case '4':
4410 case '5': case '6': case '7': case '8': case '9':
4411 number:
4412 c2 = '.';
4413 for (;;)
4415 CPP_RESERVE (pfile, 2);
4416 CPP_PUTC_Q (pfile, c);
4417 NEWLINE_FIX;
4418 c = PEEKC ();
4419 if (c == EOF)
4420 break;
4421 if (!is_idchar[c] && c != '.'
4422 && ((c2 != 'e' && c2 != 'E'
4423 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4424 || (c != '+' && c != '-')))
4425 break;
4426 FORWARD(1);
4427 c2= c;
4429 CPP_NUL_TERMINATE_Q (pfile);
4430 pfile->only_seen_white = 0;
4431 return CPP_NUMBER;
4432 case 'b': case 'c': case 'd': case 'h': case 'o':
4433 case 'B': case 'C': case 'D': case 'H': case 'O':
4434 if (opts->chill && PEEKC () == '\'')
4436 pfile->only_seen_white = 0;
4437 CPP_RESERVE (pfile, 2);
4438 CPP_PUTC_Q (pfile, c);
4439 CPP_PUTC_Q (pfile, '\'');
4440 FORWARD(1);
4441 for (;;)
4443 c = GETC();
4444 if (c == EOF)
4445 goto chill_number_eof;
4446 if (!is_idchar[c])
4448 if (c == '\\' && PEEKC() == '\n')
4450 FORWARD(2);
4451 continue;
4453 break;
4455 CPP_PUTC (pfile, c);
4457 if (c == '\'')
4459 CPP_RESERVE (pfile, 2);
4460 CPP_PUTC_Q (pfile, c);
4461 CPP_NUL_TERMINATE_Q (pfile);
4462 return CPP_STRING;
4464 else
4466 FORWARD(-1);
4467 chill_number_eof:
4468 CPP_NUL_TERMINATE (pfile);
4469 return CPP_NUMBER;
4472 else
4473 goto letter;
4474 case '_':
4475 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4476 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4477 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4478 case 'x': case 'y': case 'z':
4479 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4480 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4481 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4482 case 'Y': case 'Z':
4483 letter:
4485 HASHNODE *hp;
4486 unsigned char *ident;
4487 int before_name_written = CPP_WRITTEN (pfile);
4488 int ident_len;
4489 parse_name (pfile, c);
4490 pfile->only_seen_white = 0;
4491 if (pfile->no_macro_expand)
4492 return CPP_NAME;
4493 ident = pfile->token_buffer + before_name_written;
4494 ident_len = CPP_PWRITTEN (pfile) - ident;
4495 hp = cpp_lookup (pfile, ident, ident_len, -1);
4496 if (!hp)
4497 return CPP_NAME;
4498 if (hp->type == T_DISABLED)
4500 if (pfile->output_escapes)
4501 { /* Return "@-IDENT", followed by '\0'. */
4502 int i;
4503 CPP_RESERVE (pfile, 3);
4504 ident = pfile->token_buffer + before_name_written;
4505 CPP_ADJUST_WRITTEN (pfile, 2);
4506 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
4507 ident[0] = '@';
4508 ident[1] = '-';
4510 return CPP_NAME;
4513 /* If macro wants an arglist, verify that a '(' follows.
4514 first skip all whitespace, copying it to the output
4515 after the macro name. Then, if there is no '(',
4516 decide this is not a macro call and leave things that way. */
4517 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
4519 struct parse_marker macro_mark;
4520 int is_macro_call, macbuf_whitespace = 0;
4522 parse_set_mark (&macro_mark, pfile);
4523 for (;;)
4525 cpp_skip_hspace (pfile);
4526 c = PEEKC ();
4527 is_macro_call = c == '(';
4528 if (c != EOF)
4530 if (c != '\n')
4531 break;
4532 FORWARD (1);
4534 else
4536 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4538 if (macro_mark.position !=
4539 (CPP_BUFFER (pfile)->cur
4540 - CPP_BUFFER (pfile)->buf))
4541 macbuf_whitespace = 1;
4543 parse_clear_mark (&macro_mark);
4544 cpp_pop_buffer (pfile);
4545 parse_set_mark (&macro_mark, pfile);
4547 else
4548 break;
4551 if (!is_macro_call)
4553 parse_goto_mark (&macro_mark, pfile);
4554 if (macbuf_whitespace)
4555 CPP_PUTC (pfile, ' ');
4557 parse_clear_mark (&macro_mark);
4558 if (!is_macro_call)
4559 return CPP_NAME;
4561 /* This is now known to be a macro call. */
4563 /* it might not actually be a macro. */
4564 if (hp->type != T_MACRO) {
4565 int xbuf_len; U_CHAR *xbuf;
4566 CPP_SET_WRITTEN (pfile, before_name_written);
4567 special_symbol (hp, pfile);
4568 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
4569 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4570 CPP_SET_WRITTEN (pfile, before_name_written);
4571 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
4572 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4574 else
4576 /* Expand the macro, reading arguments as needed,
4577 and push the expansion on the input stack. */
4578 macroexpand (pfile, hp);
4579 CPP_SET_WRITTEN (pfile, before_name_written);
4582 /* An extra "@ " is added to the end of a macro expansion
4583 to prevent accidental token pasting. We prefer to avoid
4584 unneeded extra spaces (for the sake of cpp-using tools like
4585 imake). Here we remove the space if it is safe to do so. */
4586 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
4587 && pfile->buffer->rlimit[-2] == '@'
4588 && pfile->buffer->rlimit[-1] == ' ')
4590 int c1 = pfile->buffer->rlimit[-3];
4591 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
4592 if (c2 == EOF || ! unsafe_chars (c1, c2))
4593 pfile->buffer->rlimit -= 2;
4596 goto get_next;
4598 case ' ': case '\t': case '\v': case '\r':
4599 for (;;)
4601 CPP_PUTC (pfile, c);
4602 c = PEEKC ();
4603 if (c == EOF || !is_hor_space[c])
4604 break;
4605 FORWARD(1);
4607 return CPP_HSPACE;
4609 case '\\':
4610 c2 = PEEKC ();
4611 if (c2 != '\n')
4612 goto randomchar;
4613 token = CPP_HSPACE;
4614 goto op2any;
4616 case '\n':
4617 CPP_PUTC (pfile, c);
4618 if (pfile->only_seen_white == 0)
4619 pfile->only_seen_white = 1;
4620 pfile->lineno++;
4621 output_line_command (pfile, 1, same_file);
4622 return CPP_VSPACE;
4624 case '(': token = CPP_LPAREN; goto char1;
4625 case ')': token = CPP_RPAREN; goto char1;
4626 case '{': token = CPP_LBRACE; goto char1;
4627 case '}': token = CPP_RBRACE; goto char1;
4628 case ',': token = CPP_COMMA; goto char1;
4629 case ';': token = CPP_SEMICOLON; goto char1;
4631 randomchar:
4632 default:
4633 token = CPP_OTHER;
4634 char1:
4635 pfile->only_seen_white = 0;
4636 CPP_PUTC (pfile, c);
4637 return token;
4642 /* Like cpp_get_token, but skip spaces and comments. */
4644 enum cpp_token
4645 cpp_get_non_space_token (pfile)
4646 cpp_reader *pfile;
4648 int old_written = CPP_WRITTEN (pfile);
4649 for (;;)
4651 enum cpp_token token = cpp_get_token (pfile);
4652 if (token != CPP_COMMENT && token != CPP_POP
4653 && token != CPP_HSPACE && token != CPP_VSPACE)
4654 return token;
4655 CPP_SET_WRITTEN (pfile, old_written);
4659 /* Parse an identifier starting with C. */
4661 static int
4662 parse_name (pfile, c)
4663 cpp_reader *pfile; int c;
4665 for (;;)
4667 if (! is_idchar[c])
4669 if (c == '\\' && PEEKC() == '\n')
4671 FORWARD(2);
4672 continue;
4674 FORWARD (-1);
4675 break;
4678 if (c == '$' && CPP_PEDANTIC (pfile))
4679 cpp_pedwarn (pfile, "`$' in identifier");
4681 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
4682 CPP_PUTC_Q (pfile, c);
4683 c = GETC();
4684 if (c == EOF)
4685 break;
4687 CPP_NUL_TERMINATE_Q (pfile);
4688 return 1;
4691 /* This is called after options have been processed.
4692 * Check options for consistency, and setup for processing input
4693 * from the file named FNAME. (Use standard input if FNAME==NULL.)
4694 * Return 1 on success, 0 on failure.
4698 cpp_start_read (pfile, fname)
4699 cpp_reader *pfile;
4700 char *fname;
4702 struct cpp_options *opts = CPP_OPTIONS (pfile);
4703 struct cpp_pending *pend;
4704 char *p;
4705 int f;
4706 cpp_buffer *fp;
4707 struct include_hash *ih_fake;
4709 /* The code looks at the defaults through this pointer, rather than through
4710 the constant structure above. This pointer gets changed if an environment
4711 variable specifies other defaults. */
4712 struct default_include *include_defaults = include_defaults_array;
4714 /* Now that we know dollars_in_ident for real,
4715 reset is_idchar/is_idstart. */
4716 is_idchar['$'] = opts->dollars_in_ident;
4717 is_idstart['$'] = opts->dollars_in_ident;
4719 /* Add dirs from CPATH after dirs from -I. */
4720 /* There seems to be confusion about what CPATH should do,
4721 so for the moment it is not documented. */
4722 /* Some people say that CPATH should replace the standard include dirs,
4723 but that seems pointless: it comes before them, so it overrides them
4724 anyway. */
4725 GET_ENV_PATH_LIST (p, "CPATH");
4726 if (p != 0 && ! opts->no_standard_includes)
4727 path_include (pfile, p);
4729 /* Do partial setup of input buffer for the sake of generating
4730 early #line directives (when -g is in effect). */
4731 fp = cpp_push_buffer (pfile, NULL, 0);
4732 if (!fp)
4733 return 0;
4734 if (opts->in_fname == NULL || *opts->in_fname == 0)
4736 opts->in_fname = fname;
4737 if (opts->in_fname == NULL)
4738 opts->in_fname = "";
4740 fp->nominal_fname = fp->fname = opts->in_fname;
4741 fp->lineno = 0;
4743 /* Install __LINE__, etc. Must follow initialize_char_syntax
4744 and option processing. */
4745 initialize_builtins (pfile);
4747 /* Do standard #defines and assertions
4748 that identify system and machine type. */
4750 if (!opts->inhibit_predefs) {
4751 char *p = (char *) alloca (strlen (predefs) + 1);
4752 strcpy (p, predefs);
4753 while (*p) {
4754 char *q;
4755 while (*p == ' ' || *p == '\t')
4756 p++;
4757 /* Handle -D options. */
4758 if (p[0] == '-' && p[1] == 'D') {
4759 q = &p[2];
4760 while (*p && *p != ' ' && *p != '\t')
4761 p++;
4762 if (*p != 0)
4763 *p++= 0;
4764 if (opts->debug_output)
4765 output_line_command (pfile, 0, same_file);
4766 cpp_define (pfile, q);
4767 while (*p == ' ' || *p == '\t')
4768 p++;
4769 } else if (p[0] == '-' && p[1] == 'A') {
4770 /* Handle -A options (assertions). */
4771 char *assertion;
4772 char *past_name;
4773 char *value;
4774 char *past_value;
4775 char *termination;
4776 int save_char;
4778 assertion = &p[2];
4779 past_name = assertion;
4780 /* Locate end of name. */
4781 while (*past_name && *past_name != ' '
4782 && *past_name != '\t' && *past_name != '(')
4783 past_name++;
4784 /* Locate `(' at start of value. */
4785 value = past_name;
4786 while (*value && (*value == ' ' || *value == '\t'))
4787 value++;
4788 if (*value++ != '(')
4789 abort ();
4790 while (*value && (*value == ' ' || *value == '\t'))
4791 value++;
4792 past_value = value;
4793 /* Locate end of value. */
4794 while (*past_value && *past_value != ' '
4795 && *past_value != '\t' && *past_value != ')')
4796 past_value++;
4797 termination = past_value;
4798 while (*termination && (*termination == ' ' || *termination == '\t'))
4799 termination++;
4800 if (*termination++ != ')')
4801 abort ();
4802 if (*termination && *termination != ' ' && *termination != '\t')
4803 abort ();
4804 /* Temporarily null-terminate the value. */
4805 save_char = *termination;
4806 *termination = '\0';
4807 /* Install the assertion. */
4808 make_assertion (pfile, "-A", assertion);
4809 *termination = (char) save_char;
4810 p = termination;
4811 while (*p == ' ' || *p == '\t')
4812 p++;
4813 } else {
4814 abort ();
4819 /* Now handle the command line options. */
4821 /* Do -U's, -D's and -A's in the order they were seen. */
4822 /* First reverse the list. */
4823 opts->pending = nreverse_pending (opts->pending);
4825 for (pend = opts->pending; pend; pend = pend->next)
4827 if (pend->cmd != NULL && pend->cmd[0] == '-')
4829 switch (pend->cmd[1])
4831 case 'U':
4832 if (opts->debug_output)
4833 output_line_command (pfile, 0, same_file);
4834 cpp_undef (pfile, pend->arg);
4835 break;
4836 case 'D':
4837 if (opts->debug_output)
4838 output_line_command (pfile, 0, same_file);
4839 cpp_define (pfile, pend->arg);
4840 break;
4841 case 'A':
4842 make_assertion (pfile, "-A", pend->arg);
4843 break;
4848 opts->done_initializing = 1;
4850 { /* Read the appropriate environment variable and if it exists
4851 replace include_defaults with the listed path. */
4852 char *epath = 0;
4853 switch ((opts->objc << 1) + opts->cplusplus)
4855 case 0:
4856 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
4857 break;
4858 case 1:
4859 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
4860 break;
4861 case 2:
4862 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
4863 break;
4864 case 3:
4865 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
4866 break;
4868 /* If the environment var for this language is set,
4869 add to the default list of include directories. */
4870 if (epath) {
4871 char *nstore = (char *) alloca (strlen (epath) + 2);
4872 int num_dirs;
4873 char *startp, *endp;
4875 for (num_dirs = 1, startp = epath; *startp; startp++)
4876 if (*startp == PATH_SEPARATOR)
4877 num_dirs++;
4878 include_defaults
4879 = (struct default_include *) xmalloc ((num_dirs
4880 * sizeof (struct default_include))
4881 + sizeof (include_defaults_array));
4882 startp = endp = epath;
4883 num_dirs = 0;
4884 while (1) {
4885 /* Handle cases like c:/usr/lib:d:/gcc/lib */
4886 if ((*endp == PATH_SEPARATOR)
4887 || *endp == 0) {
4888 strncpy (nstore, startp, endp-startp);
4889 if (endp == startp)
4890 strcpy (nstore, ".");
4891 else
4892 nstore[endp-startp] = '\0';
4894 include_defaults[num_dirs].fname = xstrdup (nstore);
4895 include_defaults[num_dirs].component = 0;
4896 include_defaults[num_dirs].cplusplus = opts->cplusplus;
4897 include_defaults[num_dirs].cxx_aware = 1;
4898 num_dirs++;
4899 if (*endp == '\0')
4900 break;
4901 endp = startp = endp + 1;
4902 } else
4903 endp++;
4905 /* Put the usual defaults back in at the end. */
4906 bcopy ((char *) include_defaults_array,
4907 (char *) &include_defaults[num_dirs],
4908 sizeof (include_defaults_array));
4912 /* Unless -fnostdinc,
4913 tack on the standard include file dirs to the specified list */
4914 if (!opts->no_standard_includes) {
4915 struct default_include *p = include_defaults;
4916 char *specd_prefix = opts->include_prefix;
4917 char *default_prefix = xstrdup (GCC_INCLUDE_DIR);
4918 int default_len = 0;
4919 /* Remove the `include' from /usr/local/lib/gcc.../include. */
4920 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
4921 default_len = strlen (default_prefix) - 7;
4922 default_prefix[default_len] = 0;
4924 /* Search "translated" versions of GNU directories.
4925 These have /usr/local/lib/gcc... replaced by specd_prefix. */
4926 if (specd_prefix != 0 && default_len != 0)
4927 for (p = include_defaults; p->fname; p++) {
4928 /* Some standard dirs are only for C++. */
4929 if (!p->cplusplus
4930 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
4931 /* Does this dir start with the prefix? */
4932 if (!strncmp (p->fname, default_prefix, default_len)) {
4933 /* Yes; change prefix and add to search list. */
4934 int this_len = strlen (specd_prefix)
4935 + strlen (p->fname) - default_len;
4936 char *str = (char *) xmalloc (this_len + 1);
4937 strcpy (str, specd_prefix);
4938 strcat (str, p->fname + default_len);
4940 append_include_chain (pfile, &opts->system_include,
4941 str, !p->cxx_aware);
4945 /* Search ordinary names for GNU include directories. */
4946 for (p = include_defaults; p->fname; p++) {
4947 /* Some standard dirs are only for C++. */
4948 if (!p->cplusplus
4949 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
4950 const char *str = update_path (p->fname, p->component);
4951 append_include_chain (pfile, &opts->system_include,
4952 str, !p->cxx_aware);
4957 merge_include_chains (opts);
4959 /* With -v, print the list of dirs to search. */
4960 if (opts->verbose) {
4961 struct file_name_list *p;
4962 fprintf (stderr, "#include \"...\" search starts here:\n");
4963 for (p = opts->quote_include; p; p = p->next) {
4964 if (p == opts->bracket_include)
4965 fprintf (stderr, "#include <...> search starts here:\n");
4966 fprintf (stderr, " %s\n", p->name);
4968 fprintf (stderr, "End of search list.\n");
4971 /* Copy the entire contents of the main input file into
4972 the stacked input buffer previously allocated for it. */
4973 if (fname == NULL || *fname == 0) {
4974 fname = "";
4975 f = 0;
4976 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
4977 cpp_pfatal_with_name (pfile, fname);
4979 /* -MG doesn't select the form of output and must be specified with one of
4980 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
4981 inhibit compilation. */
4982 if (opts->print_deps_missing_files
4983 && (opts->print_deps == 0 || !opts->no_output))
4985 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
4986 return 0;
4989 /* Either of two environment variables can specify output of deps.
4990 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
4991 where OUTPUT_FILE is the file to write deps info to
4992 and DEPS_TARGET is the target to mention in the deps. */
4994 if (opts->print_deps == 0
4995 && (getenv ("SUNPRO_DEPENDENCIES") != 0
4996 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
4997 char *spec = getenv ("DEPENDENCIES_OUTPUT");
4998 char *s;
4999 char *output_file;
5001 if (spec == 0)
5003 spec = getenv ("SUNPRO_DEPENDENCIES");
5004 opts->print_deps = 2;
5006 else
5007 opts->print_deps = 1;
5009 s = spec;
5010 /* Find the space before the DEPS_TARGET, if there is one. */
5011 /* This should use index. (mrs) */
5012 while (*s != 0 && *s != ' ') s++;
5013 if (*s != 0)
5015 opts->deps_target = s + 1;
5016 output_file = (char *) xmalloc (s - spec + 1);
5017 bcopy (spec, output_file, s - spec);
5018 output_file[s - spec] = 0;
5020 else
5022 opts->deps_target = 0;
5023 output_file = spec;
5026 opts->deps_file = output_file;
5027 opts->print_deps_append = 1;
5030 /* For -M, print the expected object file name
5031 as the target of this Make-rule. */
5032 if (opts->print_deps)
5034 pfile->deps_allocated_size = 200;
5035 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
5036 pfile->deps_buffer[0] = 0;
5037 pfile->deps_size = 0;
5038 pfile->deps_column = 0;
5040 if (opts->deps_target)
5041 deps_output (pfile, opts->deps_target, ':');
5042 else if (*opts->in_fname == 0)
5043 deps_output (pfile, "-", ':');
5044 else
5046 char *p, *q, *r;
5047 int len, x;
5048 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
5049 ".cc", ".cxx", ".cpp", ".cp",
5050 ".c++", 0
5053 /* Discard all directory prefixes from filename. */
5054 if ((q = rindex (opts->in_fname, '/')) != NULL
5055 #ifdef DIR_SEPARATOR
5056 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
5057 #endif
5059 ++q;
5060 else
5061 q = opts->in_fname;
5063 /* Copy remainder to mungable area. */
5064 p = (char *) alloca (strlen(q) + 8);
5065 strcpy (p, q);
5067 /* Output P, but remove known suffixes. */
5068 len = strlen (p);
5069 q = p + len;
5070 /* Point to the filename suffix. */
5071 r = rindex (p, '.');
5072 /* Compare against the known suffixes. */
5073 x = 0;
5074 while (known_suffixes[x] != 0)
5076 if (strncmp (known_suffixes[x], r, q - r) == 0)
5078 /* Make q point to the bit we're going to overwrite
5079 with an object suffix. */
5080 q = r;
5081 break;
5083 x++;
5086 /* Supply our own suffix. */
5087 #ifndef VMS
5088 strcpy (q, ".o");
5089 #else
5090 strcpy (q, ".obj");
5091 #endif
5093 deps_output (pfile, p, ':');
5094 deps_output (pfile, opts->in_fname, ' ');
5098 #if 0
5099 /* Make sure data ends with a newline. And put a null after it. */
5101 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5102 /* Backslash-newline at end is not good enough. */
5103 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5104 fp->buf[fp->length++] = '\n';
5105 missing_newline = 1;
5107 fp->buf[fp->length] = '\0';
5109 /* Unless inhibited, convert trigraphs in the input. */
5111 if (!no_trigraphs)
5112 trigraph_pcp (fp);
5113 #endif
5115 /* Must call finclude() on the main input before processing
5116 -include switches; otherwise the -included text winds up
5117 after the main input. */
5118 ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
5119 ih_fake->next = 0;
5120 ih_fake->next_this_file = 0;
5121 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5122 ih_fake->name = fname;
5123 ih_fake->control_macro = 0;
5124 ih_fake->buf = (char *)-1;
5125 ih_fake->limit = 0;
5126 if (!finclude (pfile, f, ih_fake))
5127 return 0;
5128 output_line_command (pfile, 0, same_file);
5129 pfile->only_seen_white = 2;
5131 /* The -imacros files can be scanned now, but the -include files
5132 have to be pushed onto the include stack and processed later,
5133 in the main loop calling cpp_get_token. That means the -include
5134 files have to be processed in reverse order of the pending list,
5135 which means the pending list has to be reversed again, which
5136 means the -imacros files have to be done separately and first. */
5138 pfile->no_record_file++;
5139 opts->no_output++;
5140 for (pend = opts->pending; pend; pend = pend->next)
5142 if (pend->cmd != NULL)
5144 if (strcmp (pend->cmd, "-imacros") == 0)
5146 int fd = open (pend->arg, O_RDONLY, 0666);
5147 if (fd < 0)
5149 cpp_perror_with_name (pfile, pend->arg);
5150 return 0;
5152 if (!cpp_push_buffer (pfile, NULL, 0))
5153 return 0;
5155 ih_fake = (struct include_hash *)
5156 xmalloc (sizeof (struct include_hash));
5157 ih_fake->next = 0;
5158 ih_fake->next_this_file = 0;
5159 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5160 ih_fake->name = pend->arg;
5161 ih_fake->control_macro = 0;
5162 ih_fake->buf = (char *)-1;
5163 ih_fake->limit = 0;
5164 if (!finclude (pfile, fd, ih_fake))
5165 cpp_scan_buffer (pfile);
5166 free (ih_fake);
5170 opts->no_output--;
5171 opts->pending = nreverse_pending (opts->pending);
5172 for (pend = opts->pending; pend; pend = pend->next)
5174 if (pend->cmd != NULL)
5176 if (strcmp (pend->cmd, "-include") == 0)
5178 int fd = open (pend->arg, O_RDONLY, 0666);
5179 if (fd < 0)
5181 cpp_perror_with_name (pfile, pend->arg);
5182 return 0;
5184 if (!cpp_push_buffer (pfile, NULL, 0))
5185 return 0;
5187 ih_fake = (struct include_hash *)
5188 xmalloc (sizeof (struct include_hash));
5189 ih_fake->next = 0;
5190 ih_fake->next_this_file = 0;
5191 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5192 ih_fake->name = pend->arg;
5193 ih_fake->control_macro = 0;
5194 ih_fake->buf = (char *)-1;
5195 ih_fake->limit = 0;
5196 if (finclude (pfile, fd, ih_fake))
5197 output_line_command (pfile, 0, enter_file);
5201 pfile->no_record_file--;
5203 /* Free the pending list. */
5204 for (pend = opts->pending; pend; )
5206 struct cpp_pending *next = pend->next;
5207 free (pend);
5208 pend = next;
5210 opts->pending = NULL;
5212 return 1;
5215 void
5216 cpp_reader_init (pfile)
5217 cpp_reader *pfile;
5219 bzero ((char *) pfile, sizeof (cpp_reader));
5220 pfile->get_token = cpp_get_token;
5222 pfile->token_buffer_size = 200;
5223 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
5224 CPP_SET_WRITTEN (pfile, 0);
5226 pfile->timebuf = NULL;
5227 pfile->only_seen_white = 1;
5228 pfile->buffer = CPP_NULL_BUFFER(pfile);
5229 pfile->actual_dirs = NULL;
5232 static struct cpp_pending *
5233 nreverse_pending (list)
5234 struct cpp_pending *list;
5237 register struct cpp_pending *prev = 0, *next, *pend;
5238 for (pend = list; pend; pend = next)
5240 next = pend->next;
5241 pend->next = prev;
5242 prev = pend;
5244 return prev;
5247 static void
5248 push_pending (pfile, cmd, arg)
5249 cpp_reader *pfile;
5250 char *cmd;
5251 char *arg;
5253 struct cpp_pending *pend
5254 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
5255 pend->cmd = cmd;
5256 pend->arg = arg;
5257 pend->next = CPP_OPTIONS (pfile)->pending;
5258 CPP_OPTIONS (pfile)->pending = pend;
5262 static void
5263 print_help ()
5265 printf ("Usage: %s [switches] input output\n", progname);
5266 printf ("Switches:\n");
5267 printf (" -include <file> Include the contents of <file> before other files\n");
5268 printf (" -imacros <file> Accept definition of marcos in <file>\n");
5269 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
5270 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
5271 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
5272 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
5273 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
5274 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
5275 printf (" -nostdinc Do not search the system include directories\n");
5276 printf (" -nostdinc++ Do not search the system include directories for C++\n");
5277 printf (" -o <file> Put output into <file>\n");
5278 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
5279 printf (" -traditional Follow K&R pre-processor behaviour\n");
5280 printf (" -trigraphs Support ANSI C trigraphs\n");
5281 printf (" -lang-c Assume that the input sources are in C\n");
5282 printf (" -lang-c89 Assume that the input sources are in C89\n");
5283 printf (" -lang-c++ Assume that the input sources are in C++\n");
5284 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
5285 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
5286 printf (" -lang-asm Assume that the input sources are in assembler\n");
5287 printf (" -lang-chill Assume that the input sources are in Chill\n");
5288 printf (" -+ Allow parsing of C++ style features\n");
5289 printf (" -w Inhibit warning messages\n");
5290 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
5291 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
5292 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
5293 printf (" -Wno-comment{s} Do not warn about comments\n");
5294 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
5295 printf (" a string if -traditional is specified\n");
5296 printf (" -Wno-traditional Do not warn about stringification\n");
5297 printf (" -Wundef Warn if an undefined macro is used by #if\n");
5298 printf (" -Wno-undef Do not warn about testing undefined macros\n");
5299 printf (" -Wimport Warn about the use of the #import directive\n");
5300 printf (" -Wno-import Do not warn about the use of #import\n");
5301 printf (" -Werror Treat all warnings as errors\n");
5302 printf (" -Wno-error Do not treat warnings as errors\n");
5303 printf (" -Wall Enable all preprocessor warnings\n");
5304 printf (" -M Generate make dependencies\n");
5305 printf (" -MM As -M, but ignore system header files\n");
5306 printf (" -MD As -M, but put output in a .d file\n");
5307 printf (" -MMD As -MD, but ignore system header files\n");
5308 printf (" -MG Treat missing header file as generated files\n");
5309 printf (" -g Include #define and #undef directives in the output\n");
5310 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
5311 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
5312 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
5313 printf (" -U<macro> Undefine <macro> \n");
5314 printf (" -u or -undef Do not predefine any macros\n");
5315 printf (" -v Display the version number\n");
5316 printf (" -H Print the name of header files as they are used\n");
5317 printf (" -C Do not discard comments\n");
5318 printf (" -dM Display a list of macro definitions active at end\n");
5319 printf (" -dD Preserve macro definitions in output\n");
5320 printf (" -dN As -dD except that only the names are preserved\n");
5321 printf (" -dI Include #include directives in the output\n");
5322 printf (" -ifoutput Describe skipped code blocks in output \n");
5323 printf (" -P Do not generate #line directives\n");
5324 printf (" -$ Do not include '$' in identifiers\n");
5325 printf (" -remap Remap file names when including files.\n");
5326 printf (" -h or --help Display this information\n");
5330 /* Handle one command-line option in (argc, argv).
5331 Can be called multiple times, to handle multiple sets of options.
5332 Returns number of strings consumed. */
5334 cpp_handle_option (pfile, argc, argv)
5335 cpp_reader *pfile;
5336 int argc;
5337 char **argv;
5339 struct cpp_options *opts = CPP_OPTIONS (pfile);
5340 int i = 0;
5342 if (user_label_prefix == NULL)
5343 user_label_prefix = USER_LABEL_PREFIX;
5345 if (argv[i][0] != '-') {
5346 if (opts->out_fname != NULL)
5348 print_help ();
5349 cpp_fatal (pfile, "Too many arguments");
5351 else if (opts->in_fname != NULL)
5352 opts->out_fname = argv[i];
5353 else
5354 opts->in_fname = argv[i];
5355 } else {
5356 switch (argv[i][1]) {
5358 missing_filename:
5359 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
5360 return argc;
5361 missing_dirname:
5362 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
5363 return argc;
5365 case 'f':
5366 if (!strcmp (argv[i], "-fleading-underscore"))
5367 user_label_prefix = "_";
5368 else if (!strcmp (argv[i], "-fno-leading-underscore"))
5369 user_label_prefix = "";
5370 break;
5372 case 'I': /* Add directory to path for includes. */
5373 if (!strcmp (argv[i] + 2, "-"))
5375 if (! opts->ignore_srcdir)
5377 opts->ignore_srcdir = 1;
5378 /* Don't use any preceding -I directories for #include <...>. */
5379 opts->quote_include = opts->bracket_include;
5380 opts->bracket_include = 0;
5383 else
5385 char *fname;
5386 if (argv[i][2] != 0)
5387 fname = argv[i] + 2;
5388 else if (i + 1 == argc)
5389 goto missing_dirname;
5390 else
5391 fname = argv[++i];
5392 append_include_chain (pfile, &opts->bracket_include, fname, 0);
5394 break;
5396 case 'i':
5397 /* Add directory to beginning of system include path, as a system
5398 include directory. */
5399 if (!strcmp (argv[i], "-isystem"))
5401 if (i + 1 == argc)
5402 goto missing_filename;
5403 append_include_chain (pfile, &opts->system_include, argv[++i], 1);
5405 /* Add directory to end of path for includes,
5406 with the default prefix at the front of its name. */
5407 else if (!strcmp (argv[i], "-iwithprefix"))
5409 char *fname;
5410 if (i + 1 == argc)
5411 goto missing_dirname;
5412 ++i;
5414 if (opts->include_prefix != 0)
5416 fname = xmalloc (strlen (opts->include_prefix)
5417 + strlen (argv[i]) + 1);
5418 strcpy (fname, opts->include_prefix);
5419 strcat (fname, argv[i]);
5421 else
5423 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5424 + strlen (argv[i]) + 1);
5425 strcpy (fname, GCC_INCLUDE_DIR);
5426 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5427 if (!strcmp (fname + strlen (fname) - 8, "/include"))
5428 fname[strlen (fname) - 7] = 0;
5429 strcat (fname, argv[i]);
5432 append_include_chain (pfile, &opts->system_include, fname, 0);
5434 /* Add directory to main path for includes,
5435 with the default prefix at the front of its name. */
5436 else if (!strcmp (argv[i], "-iwithprefix"))
5438 char *fname;
5439 if (i + 1 == argc)
5440 goto missing_dirname;
5441 ++i;
5443 if (opts->include_prefix != 0)
5445 fname = xmalloc (strlen (opts->include_prefix)
5446 + strlen (argv[i]) + 1);
5447 strcpy (fname, opts->include_prefix);
5448 strcat (fname, argv[i]);
5450 else
5452 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5453 + strlen (argv[i]) + 1);
5454 strcpy (fname, GCC_INCLUDE_DIR);
5455 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5456 if (!strcmp (fname + strlen (fname) - 8, "/include"))
5457 fname[strlen (fname) - 7] = 0;
5458 strcat (fname, argv[i]);
5461 append_include_chain (pfile, &opts->bracket_include, fname, 0);
5463 /* Add directory to end of path for includes. */
5464 else if (!strcmp (argv[i], "-idirafter"))
5466 if (i + 1 == argc)
5467 goto missing_dirname;
5468 append_include_chain (pfile, &opts->after_include, argv[++i], 0);
5470 else if (!strcmp (argv[i], "-include") || !strcmp (argv[i], "-imacros"))
5472 if (i + 1 == argc)
5473 goto missing_filename;
5474 else
5475 push_pending (pfile, argv[i], argv[i+1]), i++;
5477 else if (!strcmp (argv[i], "-iprefix"))
5479 if (i + 1 == argc)
5480 goto missing_filename;
5481 else
5482 opts->include_prefix = argv[++i];
5484 else if (!strcmp (argv[i], "-ifoutput"))
5485 opts->output_conditionals = 1;
5487 break;
5489 case 'o':
5490 if (opts->out_fname != NULL)
5492 cpp_fatal (pfile, "Output filename specified twice");
5493 return argc;
5495 if (i + 1 == argc)
5496 goto missing_filename;
5497 opts->out_fname = argv[++i];
5498 if (!strcmp (opts->out_fname, "-"))
5499 opts->out_fname = "";
5500 break;
5502 case 'p':
5503 if (!strcmp (argv[i], "-pedantic"))
5504 CPP_PEDANTIC (pfile) = 1;
5505 else if (!strcmp (argv[i], "-pedantic-errors")) {
5506 CPP_PEDANTIC (pfile) = 1;
5507 opts->pedantic_errors = 1;
5509 #if 0
5510 else if (!strcmp (argv[i], "-pcp")) {
5511 char *pcp_fname = argv[++i];
5512 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
5513 ? fopen (pcp_fname, "w")
5514 : fdopen (dup (fileno (stdout)), "w"));
5515 if (pcp_outfile == 0)
5516 cpp_pfatal_with_name (pfile, pcp_fname);
5517 no_precomp = 1;
5519 #endif
5520 break;
5522 case 't':
5523 if (!strcmp (argv[i], "-traditional")) {
5524 opts->traditional = 1;
5525 opts->cplusplus_comments = 0;
5526 } else if (!strcmp (argv[i], "-trigraphs")) {
5527 if (!opts->chill)
5528 opts->no_trigraphs = 0;
5530 break;
5532 case 'l':
5533 if (! strcmp (argv[i], "-lang-c"))
5534 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5535 opts->objc = 0;
5536 if (! strcmp (argv[i], "-lang-c89"))
5537 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
5538 opts->objc = 0;
5539 if (! strcmp (argv[i], "-lang-c++"))
5540 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5541 opts->objc = 0;
5542 if (! strcmp (argv[i], "-lang-objc"))
5543 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5544 opts->objc = 1;
5545 if (! strcmp (argv[i], "-lang-objc++"))
5546 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5547 opts->objc = 1;
5548 if (! strcmp (argv[i], "-lang-asm"))
5549 opts->lang_asm = 1;
5550 if (! strcmp (argv[i], "-lint"))
5551 opts->for_lint = 1;
5552 if (! strcmp (argv[i], "-lang-chill"))
5553 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
5554 opts->traditional = 1, opts->no_trigraphs = 1;
5555 break;
5557 case '+':
5558 opts->cplusplus = 1, opts->cplusplus_comments = 1;
5559 break;
5561 case 'w':
5562 opts->inhibit_warnings = 1;
5563 break;
5565 case 'W':
5566 if (!strcmp (argv[i], "-Wtrigraphs"))
5567 opts->warn_trigraphs = 1;
5568 else if (!strcmp (argv[i], "-Wno-trigraphs"))
5569 opts->warn_trigraphs = 0;
5570 else if (!strcmp (argv[i], "-Wcomment"))
5571 opts->warn_comments = 1;
5572 else if (!strcmp (argv[i], "-Wno-comment"))
5573 opts->warn_comments = 0;
5574 else if (!strcmp (argv[i], "-Wcomments"))
5575 opts->warn_comments = 1;
5576 else if (!strcmp (argv[i], "-Wno-comments"))
5577 opts->warn_comments = 0;
5578 else if (!strcmp (argv[i], "-Wtraditional"))
5579 opts->warn_stringify = 1;
5580 else if (!strcmp (argv[i], "-Wno-traditional"))
5581 opts->warn_stringify = 0;
5582 else if (!strcmp (argv[i], "-Wundef"))
5583 opts->warn_undef = 1;
5584 else if (!strcmp (argv[i], "-Wno-undef"))
5585 opts->warn_undef = 0;
5586 else if (!strcmp (argv[i], "-Wimport"))
5587 opts->warn_import = 1;
5588 else if (!strcmp (argv[i], "-Wno-import"))
5589 opts->warn_import = 0;
5590 else if (!strcmp (argv[i], "-Werror"))
5591 opts->warnings_are_errors = 1;
5592 else if (!strcmp (argv[i], "-Wno-error"))
5593 opts->warnings_are_errors = 0;
5594 else if (!strcmp (argv[i], "-Wall"))
5596 opts->warn_trigraphs = 1;
5597 opts->warn_comments = 1;
5599 break;
5601 case 'M':
5602 /* The style of the choices here is a bit mixed.
5603 The chosen scheme is a hybrid of keeping all options in one string
5604 and specifying each option in a separate argument:
5605 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
5606 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5607 -M[M][G][D file]. This is awkward to handle in specs, and is not
5608 as extensible. */
5609 /* ??? -MG must be specified in addition to one of -M or -MM.
5610 This can be relaxed in the future without breaking anything.
5611 The converse isn't true. */
5613 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
5614 if (!strcmp (argv[i], "-MG"))
5616 opts->print_deps_missing_files = 1;
5617 break;
5619 if (!strcmp (argv[i], "-M"))
5620 opts->print_deps = 2;
5621 else if (!strcmp (argv[i], "-MM"))
5622 opts->print_deps = 1;
5623 else if (!strcmp (argv[i], "-MD"))
5624 opts->print_deps = 2;
5625 else if (!strcmp (argv[i], "-MMD"))
5626 opts->print_deps = 1;
5627 /* For -MD and -MMD options, write deps on file named by next arg. */
5628 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
5630 if (i+1 == argc)
5631 goto missing_filename;
5632 opts->deps_file = argv[++i];
5634 else
5636 /* For -M and -MM, write deps on standard output
5637 and suppress the usual output. */
5638 opts->no_output = 1;
5640 break;
5642 case 'd':
5644 char *p = argv[i] + 2;
5645 char c;
5646 while ((c = *p++) != 0) {
5647 /* Arg to -d specifies what parts of macros to dump */
5648 switch (c) {
5649 case 'M':
5650 opts->dump_macros = dump_only;
5651 opts->no_output = 1;
5652 break;
5653 case 'N':
5654 opts->dump_macros = dump_names;
5655 break;
5656 case 'D':
5657 opts->dump_macros = dump_definitions;
5658 break;
5659 case 'I':
5660 opts->dump_includes = 1;
5661 break;
5665 break;
5667 case 'g':
5668 if (argv[i][2] == '3')
5669 opts->debug_output = 1;
5670 break;
5672 case '-':
5673 if (strcmp (argv[i], "--help") != 0)
5674 return i;
5675 print_help ();
5676 break;
5678 case 'v':
5679 fprintf (stderr, "GNU CPP version %s", version_string);
5680 #ifdef TARGET_VERSION
5681 TARGET_VERSION;
5682 #endif
5683 fprintf (stderr, "\n");
5684 opts->verbose = 1;
5685 break;
5687 case 'H':
5688 opts->print_include_names = 1;
5689 break;
5691 case 'D':
5692 if (argv[i][2] != 0)
5693 push_pending (pfile, "-D", argv[i] + 2);
5694 else if (i + 1 == argc)
5696 cpp_fatal (pfile, "Macro name missing after -D option");
5697 return argc;
5699 else
5700 i++, push_pending (pfile, "-D", argv[i]);
5701 break;
5703 case 'A':
5705 char *p;
5707 if (argv[i][2] != 0)
5708 p = argv[i] + 2;
5709 else if (i + 1 == argc)
5711 cpp_fatal (pfile, "Assertion missing after -A option");
5712 return argc;
5714 else
5715 p = argv[++i];
5717 if (!strcmp (p, "-")) {
5718 struct cpp_pending **ptr;
5719 /* -A- eliminates all predefined macros and assertions.
5720 Let's include also any that were specified earlier
5721 on the command line. That way we can get rid of any
5722 that were passed automatically in from GCC. */
5723 opts->inhibit_predefs = 1;
5724 for (ptr = &opts->pending; *ptr != NULL; )
5726 struct cpp_pending *pend = *ptr;
5727 if (pend->cmd && pend->cmd[0] == '-'
5728 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
5730 *ptr = pend->next;
5731 free (pend);
5733 else
5734 ptr = &pend->next;
5736 } else {
5737 push_pending (pfile, "-A", p);
5740 break;
5742 case 'U': /* JF #undef something */
5743 if (argv[i][2] != 0)
5744 push_pending (pfile, "-U", argv[i] + 2);
5745 else if (i + 1 == argc)
5747 cpp_fatal (pfile, "Macro name missing after -U option");
5748 return argc;
5750 else
5751 push_pending (pfile, "-U", argv[i+1]), i++;
5752 break;
5754 case 'C':
5755 opts->put_out_comments = 1;
5756 break;
5758 case 'E': /* -E comes from cc -E; ignore it. */
5759 break;
5761 case 'P':
5762 opts->no_line_commands = 1;
5763 break;
5765 case '$': /* Don't include $ in identifiers. */
5766 opts->dollars_in_ident = 0;
5767 break;
5769 case 'n':
5770 if (!strcmp (argv[i], "-nostdinc"))
5771 /* -nostdinc causes no default include directories.
5772 You must specify all include-file directories with -I. */
5773 opts->no_standard_includes = 1;
5774 else if (!strcmp (argv[i], "-nostdinc++"))
5775 /* -nostdinc++ causes no default C++-specific include directories. */
5776 opts->no_standard_cplusplus_includes = 1;
5777 #if 0
5778 else if (!strcmp (argv[i], "-noprecomp"))
5779 no_precomp = 1;
5780 #endif
5781 break;
5783 case 'r':
5784 if (!strcmp (argv[i], "-remap"))
5785 opts->remap = 1;
5786 break;
5788 case 'u':
5789 /* Sun compiler passes undocumented switch "-undef".
5790 Let's assume it means to inhibit the predefined symbols. */
5791 opts->inhibit_predefs = 1;
5792 break;
5794 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
5795 if (opts->in_fname == NULL) {
5796 opts->in_fname = "";
5797 break;
5798 } else if (opts->out_fname == NULL) {
5799 opts->out_fname = "";
5800 break;
5801 } /* else fall through into error */
5803 default:
5804 return i;
5808 return i + 1;
5811 /* Handle command-line options in (argc, argv).
5812 Can be called multiple times, to handle multiple sets of options.
5813 Returns if an unrecognized option is seen.
5814 Returns number of strings consumed. */
5817 cpp_handle_options (pfile, argc, argv)
5818 cpp_reader *pfile;
5819 int argc;
5820 char **argv;
5822 int i;
5823 int strings_processed;
5824 for (i = 0; i < argc; i += strings_processed)
5826 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
5827 if (strings_processed == 0)
5828 break;
5830 return i;
5833 void
5834 cpp_finish (pfile)
5835 cpp_reader *pfile;
5837 struct cpp_options *opts = CPP_OPTIONS (pfile);
5839 if (opts->print_deps)
5841 /* Stream on which to print the dependency information. */
5842 FILE *deps_stream;
5844 /* Don't actually write the deps file if compilation has failed. */
5845 if (pfile->errors == 0)
5847 char *deps_mode = opts->print_deps_append ? "a" : "w";
5848 if (opts->deps_file == 0)
5849 deps_stream = stdout;
5850 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
5851 cpp_pfatal_with_name (pfile, opts->deps_file);
5852 fputs (pfile->deps_buffer, deps_stream);
5853 putc ('\n', deps_stream);
5854 if (opts->deps_file)
5856 if (ferror (deps_stream) || fclose (deps_stream) != 0)
5857 cpp_fatal (pfile, "I/O error on output");
5862 #if 0
5863 /* Debugging: dump statistics on the include hash table. */
5865 struct include_hash *x;
5866 int i, j;
5868 for(i = 0; i < ALL_INCLUDE_HASHSIZE; i++)
5870 x = pfile->all_include_files[i];
5871 j = 0;
5872 while(x)
5874 j++;
5875 x = x->next;
5877 fprintf(stderr, "%d/%d ", i, j);
5879 fputc('\n', stderr);
5881 #endif
5885 /* Free resources used by PFILE.
5886 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
5888 void
5889 cpp_cleanup (pfile)
5890 cpp_reader *pfile;
5892 int i;
5893 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
5894 cpp_pop_buffer (pfile);
5896 if (pfile->token_buffer)
5898 free (pfile->token_buffer);
5899 pfile->token_buffer = NULL;
5902 if (pfile->deps_buffer)
5904 free (pfile->deps_buffer);
5905 pfile->deps_buffer = NULL;
5906 pfile->deps_allocated_size = 0;
5909 while (pfile->if_stack)
5911 IF_STACK_FRAME *temp = pfile->if_stack;
5912 pfile->if_stack = temp->next;
5913 free (temp);
5916 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
5918 struct include_hash *imp = pfile->all_include_files[i];
5919 while (imp)
5921 struct include_hash *next = imp->next;
5922 #if 0
5923 /* This gets freed elsewhere - I think. */
5924 free (imp->name);
5925 #endif
5926 free (imp);
5927 imp = next;
5929 pfile->all_include_files[i] = 0;
5932 cpp_hash_cleanup (pfile);
5935 /* Read an assertion into the token buffer, converting to
5936 canonical form: `#predicate(a n swe r)' The next non-whitespace
5937 character to read should be the first letter of the predicate.
5938 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
5939 with answer (see callers for why). In case of 0, an error has been
5940 printed. */
5941 static int
5942 parse_assertion (pfile)
5943 cpp_reader *pfile;
5945 int c, dropwhite;
5946 cpp_skip_hspace (pfile);
5947 c = PEEKC();
5948 if (! is_idstart[c])
5950 cpp_error (pfile, "assertion predicate is not an identifier");
5951 return 0;
5953 CPP_PUTC(pfile, '#');
5954 FORWARD(1);
5955 parse_name(pfile, c);
5957 c = PEEKC();
5958 if (c != '(')
5960 if (is_hor_space[c])
5961 cpp_skip_hspace (pfile);
5962 c = PEEKC();
5964 if (c != '(')
5965 return 1;
5967 CPP_PUTC(pfile, '(');
5968 FORWARD(1);
5969 dropwhite = 1;
5970 while ((c = GETC()) != ')')
5972 if (is_hor_space[c])
5974 if (! dropwhite)
5976 CPP_PUTC(pfile, ' ');
5977 dropwhite = 1;
5980 else if (c == '\\' && PEEKC() == '\n')
5981 FORWARD(1);
5982 else if (c == '\n' || c == EOF)
5984 if (c == '\n') FORWARD(-1);
5985 cpp_error (pfile, "un-terminated assertion answer");
5986 return 0;
5988 else
5990 CPP_PUTC(pfile, c);
5991 dropwhite = 0;
5995 if (pfile->limit[-1] == ' ')
5996 pfile->limit[-1] = ')';
5997 else if (pfile->limit[-1] == '(')
5999 cpp_error (pfile, "empty token sequence in assertion");
6000 return 0;
6002 else
6003 CPP_PUTC(pfile, ')');
6005 CPP_NUL_TERMINATE(pfile);
6006 return 2;
6009 static int
6010 do_assert (pfile, keyword)
6011 cpp_reader *pfile;
6012 struct directive *keyword ATTRIBUTE_UNUSED;
6014 char *sym;
6015 int ret, c;
6016 HASHNODE *base, *this;
6017 int baselen, thislen;
6019 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6020 && !CPP_BUFFER (pfile)->system_header_p)
6021 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6023 cpp_skip_hspace (pfile);
6024 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
6025 ret = parse_assertion (pfile);
6026 if (ret == 0)
6027 goto error;
6028 else if (ret == 1)
6030 cpp_error (pfile, "missing token-sequence in `#assert'");
6031 goto error;
6034 cpp_skip_hspace (pfile);
6035 c = PEEKC();
6036 if (c != EOF && c != '\n')
6038 cpp_error (pfile, "junk at end of `#assert'");
6039 goto error;
6042 thislen = strlen (sym);
6043 baselen = index (sym, '(') - sym;
6044 this = cpp_lookup (pfile, sym, thislen, -1);
6045 if (this)
6047 cpp_warning (pfile, "`%s' re-asserted", sym);
6048 goto error;
6051 base = cpp_lookup (pfile, sym, baselen, -1);
6052 if (! base)
6053 base = install (sym, baselen, T_ASSERT, 0, 0, -1);
6054 else if (base->type != T_ASSERT)
6056 /* Token clash - but with what?! */
6057 cpp_fatal (pfile,
6058 "cpp internal error: base->type != T_ASSERT in do_assert");
6059 goto error;
6062 this = install (sym, thislen, T_ASSERT, 0,
6063 (char *)base->value.aschain, -1);
6064 base->value.aschain = this;
6066 pfile->limit = sym; /* Pop */
6067 return 0;
6069 error:
6070 pfile->limit = sym; /* Pop */
6071 skip_rest_of_line (pfile);
6072 return 1;
6075 static int
6076 do_unassert (pfile, keyword)
6077 cpp_reader *pfile;
6078 struct directive *keyword ATTRIBUTE_UNUSED;
6080 int c, ret;
6081 char *sym;
6082 long baselen, thislen;
6083 HASHNODE *base, *this, *next;
6085 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6086 && !CPP_BUFFER (pfile)->system_header_p)
6087 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6089 cpp_skip_hspace (pfile);
6091 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
6092 ret = parse_assertion (pfile);
6093 if (ret == 0)
6094 goto error;
6096 cpp_skip_hspace (pfile);
6097 c = PEEKC ();
6098 if (c != EOF && c != '\n')
6099 cpp_error (pfile, "junk at end of `#unassert'");
6101 thislen = strlen (sym);
6102 if (ret == 1)
6104 base = cpp_lookup (pfile, sym, thislen, -1);
6105 if (! base)
6106 goto error; /* It isn't an error to #undef what isn't #defined,
6107 so it isn't an error to #unassert what isn't
6108 #asserted either. */
6110 for (this = base->value.aschain; this; this = next)
6112 next = this->value.aschain;
6113 delete_macro (this);
6115 delete_macro (base);
6117 else
6119 baselen = index (sym, '(') - sym;
6120 base = cpp_lookup (pfile, sym, baselen, -1);
6121 if (! base) goto error;
6122 this = cpp_lookup (pfile, sym, thislen, -1);
6123 if (! this) goto error;
6125 next = base;
6126 while (next->value.aschain != this)
6127 next = next->value.aschain;
6129 next->value.aschain = this->value.aschain;
6130 delete_macro (this);
6132 if (base->value.aschain == NULL)
6133 delete_macro (base); /* Last answer for this predicate deleted. */
6136 pfile->limit = sym; /* Pop */
6137 return 0;
6138 error:
6139 pfile->limit = sym; /* Pop */
6140 skip_rest_of_line (pfile);
6141 return 1;
6145 cpp_read_check_assertion (pfile)
6146 cpp_reader *pfile;
6148 char *name = CPP_PWRITTEN (pfile);
6149 int result;
6150 HASHNODE *hp;
6152 FORWARD (1); /* Skip '#' */
6153 cpp_skip_hspace (pfile);
6154 if (! parse_assertion (pfile))
6155 result = 0;
6156 else
6158 hp = cpp_lookup (pfile, name, (char *)CPP_PWRITTEN (pfile) - name, -1);
6159 result = (hp != 0);
6162 pfile->limit = name;
6163 return result;
6166 /* Initialize PMARK to remember the current position of PFILE. */
6168 void
6169 parse_set_mark (pmark, pfile)
6170 struct parse_marker *pmark;
6171 cpp_reader *pfile;
6173 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6174 pmark->next = pbuf->marks;
6175 pbuf->marks = pmark;
6176 pmark->buf = pbuf;
6177 pmark->position = pbuf->cur - pbuf->buf;
6180 /* Cleanup PMARK - we no longer need it. */
6182 void
6183 parse_clear_mark (pmark)
6184 struct parse_marker *pmark;
6186 struct parse_marker **pp = &pmark->buf->marks;
6187 for (; ; pp = &(*pp)->next) {
6188 if (*pp == NULL) abort ();
6189 if (*pp == pmark) break;
6191 *pp = pmark->next;
6194 /* Backup the current position of PFILE to that saved in PMARK. */
6196 void
6197 parse_goto_mark (pmark, pfile)
6198 struct parse_marker *pmark;
6199 cpp_reader *pfile;
6201 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6202 if (pbuf != pmark->buf)
6203 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
6204 pbuf->cur = pbuf->buf + pmark->position;
6207 /* Reset PMARK to point to the current position of PFILE. (Same
6208 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
6210 void
6211 parse_move_mark (pmark, pfile)
6212 struct parse_marker *pmark;
6213 cpp_reader *pfile;
6215 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6216 if (pbuf != pmark->buf)
6217 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
6218 pmark->position = pbuf->cur - pbuf->buf;
6222 void
6223 cpp_print_file_and_line (pfile)
6224 cpp_reader *pfile;
6226 cpp_buffer *ip = cpp_file_buffer (pfile);
6228 if (ip != NULL)
6230 long line, col;
6231 cpp_buf_line_and_col (ip, &line, &col);
6232 cpp_file_line_for_message (pfile, ip->nominal_fname,
6233 line, pfile->show_column ? col : -1);
6237 static void
6238 v_cpp_error (pfile, msg, ap)
6239 cpp_reader *pfile;
6240 const char *msg;
6241 va_list ap;
6243 cpp_print_containing_files (pfile);
6244 cpp_print_file_and_line (pfile);
6245 v_cpp_message (pfile, 1, msg, ap);
6248 void
6249 cpp_error VPROTO ((cpp_reader * pfile, const char *msg, ...))
6251 #ifndef ANSI_PROTOTYPES
6252 cpp_reader *pfile;
6253 const char *msg;
6254 #endif
6255 va_list ap;
6257 VA_START(ap, msg);
6259 #ifndef ANSI_PROTOTYPES
6260 pfile = va_arg (ap, cpp_reader *);
6261 msg = va_arg (ap, const char *);
6262 #endif
6264 v_cpp_error (pfile, msg, ap);
6265 va_end(ap);
6268 /* Print error message but don't count it. */
6270 static void
6271 v_cpp_warning (pfile, msg, ap)
6272 cpp_reader *pfile;
6273 const char *msg;
6274 va_list ap;
6276 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6277 return;
6279 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6280 pfile->errors++;
6282 cpp_print_containing_files (pfile);
6283 cpp_print_file_and_line (pfile);
6284 v_cpp_message (pfile, 0, msg, ap);
6287 void
6288 cpp_warning VPROTO ((cpp_reader * pfile, const char *msg, ...))
6290 #ifndef ANSI_PROTOTYPES
6291 cpp_reader *pfile;
6292 const char *msg;
6293 #endif
6294 va_list ap;
6296 VA_START (ap, msg);
6298 #ifndef ANSI_PROTOTYPES
6299 pfile = va_arg (ap, cpp_reader *);
6300 msg = va_arg (ap, const char *);
6301 #endif
6303 v_cpp_warning (pfile, msg, ap);
6304 va_end(ap);
6307 /* Print an error message and maybe count it. */
6309 void
6310 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msg, ...))
6312 #ifndef ANSI_PROTOTYPES
6313 cpp_reader *pfile;
6314 const char *msg;
6315 #endif
6316 va_list ap;
6318 VA_START (ap, msg);
6320 #ifndef ANSI_PROTOTYPES
6321 pfile = va_arg (ap, cpp_reader *);
6322 msg = va_arg (ap, const char *);
6323 #endif
6325 if (CPP_OPTIONS (pfile)->pedantic_errors)
6326 v_cpp_error (pfile, msg, ap);
6327 else
6328 v_cpp_warning (pfile, msg, ap);
6329 va_end(ap);
6332 static void
6333 v_cpp_error_with_line (pfile, line, column, msg, ap)
6334 cpp_reader * pfile;
6335 int line;
6336 int column;
6337 const char * msg;
6338 va_list ap;
6340 cpp_buffer *ip = cpp_file_buffer (pfile);
6342 cpp_print_containing_files (pfile);
6344 if (ip != NULL)
6345 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6347 v_cpp_message (pfile, 1, msg, ap);
6350 void
6351 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6353 #ifndef ANSI_PROTOTYPES
6354 cpp_reader *pfile;
6355 int line;
6356 int column;
6357 const char *msg;
6358 #endif
6359 va_list ap;
6361 VA_START (ap, msg);
6363 #ifndef ANSI_PROTOTYPES
6364 pfile = va_arg (ap, cpp_reader *);
6365 line = va_arg (ap, int);
6366 column = va_arg (ap, int);
6367 msg = va_arg (ap, const char *);
6368 #endif
6370 v_cpp_error_with_line(pfile, line, column, msg, ap);
6371 va_end(ap);
6374 static void
6375 v_cpp_warning_with_line (pfile, line, column, msg, ap)
6376 cpp_reader * pfile;
6377 int line;
6378 int column;
6379 const char *msg;
6380 va_list ap;
6382 cpp_buffer *ip;
6384 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6385 return;
6387 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6388 pfile->errors++;
6390 cpp_print_containing_files (pfile);
6392 ip = cpp_file_buffer (pfile);
6394 if (ip != NULL)
6395 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6397 v_cpp_message (pfile, 0, msg, ap);
6400 #if 0
6401 static void
6402 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6404 #ifndef ANSI_PROTOTYPES
6405 cpp_reader *pfile;
6406 int line;
6407 int column;
6408 const char *msg;
6409 #endif
6410 va_list ap;
6412 VA_START (ap, msg);
6414 #ifndef ANSI_PROTOTYPES
6415 pfile = va_arg (ap, cpp_reader *);
6416 line = va_arg (ap, int);
6417 column = va_arg (ap, int);
6418 msg = va_arg (ap, const char *);
6419 #endif
6421 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6422 va_end(ap);
6424 #endif
6426 void
6427 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6429 #ifndef ANSI_PROTOTYPES
6430 cpp_reader *pfile;
6431 int line;
6432 int column;
6433 const char *msg;
6434 #endif
6435 va_list ap;
6437 VA_START (ap, msg);
6439 #ifndef ANSI_PROTOTYPES
6440 pfile = va_arg (ap, cpp_reader *);
6441 line = va_arg (ap, int);
6442 column = va_arg (ap, int);
6443 msg = va_arg (ap, const char *);
6444 #endif
6446 if (CPP_OPTIONS (pfile)->pedantic_errors)
6447 v_cpp_error_with_line (pfile, column, line, msg, ap);
6448 else
6449 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6450 va_end(ap);
6453 /* Report a warning (or an error if pedantic_errors)
6454 giving specified file name and line number, not current. */
6456 void
6457 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line, const char *msg, ...))
6459 #ifndef ANSI_PROTOTYPES
6460 cpp_reader *pfile;
6461 char *file;
6462 int line;
6463 const char *msg;
6464 #endif
6465 va_list ap;
6467 VA_START (ap, msg);
6469 #ifndef ANSI_PROTOTYPES
6470 pfile = va_arg (ap, cpp_reader *);
6471 file = va_arg (ap, char *);
6472 line = va_arg (ap, int);
6473 msg = va_arg (ap, const char *);
6474 #endif
6476 if (!CPP_OPTIONS (pfile)->pedantic_errors
6477 && CPP_OPTIONS (pfile)->inhibit_warnings)
6478 return;
6479 if (file != NULL)
6480 cpp_file_line_for_message (pfile, file, line, -1);
6481 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msg, ap);
6482 va_end(ap);
6485 /* my_strerror - return the descriptive text associated with an
6486 `errno' code. */
6488 static char *
6489 my_strerror (errnum)
6490 int errnum;
6492 char *result;
6494 #ifndef VMS
6495 #ifndef HAVE_STRERROR
6496 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
6497 #else
6498 result = strerror (errnum);
6499 #endif
6500 #else /* VMS */
6501 /* VAXCRTL's strerror() takes an optional second argument, which only
6502 matters when the first argument is EVMSERR. However, it's simplest
6503 just to pass it unconditionally. `vaxc$errno' is declared in
6504 <errno.h>, and maintained by the library in parallel with `errno'.
6505 We assume that caller's `errnum' either matches the last setting of
6506 `errno' by the library or else does not have the value `EVMSERR'. */
6508 result = strerror (errnum, vaxc$errno);
6509 #endif
6511 if (!result)
6512 result = "undocumented I/O error";
6514 return result;
6517 /* Error including a message from `errno'. */
6519 void
6520 cpp_error_from_errno (pfile, name)
6521 cpp_reader *pfile;
6522 const char *name;
6524 cpp_message_from_errno (pfile, 1, name);
6527 void
6528 cpp_message_from_errno (pfile, is_error, name)
6529 cpp_reader *pfile;
6530 int is_error;
6531 const char *name;
6533 int e = errno;
6534 cpp_buffer *ip = cpp_file_buffer (pfile);
6536 cpp_print_containing_files (pfile);
6538 if (ip != NULL)
6539 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
6541 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
6544 void
6545 cpp_perror_with_name (pfile, name)
6546 cpp_reader *pfile;
6547 const char *name;
6549 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
6552 /* TODO:
6553 * No pre-compiled header file support.
6555 * Possibly different enum token codes for each C/C++ token.
6557 * Find and cleanup remaining uses of static variables,
6559 * Support for trigraphs.
6561 * Support -dM flag (dump_all_macros).
6563 * Support for_lint flag.