Bump for snapshot
[gcc.git] / gcc / cpplib.c
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
6
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.
11
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.
16
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. */
20
21 #include "config.h"
22 #include "system.h"
23
24 #ifndef STDC_VALUE
25 #define STDC_VALUE 1
26 #endif
27
28 #include <signal.h>
29
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
32 #endif
33
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
36 #endif
37
38 #include "cpplib.h"
39 #include "cpphash.h"
40 #include "output.h"
41 #include "prefix.h"
42
43 #ifndef GET_ENV_PATH_LIST
44 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
45 #endif
46
47 /* By default, colon separates directories in a path. */
48 #ifndef PATH_SEPARATOR
49 #define PATH_SEPARATOR ':'
50 #endif
51
52 #ifndef STANDARD_INCLUDE_DIR
53 #define STANDARD_INCLUDE_DIR "/usr/include"
54 #endif
55
56 /* Symbols to predefine. */
57
58 #ifdef CPP_PREDEFINES
59 static char *predefs = CPP_PREDEFINES;
60 #else
61 static char *predefs = "";
62 #endif
63 \f
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. */
69
70 /* The string value for __SIZE_TYPE__. */
71
72 #ifndef SIZE_TYPE
73 #define SIZE_TYPE "long unsigned int"
74 #endif
75
76 /* The string value for __PTRDIFF_TYPE__. */
77
78 #ifndef PTRDIFF_TYPE
79 #define PTRDIFF_TYPE "long int"
80 #endif
81
82 /* The string value for __WCHAR_TYPE__. */
83
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)
89
90 /* The string value for __USER_LABEL_PREFIX__ */
91
92 #ifndef USER_LABEL_PREFIX
93 #define USER_LABEL_PREFIX ""
94 #endif
95
96 /* The string value for __REGISTER_PREFIX__ */
97
98 #ifndef REGISTER_PREFIX
99 #define REGISTER_PREFIX ""
100 #endif
101 \f
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)
104
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)
112
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). */
119
120 #define NEWLINE_FIX \
121 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
122
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)
127
128 struct cpp_pending {
129 struct cpp_pending *next;
130 char *cmd;
131 char *arg;
132 };
133
134 /* Forward declarations. */
135
136 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
137
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 *));
160
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));
166
167 /* Last arg to output_line_command. */
168 enum file_change_code {same_file, enter_file, leave_file};
169
170 /* External declarations. */
171
172 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
173
174 extern char *version_string;
175 extern struct tm *localtime ();
176 \f
177
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
193 = {
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 }
231 };
232 #endif /* no INCLUDE_DEFAULTS */
233
234 /* `struct directive' defines one #-directive, including how to handle it. */
235
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. */
242 };
243
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. */
247
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 *));
266
267 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
268 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
269
270 /* Here is the actual list of #-directives, most-often-used first.
271 The initialize_builtins function assumes #define is the very first. */
272
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 }
296 };
297 \f
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 };
306
307 /* Initialize syntactic classifications of characters. */
308 static void
309 initialize_char_syntax ()
310 {
311 register int i;
312
313 /*
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.
319 */
320 for (i = 0; i < 256; i++)
321 {
322 is_idchar[i] = ISALNUM (i);
323 is_idstart[i] = ISALPHA (i);
324 }
325
326 is_idchar['_'] = 1;
327 is_idstart['_'] = 1;
328
329 /* These will be reset later if -$ is in effect. */
330 is_idchar['$'] = 1;
331 is_idstart['$'] = 1;
332
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;
339
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;
346 }
347
348
349 /* Place into PFILE a quoted string representing the string SRC.
350 Caller must reserve enough space in pfile->token_buffer. */
351
352 static void
353 quote_string (pfile, src)
354 cpp_reader *pfile;
355 char *src;
356 {
357 U_CHAR c;
358
359 CPP_PUTC_Q (pfile, '\"');
360 for (;;)
361 switch ((c = *src++))
362 {
363 default:
364 if (ISPRINT (c))
365 CPP_PUTC_Q (pfile, c);
366 else
367 {
368 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
369 CPP_ADJUST_WRITTEN (pfile, 4);
370 }
371 break;
372
373 case '\"':
374 case '\\':
375 CPP_PUTC_Q (pfile, '\\');
376 CPP_PUTC_Q (pfile, c);
377 break;
378
379 case '\0':
380 CPP_PUTC_Q (pfile, '\"');
381 CPP_NUL_TERMINATE_Q (pfile);
382 return;
383 }
384 }
385
386 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
387
388 void
389 cpp_grow_buffer (pfile, n)
390 cpp_reader *pfile;
391 long n;
392 {
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);
398 }
399
400 \f
401 /*
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.
406 */
407
408 void
409 cpp_define (pfile, str)
410 cpp_reader *pfile;
411 U_CHAR *str;
412 {
413 U_CHAR *buf, *p;
414
415 buf = str;
416 p = str;
417 if (!is_idstart[*p])
418 {
419 cpp_error (pfile, "malformed option `-D %s'", str);
420 return;
421 }
422 while (is_idchar[*++p])
423 ;
424 if (*p == '(') {
425 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
426 ;
427 if (*p++ != ')')
428 p = (U_CHAR *) str; /* Error */
429 }
430 if (*p == 0)
431 {
432 buf = (U_CHAR *) alloca (p - buf + 4);
433 strcpy ((char *)buf, str);
434 strcat ((char *)buf, " 1");
435 }
436 else if (*p != '=')
437 {
438 cpp_error (pfile, "malformed option `-D %s'", str);
439 return;
440 }
441 else
442 {
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)
453 {
454 if (*p == '\\' && p[1] == '\n')
455 p += 2;
456 else
457 *q++ = *p++;
458 }
459 *q = 0;
460 }
461
462 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
463 {
464 do_define (pfile, NULL);
465 cpp_pop_buffer (pfile);
466 }
467 }
468 \f
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. */
471
472 static void
473 make_assertion (pfile, option, str)
474 cpp_reader *pfile;
475 char *option;
476 U_CHAR *str;
477 {
478 U_CHAR *buf, *p, *q;
479
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++;
492 }
493 *q = 0;
494
495 p = buf;
496 if (!is_idstart[*p]) {
497 cpp_error (pfile, "malformed option `%s %s'", option, str);
498 return;
499 }
500 while (is_idchar[*++p])
501 ;
502 while (*p == ' ' || *p == '\t') p++;
503 if (! (*p == 0 || *p == '(')) {
504 cpp_error (pfile, "malformed option `%s %s'", option, str);
505 return;
506 }
507
508 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
509 {
510 do_assert (pfile, NULL);
511 cpp_pop_buffer (pfile);
512 }
513 }
514
515 /* Given a colon-separated list of file names PATH,
516 add all the names to the search path for include files. */
517
518 static void
519 path_include (pfile, path)
520 cpp_reader *pfile;
521 char *path;
522 {
523 char *p;
524
525 p = path;
526
527 if (*p)
528 while (1) {
529 char *q = p;
530 char *name;
531
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;
544 }
545
546 append_include_chain (pfile,
547 &(CPP_OPTIONS (pfile)->bracket_include), name, 0);
548
549 /* Advance past this name. */
550 p = q;
551 if (*p == 0)
552 break;
553 /* Skip the colon. */
554 p++;
555 }
556 }
557 \f
558 void
559 cpp_options_init (opts)
560 cpp_options *opts;
561 {
562 bzero ((char *) opts, sizeof *opts);
563 opts->in_fname = NULL;
564 opts->out_fname = NULL;
565
566 opts->dollars_in_ident = 1;
567 initialize_char_syntax ();
568
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;
578
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;
589 }
590
591 enum cpp_token
592 null_underflow (pfile)
593 cpp_reader *pfile ATTRIBUTE_UNUSED;
594 {
595 return CPP_EOF;
596 }
597
598 int
599 null_cleanup (pbuf, pfile)
600 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
601 cpp_reader *pfile ATTRIBUTE_UNUSED;
602 {
603 return 0;
604 }
605
606 int
607 macro_cleanup (pbuf, pfile)
608 cpp_buffer *pbuf;
609 cpp_reader *pfile ATTRIBUTE_UNUSED;
610 {
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;
617 }
618
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 '/'. */
624
625 static int
626 skip_comment (pfile, linep)
627 cpp_reader *pfile;
628 long *linep;
629 {
630 int c = 0;
631 while (PEEKC() == '\\' && PEEKN(1) == '\n')
632 {
633 if (linep)
634 (*linep)++;
635 FORWARD(2);
636 }
637 if (PEEKC() == '*')
638 {
639 FORWARD(1);
640 for (;;)
641 {
642 int prev_c = c;
643 c = GETC ();
644 if (c == EOF)
645 return EOF;
646 while (c == '\\' && PEEKC() == '\n')
647 {
648 if (linep)
649 (*linep)++;
650 FORWARD(1), c = GETC();
651 }
652 if (prev_c == '*' && c == '/')
653 return ' ';
654 if (c == '\n' && linep)
655 (*linep)++;
656 }
657 }
658 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
659 {
660 FORWARD(1);
661 for (;;)
662 {
663 c = GETC ();
664 if (c == EOF)
665 return ' '; /* Allow // to be terminated by EOF. */
666 while (c == '\\' && PEEKC() == '\n')
667 {
668 FORWARD(1);
669 c = GETC();
670 if (linep)
671 (*linep)++;
672 }
673 if (c == '\n')
674 {
675 /* Don't consider final '\n' to be part of comment. */
676 FORWARD(-1);
677 return ' ';
678 }
679 }
680 }
681 else
682 return '/';
683 }
684
685 /* Skip whitespace \-newline and comments. Does not macro-expand. */
686
687 void
688 cpp_skip_hspace (pfile)
689 cpp_reader *pfile;
690 {
691 while (1)
692 {
693 int c = PEEKC();
694 if (c == EOF)
695 return; /* FIXME */
696 if (is_hor_space[c])
697 {
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);
702 }
703 else if (c == '/')
704 {
705 FORWARD (1);
706 c = skip_comment (pfile, NULL);
707 if (c == '/')
708 FORWARD(-1);
709 if (c == EOF || c == '/')
710 return;
711 }
712 else if (c == '\\' && PEEKN(1) == '\n') {
713 FORWARD(2);
714 }
715 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
716 && is_hor_space[PEEKN(1)])
717 FORWARD(2);
718 else return;
719 }
720 }
721
722 /* Read the rest of the current line.
723 The line is appended to PFILE's output buffer. */
724
725 static void
726 copy_rest_of_line (pfile)
727 cpp_reader *pfile;
728 {
729 struct cpp_options *opts = CPP_OPTIONS (pfile);
730 for (;;)
731 {
732 int c = GETC();
733 int nextc;
734 switch (c)
735 {
736 case EOF:
737 goto end_directive;
738 case '\\':
739 if (PEEKC() == '\n')
740 {
741 FORWARD (1);
742 continue;
743 }
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;
759
760 case '\n':
761 FORWARD(-1);
762 goto end_directive;
763 scan_directive_token:
764 FORWARD(-1);
765 cpp_get_token (pfile);
766 continue;
767 }
768 CPP_PUTC (pfile, c);
769 }
770 end_directive: ;
771 CPP_NUL_TERMINATE (pfile);
772 }
773
774 void
775 skip_rest_of_line (pfile)
776 cpp_reader *pfile;
777 {
778 long old = CPP_WRITTEN (pfile);
779 copy_rest_of_line (pfile);
780 CPP_SET_WRITTEN (pfile, old);
781 }
782
783 /* Handle a possible # directive.
784 '#' has already been read. */
785
786 int
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);
794
795 cpp_skip_hspace (pfile);
796
797 c = PEEKC ();
798 if (c >= '0' && c <= '9')
799 {
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;
805 }
806
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')
813 {
814 /* A line of just `#' becomes blank. */
815 goto done_a_directive;
816 }
817
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++;
825 }
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;
833 }
834 }
835
836 if (!lang_asm)
837 cpp_error (pfile, "invalid preprocessor directive name");
838
839 return 0;
840 }
841 #endif
842 /*
843 * Decode the keyword and call the appropriate expansion
844 * routine, after moving the input pointer up to the next line.
845 */
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;
852 }
853
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. */
858
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);
864
865 (*kt->func) (pfile, kt);
866
867 if (kt->type == T_DEFINE)
868 {
869 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
870 {
871 /* Skip "#define". */
872 U_CHAR *p = pfile->token_buffer + old_written + 7;
873
874 SKIP_WHITE_SPACE (p);
875 while (is_idchar[*p]) p++;
876 pfile->limit = p;
877 CPP_PUTC (pfile, '\n');
878 }
879 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
880 CPP_SET_WRITTEN (pfile, old_written);
881 }
882
883 done_a_directive:
884 return 1;
885
886 not_a_directive:
887 return 0;
888 }
889
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. */
894
895 static void
896 pass_thru_directive (buf, limit, pfile, keyword)
897 U_CHAR *buf, *limit;
898 cpp_reader *pfile;
899 struct directive *keyword;
900 {
901 register unsigned keyword_length = keyword->length;
902
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
915 }
916 \f
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. */
927
928 struct arglist {
929 struct arglist *next;
930 U_CHAR *name;
931 int length;
932 int argno;
933 char rest_args;
934 };
935
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.
944
945 Note that comments, backslash-newlines, and leading white space
946 have already been deleted from the argument. */
947
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;
954 {
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';
964
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. */
969
970 if (limit < buf)
971 abort ();
972
973 /* Find the beginning of the trailing whitespace. */
974 p = buf;
975 while (p < limit && is_space[limit[-1]]) limit--;
976
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);
989
990 defn->nargs = nargs;
991 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
992 lastp = exp_p;
993
994 p = buf;
995
996 /* Add one initial space escape-marker to prevent accidental
997 token-pasting (often removed by macroexpand). */
998 *exp_p++ = '@';
999 *exp_p++ = ' ';
1000
1001 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1002 cpp_error (pfile, "`##' at start of macro definition");
1003 p += 2;
1004 }
1005
1006 /* Process the main body of the definition. */
1007 while (p < limit) {
1008 int skipped_arg = 0;
1009 register U_CHAR c = *p++;
1010
1011 *exp_p++ = c;
1012
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;
1023
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++;
1029 }
1030 break;
1031
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;
1038
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;
1067 }
1068 break;
1069 }
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;
1084
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;
1091 }
1092 break;
1093
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
1109 }
1110 break;
1111 }
1112 }
1113
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;
1118
1119 --exp_p;
1120 while (p != limit && is_idchar[*p]) p++;
1121 id_len = p - id_beg;
1122
1123 if (is_idstart[c]
1124 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1125 register struct arglist *arg;
1126
1127 for (arg = arglist; arg != NULL; arg = arg->next) {
1128 struct reflist *tpat;
1129
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);
1141 }
1142 }
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);
1156
1157 if (endpat == NULL)
1158 defn->pattern = tpat;
1159 else
1160 endpat->next = tpat;
1161 endpat = tpat;
1162
1163 tpat->argno = arg->argno;
1164 tpat->nchars = exp_p - lastp;
1165 {
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;
1170 }
1171 lastp = exp_p; /* place to start copying from next time */
1172 skipped_arg = 1;
1173 break;
1174 }
1175 }
1176 }
1177
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");
1187 }
1188 }
1189 }
1190
1191 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1192 {
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++ = ' ';
1198 }
1199
1200 *exp_p = '\0';
1201
1202 defn->length = exp_p - defn->expansion;
1203
1204 /* Crash now if we overrun the allocated size. */
1205 if (defn->length + 1 > maxsize)
1206 abort ();
1207
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
1213
1214 return defn;
1215 }
1216
1217 /*
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); {
1228 */
1229 static char rest_extension[] = "...";
1230 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1231
1232 /* Create a DEFINITION node from a #define directive. Arguments are
1233 as for do_define. */
1234
1235 static MACRODEF
1236 create_definition (buf, limit, pfile, predefinition)
1237 U_CHAR *buf, *limit;
1238 cpp_reader *pfile;
1239 int predefinition;
1240 {
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);
1252
1253 bp = buf;
1254
1255 while (is_hor_space[*bp])
1256 bp++;
1257
1258 symname = bp; /* remember where it starts */
1259
1260 sym_length = check_macro_name (pfile, bp, "macro");
1261 bp += sym_length;
1262
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. */
1266
1267 if (*bp == '(') {
1268 struct arglist *arg_ptrs = NULL;
1269 int argno = 0;
1270
1271 bp++; /* skip '(' */
1272 SKIP_WHITE_SPACE (bp);
1273
1274 /* Loop over macro argument names. */
1275 while (*bp != ')') {
1276 struct arglist *temp;
1277
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;
1284
1285 if (rest_args)
1286 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1287
1288 if (!is_idstart[*bp])
1289 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1290
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;
1300 }
1301 }
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;
1310 }
1311 if (*bp == ',') {
1312 bp++;
1313 SKIP_WHITE_SPACE (bp);
1314 }
1315 if (bp >= limit) {
1316 cpp_error (pfile, "unterminated parameter list in `#define'");
1317 goto nope;
1318 }
1319 {
1320 struct arglist *otemp;
1321
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;
1326
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;
1333 }
1334 }
1335 }
1336
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;
1342
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);
1347 {
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++] = ' ';
1356 }
1357 }
1358 defn->args.argnames[i] = 0;
1359 }
1360 } else {
1361 /* Simple expansion or empty definition. */
1362
1363 if (bp < limit)
1364 {
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;
1378
1379 default:
1380 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1381 sym_length, symname);
1382 break;
1383 }
1384 }
1385 }
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 *) "";
1389 }
1390
1391 defn->line = line;
1392 defn->file = file;
1393
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;
1399
1400 return mdef;
1401
1402 nope:
1403 mdef.defn = 0;
1404 return mdef;
1405 }
1406
1407 /* Check a purported macro name SYMNAME, and yield its length.
1408 USAGE is the kind of name this is intended for. */
1409
1410 static int
1411 check_macro_name (pfile, symname, usage)
1412 cpp_reader *pfile;
1413 U_CHAR *symname;
1414 char *usage;
1415 {
1416 U_CHAR *p;
1417 int sym_length;
1418
1419 for (p = symname; is_idchar[*p]; p++)
1420 ;
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);
1434 }
1435 return sym_length;
1436 }
1437
1438 /* Return zero if two DEFINITIONs are isomorphic. */
1439
1440 static int
1441 compare_defs (pfile, d1, d2)
1442 cpp_reader *pfile;
1443 DEFINITION *d1, *d2;
1444 {
1445 register struct reflist *a1, *a2;
1446 register U_CHAR *p1 = d1->expansion;
1447 register U_CHAR *p2 = d2->expansion;
1448 int first = 1;
1449
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;
1467 }
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;
1474 }
1475
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. */
1484
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;
1491 {
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++;
1497 }
1498 if (last) {
1499 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1500 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1501 }
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;
1509 }
1510 return (beg1 != end1) || (beg2 != end2);
1511 }
1512
1513 /* Process a #define command.
1514 KEYWORD is the keyword-table entry for #define,
1515 or NULL for a "predefined" macro. */
1516
1517 static int
1518 do_define (pfile, keyword)
1519 cpp_reader *pfile;
1520 struct directive *keyword;
1521 {
1522 int hashcode;
1523 MACRODEF mdef;
1524 HASHNODE *hp;
1525 int save_put_out_comments;
1526 long here;
1527 U_CHAR *macro, *buf, *end;
1528
1529 here = CPP_WRITTEN (pfile);
1530
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;
1535
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);
1542
1543 CPP_SET_WRITTEN (pfile, here);
1544
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
1550
1551 mdef = create_definition (macro, end, pfile, keyword == NULL);
1552 if (mdef.defn == 0)
1553 goto nope;
1554
1555 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1556
1557 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1558 {
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)
1571 {
1572 U_CHAR *msg; /* what pain... */
1573
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);
1578
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");
1587 }
1588 /* Replace the old definition. */
1589 hp->type = T_MACRO;
1590 hp->value.defn = mdef.defn;
1591 }
1592 else
1593 {
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);
1600 }
1601
1602 return 0;
1603
1604 nope:
1605
1606 return 1;
1607 }
1608
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. */
1618
1619 /* raw and expanded are relative to ARG_BASE */
1620 #define ARG_BASE ((pfile)->token_buffer)
1621
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;
1629 };
1630
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. */
1635
1636 cpp_buffer *
1637 cpp_push_buffer (pfile, buffer, length)
1638 cpp_reader *pfile;
1639 U_CHAR *buffer;
1640 long length;
1641 {
1642 register cpp_buffer *buf = CPP_BUFFER (pfile);
1643 if (buf == pfile->buffer_stack)
1644 {
1645 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1646 buf->fname);
1647 return NULL;
1648 }
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;
1657
1658 return buf;
1659 }
1660
1661 cpp_buffer *
1662 cpp_pop_buffer (pfile)
1663 cpp_reader *pfile;
1664 {
1665 cpp_buffer *buf = CPP_BUFFER (pfile);
1666 (*buf->cleanup) (buf, pfile);
1667 return ++CPP_BUFFER (pfile);
1668 }
1669
1670 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1671 Pop the buffer when done. */
1672
1673 void
1674 cpp_scan_buffer (pfile)
1675 cpp_reader *pfile;
1676 {
1677 cpp_buffer *buffer = CPP_BUFFER (pfile);
1678 for (;;)
1679 {
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)
1684 {
1685 cpp_pop_buffer (pfile);
1686 break;
1687 }
1688 }
1689 }
1690
1691 /*
1692 * Rescan a string (which may have escape marks) into pfile's buffer.
1693 * Place the result in pfile->token_buffer.
1694 *
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.
1698 */
1699
1700 static void
1701 cpp_expand_to_buffer (pfile, buf, length)
1702 cpp_reader *pfile;
1703 U_CHAR *buf;
1704 int length;
1705 {
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
1715
1716 if (length < 0)
1717 abort ();
1718
1719 /* Set up the input on the input stack. */
1720
1721 buf1 = (U_CHAR *) alloca (length + 1);
1722 {
1723 register U_CHAR *p1 = buf;
1724 register U_CHAR *p2 = buf1;
1725
1726 while (p1 != limit)
1727 *p2++ = *p1++;
1728 }
1729 buf1[length] = 0;
1730
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
1738
1739 /* Scan the input, create the output. */
1740 cpp_scan_buffer (pfile);
1741
1742 #if 0
1743 if (indepth != odepth)
1744 abort ();
1745 #endif
1746
1747 CPP_NUL_TERMINATE (pfile);
1748 }
1749
1750 \f
1751 static void
1752 adjust_position (buf, limit, linep, colp)
1753 U_CHAR *buf;
1754 U_CHAR *limit;
1755 long *linep;
1756 long *colp;
1757 {
1758 while (buf < limit)
1759 {
1760 U_CHAR ch = *buf++;
1761 if (ch == '\n')
1762 (*linep)++, (*colp) = 1;
1763 else
1764 (*colp)++;
1765 }
1766 }
1767
1768 /* Move line_base forward, updating lineno and colno. */
1769
1770 static void
1771 update_position (pbuf)
1772 register cpp_buffer *pbuf;
1773 {
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)
1778 {
1779 if (pbuf->buf + mark->position < new_pos)
1780 new_pos = pbuf->buf + mark->position;
1781 }
1782 pbuf->line_base += new_pos - old_pos;
1783 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1784 }
1785
1786 void
1787 cpp_buf_line_and_col (pbuf, linep, colp)
1788 register cpp_buffer *pbuf;
1789 long *linep, *colp;
1790 {
1791 long dummy;
1792 if (colp == NULL)
1793 colp = &dummy;
1794 if (pbuf)
1795 {
1796 *linep = pbuf->lineno;
1797 *colp = pbuf->colno;
1798 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
1799 }
1800 else
1801 {
1802 *linep = 0;
1803 *colp = 0;
1804 }
1805 }
1806
1807 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1808
1809 cpp_buffer *
1810 cpp_file_buffer (pfile)
1811 cpp_reader *pfile;
1812 {
1813 cpp_buffer *ip = CPP_BUFFER (pfile);
1814
1815 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
1816 if (ip->fname != NULL)
1817 return ip;
1818 return NULL;
1819 }
1820
1821 static long
1822 count_newlines (buf, limit)
1823 register U_CHAR *buf;
1824 register U_CHAR *limit;
1825 {
1826 register long count = 0;
1827 while (buf < limit)
1828 {
1829 U_CHAR ch = *buf++;
1830 if (ch == '\n')
1831 count++;
1832 }
1833 return count;
1834 }
1835
1836 /*
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.
1842 */
1843
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;
1849 {
1850 long line, col;
1851 cpp_buffer *ip = CPP_BUFFER (pfile);
1852
1853 if (ip->fname == NULL)
1854 return;
1855
1856 update_position (ip);
1857
1858 if (CPP_OPTIONS (pfile)->no_line_commands
1859 || CPP_OPTIONS (pfile)->no_output)
1860 return;
1861
1862 line = CPP_BUFFER (pfile)->lineno;
1863 col = CPP_BUFFER (pfile)->colno;
1864 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
1865
1866 if (CPP_OPTIONS (pfile)->no_line_commands)
1867 return;
1868
1869 if (conditional) {
1870 if (line == pfile->lineno)
1871 return;
1872
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++;
1880 }
1881 return;
1882 }
1883 }
1884
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++;
1891 }
1892 #endif
1893
1894 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
1895 {
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);
1902 }
1903
1904 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
1905 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
1906
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');
1911 }
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');
1916 }
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');
1922 }
1923 #endif
1924 CPP_PUTC_Q (pfile, '\n');
1925 pfile->lineno = line;
1926 }
1927 \f
1928 /*
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.
1932 */
1933
1934 static enum cpp_token
1935 macarg (pfile, rest_args)
1936 cpp_reader *pfile;
1937 int rest_args;
1938 {
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;
1943
1944 /* Try to parse as much of the argument as exists at this
1945 input stack level. */
1946 pfile->no_macro_expand++;
1947 for (;;)
1948 {
1949 token = cpp_get_token (pfile);
1950 switch (token)
1951 {
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: ;
1979 }
1980 }
1981
1982 done:
1983 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1984 pfile->no_macro_expand--;
1985
1986 return token;
1987 }
1988 \f
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. */
1992
1993 static int
1994 change_newlines (start, length)
1995 U_CHAR *start;
1996 int length;
1997 {
1998 register U_CHAR *ibp;
1999 register U_CHAR *obp;
2000 register U_CHAR *limit;
2001 register int c;
2002
2003 ibp = start;
2004 limit = start + length;
2005 obp = start;
2006
2007 while (ibp < limit) {
2008 *obp++ = c = *ibp++;
2009 switch (c) {
2010
2011 case '\'':
2012 case '\"':
2013 /* Notice and skip strings, so that we don't delete newlines in them. */
2014 {
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;
2022 }
2023 }
2024 break;
2025 }
2026 }
2027
2028 return obp - start;
2029 }
2030
2031 \f
2032 static struct tm *
2033 timestamp (pfile)
2034 cpp_reader *pfile;
2035 {
2036 if (!pfile->timebuf) {
2037 time_t t = time ((time_t *) 0);
2038 pfile->timebuf = localtime (&t);
2039 }
2040 return pfile->timebuf;
2041 }
2042
2043 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2044 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2045 };
2046
2047 /*
2048 * expand things like __FILE__. Place the expansion into the output
2049 * buffer *without* rescanning.
2050 */
2051
2052 static void
2053 special_symbol (hp, pfile)
2054 HASHNODE *hp;
2055 cpp_reader *pfile;
2056 {
2057 char *buf;
2058 int len;
2059 int true_indepth;
2060 cpp_buffer *ip = NULL;
2061 struct tm *timebuf;
2062
2063 int paren = 0; /* For special `defined' keyword */
2064
2065 #if 0
2066 if (pcp_outfile && pcp_inside_if
2067 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2068 cpp_error (pfile,
2069 "Predefined macro `%s' used inside `#if' during precompilation",
2070 hp->name);
2071 #endif
2072
2073 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2074 {
2075 if (ip == CPP_NULL_BUFFER (pfile))
2076 {
2077 cpp_error (pfile, "cccp error: not in any file?!");
2078 return; /* the show must go on */
2079 }
2080 if (ip->fname != NULL)
2081 break;
2082 }
2083
2084 switch (hp->type)
2085 {
2086 case T_FILE:
2087 case T_BASE_FILE:
2088 {
2089 char *string;
2090 if (hp->type == T_BASE_FILE)
2091 {
2092 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2093 ip = CPP_PREV_BUFFER (ip);
2094 }
2095 string = ip->nominal_fname;
2096
2097 if (!string)
2098 string = "";
2099 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2100 quote_string (pfile, string);
2101 return;
2102 }
2103
2104 case T_INCLUDE_LEVEL:
2105 true_indepth = 0;
2106 ip = CPP_BUFFER (pfile);
2107 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2108 if (ip->fname != NULL)
2109 true_indepth++;
2110
2111 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2112 sprintf (buf, "%d", true_indepth - 1);
2113 break;
2114
2115 case T_VERSION:
2116 buf = (char *) alloca (3 + strlen (version_string));
2117 sprintf (buf, "\"%s\"", version_string);
2118 break;
2119
2120 #ifndef NO_BUILTIN_SIZE_TYPE
2121 case T_SIZE_TYPE:
2122 buf = SIZE_TYPE;
2123 break;
2124 #endif
2125
2126 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2127 case T_PTRDIFF_TYPE:
2128 buf = PTRDIFF_TYPE;
2129 break;
2130 #endif
2131
2132 case T_WCHAR_TYPE:
2133 buf = CPP_WCHAR_TYPE (pfile);
2134 break;
2135
2136 case T_USER_LABEL_PREFIX_TYPE:
2137 buf = user_label_prefix;
2138 break;
2139
2140 case T_REGISTER_PREFIX_TYPE:
2141 buf = REGISTER_PREFIX;
2142 break;
2143
2144 case T_CONST:
2145 buf = (char *) alloca (4 * sizeof (int));
2146 sprintf (buf, "%d", hp->value.ival);
2147 #ifdef STDC_0_IN_SYSTEM_HEADERS
2148 if (ip->system_header_p
2149 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2150 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2151 strcpy (buf, "0");
2152 #endif
2153 #if 0
2154 if (pcp_inside_if && pcp_outfile)
2155 /* Output a precondition for this macro use */
2156 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2157 #endif
2158 break;
2159
2160 case T_SPECLINE:
2161 {
2162 long line = ip->lineno;
2163 long col = ip->colno;
2164 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2165
2166 buf = (char *) alloca (10);
2167 sprintf (buf, "%ld", line);
2168 }
2169 break;
2170
2171 case T_DATE:
2172 case T_TIME:
2173 buf = (char *) alloca (20);
2174 timebuf = timestamp (pfile);
2175 if (hp->type == T_DATE)
2176 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2177 timebuf->tm_mday, timebuf->tm_year + 1900);
2178 else
2179 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2180 timebuf->tm_sec);
2181 break;
2182
2183 case T_SPEC_DEFINED:
2184 buf = " 0 "; /* Assume symbol is not defined */
2185 ip = CPP_BUFFER (pfile);
2186 SKIP_WHITE_SPACE (ip->cur);
2187 if (*ip->cur == '(')
2188 {
2189 paren++;
2190 ip->cur++; /* Skip over the paren */
2191 SKIP_WHITE_SPACE (ip->cur);
2192 }
2193
2194 if (!is_idstart[*ip->cur])
2195 goto oops;
2196 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2197 goto oops;
2198 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2199 {
2200 #if 0
2201 if (pcp_outfile && pcp_inside_if
2202 && (hp->type == T_CONST
2203 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2204 /* Output a precondition for this macro use. */
2205 fprintf (pcp_outfile, "#define %s\n", hp->name);
2206 #endif
2207 buf = " 1 ";
2208 }
2209 #if 0
2210 else
2211 if (pcp_outfile && pcp_inside_if)
2212 {
2213 /* Output a precondition for this macro use */
2214 U_CHAR *cp = ip->bufp;
2215 fprintf (pcp_outfile, "#undef ");
2216 while (is_idchar[*cp]) /* Ick! */
2217 fputc (*cp++, pcp_outfile);
2218 putc ('\n', pcp_outfile);
2219 }
2220 #endif
2221 while (is_idchar[*ip->cur])
2222 ++ip->cur;
2223 SKIP_WHITE_SPACE (ip->cur);
2224 if (paren)
2225 {
2226 if (*ip->cur != ')')
2227 goto oops;
2228 ++ip->cur;
2229 }
2230 break;
2231
2232 oops:
2233
2234 cpp_error (pfile, "`defined' without an identifier");
2235 break;
2236
2237 default:
2238 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2239 abort ();
2240 }
2241 len = strlen (buf);
2242 CPP_RESERVE (pfile, len + 1);
2243 CPP_PUTS_Q (pfile, buf, len);
2244 CPP_NUL_TERMINATE_Q (pfile);
2245
2246 return;
2247 }
2248
2249 /* Write out a #define command for the special named MACRO_NAME
2250 to PFILE's token_buffer. */
2251
2252 static void
2253 dump_special_to_buffer (pfile, macro_name)
2254 cpp_reader *pfile;
2255 char *macro_name;
2256 {
2257 static char define_directive[] = "#define ";
2258 int macro_name_length = strlen (macro_name);
2259 output_line_command (pfile, 0, same_file);
2260 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2261 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2262 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2263 CPP_PUTC_Q (pfile, ' ');
2264 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2265 CPP_PUTC (pfile, '\n');
2266 }
2267
2268 /* Initialize the built-in macros. */
2269
2270 static void
2271 initialize_builtins (pfile)
2272 cpp_reader *pfile;
2273 {
2274 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2275 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2276 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2277 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2278 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2279 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2280 #ifndef NO_BUILTIN_SIZE_TYPE
2281 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2282 #endif
2283 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2284 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2285 #endif
2286 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2287 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2288 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2289 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2290 if (!CPP_TRADITIONAL (pfile))
2291 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2292 if (CPP_OPTIONS (pfile)->objc)
2293 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2294 /* This is supplied using a -D by the compiler driver
2295 so that it is present only when truly compiling with GNU C. */
2296 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2297
2298 if (CPP_OPTIONS (pfile)->debug_output)
2299 {
2300 dump_special_to_buffer (pfile, "__BASE_FILE__");
2301 dump_special_to_buffer (pfile, "__VERSION__");
2302 #ifndef NO_BUILTIN_SIZE_TYPE
2303 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2304 #endif
2305 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2306 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2307 #endif
2308 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2309 dump_special_to_buffer (pfile, "__DATE__");
2310 dump_special_to_buffer (pfile, "__TIME__");
2311 if (!CPP_TRADITIONAL (pfile))
2312 dump_special_to_buffer (pfile, "__STDC__");
2313 if (CPP_OPTIONS (pfile)->objc)
2314 dump_special_to_buffer (pfile, "__OBJC__");
2315 }
2316 }
2317 \f
2318 /* Return 1 iff a token ending in C1 followed directly by a token C2
2319 could cause mis-tokenization. */
2320
2321 static int
2322 unsafe_chars (c1, c2)
2323 int c1, c2;
2324 {
2325 switch (c1)
2326 {
2327 case '+': case '-':
2328 if (c2 == c1 || c2 == '=')
2329 return 1;
2330 goto letter;
2331 case '.':
2332 case '0': case '1': case '2': case '3': case '4':
2333 case '5': case '6': case '7': case '8': case '9':
2334 case 'e': case 'E': case 'p': case 'P':
2335 if (c2 == '-' || c2 == '+')
2336 return 1; /* could extend a pre-processing number */
2337 goto letter;
2338 case 'L':
2339 if (c2 == '\'' || c2 == '\"')
2340 return 1; /* Could turn into L"xxx" or L'xxx'. */
2341 goto letter;
2342 letter:
2343 case '_':
2344 case 'a': case 'b': case 'c': case 'd': case 'f':
2345 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2346 case 'm': case 'n': case 'o': case 'q': case 'r':
2347 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2348 case 'y': case 'z':
2349 case 'A': case 'B': case 'C': case 'D': case 'F':
2350 case 'G': case 'H': case 'I': case 'J': case 'K':
2351 case 'M': case 'N': case 'O': case 'Q': case 'R':
2352 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2353 case 'Y': case 'Z':
2354 /* We're in the middle of either a name or a pre-processing number. */
2355 return (is_idchar[c2] || c2 == '.');
2356 case '<': case '>': case '!': case '%': case '#': case ':':
2357 case '^': case '&': case '|': case '*': case '/': case '=':
2358 return (c2 == c1 || c2 == '=');
2359 }
2360 return 0;
2361 }
2362
2363 /* Expand a macro call.
2364 HP points to the symbol that is the macro being called.
2365 Put the result of expansion onto the input stack
2366 so that subsequent input by our caller will use it.
2367
2368 If macro wants arguments, caller has already verified that
2369 an argument list follows; arguments come from the input stack. */
2370
2371 static void
2372 macroexpand (pfile, hp)
2373 cpp_reader *pfile;
2374 HASHNODE *hp;
2375 {
2376 int nargs;
2377 DEFINITION *defn = hp->value.defn;
2378 register U_CHAR *xbuf;
2379 long start_line, start_column;
2380 int xbuf_len;
2381 struct argdata *args;
2382 long old_written = CPP_WRITTEN (pfile);
2383 #if 0
2384 int start_line = instack[indepth].lineno;
2385 #endif
2386 int rest_args, rest_zero;
2387 register int i;
2388
2389 #if 0
2390 CHECK_DEPTH (return;);
2391 #endif
2392
2393 #if 0
2394 /* This macro is being used inside a #if, which means it must be */
2395 /* recorded as a precondition. */
2396 if (pcp_inside_if && pcp_outfile && defn->predefined)
2397 dump_single_macro (hp, pcp_outfile);
2398 #endif
2399
2400 pfile->output_escapes++;
2401 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2402
2403 nargs = defn->nargs;
2404
2405 if (nargs >= 0)
2406 {
2407 enum cpp_token token;
2408
2409 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2410
2411 for (i = 0; i < nargs; i++)
2412 {
2413 args[i].raw = args[i].expanded = 0;
2414 args[i].raw_length = 0;
2415 args[i].expand_length = args[i].stringified_length = -1;
2416 args[i].use_count = 0;
2417 }
2418
2419 /* Parse all the macro args that are supplied. I counts them.
2420 The first NARGS args are stored in ARGS.
2421 The rest are discarded. If rest_args is set then we assume
2422 macarg absorbed the rest of the args. */
2423 i = 0;
2424 rest_args = 0;
2425 rest_args = 0;
2426 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2427 do
2428 {
2429 if (rest_args)
2430 continue;
2431 if (i < nargs || (nargs == 0 && i == 0))
2432 {
2433 /* if we are working on last arg which absorbs rest of args... */
2434 if (i == nargs - 1 && defn->rest_args)
2435 rest_args = 1;
2436 args[i].raw = CPP_WRITTEN (pfile);
2437 token = macarg (pfile, rest_args);
2438 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2439 args[i].newlines = 0; /* FIXME */
2440 }
2441 else
2442 token = macarg (pfile, 0);
2443 if (token == CPP_EOF || token == CPP_POP)
2444 {
2445 cpp_error_with_line (pfile, start_line, start_column,
2446 "unterminated macro call");
2447 return;
2448 }
2449 i++;
2450 } while (token == CPP_COMMA);
2451
2452 /* If we got one arg but it was just whitespace, call that 0 args. */
2453 if (i == 1)
2454 {
2455 register U_CHAR *bp = ARG_BASE + args[0].raw;
2456 register U_CHAR *lim = bp + args[0].raw_length;
2457 /* cpp.texi says for foo ( ) we provide one argument.
2458 However, if foo wants just 0 arguments, treat this as 0. */
2459 if (nargs == 0)
2460 while (bp != lim && is_space[*bp]) bp++;
2461 if (bp == lim)
2462 i = 0;
2463 }
2464
2465 /* Don't output an error message if we have already output one for
2466 a parse error above. */
2467 rest_zero = 0;
2468 if (nargs == 0 && i > 0)
2469 {
2470 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2471 }
2472 else if (i < nargs)
2473 {
2474 /* traditional C allows foo() if foo wants one argument. */
2475 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2476 ;
2477 /* the rest args token is allowed to absorb 0 tokens */
2478 else if (i == nargs - 1 && defn->rest_args)
2479 rest_zero = 1;
2480 else if (i == 0)
2481 cpp_error (pfile, "macro `%s' used without args", hp->name);
2482 else if (i == 1)
2483 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2484 else
2485 cpp_error (pfile, "macro `%s' used with only %d args",
2486 hp->name, i);
2487 }
2488 else if (i > nargs)
2489 {
2490 cpp_error (pfile,
2491 "macro `%s' used with too many (%d) args", hp->name, i);
2492 }
2493 }
2494
2495 /* If macro wants zero args, we parsed the arglist for checking only.
2496 Read directly from the macro definition. */
2497 if (nargs <= 0)
2498 {
2499 xbuf = defn->expansion;
2500 xbuf_len = defn->length;
2501 }
2502 else
2503 {
2504 register U_CHAR *exp = defn->expansion;
2505 register int offset; /* offset in expansion,
2506 copied a piece at a time */
2507 register int totlen; /* total amount of exp buffer filled so far */
2508
2509 register struct reflist *ap, *last_ap;
2510
2511 /* Macro really takes args. Compute the expansion of this call. */
2512
2513 /* Compute length in characters of the macro's expansion.
2514 Also count number of times each arg is used. */
2515 xbuf_len = defn->length;
2516 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2517 {
2518 if (ap->stringify)
2519 {
2520 register struct argdata *arg = &args[ap->argno];
2521 /* Stringify if it hasn't already been */
2522 if (arg->stringified_length < 0)
2523 {
2524 int arglen = arg->raw_length;
2525 int escaped = 0;
2526 int in_string = 0;
2527 int c;
2528 /* Initially need_space is -1. Otherwise, 1 means the
2529 previous character was a space, but we suppressed it;
2530 0 means the previous character was a non-space. */
2531 int need_space = -1;
2532 i = 0;
2533 arg->stringified = CPP_WRITTEN (pfile);
2534 if (!CPP_TRADITIONAL (pfile))
2535 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2536 for (; i < arglen; i++)
2537 {
2538 c = (ARG_BASE + arg->raw)[i];
2539
2540 if (! in_string)
2541 {
2542 /* Internal sequences of whitespace are replaced by
2543 one space except within an string or char token.*/
2544 if (is_space[c])
2545 {
2546 if (CPP_WRITTEN (pfile) > (unsigned)arg->stringified
2547 && (CPP_PWRITTEN (pfile))[-1] == '@')
2548 {
2549 /* "@ " escape markers are removed */
2550 CPP_ADJUST_WRITTEN (pfile, -1);
2551 continue;
2552 }
2553 if (need_space == 0)
2554 need_space = 1;
2555 continue;
2556 }
2557 else if (need_space > 0)
2558 CPP_PUTC (pfile, ' ');
2559 need_space = 0;
2560 }
2561
2562 if (escaped)
2563 escaped = 0;
2564 else
2565 {
2566 if (c == '\\')
2567 escaped = 1;
2568 if (in_string)
2569 {
2570 if (c == in_string)
2571 in_string = 0;
2572 }
2573 else if (c == '\"' || c == '\'')
2574 in_string = c;
2575 }
2576
2577 /* Escape these chars */
2578 if (c == '\"' || (in_string && c == '\\'))
2579 CPP_PUTC (pfile, '\\');
2580 if (ISPRINT (c))
2581 CPP_PUTC (pfile, c);
2582 else
2583 {
2584 CPP_RESERVE (pfile, 4);
2585 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2586 (unsigned int) c);
2587 CPP_ADJUST_WRITTEN (pfile, 4);
2588 }
2589 }
2590 if (!CPP_TRADITIONAL (pfile))
2591 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2592 arg->stringified_length
2593 = CPP_WRITTEN (pfile) - arg->stringified;
2594 }
2595 xbuf_len += args[ap->argno].stringified_length;
2596 }
2597 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2598 /* Add 4 for two newline-space markers to prevent
2599 token concatenation. */
2600 xbuf_len += args[ap->argno].raw_length + 4;
2601 else
2602 {
2603 /* We have an ordinary (expanded) occurrence of the arg.
2604 So compute its expansion, if we have not already. */
2605 if (args[ap->argno].expand_length < 0)
2606 {
2607 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2608 cpp_expand_to_buffer (pfile,
2609 ARG_BASE + args[ap->argno].raw,
2610 args[ap->argno].raw_length);
2611
2612 args[ap->argno].expand_length
2613 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2614 }
2615
2616 /* Add 4 for two newline-space markers to prevent
2617 token concatenation. */
2618 xbuf_len += args[ap->argno].expand_length + 4;
2619 }
2620 if (args[ap->argno].use_count < 10)
2621 args[ap->argno].use_count++;
2622 }
2623
2624 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2625
2626 /* Generate in XBUF the complete expansion
2627 with arguments substituted in.
2628 TOTLEN is the total size generated so far.
2629 OFFSET is the index in the definition
2630 of where we are copying from. */
2631 offset = totlen = 0;
2632 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2633 last_ap = ap, ap = ap->next)
2634 {
2635 register struct argdata *arg = &args[ap->argno];
2636 int count_before = totlen;
2637
2638 /* Add chars to XBUF. */
2639 for (i = 0; i < ap->nchars; i++, offset++)
2640 xbuf[totlen++] = exp[offset];
2641
2642 /* If followed by an empty rest arg with concatenation,
2643 delete the last run of nonwhite chars. */
2644 if (rest_zero && totlen > count_before
2645 && ((ap->rest_args && ap->raw_before)
2646 || (last_ap != NULL && last_ap->rest_args
2647 && last_ap->raw_after)))
2648 {
2649 /* Delete final whitespace. */
2650 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2651 totlen--;
2652
2653 /* Delete the nonwhites before them. */
2654 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2655 totlen--;
2656 }
2657
2658 if (ap->stringify != 0)
2659 {
2660 bcopy (ARG_BASE + arg->stringified,
2661 xbuf + totlen, arg->stringified_length);
2662 totlen += arg->stringified_length;
2663 }
2664 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2665 {
2666 U_CHAR *p1 = ARG_BASE + arg->raw;
2667 U_CHAR *l1 = p1 + arg->raw_length;
2668 if (ap->raw_before)
2669 {
2670 while (p1 != l1 && is_space[*p1]) p1++;
2671 while (p1 != l1 && is_idchar[*p1])
2672 xbuf[totlen++] = *p1++;
2673 }
2674 if (ap->raw_after)
2675 {
2676 /* Arg is concatenated after: delete trailing whitespace,
2677 whitespace markers, and no-reexpansion markers. */
2678 while (p1 != l1)
2679 {
2680 if (is_space[l1[-1]]) l1--;
2681 else if (l1[-1] == '@')
2682 {
2683 U_CHAR *p2 = l1 - 1;
2684 /* If whitespace is preceded by an odd number
2685 of `@' signs, the last `@' was a whitespace
2686 marker; drop it too. */
2687 while (p2 != p1 && p2[0] == '@') p2--;
2688 if ((l1 - p2) & 1)
2689 l1--;
2690 break;
2691 }
2692 else if (l1[-1] == '-')
2693 {
2694 U_CHAR *p2 = l1 - 1;
2695 /* If a `-' is preceded by an odd number of
2696 `@' signs then it and the last `@' are
2697 a no-reexpansion marker. */
2698 while (p2 != p1 && p2[0] == '@') p2--;
2699 if ((l1 - p2) & 1)
2700 l1 -= 2;
2701 else
2702 break;
2703 }
2704 else break;
2705 }
2706 }
2707
2708 /* Delete any no-reexpansion marker that precedes
2709 an identifier at the beginning of the argument. */
2710 if (p1[0] == '@' && p1[1] == '-')
2711 p1 += 2;
2712
2713 bcopy (p1, xbuf + totlen, l1 - p1);
2714 totlen += l1 - p1;
2715 }
2716 else
2717 {
2718 U_CHAR *expanded = ARG_BASE + arg->expanded;
2719 if (!ap->raw_before && totlen > 0 && arg->expand_length
2720 && !CPP_TRADITIONAL(pfile)
2721 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2722 {
2723 xbuf[totlen++] = '@';
2724 xbuf[totlen++] = ' ';
2725 }
2726
2727 bcopy (expanded, xbuf + totlen, arg->expand_length);
2728 totlen += arg->expand_length;
2729
2730 if (!ap->raw_after && totlen > 0 && offset < defn->length
2731 && !CPP_TRADITIONAL(pfile)
2732 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2733 {
2734 xbuf[totlen++] = '@';
2735 xbuf[totlen++] = ' ';
2736 }
2737
2738 /* If a macro argument with newlines is used multiple times,
2739 then only expand the newlines once. This avoids creating
2740 output lines which don't correspond to any input line,
2741 which confuses gdb and gcov. */
2742 if (arg->use_count > 1 && arg->newlines > 0)
2743 {
2744 /* Don't bother doing change_newlines for subsequent
2745 uses of arg. */
2746 arg->use_count = 1;
2747 arg->expand_length
2748 = change_newlines (expanded, arg->expand_length);
2749 }
2750 }
2751
2752 if (totlen > xbuf_len)
2753 abort ();
2754 }
2755
2756 /* if there is anything left of the definition
2757 after handling the arg list, copy that in too. */
2758
2759 for (i = offset; i < defn->length; i++)
2760 {
2761 /* if we've reached the end of the macro */
2762 if (exp[i] == ')')
2763 rest_zero = 0;
2764 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2765 && last_ap->raw_after))
2766 xbuf[totlen++] = exp[i];
2767 }
2768
2769 xbuf[totlen] = 0;
2770 xbuf_len = totlen;
2771
2772 }
2773
2774 pfile->output_escapes--;
2775
2776 /* Now put the expansion on the input stack
2777 so our caller will commence reading from it. */
2778 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2779 CPP_BUFFER (pfile)->has_escapes = 1;
2780
2781 /* Pop the space we've used in the token_buffer for argument expansion. */
2782 CPP_SET_WRITTEN (pfile, old_written);
2783
2784 /* Recursive macro use sometimes works traditionally.
2785 #define foo(x,y) bar (x (y,0), y)
2786 foo (foo, baz) */
2787
2788 if (!CPP_TRADITIONAL (pfile))
2789 hp->type = T_DISABLED;
2790 }
2791
2792 static void
2793 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2794 cpp_reader *pfile;
2795 register U_CHAR *xbuf;
2796 int xbuf_len;
2797 HASHNODE *hp;
2798 {
2799 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2800 if (mbuf == NULL)
2801 return;
2802 mbuf->cleanup = macro_cleanup;
2803 mbuf->data = hp;
2804
2805 /* The first chars of the expansion should be a "@ " added by
2806 collect_expansion. This is to prevent accidental token-pasting
2807 between the text preceding the macro invocation, and the macro
2808 expansion text.
2809
2810 We would like to avoid adding unneeded spaces (for the sake of
2811 tools that use cpp, such as imake). In some common cases we can
2812 tell that it is safe to omit the space.
2813
2814 The character before the macro invocation cannot have been an
2815 idchar (or else it would have been pasted with the idchars of
2816 the macro name). Therefore, if the first non-space character
2817 of the expansion is an idchar, we do not need the extra space
2818 to prevent token pasting.
2819
2820 Also, we don't need the extra space if the first char is '(',
2821 or some other (less common) characters. */
2822
2823 if (xbuf[0] == '@' && xbuf[1] == ' '
2824 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
2825 || xbuf[2] == '\"'))
2826 mbuf->cur += 2;
2827 }
2828 \f
2829 /* Like cpp_get_token, except that it does not read past end-of-line.
2830 Also, horizontal space is skipped, and macros are popped. */
2831
2832 static enum cpp_token
2833 get_directive_token (pfile)
2834 cpp_reader *pfile;
2835 {
2836 for (;;)
2837 {
2838 long old_written = CPP_WRITTEN (pfile);
2839 enum cpp_token token;
2840 cpp_skip_hspace (pfile);
2841 if (PEEKC () == '\n')
2842 return CPP_VSPACE;
2843 token = cpp_get_token (pfile);
2844 switch (token)
2845 {
2846 case CPP_POP:
2847 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2848 return token;
2849 /* ... else fall though ... */
2850 case CPP_HSPACE: case CPP_COMMENT:
2851 CPP_SET_WRITTEN (pfile, old_written);
2852 break;
2853 default:
2854 return token;
2855 }
2856 }
2857 }
2858 \f
2859 /* Handle #include and #import.
2860 This function expects to see "fname" or <fname> on the input.
2861
2862 The input is normally in part of the output_buffer following
2863 CPP_WRITTEN, and will get overwritten by output_line_command.
2864 I.e. in input file specification has been popped by handle_directive.
2865 This is safe. */
2866
2867 static int
2868 do_include (pfile, keyword)
2869 cpp_reader *pfile;
2870 struct directive *keyword;
2871 {
2872 int importing = (keyword->type == T_IMPORT);
2873 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
2874 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
2875 int before; /* included before? */
2876 long flen;
2877 char *fbeg, *fend;
2878 cpp_buffer *fp;
2879
2880 enum cpp_token token;
2881
2882 /* Chain of dirs to search */
2883 struct include_hash *ihash;
2884 struct file_name_list *search_start;
2885
2886 long old_written = CPP_WRITTEN (pfile);
2887
2888 int fd;
2889
2890 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
2891 {
2892 if (importing)
2893 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
2894 if (skip_dirs)
2895 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
2896 }
2897
2898 if (importing && CPP_OPTIONS (pfile)->warn_import
2899 && !CPP_OPTIONS (pfile)->inhibit_warnings
2900 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
2901 {
2902 pfile->import_warning = 1;
2903 cpp_warning (pfile, "`#import' is obsolete, use an #ifdef wrapper in the header file");
2904 }
2905
2906 pfile->parsing_include_directive++;
2907 token = get_directive_token (pfile);
2908 pfile->parsing_include_directive--;
2909
2910 if (token == CPP_STRING)
2911 {
2912 fbeg = pfile->token_buffer + old_written + 1;
2913 fend = CPP_PWRITTEN (pfile) - 1;
2914 *fend = '\0';
2915 if (fbeg[-1] == '<')
2916 angle_brackets = 1;
2917 }
2918 #ifdef VMS
2919 else if (token == CPP_NAME)
2920 {
2921 /* Support '#include xyz' like VAX-C to allow for easy use of
2922 * all the decwindow include files. It defaults to '#include
2923 * <xyz.h>' and generates a warning. */
2924 cpp_warning (pfile,
2925 "VAX-C-style include specification found, use '#include <filename.h>' !");
2926 angle_brackets = 1;
2927
2928 /* Append the missing `.h' to the name. */
2929 CPP_PUTS (pfile, ".h", 3)
2930 CPP_NUL_TERMINATE_Q (pfile);
2931
2932 fbeg = pfile->token_buffer + old_written;
2933 fend = CPP_PWRITTEN (pfile);
2934 }
2935 #endif
2936 else
2937 {
2938 cpp_error (pfile,
2939 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
2940 CPP_SET_WRITTEN (pfile, old_written);
2941 skip_rest_of_line (pfile);
2942 return 0;
2943 }
2944
2945 token = get_directive_token (pfile);
2946 if (token != CPP_VSPACE)
2947 {
2948 cpp_error (pfile, "junk at end of `#include'");
2949 skip_rest_of_line (pfile);
2950 }
2951
2952 CPP_SET_WRITTEN (pfile, old_written);
2953
2954 flen = fend - fbeg;
2955
2956 if (flen == 0)
2957 {
2958 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
2959 return 0;
2960 }
2961
2962 search_start = 0;
2963
2964 for (fp = CPP_BUFFER (pfile);
2965 fp != CPP_NULL_BUFFER (pfile);
2966 fp = CPP_PREV_BUFFER (fp))
2967 if (fp->fname != NULL)
2968 break;
2969
2970 if (fp == CPP_NULL_BUFFER (pfile))
2971 {
2972 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
2973 return 1;
2974 }
2975
2976 /* For #include_next, skip in the search path past the dir in which the
2977 containing file was found. Treat files specified using an absolute path
2978 as if there are no more directories to search. Treat the primary source
2979 file like any other included source, but generate a warning. */
2980 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
2981 {
2982 if (fp->ihash->foundhere != ABSOLUTE_PATH)
2983 search_start = fp->ihash->foundhere->next;
2984 }
2985 else
2986 {
2987 if (skip_dirs)
2988 cpp_warning (pfile, "#include_next in primary source file");
2989
2990 if (angle_brackets)
2991 search_start = CPP_OPTIONS (pfile)->bracket_include;
2992 else
2993 {
2994 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
2995 {
2996 if (fp)
2997 search_start = fp->actual_dir;
2998 }
2999 else
3000 search_start = CPP_OPTIONS (pfile)->quote_include;
3001 }
3002 }
3003
3004 if (!search_start)
3005 {
3006 cpp_error (pfile, "No include path in which to find %s", fbeg);
3007 return 0;
3008 }
3009
3010 fd = find_include_file (pfile, fbeg, search_start, &ihash, &before);
3011
3012 if (fd == -2)
3013 return 0;
3014
3015 if (fd == -1)
3016 {
3017 if (CPP_OPTIONS (pfile)->print_deps_missing_files
3018 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
3019 (pfile->system_include_depth > 0)))
3020 {
3021 if (!angle_brackets)
3022 deps_output (pfile, fbeg, ' ');
3023 else
3024 {
3025 char *p;
3026 struct file_name_list *ptr;
3027 /* If requested as a system header, assume it belongs in
3028 the first system header directory. */
3029 if (CPP_OPTIONS (pfile)->bracket_include)
3030 ptr = CPP_OPTIONS (pfile)->bracket_include;
3031 else
3032 ptr = CPP_OPTIONS (pfile)->quote_include;
3033
3034 p = (char *) alloca (strlen (ptr->name)
3035 + strlen (fbeg) + 2);
3036 if (*ptr->name != '\0')
3037 {
3038 strcpy (p, ptr->name);
3039 strcat (p, "/");
3040 }
3041 strcat (p, fbeg);
3042 deps_output (pfile, p, ' ');
3043 }
3044 }
3045 /* If -M was specified, and this header file won't be added to
3046 the dependency list, then don't count this as an error,
3047 because we can still produce correct output. Otherwise, we
3048 can't produce correct output, because there may be
3049 dependencies we need inside the missing file, and we don't
3050 know what directory this missing file exists in. */
3051 else if (CPP_PRINT_DEPS (pfile)
3052 && (CPP_PRINT_DEPS (pfile)
3053 <= (angle_brackets || (pfile->system_include_depth > 0))))
3054 cpp_warning (pfile, "No include path in which to find %s", fbeg);
3055 else
3056 cpp_error_from_errno (pfile, fbeg);
3057
3058 return 0;
3059 }
3060
3061 /* For -M, add the file to the dependencies on its first inclusion. */
3062 if (!before && (CPP_PRINT_DEPS (pfile)
3063 > (angle_brackets || (pfile->system_include_depth > 0))))
3064 deps_output (pfile, ihash->name, ' ');
3065
3066 /* Handle -H option. */
3067 if (CPP_OPTIONS(pfile)->print_include_names)
3068 {
3069 fp = CPP_BUFFER (pfile);
3070 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
3071 putc ('.', stderr);
3072 fprintf (stderr, " %s\n", ihash->name);
3073 }
3074
3075 /* Actually process the file */
3076
3077 if (importing)
3078 ihash->control_macro = "";
3079
3080 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3081 {
3082 close (fd);
3083 return 0;
3084 }
3085
3086 if (angle_brackets)
3087 pfile->system_include_depth++; /* Decremented in file_cleanup. */
3088
3089 if (finclude (pfile, fd, ihash))
3090 {
3091 output_line_command (pfile, 0, enter_file);
3092 pfile->only_seen_white = 2;
3093 }
3094
3095 return 0;
3096 }
3097
3098 \f
3099 /* Convert a character string literal into a nul-terminated string.
3100 The input string is [IN ... LIMIT).
3101 The result is placed in RESULT. RESULT can be the same as IN.
3102 The value returned in the end of the string written to RESULT,
3103 or NULL on error. */
3104
3105 static U_CHAR *
3106 convert_string (pfile, result, in, limit, handle_escapes)
3107 cpp_reader *pfile;
3108 register U_CHAR *result, *in, *limit;
3109 int handle_escapes;
3110 {
3111 U_CHAR c;
3112 c = *in++;
3113 if (c != '\"')
3114 return NULL;
3115 while (in < limit)
3116 {
3117 U_CHAR c = *in++;
3118 switch (c)
3119 {
3120 case '\0':
3121 return NULL;
3122 case '\"':
3123 limit = in;
3124 break;
3125 case '\\':
3126 if (handle_escapes)
3127 {
3128 char *bpc = (char *) in;
3129 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc, 0x00ff);
3130 in = (U_CHAR *) bpc;
3131 if (i >= 0)
3132 *result++ = (U_CHAR)c;
3133 break;
3134 }
3135 /* else fall through */
3136 default:
3137 *result++ = c;
3138 }
3139 }
3140 *result = 0;
3141 return result;
3142 }
3143
3144 /*
3145 * interpret #line command. Remembers previously seen fnames
3146 * in its very own hash table.
3147 */
3148 #define FNAME_HASHSIZE 37
3149
3150 static int
3151 do_line (pfile, keyword)
3152 cpp_reader *pfile;
3153 struct directive *keyword ATTRIBUTE_UNUSED;
3154 {
3155 cpp_buffer *ip = CPP_BUFFER (pfile);
3156 int new_lineno;
3157 long old_written = CPP_WRITTEN (pfile);
3158 enum file_change_code file_change = same_file;
3159 enum cpp_token token;
3160
3161 token = get_directive_token (pfile);
3162
3163 if (token != CPP_NUMBER
3164 || !ISDIGIT(pfile->token_buffer[old_written]))
3165 {
3166 cpp_error (pfile, "invalid format `#line' command");
3167 goto bad_line_directive;
3168 }
3169
3170 /* The Newline at the end of this line remains to be processed.
3171 To put the next line at the specified line number,
3172 we must store a line number now that is one less. */
3173 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3174 CPP_SET_WRITTEN (pfile, old_written);
3175
3176 /* NEW_LINENO is one less than the actual line number here. */
3177 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3178 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3179
3180 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3181 if (PEEKC() && !is_space[PEEKC()]) {
3182 cpp_error (pfile, "invalid format `#line' command");
3183 goto bad_line_directive;
3184 }
3185 #endif
3186
3187 token = get_directive_token (pfile);
3188
3189 if (token == CPP_STRING) {
3190 U_CHAR *fname = pfile->token_buffer + old_written;
3191 U_CHAR *end_name;
3192 static HASHNODE *fname_table[FNAME_HASHSIZE];
3193 HASHNODE *hp, **hash_bucket;
3194 U_CHAR *p;
3195 long num_start;
3196 int fname_length;
3197
3198 /* Turn the file name, which is a character string literal,
3199 into a null-terminated string. Do this in place. */
3200 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3201 if (end_name == NULL)
3202 {
3203 cpp_error (pfile, "invalid format `#line' command");
3204 goto bad_line_directive;
3205 }
3206
3207 fname_length = end_name - fname;
3208
3209 num_start = CPP_WRITTEN (pfile);
3210 token = get_directive_token (pfile);
3211 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3212 p = pfile->token_buffer + num_start;
3213 if (CPP_PEDANTIC (pfile))
3214 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3215
3216 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3217 {
3218 cpp_error (pfile, "invalid format `#line' command");
3219 goto bad_line_directive;
3220 }
3221 if (*p == '1')
3222 file_change = enter_file;
3223 else if (*p == '2')
3224 file_change = leave_file;
3225 else if (*p == '3')
3226 ip->system_header_p = 1;
3227 else /* if (*p == '4') */
3228 ip->system_header_p = 2;
3229
3230 CPP_SET_WRITTEN (pfile, num_start);
3231 token = get_directive_token (pfile);
3232 p = pfile->token_buffer + num_start;
3233 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3234 ip->system_header_p = *p == '3' ? 1 : 2;
3235 token = get_directive_token (pfile);
3236 }
3237 if (token != CPP_VSPACE) {
3238 cpp_error (pfile, "invalid format `#line' command");
3239 goto bad_line_directive;
3240 }
3241 }
3242
3243 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3244 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3245 if (hp->length == fname_length
3246 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3247 ip->nominal_fname = hp->value.cpval;
3248 break;
3249 }
3250 if (hp == 0) {
3251 /* Didn't find it; cons up a new one. */
3252 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3253 hp->next = *hash_bucket;
3254 *hash_bucket = hp;
3255
3256 hp->length = fname_length;
3257 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3258 bcopy (fname, hp->value.cpval, fname_length);
3259 }
3260 }
3261 else if (token != CPP_VSPACE && token != CPP_EOF) {
3262 cpp_error (pfile, "invalid format `#line' command");
3263 goto bad_line_directive;
3264 }
3265
3266 ip->lineno = new_lineno;
3267 bad_line_directive:
3268 skip_rest_of_line (pfile);
3269 CPP_SET_WRITTEN (pfile, old_written);
3270 output_line_command (pfile, 0, file_change);
3271 return 0;
3272 }
3273
3274 /*
3275 * remove the definition of a symbol from the symbol table.
3276 * according to un*x /lib/cpp, it is not an error to undef
3277 * something that has no definitions, so it isn't one here either.
3278 */
3279
3280 static int
3281 do_undef (pfile, keyword)
3282 cpp_reader *pfile;
3283 struct directive *keyword;
3284 {
3285 int sym_length;
3286 HASHNODE *hp;
3287 U_CHAR *buf, *name, *limit;
3288 int c;
3289 long here = CPP_WRITTEN (pfile);
3290 enum cpp_token token;
3291
3292 cpp_skip_hspace (pfile);
3293 c = GETC();
3294 if (! is_idstart[c])
3295 {
3296 cpp_error (pfile, "token after #undef is not an identifier");
3297 skip_rest_of_line (pfile);
3298 return 1;
3299 }
3300
3301 parse_name (pfile, c);
3302 buf = pfile->token_buffer + here;
3303 limit = CPP_PWRITTEN(pfile);
3304
3305 /* Copy out the token so we can pop the token buffer. */
3306 name = alloca (limit - buf + 1);
3307 bcopy(buf, name, limit - buf);
3308 name[limit - buf] = '\0';
3309
3310 token = get_directive_token (pfile);
3311 if (token != CPP_VSPACE && token != CPP_POP)
3312 {
3313 cpp_pedwarn (pfile, "junk on line after #undef");
3314 skip_rest_of_line (pfile);
3315 }
3316
3317 CPP_SET_WRITTEN (pfile, here);
3318
3319 #if 0
3320 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3321 if (pcp_outfile && keyword)
3322 pass_thru_directive (buf, limit, pfile, keyword);
3323 #endif
3324
3325 sym_length = check_macro_name (pfile, name, "macro");
3326
3327 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
3328 {
3329 /* If we are generating additional info for debugging (with -g) we
3330 need to pass through all effective #undef commands. */
3331 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3332 pass_thru_directive (name, name+sym_length, pfile, keyword);
3333 if (hp->type != T_MACRO)
3334 cpp_warning (pfile, "undefining `%s'", hp->name);
3335 delete_macro (hp);
3336 }
3337
3338 return 0;
3339 }
3340
3341 /* Wrap do_undef for -U processing. */
3342 static void
3343 cpp_undef (pfile, macro)
3344 cpp_reader *pfile;
3345 U_CHAR *macro;
3346 {
3347 if (cpp_push_buffer (pfile, macro, strlen(macro)))
3348 {
3349 do_undef (pfile, NULL);
3350 cpp_pop_buffer (pfile);
3351 }
3352 }
3353
3354 \f
3355 /*
3356 * Report an error detected by the program we are processing.
3357 * Use the text of the line in the error message.
3358 * (We use error because it prints the filename & line#.)
3359 */
3360
3361 static int
3362 do_error (pfile, keyword)
3363 cpp_reader *pfile;
3364 struct directive *keyword ATTRIBUTE_UNUSED;
3365 {
3366 long here = CPP_WRITTEN (pfile);
3367 U_CHAR *text;
3368 copy_rest_of_line (pfile);
3369 text = pfile->token_buffer + here;
3370 SKIP_WHITE_SPACE(text);
3371
3372 cpp_error (pfile, "#error %s", text);
3373 CPP_SET_WRITTEN (pfile, here);
3374 return 0;
3375 }
3376
3377 /*
3378 * Report a warning detected by the program we are processing.
3379 * Use the text of the line in the warning message, then continue.
3380 */
3381
3382 static int
3383 do_warning (pfile, keyword)
3384 cpp_reader *pfile;
3385 struct directive *keyword ATTRIBUTE_UNUSED;
3386 {
3387 U_CHAR *text;
3388 long here = CPP_WRITTEN(pfile);
3389 copy_rest_of_line (pfile);
3390 text = pfile->token_buffer + here;
3391 SKIP_WHITE_SPACE(text);
3392
3393 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3394 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3395
3396 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3397 if -pedantic-errors is given, #warning should cause an error. */
3398 cpp_pedwarn (pfile, "#warning %s", text);
3399 CPP_SET_WRITTEN (pfile, here);
3400 return 0;
3401 }
3402
3403 /* Report program identification. */
3404
3405 static int
3406 do_ident (pfile, keyword)
3407 cpp_reader *pfile;
3408 struct directive *keyword ATTRIBUTE_UNUSED;
3409 {
3410 /* Allow #ident in system headers, since that's not user's fault. */
3411 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3412 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3413
3414 skip_rest_of_line (pfile); /* Correct? Appears to match cccp. */
3415
3416 return 0;
3417 }
3418
3419 /* Just check for some recognized pragmas that need validation here,
3420 and leave the text in the token buffer to be output. */
3421
3422 static int
3423 do_pragma (pfile, keyword)
3424 cpp_reader *pfile;
3425 struct directive *keyword ATTRIBUTE_UNUSED;
3426 {
3427 long here = CPP_WRITTEN (pfile);
3428 U_CHAR *buf;
3429
3430 copy_rest_of_line (pfile);
3431 buf = pfile->token_buffer + here;
3432 SKIP_WHITE_SPACE (buf);
3433
3434 if (!strncmp (buf, "once", 4))
3435 {
3436 cpp_buffer *ip = NULL;
3437
3438 /* Allow #pragma once in system headers, since that's not the user's
3439 fault. */
3440 if (!CPP_BUFFER (pfile)->system_header_p)
3441 cpp_warning (pfile, "`#pragma once' is obsolete");
3442
3443 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3444 {
3445 if (ip == CPP_NULL_BUFFER (pfile))
3446 return 0;
3447 if (ip->fname != NULL)
3448 break;
3449 }
3450
3451 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
3452 cpp_warning (pfile, "`#pragma once' outside include file");
3453 else
3454 ip->ihash->control_macro = ""; /* never repeat */
3455 }
3456
3457 if (!strncmp (buf, "implementation", 14))
3458 {
3459 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3460 been included yet. */
3461 struct include_hash *ptr;
3462 U_CHAR *p = buf + 14, *fname, *fcopy;
3463 SKIP_WHITE_SPACE (p);
3464 if (*p == '\n' || *p != '\"')
3465 return 0;
3466
3467 fname = p + 1;
3468 p = (U_CHAR *) index (fname, '\"');
3469
3470 fcopy = alloca (p - fname + 1);
3471 bcopy (fname, fcopy, p - fname);
3472 fcopy[p-fname] = '\0';
3473
3474 ptr = include_hash (pfile, fcopy, 0);
3475 if (ptr)
3476 cpp_warning (pfile,
3477 "`#pragma implementation' for `%s' appears after file is included",
3478 fcopy);
3479 }
3480
3481 return 0;
3482 }
3483
3484 #ifdef SCCS_DIRECTIVE
3485 /* Just ignore #sccs, on systems where we define it at all. */
3486
3487 static int
3488 do_sccs (pfile, keyword)
3489 cpp_reader *pfile;
3490 struct directive *keyword ATTRIBUTE_UNUSED;
3491 {
3492 if (CPP_PEDANTIC (pfile))
3493 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
3494 skip_rest_of_line (pfile);
3495 return 0;
3496 }
3497 #endif
3498 \f
3499 /*
3500 * handle #if command by
3501 * 1) inserting special `defined' keyword into the hash table
3502 * that gets turned into 0 or 1 by special_symbol (thus,
3503 * if the luser has a symbol called `defined' already, it won't
3504 * work inside the #if command)
3505 * 2) rescan the input into a temporary output buffer
3506 * 3) pass the output buffer to the yacc parser and collect a value
3507 * 4) clean up the mess left from steps 1 and 2.
3508 * 5) call conditional_skip to skip til the next #endif (etc.),
3509 * or not, depending on the value from step 3.
3510 */
3511
3512 static int
3513 do_if (pfile, keyword)
3514 cpp_reader *pfile;
3515 struct directive *keyword ATTRIBUTE_UNUSED;
3516 {
3517 HOST_WIDE_INT value = eval_if_expression (pfile);
3518 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
3519 return 0;
3520 }
3521
3522 /*
3523 * handle a #elif directive by not changing if_stack either.
3524 * see the comment above do_else.
3525 */
3526
3527 static int
3528 do_elif (pfile, keyword)
3529 cpp_reader *pfile;
3530 struct directive *keyword ATTRIBUTE_UNUSED;
3531 {
3532 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3533 cpp_error (pfile, "`#elif' not within a conditional");
3534 return 0;
3535 } else {
3536 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3537 cpp_error (pfile, "`#elif' after `#else'");
3538 #if 0
3539 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3540 #endif
3541 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
3542 && strcmp (pfile->if_stack->fname,
3543 CPP_BUFFER (pfile)->nominal_fname) != 0)
3544 fprintf (stderr, ", file %s", pfile->if_stack->fname);
3545 fprintf (stderr, ")\n");
3546 }
3547 pfile->if_stack->type = T_ELIF;
3548 }
3549
3550 if (pfile->if_stack->if_succeeded)
3551 skip_if_group (pfile);
3552 else {
3553 HOST_WIDE_INT value = eval_if_expression (pfile);
3554 if (value == 0)
3555 skip_if_group (pfile);
3556 else {
3557 ++pfile->if_stack->if_succeeded; /* continue processing input */
3558 output_line_command (pfile, 1, same_file);
3559 }
3560 }
3561 return 0;
3562 }
3563
3564 /*
3565 * evaluate a #if expression in BUF, of length LENGTH,
3566 * then parse the result as a C expression and return the value as an int.
3567 */
3568
3569 static HOST_WIDE_INT
3570 eval_if_expression (pfile)
3571 cpp_reader *pfile;
3572 {
3573 HASHNODE *save_defined;
3574 HOST_WIDE_INT value;
3575 long old_written = CPP_WRITTEN (pfile);
3576
3577 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
3578 pfile->pcp_inside_if = 1;
3579
3580 value = cpp_parse_expr (pfile);
3581 pfile->pcp_inside_if = 0;
3582 delete_macro (save_defined); /* clean up special symbol */
3583
3584 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3585
3586 return value;
3587 }
3588
3589 /*
3590 * routine to handle ifdef/ifndef. Try to look up the symbol,
3591 * then do or don't skip to the #endif/#else/#elif depending
3592 * on what directive is actually being processed.
3593 */
3594
3595 static int
3596 do_xifdef (pfile, keyword)
3597 cpp_reader *pfile;
3598 struct directive *keyword;
3599 {
3600 int skip;
3601 cpp_buffer *ip = CPP_BUFFER (pfile);
3602 U_CHAR *ident;
3603 int ident_length;
3604 enum cpp_token token;
3605 int start_of_file = 0;
3606 U_CHAR *control_macro = 0;
3607 int old_written = CPP_WRITTEN (pfile);
3608
3609 /* Detect a #ifndef at start of file (not counting comments). */
3610 if (ip->fname != 0 && keyword->type == T_IFNDEF)
3611 start_of_file = pfile->only_seen_white == 2;
3612
3613 pfile->no_macro_expand++;
3614 token = get_directive_token (pfile);
3615 pfile->no_macro_expand--;
3616
3617 ident = pfile->token_buffer + old_written;
3618 ident_length = CPP_WRITTEN (pfile) - old_written;
3619 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3620
3621 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
3622 {
3623 skip = (keyword->type == T_IFDEF);
3624 if (! CPP_TRADITIONAL (pfile))
3625 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
3626 }
3627 else if (token == CPP_NAME)
3628 {
3629 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
3630 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
3631 if (start_of_file && !skip)
3632 {
3633 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
3634 bcopy (ident, control_macro, ident_length + 1);
3635 }
3636 }
3637 else
3638 {
3639 skip = (keyword->type == T_IFDEF);
3640 if (! CPP_TRADITIONAL (pfile))
3641 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
3642 }
3643
3644 if (!CPP_TRADITIONAL (pfile))
3645 { int c;
3646 cpp_skip_hspace (pfile);
3647 c = PEEKC ();
3648 if (c != EOF && c != '\n')
3649 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
3650 }
3651 skip_rest_of_line (pfile);
3652
3653 #if 0
3654 if (pcp_outfile) {
3655 /* Output a precondition for this macro. */
3656 if (hp && hp->value.defn->predefined)
3657 fprintf (pcp_outfile, "#define %s\n", hp->name);
3658 else {
3659 U_CHAR *cp = buf;
3660 fprintf (pcp_outfile, "#undef ");
3661 while (is_idchar[*cp]) /* Ick! */
3662 fputc (*cp++, pcp_outfile);
3663 putc ('\n', pcp_outfile);
3664 }
3665 #endif
3666
3667 conditional_skip (pfile, skip, T_IF, control_macro);
3668 return 0;
3669 }
3670
3671 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3672 If this is a #ifndef starting at the beginning of a file,
3673 CONTROL_MACRO is the macro name tested by the #ifndef.
3674 Otherwise, CONTROL_MACRO is 0. */
3675
3676 static void
3677 conditional_skip (pfile, skip, type, control_macro)
3678 cpp_reader *pfile;
3679 int skip;
3680 enum node_type type;
3681 U_CHAR *control_macro;
3682 {
3683 IF_STACK_FRAME *temp;
3684
3685 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3686 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
3687 #if 0
3688 temp->lineno = CPP_BUFFER (pfile)->lineno;
3689 #endif
3690 temp->next = pfile->if_stack;
3691 temp->control_macro = control_macro;
3692 pfile->if_stack = temp;
3693
3694 pfile->if_stack->type = type;
3695
3696 if (skip != 0) {
3697 skip_if_group (pfile);
3698 return;
3699 } else {
3700 ++pfile->if_stack->if_succeeded;
3701 output_line_command (pfile, 1, same_file);
3702 }
3703 }
3704
3705 /* Subroutine of skip_if_group. Examine one preprocessing directive and
3706 return 0 if skipping should continue, 1 if it should halt. Also
3707 adjusts the if_stack as appropriate.
3708 The `#' has been read, but not the identifier. */
3709
3710 static int
3711 consider_directive_while_skipping (pfile, stack)
3712 cpp_reader *pfile;
3713 IF_STACK_FRAME *stack;
3714 {
3715 long ident_len, ident;
3716 struct directive *kt;
3717 IF_STACK_FRAME *temp;
3718
3719 cpp_skip_hspace (pfile);
3720
3721 ident = CPP_WRITTEN (pfile);
3722 parse_name (pfile, GETC());
3723 ident_len = CPP_WRITTEN (pfile) - ident;
3724
3725 CPP_SET_WRITTEN (pfile, ident);
3726
3727 for (kt = directive_table; kt->length >= 0; kt++)
3728 if (kt->length == ident_len
3729 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
3730 switch (kt->type)
3731 {
3732 case T_IF:
3733 case T_IFDEF:
3734 case T_IFNDEF:
3735 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
3736 temp->next = pfile->if_stack;
3737 pfile->if_stack = temp;
3738 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
3739 temp->type = kt->type;
3740 return 0;
3741
3742 case T_ELSE:
3743 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
3744 validate_else (pfile, "#else");
3745 /* fall through */
3746 case T_ELIF:
3747 if (pfile->if_stack->type == T_ELSE)
3748 cpp_error (pfile, "`%s' after `#else'", kt->name);
3749
3750 if (pfile->if_stack == stack)
3751 return 1;
3752 else
3753 {
3754 pfile->if_stack->type = kt->type;
3755 return 0;
3756 }
3757
3758 case T_ENDIF:
3759 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
3760 validate_else (pfile, "#endif");
3761
3762 if (pfile->if_stack == stack)
3763 return 1;
3764
3765 temp = pfile->if_stack;
3766 pfile->if_stack = temp->next;
3767 free (temp);
3768 return 0;
3769
3770 default:
3771 return 0;
3772 }
3773
3774 /* Don't let erroneous code go by. */
3775 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
3776 cpp_pedwarn (pfile, "invalid preprocessor directive name");
3777 return 0;
3778 }
3779
3780 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
3781 * leaves input ptr at the sharp sign found.
3782 */
3783 static void
3784 skip_if_group (pfile)
3785 cpp_reader *pfile;
3786 {
3787 int c;
3788 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
3789 U_CHAR *beg_of_line;
3790 long old_written;
3791
3792 if (CPP_OPTIONS (pfile)->output_conditionals)
3793 {
3794 CPP_PUTS (pfile, "#failed\n", 8);
3795 pfile->lineno++;
3796 output_line_command (pfile, 1, same_file);
3797 }
3798
3799 old_written = CPP_WRITTEN (pfile);
3800
3801 for (;;)
3802 {
3803 beg_of_line = CPP_BUFFER (pfile)->cur;
3804
3805 if (! CPP_TRADITIONAL (pfile))
3806 cpp_skip_hspace (pfile);
3807 c = GETC();
3808 if (c == '\n')
3809 {
3810 if (CPP_OPTIONS (pfile)->output_conditionals)
3811 CPP_PUTC (pfile, c);
3812 continue;
3813 }
3814 else if (c == '#')
3815 {
3816 if (consider_directive_while_skipping (pfile, save_if_stack))
3817 break;
3818 }
3819 else if (c == EOF)
3820 return; /* Caller will issue error. */
3821
3822 FORWARD(-1);
3823 if (CPP_OPTIONS (pfile)->output_conditionals)
3824 {
3825 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
3826 copy_rest_of_line (pfile);
3827 }
3828 else
3829 {
3830 copy_rest_of_line (pfile);
3831 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
3832 }
3833
3834 c = GETC();
3835 if (c == EOF)
3836 return; /* Caller will issue error. */
3837 else
3838 {
3839 /* \n */
3840 if (CPP_OPTIONS (pfile)->output_conditionals)
3841 CPP_PUTC (pfile, c);
3842 }
3843 }
3844
3845 /* Back up to the beginning of this line. Caller will process the
3846 directive. */
3847 CPP_BUFFER (pfile)->cur = beg_of_line;
3848 pfile->only_seen_white = 1;
3849 if (CPP_OPTIONS (pfile)->output_conditionals)
3850 {
3851 CPP_PUTS (pfile, "#endfailed\n", 11);
3852 pfile->lineno++;
3853 }
3854 }
3855
3856 /*
3857 * handle a #else directive. Do this by just continuing processing
3858 * without changing if_stack ; this is so that the error message
3859 * for missing #endif's etc. will point to the original #if. It
3860 * is possible that something different would be better.
3861 */
3862
3863 static int
3864 do_else (pfile, keyword)
3865 cpp_reader *pfile;
3866 struct directive *keyword ATTRIBUTE_UNUSED;
3867 {
3868 cpp_buffer *ip = CPP_BUFFER (pfile);
3869
3870 if (CPP_PEDANTIC (pfile))
3871 validate_else (pfile, "#else");
3872 skip_rest_of_line (pfile);
3873
3874 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3875 cpp_error (pfile, "`#else' not within a conditional");
3876 return 0;
3877 } else {
3878 /* #ifndef can't have its special treatment for containing the whole file
3879 if it has a #else clause. */
3880 pfile->if_stack->control_macro = 0;
3881
3882 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3883 cpp_error (pfile, "`#else' after `#else'");
3884 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3885 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
3886 fprintf (stderr, ", file %s", pfile->if_stack->fname);
3887 fprintf (stderr, ")\n");
3888 }
3889 pfile->if_stack->type = T_ELSE;
3890 }
3891
3892 if (pfile->if_stack->if_succeeded)
3893 skip_if_group (pfile);
3894 else {
3895 ++pfile->if_stack->if_succeeded; /* continue processing input */
3896 output_line_command (pfile, 1, same_file);
3897 }
3898 return 0;
3899 }
3900
3901 /*
3902 * unstack after #endif command
3903 */
3904
3905 static int
3906 do_endif (pfile, keyword)
3907 cpp_reader *pfile;
3908 struct directive *keyword ATTRIBUTE_UNUSED;
3909 {
3910 if (CPP_PEDANTIC (pfile))
3911 validate_else (pfile, "#endif");
3912 skip_rest_of_line (pfile);
3913
3914 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
3915 cpp_error (pfile, "unbalanced `#endif'");
3916 else
3917 {
3918 IF_STACK_FRAME *temp = pfile->if_stack;
3919 pfile->if_stack = temp->next;
3920 if (temp->control_macro != 0)
3921 {
3922 /* This #endif matched a #ifndef at the start of the file.
3923 See if it is at the end of the file. */
3924 struct parse_marker start_mark;
3925 int c;
3926
3927 parse_set_mark (&start_mark, pfile);
3928
3929 for (;;)
3930 {
3931 cpp_skip_hspace (pfile);
3932 c = GETC ();
3933 if (c != '\n')
3934 break;
3935 }
3936 parse_goto_mark (&start_mark, pfile);
3937 parse_clear_mark (&start_mark);
3938
3939 if (c == EOF)
3940 {
3941 /* This #endif ends a #ifndef
3942 that contains all of the file (aside from whitespace).
3943 Arrange not to include the file again
3944 if the macro that was tested is defined. */
3945 struct cpp_buffer *ip;
3946 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3947 if (ip->fname != NULL)
3948 break;
3949 ip->ihash->control_macro = temp->control_macro;
3950 }
3951 }
3952 free (temp);
3953 output_line_command (pfile, 1, same_file);
3954 }
3955 return 0;
3956 }
3957
3958 /* When an #else or #endif is found while skipping failed conditional,
3959 if -pedantic was specified, this is called to warn about text after
3960 the command name. P points to the first char after the command name. */
3961
3962 static void
3963 validate_else (pfile, directive)
3964 cpp_reader *pfile;
3965 char *directive;
3966 {
3967 int c;
3968 cpp_skip_hspace (pfile);
3969 c = PEEKC ();
3970 if (c != EOF && c != '\n')
3971 cpp_pedwarn (pfile,
3972 "text following `%s' violates ANSI standard", directive);
3973 }
3974
3975 /* Get the next token, and add it to the text in pfile->token_buffer.
3976 Return the kind of token we got. */
3977
3978 enum cpp_token
3979 cpp_get_token (pfile)
3980 cpp_reader *pfile;
3981 {
3982 register int c, c2, c3;
3983 long old_written;
3984 long start_line, start_column;
3985 enum cpp_token token;
3986 struct cpp_options *opts = CPP_OPTIONS (pfile);
3987 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
3988 get_next:
3989 c = GETC();
3990 if (c == EOF)
3991 {
3992 handle_eof:
3993 if (CPP_BUFFER (pfile)->seen_eof)
3994 {
3995 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
3996 goto get_next;
3997 else
3998 return CPP_EOF;
3999 }
4000 else
4001 {
4002 cpp_buffer *next_buf
4003 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4004 CPP_BUFFER (pfile)->seen_eof = 1;
4005 if (CPP_BUFFER (pfile)->nominal_fname
4006 && next_buf != CPP_NULL_BUFFER (pfile))
4007 {
4008 /* We're about to return from an #include file.
4009 Emit #line information now (as part of the CPP_POP) result.
4010 But the #line refers to the file we will pop to. */
4011 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4012 CPP_BUFFER (pfile) = next_buf;
4013 pfile->input_stack_listing_current = 0;
4014 output_line_command (pfile, 0, leave_file);
4015 CPP_BUFFER (pfile) = cur_buffer;
4016 }
4017 return CPP_POP;
4018 }
4019 }
4020 else
4021 {
4022 switch (c)
4023 {
4024 long newlines;
4025 struct parse_marker start_mark;
4026 case '/':
4027 if (PEEKC () == '=')
4028 goto op2;
4029 if (opts->put_out_comments)
4030 parse_set_mark (&start_mark, pfile);
4031 newlines = 0;
4032 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4033 &start_line, &start_column);
4034 c = skip_comment (pfile, &newlines);
4035 if (opts->put_out_comments && (c == '/' || c == EOF))
4036 parse_clear_mark (&start_mark);
4037 if (c == '/')
4038 goto randomchar;
4039 if (c == EOF)
4040 {
4041 cpp_error_with_line (pfile, start_line, start_column,
4042 "unterminated comment");
4043 goto handle_eof;
4044 }
4045 c = '/'; /* Initial letter of comment. */
4046 return_comment:
4047 /* Comments are equivalent to spaces.
4048 For -traditional, a comment is equivalent to nothing. */
4049 if (opts->put_out_comments)
4050 {
4051 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4052 U_CHAR *start = pbuf->buf + start_mark.position;
4053 int len = pbuf->cur - start;
4054 CPP_RESERVE(pfile, 1 + len);
4055 CPP_PUTC_Q (pfile, c);
4056 CPP_PUTS_Q (pfile, start, len);
4057 pfile->lineno += newlines;
4058 parse_clear_mark (&start_mark);
4059 return CPP_COMMENT;
4060 }
4061 else if (CPP_TRADITIONAL (pfile))
4062 {
4063 return CPP_COMMENT;
4064 }
4065 else
4066 {
4067 #if 0
4068 /* This may not work if cpp_get_token is called recursively,
4069 since many places look for horizontal space. */
4070 if (newlines)
4071 {
4072 /* Copy the newlines into the output buffer, in order to
4073 avoid the pain of a #line every time a multiline comment
4074 is seen. */
4075 CPP_RESERVE(pfile, newlines);
4076 while (--newlines >= 0)
4077 {
4078 CPP_PUTC_Q (pfile, '\n');
4079 pfile->lineno++;
4080 }
4081 return CPP_VSPACE;
4082 }
4083 #endif
4084 CPP_RESERVE(pfile, 1);
4085 CPP_PUTC_Q (pfile, ' ');
4086 return CPP_HSPACE;
4087 }
4088 #if 0
4089 if (opts->for_lint) {
4090 U_CHAR *argbp;
4091 int cmdlen, arglen;
4092 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4093
4094 if (lintcmd != NULL) {
4095 /* I believe it is always safe to emit this newline: */
4096 obp[-1] = '\n';
4097 bcopy ("#pragma lint ", (char *) obp, 13);
4098 obp += 13;
4099 bcopy (lintcmd, (char *) obp, cmdlen);
4100 obp += cmdlen;
4101
4102 if (arglen != 0) {
4103 *(obp++) = ' ';
4104 bcopy (argbp, (char *) obp, arglen);
4105 obp += arglen;
4106 }
4107
4108 /* OK, now bring us back to the state we were in before we entered
4109 this branch. We need #line because the newline for the pragma
4110 could mess things up. */
4111 output_line_command (pfile, 0, same_file);
4112 *(obp++) = ' '; /* just in case, if comments are copied thru */
4113 *(obp++) = '/';
4114 }
4115 }
4116 #endif
4117
4118 case '#':
4119 #if 0
4120 /* If this is expanding a macro definition, don't recognize
4121 preprocessor directives. */
4122 if (ip->macro != 0)
4123 goto randomchar;
4124 /* If this is expand_into_temp_buffer, recognize them
4125 only after an actual newline at this level,
4126 not at the beginning of the input level. */
4127 if (ip->fname == 0 && beg_of_line == ip->buf)
4128 goto randomchar;
4129 if (ident_length)
4130 goto specialchar;
4131 #endif
4132
4133 if (!pfile->only_seen_white)
4134 goto randomchar;
4135 if (handle_directive (pfile))
4136 return CPP_DIRECTIVE;
4137 pfile->only_seen_white = 0;
4138 return CPP_OTHER;
4139
4140 case '\"':
4141 case '\'':
4142 /* A single quoted string is treated like a double -- some
4143 programs (e.g., troff) are perverse this way */
4144 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4145 &start_line, &start_column);
4146 old_written = CPP_WRITTEN (pfile);
4147 string:
4148 CPP_PUTC (pfile, c);
4149 while (1)
4150 {
4151 int cc = GETC();
4152 if (cc == EOF)
4153 {
4154 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4155 {
4156 /* try harder: this string crosses a macro expansion
4157 boundary. This can happen naturally if -traditional.
4158 Otherwise, only -D can make a macro with an unmatched
4159 quote. */
4160 cpp_buffer *next_buf
4161 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4162 (*CPP_BUFFER (pfile)->cleanup)
4163 (CPP_BUFFER (pfile), pfile);
4164 CPP_BUFFER (pfile) = next_buf;
4165 continue;
4166 }
4167 if (!CPP_TRADITIONAL (pfile))
4168 {
4169 cpp_error_with_line (pfile, start_line, start_column,
4170 "unterminated string or character constant");
4171 if (pfile->multiline_string_line != start_line
4172 && pfile->multiline_string_line != 0)
4173 cpp_error_with_line (pfile,
4174 pfile->multiline_string_line, -1,
4175 "possible real start of unterminated constant");
4176 pfile->multiline_string_line = 0;
4177 }
4178 break;
4179 }
4180 CPP_PUTC (pfile, cc);
4181 switch (cc)
4182 {
4183 case '\n':
4184 /* Traditionally, end of line ends a string constant with
4185 no error. So exit the loop and record the new line. */
4186 if (CPP_TRADITIONAL (pfile))
4187 goto while2end;
4188 if (c == '\'')
4189 {
4190 cpp_error_with_line (pfile, start_line, start_column,
4191 "unterminated character constant");
4192 goto while2end;
4193 }
4194 if (CPP_PEDANTIC (pfile)
4195 && pfile->multiline_string_line == 0)
4196 {
4197 cpp_pedwarn_with_line (pfile, start_line, start_column,
4198 "string constant runs past end of line");
4199 }
4200 if (pfile->multiline_string_line == 0)
4201 pfile->multiline_string_line = start_line;
4202 break;
4203
4204 case '\\':
4205 cc = GETC();
4206 if (cc == '\n')
4207 {
4208 /* Backslash newline is replaced by nothing at all. */
4209 CPP_ADJUST_WRITTEN (pfile, -1);
4210 pfile->lineno++;
4211 }
4212 else
4213 {
4214 /* ANSI stupidly requires that in \\ the second \
4215 is *not* prevented from combining with a newline. */
4216 NEWLINE_FIX1(cc);
4217 if (cc != EOF)
4218 CPP_PUTC (pfile, cc);
4219 }
4220 break;
4221
4222 case '\"':
4223 case '\'':
4224 if (cc == c)
4225 goto while2end;
4226 break;
4227 }
4228 }
4229 while2end:
4230 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4231 CPP_PWRITTEN (pfile));
4232 pfile->only_seen_white = 0;
4233 return c == '\'' ? CPP_CHAR : CPP_STRING;
4234
4235 case '$':
4236 if (!opts->dollars_in_ident)
4237 goto randomchar;
4238 goto letter;
4239
4240 case ':':
4241 if (opts->cplusplus && PEEKC () == ':')
4242 goto op2;
4243 goto randomchar;
4244
4245 case '&':
4246 case '+':
4247 case '|':
4248 NEWLINE_FIX;
4249 c2 = PEEKC ();
4250 if (c2 == c || c2 == '=')
4251 goto op2;
4252 goto randomchar;
4253
4254 case '*':
4255 case '!':
4256 case '%':
4257 case '=':
4258 case '^':
4259 NEWLINE_FIX;
4260 if (PEEKC () == '=')
4261 goto op2;
4262 goto randomchar;
4263
4264 case '-':
4265 NEWLINE_FIX;
4266 c2 = PEEKC ();
4267 if (c2 == '-' && opts->chill)
4268 {
4269 /* Chill style comment */
4270 if (opts->put_out_comments)
4271 parse_set_mark (&start_mark, pfile);
4272 FORWARD(1); /* Skip second '-'. */
4273 for (;;)
4274 {
4275 c = GETC ();
4276 if (c == EOF)
4277 break;
4278 if (c == '\n')
4279 {
4280 /* Don't consider final '\n' to be part of comment. */
4281 FORWARD(-1);
4282 break;
4283 }
4284 }
4285 c = '-';
4286 goto return_comment;
4287 }
4288 if (c2 == '-' || c2 == '=' || c2 == '>')
4289 goto op2;
4290 goto randomchar;
4291
4292 case '<':
4293 if (pfile->parsing_include_directive)
4294 {
4295 for (;;)
4296 {
4297 CPP_PUTC (pfile, c);
4298 if (c == '>')
4299 break;
4300 c = GETC ();
4301 NEWLINE_FIX1 (c);
4302 if (c == '\n' || c == EOF)
4303 {
4304 cpp_error (pfile,
4305 "missing '>' in `#include <FILENAME>'");
4306 break;
4307 }
4308 }
4309 return CPP_STRING;
4310 }
4311 /* else fall through */
4312 case '>':
4313 NEWLINE_FIX;
4314 c2 = PEEKC ();
4315 if (c2 == '=')
4316 goto op2;
4317 if (c2 != c)
4318 goto randomchar;
4319 FORWARD(1);
4320 CPP_RESERVE (pfile, 4);
4321 CPP_PUTC (pfile, c);
4322 CPP_PUTC (pfile, c2);
4323 NEWLINE_FIX;
4324 c3 = PEEKC ();
4325 if (c3 == '=')
4326 CPP_PUTC_Q (pfile, GETC ());
4327 CPP_NUL_TERMINATE_Q (pfile);
4328 pfile->only_seen_white = 0;
4329 return CPP_OTHER;
4330
4331 case '@':
4332 if (CPP_BUFFER (pfile)->has_escapes)
4333 {
4334 c = GETC ();
4335 if (c == '-')
4336 {
4337 if (pfile->output_escapes)
4338 CPP_PUTS (pfile, "@-", 2);
4339 parse_name (pfile, GETC ());
4340 return CPP_NAME;
4341 }
4342 else if (is_space [c])
4343 {
4344 CPP_RESERVE (pfile, 2);
4345 if (pfile->output_escapes)
4346 CPP_PUTC_Q (pfile, '@');
4347 CPP_PUTC_Q (pfile, c);
4348 return CPP_HSPACE;
4349 }
4350 }
4351 if (pfile->output_escapes)
4352 {
4353 CPP_PUTS (pfile, "@@", 2);
4354 return CPP_OTHER;
4355 }
4356 goto randomchar;
4357
4358 case '.':
4359 NEWLINE_FIX;
4360 c2 = PEEKC ();
4361 if (ISDIGIT(c2))
4362 {
4363 CPP_RESERVE(pfile, 2);
4364 CPP_PUTC_Q (pfile, '.');
4365 c = GETC ();
4366 goto number;
4367 }
4368 /* FIXME - misses the case "..\\\n." */
4369 if (c2 == '.' && PEEKN(1) == '.')
4370 {
4371 CPP_RESERVE(pfile, 4);
4372 CPP_PUTC_Q (pfile, '.');
4373 CPP_PUTC_Q (pfile, '.');
4374 CPP_PUTC_Q (pfile, '.');
4375 FORWARD (2);
4376 CPP_NUL_TERMINATE_Q (pfile);
4377 pfile->only_seen_white = 0;
4378 return CPP_3DOTS;
4379 }
4380 goto randomchar;
4381
4382 op2:
4383 token = CPP_OTHER;
4384 pfile->only_seen_white = 0;
4385 op2any:
4386 CPP_RESERVE(pfile, 3);
4387 CPP_PUTC_Q (pfile, c);
4388 CPP_PUTC_Q (pfile, GETC ());
4389 CPP_NUL_TERMINATE_Q (pfile);
4390 return token;
4391
4392 case 'L':
4393 NEWLINE_FIX;
4394 c2 = PEEKC ();
4395 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4396 {
4397 CPP_PUTC (pfile, c);
4398 c = GETC ();
4399 goto string;
4400 }
4401 goto letter;
4402
4403 case '0': case '1': case '2': case '3': case '4':
4404 case '5': case '6': case '7': case '8': case '9':
4405 number:
4406 c2 = '.';
4407 for (;;)
4408 {
4409 CPP_RESERVE (pfile, 2);
4410 CPP_PUTC_Q (pfile, c);
4411 NEWLINE_FIX;
4412 c = PEEKC ();
4413 if (c == EOF)
4414 break;
4415 if (!is_idchar[c] && c != '.'
4416 && ((c2 != 'e' && c2 != 'E'
4417 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4418 || (c != '+' && c != '-')))
4419 break;
4420 FORWARD(1);
4421 c2= c;
4422 }
4423 CPP_NUL_TERMINATE_Q (pfile);
4424 pfile->only_seen_white = 0;
4425 return CPP_NUMBER;
4426 case 'b': case 'c': case 'd': case 'h': case 'o':
4427 case 'B': case 'C': case 'D': case 'H': case 'O':
4428 if (opts->chill && PEEKC () == '\'')
4429 {
4430 pfile->only_seen_white = 0;
4431 CPP_RESERVE (pfile, 2);
4432 CPP_PUTC_Q (pfile, c);
4433 CPP_PUTC_Q (pfile, '\'');
4434 FORWARD(1);
4435 for (;;)
4436 {
4437 c = GETC();
4438 if (c == EOF)
4439 goto chill_number_eof;
4440 if (!is_idchar[c])
4441 {
4442 if (c == '\\' && PEEKC() == '\n')
4443 {
4444 FORWARD(2);
4445 continue;
4446 }
4447 break;
4448 }
4449 CPP_PUTC (pfile, c);
4450 }
4451 if (c == '\'')
4452 {
4453 CPP_RESERVE (pfile, 2);
4454 CPP_PUTC_Q (pfile, c);
4455 CPP_NUL_TERMINATE_Q (pfile);
4456 return CPP_STRING;
4457 }
4458 else
4459 {
4460 FORWARD(-1);
4461 chill_number_eof:
4462 CPP_NUL_TERMINATE (pfile);
4463 return CPP_NUMBER;
4464 }
4465 }
4466 else
4467 goto letter;
4468 case '_':
4469 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4470 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4471 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4472 case 'x': case 'y': case 'z':
4473 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4474 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4475 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4476 case 'Y': case 'Z':
4477 letter:
4478 {
4479 HASHNODE *hp;
4480 unsigned char *ident;
4481 int before_name_written = CPP_WRITTEN (pfile);
4482 int ident_len;
4483 parse_name (pfile, c);
4484 pfile->only_seen_white = 0;
4485 if (pfile->no_macro_expand)
4486 return CPP_NAME;
4487 ident = pfile->token_buffer + before_name_written;
4488 ident_len = CPP_PWRITTEN (pfile) - ident;
4489 hp = cpp_lookup (pfile, ident, ident_len, -1);
4490 if (!hp)
4491 return CPP_NAME;
4492 if (hp->type == T_DISABLED)
4493 {
4494 if (pfile->output_escapes)
4495 { /* Return "@-IDENT", followed by '\0'. */
4496 int i;
4497 CPP_RESERVE (pfile, 3);
4498 ident = pfile->token_buffer + before_name_written;
4499 CPP_ADJUST_WRITTEN (pfile, 2);
4500 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
4501 ident[0] = '@';
4502 ident[1] = '-';
4503 }
4504 return CPP_NAME;
4505 }
4506
4507 /* If macro wants an arglist, verify that a '(' follows.
4508 first skip all whitespace, copying it to the output
4509 after the macro name. Then, if there is no '(',
4510 decide this is not a macro call and leave things that way. */
4511 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
4512 {
4513 struct parse_marker macro_mark;
4514 int is_macro_call, macbuf_whitespace = 0;
4515
4516 parse_set_mark (&macro_mark, pfile);
4517 for (;;)
4518 {
4519 cpp_skip_hspace (pfile);
4520 c = PEEKC ();
4521 is_macro_call = c == '(';
4522 if (c != EOF)
4523 {
4524 if (c != '\n')
4525 break;
4526 FORWARD (1);
4527 }
4528 else
4529 {
4530 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4531 {
4532 if (macro_mark.position !=
4533 (CPP_BUFFER (pfile)->cur
4534 - CPP_BUFFER (pfile)->buf))
4535 macbuf_whitespace = 1;
4536
4537 parse_clear_mark (&macro_mark);
4538 cpp_pop_buffer (pfile);
4539 parse_set_mark (&macro_mark, pfile);
4540 }
4541 else
4542 break;
4543 }
4544 }
4545 if (!is_macro_call)
4546 {
4547 parse_goto_mark (&macro_mark, pfile);
4548 if (macbuf_whitespace)
4549 CPP_PUTC (pfile, ' ');
4550 }
4551 parse_clear_mark (&macro_mark);
4552 if (!is_macro_call)
4553 return CPP_NAME;
4554 }
4555 /* This is now known to be a macro call. */
4556
4557 /* it might not actually be a macro. */
4558 if (hp->type != T_MACRO) {
4559 int xbuf_len; U_CHAR *xbuf;
4560 CPP_SET_WRITTEN (pfile, before_name_written);
4561 special_symbol (hp, pfile);
4562 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
4563 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4564 CPP_SET_WRITTEN (pfile, before_name_written);
4565 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
4566 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4567 }
4568 else
4569 {
4570 /* Expand the macro, reading arguments as needed,
4571 and push the expansion on the input stack. */
4572 macroexpand (pfile, hp);
4573 CPP_SET_WRITTEN (pfile, before_name_written);
4574 }
4575
4576 /* An extra "@ " is added to the end of a macro expansion
4577 to prevent accidental token pasting. We prefer to avoid
4578 unneeded extra spaces (for the sake of cpp-using tools like
4579 imake). Here we remove the space if it is safe to do so. */
4580 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
4581 && pfile->buffer->rlimit[-2] == '@'
4582 && pfile->buffer->rlimit[-1] == ' ')
4583 {
4584 int c1 = pfile->buffer->rlimit[-3];
4585 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
4586 if (c2 == EOF || ! unsafe_chars (c1, c2))
4587 pfile->buffer->rlimit -= 2;
4588 }
4589 }
4590 goto get_next;
4591
4592 case ' ': case '\t': case '\v': case '\r':
4593 for (;;)
4594 {
4595 CPP_PUTC (pfile, c);
4596 c = PEEKC ();
4597 if (c == EOF || !is_hor_space[c])
4598 break;
4599 FORWARD(1);
4600 }
4601 return CPP_HSPACE;
4602
4603 case '\\':
4604 c2 = PEEKC ();
4605 if (c2 != '\n')
4606 goto randomchar;
4607 token = CPP_HSPACE;
4608 goto op2any;
4609
4610 case '\n':
4611 CPP_PUTC (pfile, c);
4612 if (pfile->only_seen_white == 0)
4613 pfile->only_seen_white = 1;
4614 pfile->lineno++;
4615 output_line_command (pfile, 1, same_file);
4616 return CPP_VSPACE;
4617
4618 case '(': token = CPP_LPAREN; goto char1;
4619 case ')': token = CPP_RPAREN; goto char1;
4620 case '{': token = CPP_LBRACE; goto char1;
4621 case '}': token = CPP_RBRACE; goto char1;
4622 case ',': token = CPP_COMMA; goto char1;
4623 case ';': token = CPP_SEMICOLON; goto char1;
4624
4625 randomchar:
4626 default:
4627 token = CPP_OTHER;
4628 char1:
4629 pfile->only_seen_white = 0;
4630 CPP_PUTC (pfile, c);
4631 return token;
4632 }
4633 }
4634 }
4635
4636 /* Like cpp_get_token, but skip spaces and comments. */
4637
4638 enum cpp_token
4639 cpp_get_non_space_token (pfile)
4640 cpp_reader *pfile;
4641 {
4642 int old_written = CPP_WRITTEN (pfile);
4643 for (;;)
4644 {
4645 enum cpp_token token = cpp_get_token (pfile);
4646 if (token != CPP_COMMENT && token != CPP_POP
4647 && token != CPP_HSPACE && token != CPP_VSPACE)
4648 return token;
4649 CPP_SET_WRITTEN (pfile, old_written);
4650 }
4651 }
4652
4653 /* Parse an identifier starting with C. */
4654
4655 static int
4656 parse_name (pfile, c)
4657 cpp_reader *pfile; int c;
4658 {
4659 for (;;)
4660 {
4661 if (! is_idchar[c])
4662 {
4663 if (c == '\\' && PEEKC() == '\n')
4664 {
4665 FORWARD(2);
4666 continue;
4667 }
4668 FORWARD (-1);
4669 break;
4670 }
4671
4672 if (c == '$' && CPP_PEDANTIC (pfile))
4673 cpp_pedwarn (pfile, "`$' in identifier");
4674
4675 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
4676 CPP_PUTC_Q (pfile, c);
4677 c = GETC();
4678 if (c == EOF)
4679 break;
4680 }
4681 CPP_NUL_TERMINATE_Q (pfile);
4682 return 1;
4683 }
4684
4685 /* This is called after options have been processed.
4686 * Check options for consistency, and setup for processing input
4687 * from the file named FNAME. (Use standard input if FNAME==NULL.)
4688 * Return 1 on success, 0 on failure.
4689 */
4690
4691 int
4692 cpp_start_read (pfile, fname)
4693 cpp_reader *pfile;
4694 char *fname;
4695 {
4696 struct cpp_options *opts = CPP_OPTIONS (pfile);
4697 struct cpp_pending *pend;
4698 char *p;
4699 int f;
4700 cpp_buffer *fp;
4701 struct include_hash *ih_fake;
4702
4703 /* The code looks at the defaults through this pointer, rather than through
4704 the constant structure above. This pointer gets changed if an environment
4705 variable specifies other defaults. */
4706 struct default_include *include_defaults = include_defaults_array;
4707
4708 /* Now that we know dollars_in_ident for real,
4709 reset is_idchar/is_idstart. */
4710 is_idchar['$'] = opts->dollars_in_ident;
4711 is_idstart['$'] = opts->dollars_in_ident;
4712
4713 /* Add dirs from CPATH after dirs from -I. */
4714 /* There seems to be confusion about what CPATH should do,
4715 so for the moment it is not documented. */
4716 /* Some people say that CPATH should replace the standard include dirs,
4717 but that seems pointless: it comes before them, so it overrides them
4718 anyway. */
4719 GET_ENV_PATH_LIST (p, "CPATH");
4720 if (p != 0 && ! opts->no_standard_includes)
4721 path_include (pfile, p);
4722
4723 /* Do partial setup of input buffer for the sake of generating
4724 early #line directives (when -g is in effect). */
4725 fp = cpp_push_buffer (pfile, NULL, 0);
4726 if (!fp)
4727 return 0;
4728 if (opts->in_fname == NULL || *opts->in_fname == 0)
4729 {
4730 opts->in_fname = fname;
4731 if (opts->in_fname == NULL)
4732 opts->in_fname = "";
4733 }
4734 fp->nominal_fname = fp->fname = opts->in_fname;
4735 fp->lineno = 0;
4736
4737 /* Install __LINE__, etc. Must follow initialize_char_syntax
4738 and option processing. */
4739 initialize_builtins (pfile);
4740
4741 /* Do standard #defines and assertions
4742 that identify system and machine type. */
4743
4744 if (!opts->inhibit_predefs) {
4745 char *p = (char *) alloca (strlen (predefs) + 1);
4746 strcpy (p, predefs);
4747 while (*p) {
4748 char *q;
4749 while (*p == ' ' || *p == '\t')
4750 p++;
4751 /* Handle -D options. */
4752 if (p[0] == '-' && p[1] == 'D') {
4753 q = &p[2];
4754 while (*p && *p != ' ' && *p != '\t')
4755 p++;
4756 if (*p != 0)
4757 *p++= 0;
4758 if (opts->debug_output)
4759 output_line_command (pfile, 0, same_file);
4760 cpp_define (pfile, q);
4761 while (*p == ' ' || *p == '\t')
4762 p++;
4763 } else if (p[0] == '-' && p[1] == 'A') {
4764 /* Handle -A options (assertions). */
4765 char *assertion;
4766 char *past_name;
4767 char *value;
4768 char *past_value;
4769 char *termination;
4770 int save_char;
4771
4772 assertion = &p[2];
4773 past_name = assertion;
4774 /* Locate end of name. */
4775 while (*past_name && *past_name != ' '
4776 && *past_name != '\t' && *past_name != '(')
4777 past_name++;
4778 /* Locate `(' at start of value. */
4779 value = past_name;
4780 while (*value && (*value == ' ' || *value == '\t'))
4781 value++;
4782 if (*value++ != '(')
4783 abort ();
4784 while (*value && (*value == ' ' || *value == '\t'))
4785 value++;
4786 past_value = value;
4787 /* Locate end of value. */
4788 while (*past_value && *past_value != ' '
4789 && *past_value != '\t' && *past_value != ')')
4790 past_value++;
4791 termination = past_value;
4792 while (*termination && (*termination == ' ' || *termination == '\t'))
4793 termination++;
4794 if (*termination++ != ')')
4795 abort ();
4796 if (*termination && *termination != ' ' && *termination != '\t')
4797 abort ();
4798 /* Temporarily null-terminate the value. */
4799 save_char = *termination;
4800 *termination = '\0';
4801 /* Install the assertion. */
4802 make_assertion (pfile, "-A", assertion);
4803 *termination = (char) save_char;
4804 p = termination;
4805 while (*p == ' ' || *p == '\t')
4806 p++;
4807 } else {
4808 abort ();
4809 }
4810 }
4811 }
4812
4813 /* Now handle the command line options. */
4814
4815 /* Do -U's, -D's and -A's in the order they were seen. */
4816 /* First reverse the list. */
4817 opts->pending = nreverse_pending (opts->pending);
4818
4819 for (pend = opts->pending; pend; pend = pend->next)
4820 {
4821 if (pend->cmd != NULL && pend->cmd[0] == '-')
4822 {
4823 switch (pend->cmd[1])
4824 {
4825 case 'U':
4826 if (opts->debug_output)
4827 output_line_command (pfile, 0, same_file);
4828 cpp_undef (pfile, pend->arg);
4829 break;
4830 case 'D':
4831 if (opts->debug_output)
4832 output_line_command (pfile, 0, same_file);
4833 cpp_define (pfile, pend->arg);
4834 break;
4835 case 'A':
4836 make_assertion (pfile, "-A", pend->arg);
4837 break;
4838 }
4839 }
4840 }
4841
4842 opts->done_initializing = 1;
4843
4844 { /* Read the appropriate environment variable and if it exists
4845 replace include_defaults with the listed path. */
4846 char *epath = 0;
4847 switch ((opts->objc << 1) + opts->cplusplus)
4848 {
4849 case 0:
4850 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
4851 break;
4852 case 1:
4853 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
4854 break;
4855 case 2:
4856 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
4857 break;
4858 case 3:
4859 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
4860 break;
4861 }
4862 /* If the environment var for this language is set,
4863 add to the default list of include directories. */
4864 if (epath) {
4865 char *nstore = (char *) alloca (strlen (epath) + 2);
4866 int num_dirs;
4867 char *startp, *endp;
4868
4869 for (num_dirs = 1, startp = epath; *startp; startp++)
4870 if (*startp == PATH_SEPARATOR)
4871 num_dirs++;
4872 include_defaults
4873 = (struct default_include *) xmalloc ((num_dirs
4874 * sizeof (struct default_include))
4875 + sizeof (include_defaults_array));
4876 startp = endp = epath;
4877 num_dirs = 0;
4878 while (1) {
4879 /* Handle cases like c:/usr/lib:d:/gcc/lib */
4880 if ((*endp == PATH_SEPARATOR)
4881 || *endp == 0) {
4882 strncpy (nstore, startp, endp-startp);
4883 if (endp == startp)
4884 strcpy (nstore, ".");
4885 else
4886 nstore[endp-startp] = '\0';
4887
4888 include_defaults[num_dirs].fname = xstrdup (nstore);
4889 include_defaults[num_dirs].component = 0;
4890 include_defaults[num_dirs].cplusplus = opts->cplusplus;
4891 include_defaults[num_dirs].cxx_aware = 1;
4892 num_dirs++;
4893 if (*endp == '\0')
4894 break;
4895 endp = startp = endp + 1;
4896 } else
4897 endp++;
4898 }
4899 /* Put the usual defaults back in at the end. */
4900 bcopy ((char *) include_defaults_array,
4901 (char *) &include_defaults[num_dirs],
4902 sizeof (include_defaults_array));
4903 }
4904 }
4905
4906 /* Unless -fnostdinc,
4907 tack on the standard include file dirs to the specified list */
4908 if (!opts->no_standard_includes) {
4909 struct default_include *p = include_defaults;
4910 char *specd_prefix = opts->include_prefix;
4911 char *default_prefix = xstrdup (GCC_INCLUDE_DIR);
4912 int default_len = 0;
4913 /* Remove the `include' from /usr/local/lib/gcc.../include. */
4914 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
4915 default_len = strlen (default_prefix) - 7;
4916 default_prefix[default_len] = 0;
4917 }
4918 /* Search "translated" versions of GNU directories.
4919 These have /usr/local/lib/gcc... replaced by specd_prefix. */
4920 if (specd_prefix != 0 && default_len != 0)
4921 for (p = include_defaults; p->fname; p++) {
4922 /* Some standard dirs are only for C++. */
4923 if (!p->cplusplus
4924 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
4925 /* Does this dir start with the prefix? */
4926 if (!strncmp (p->fname, default_prefix, default_len)) {
4927 /* Yes; change prefix and add to search list. */
4928 int this_len = strlen (specd_prefix)
4929 + strlen (p->fname) - default_len;
4930 char *str = (char *) xmalloc (this_len + 1);
4931 strcpy (str, specd_prefix);
4932 strcat (str, p->fname + default_len);
4933
4934 append_include_chain (pfile, &opts->system_include,
4935 str, !p->cxx_aware);
4936 }
4937 }
4938 }
4939 /* Search ordinary names for GNU include directories. */
4940 for (p = include_defaults; p->fname; p++) {
4941 /* Some standard dirs are only for C++. */
4942 if (!p->cplusplus
4943 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
4944 const char *str = update_path (p->fname, p->component);
4945 append_include_chain (pfile, &opts->system_include,
4946 str, !p->cxx_aware);
4947 }
4948 }
4949 }
4950
4951 merge_include_chains (opts);
4952
4953 /* With -v, print the list of dirs to search. */
4954 if (opts->verbose) {
4955 struct file_name_list *p;
4956 fprintf (stderr, "#include \"...\" search starts here:\n");
4957 for (p = opts->quote_include; p; p = p->next) {
4958 if (p == opts->bracket_include)
4959 fprintf (stderr, "#include <...> search starts here:\n");
4960 fprintf (stderr, " %s\n", p->name);
4961 }
4962 fprintf (stderr, "End of search list.\n");
4963 }
4964
4965 /* Copy the entire contents of the main input file into
4966 the stacked input buffer previously allocated for it. */
4967 if (fname == NULL || *fname == 0) {
4968 fname = "";
4969 f = 0;
4970 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
4971 cpp_pfatal_with_name (pfile, fname);
4972
4973 /* -MG doesn't select the form of output and must be specified with one of
4974 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
4975 inhibit compilation. */
4976 if (opts->print_deps_missing_files
4977 && (opts->print_deps == 0 || !opts->no_output))
4978 {
4979 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
4980 return 0;
4981 }
4982
4983 /* Either of two environment variables can specify output of deps.
4984 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
4985 where OUTPUT_FILE is the file to write deps info to
4986 and DEPS_TARGET is the target to mention in the deps. */
4987
4988 if (opts->print_deps == 0
4989 && (getenv ("SUNPRO_DEPENDENCIES") != 0
4990 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
4991 char *spec = getenv ("DEPENDENCIES_OUTPUT");
4992 char *s;
4993 char *output_file;
4994
4995 if (spec == 0)
4996 {
4997 spec = getenv ("SUNPRO_DEPENDENCIES");
4998 opts->print_deps = 2;
4999 }
5000 else
5001 opts->print_deps = 1;
5002
5003 s = spec;
5004 /* Find the space before the DEPS_TARGET, if there is one. */
5005 /* This should use index. (mrs) */
5006 while (*s != 0 && *s != ' ') s++;
5007 if (*s != 0)
5008 {
5009 opts->deps_target = s + 1;
5010 output_file = (char *) xmalloc (s - spec + 1);
5011 bcopy (spec, output_file, s - spec);
5012 output_file[s - spec] = 0;
5013 }
5014 else
5015 {
5016 opts->deps_target = 0;
5017 output_file = spec;
5018 }
5019
5020 opts->deps_file = output_file;
5021 opts->print_deps_append = 1;
5022 }
5023
5024 /* For -M, print the expected object file name
5025 as the target of this Make-rule. */
5026 if (opts->print_deps)
5027 {
5028 pfile->deps_allocated_size = 200;
5029 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
5030 pfile->deps_buffer[0] = 0;
5031 pfile->deps_size = 0;
5032 pfile->deps_column = 0;
5033
5034 if (opts->deps_target)
5035 deps_output (pfile, opts->deps_target, ':');
5036 else if (*opts->in_fname == 0)
5037 deps_output (pfile, "-", ':');
5038 else
5039 {
5040 char *p, *q, *r;
5041 int len, x;
5042 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
5043 ".cc", ".cxx", ".cpp", ".cp",
5044 ".c++", 0
5045 };
5046
5047 /* Discard all directory prefixes from filename. */
5048 if ((q = rindex (opts->in_fname, '/')) != NULL
5049 #ifdef DIR_SEPARATOR
5050 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
5051 #endif
5052 )
5053 ++q;
5054 else
5055 q = opts->in_fname;
5056
5057 /* Copy remainder to mungable area. */
5058 p = (char *) alloca (strlen(q) + 8);
5059 strcpy (p, q);
5060
5061 /* Output P, but remove known suffixes. */
5062 len = strlen (p);
5063 q = p + len;
5064 /* Point to the filename suffix. */
5065 r = rindex (p, '.');
5066 /* Compare against the known suffixes. */
5067 x = 0;
5068 while (known_suffixes[x] != 0)
5069 {
5070 if (strncmp (known_suffixes[x], r, q - r) == 0)
5071 {
5072 /* Make q point to the bit we're going to overwrite
5073 with an object suffix. */
5074 q = r;
5075 break;
5076 }
5077 x++;
5078 }
5079
5080 /* Supply our own suffix. */
5081 #ifndef VMS
5082 strcpy (q, ".o");
5083 #else
5084 strcpy (q, ".obj");
5085 #endif
5086
5087 deps_output (pfile, p, ':');
5088 deps_output (pfile, opts->in_fname, ' ');
5089 }
5090 }
5091
5092 #if 0
5093 /* Make sure data ends with a newline. And put a null after it. */
5094
5095 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5096 /* Backslash-newline at end is not good enough. */
5097 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5098 fp->buf[fp->length++] = '\n';
5099 missing_newline = 1;
5100 }
5101 fp->buf[fp->length] = '\0';
5102
5103 /* Unless inhibited, convert trigraphs in the input. */
5104
5105 if (!no_trigraphs)
5106 trigraph_pcp (fp);
5107 #endif
5108
5109 /* Must call finclude() on the main input before processing
5110 -include switches; otherwise the -included text winds up
5111 after the main input. */
5112 ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
5113 ih_fake->next = 0;
5114 ih_fake->next_this_file = 0;
5115 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5116 ih_fake->name = fname;
5117 ih_fake->control_macro = 0;
5118 ih_fake->buf = (char *)-1;
5119 ih_fake->limit = 0;
5120 if (!finclude (pfile, f, ih_fake))
5121 return 0;
5122 output_line_command (pfile, 0, same_file);
5123 pfile->only_seen_white = 2;
5124
5125 /* The -imacros files can be scanned now, but the -include files
5126 have to be pushed onto the include stack and processed later,
5127 in the main loop calling cpp_get_token. That means the -include
5128 files have to be processed in reverse order of the pending list,
5129 which means the pending list has to be reversed again, which
5130 means the -imacros files have to be done separately and first. */
5131
5132 pfile->no_record_file++;
5133 opts->no_output++;
5134 for (pend = opts->pending; pend; pend = pend->next)
5135 {
5136 if (pend->cmd != NULL)
5137 {
5138 if (strcmp (pend->cmd, "-imacros") == 0)
5139 {
5140 int fd = open (pend->arg, O_RDONLY, 0666);
5141 if (fd < 0)
5142 {
5143 cpp_perror_with_name (pfile, pend->arg);
5144 return 0;
5145 }
5146 if (!cpp_push_buffer (pfile, NULL, 0))
5147 return 0;
5148
5149 ih_fake = (struct include_hash *)
5150 xmalloc (sizeof (struct include_hash));
5151 ih_fake->next = 0;
5152 ih_fake->next_this_file = 0;
5153 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5154 ih_fake->name = pend->arg;
5155 ih_fake->control_macro = 0;
5156 ih_fake->buf = (char *)-1;
5157 ih_fake->limit = 0;
5158 if (!finclude (pfile, fd, ih_fake))
5159 cpp_scan_buffer (pfile);
5160 free (ih_fake);
5161 }
5162 }
5163 }
5164 opts->no_output--;
5165 opts->pending = nreverse_pending (opts->pending);
5166 for (pend = opts->pending; pend; pend = pend->next)
5167 {
5168 if (pend->cmd != NULL)
5169 {
5170 if (strcmp (pend->cmd, "-include") == 0)
5171 {
5172 int fd = open (pend->arg, O_RDONLY, 0666);
5173 if (fd < 0)
5174 {
5175 cpp_perror_with_name (pfile, pend->arg);
5176 return 0;
5177 }
5178 if (!cpp_push_buffer (pfile, NULL, 0))
5179 return 0;
5180
5181 ih_fake = (struct include_hash *)
5182 xmalloc (sizeof (struct include_hash));
5183 ih_fake->next = 0;
5184 ih_fake->next_this_file = 0;
5185 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5186 ih_fake->name = pend->arg;
5187 ih_fake->control_macro = 0;
5188 ih_fake->buf = (char *)-1;
5189 ih_fake->limit = 0;
5190 if (finclude (pfile, fd, ih_fake))
5191 output_line_command (pfile, 0, enter_file);
5192 }
5193 }
5194 }
5195 pfile->no_record_file--;
5196
5197 /* Free the pending list. */
5198 for (pend = opts->pending; pend; )
5199 {
5200 struct cpp_pending *next = pend->next;
5201 free (pend);
5202 pend = next;
5203 }
5204 opts->pending = NULL;
5205
5206 return 1;
5207 }
5208
5209 void
5210 cpp_reader_init (pfile)
5211 cpp_reader *pfile;
5212 {
5213 bzero ((char *) pfile, sizeof (cpp_reader));
5214 pfile->get_token = cpp_get_token;
5215
5216 pfile->token_buffer_size = 200;
5217 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
5218 CPP_SET_WRITTEN (pfile, 0);
5219
5220 pfile->timebuf = NULL;
5221 pfile->only_seen_white = 1;
5222 pfile->buffer = CPP_NULL_BUFFER(pfile);
5223 pfile->actual_dirs = NULL;
5224 }
5225
5226 static struct cpp_pending *
5227 nreverse_pending (list)
5228 struct cpp_pending *list;
5229
5230 {
5231 register struct cpp_pending *prev = 0, *next, *pend;
5232 for (pend = list; pend; pend = next)
5233 {
5234 next = pend->next;
5235 pend->next = prev;
5236 prev = pend;
5237 }
5238 return prev;
5239 }
5240
5241 static void
5242 push_pending (pfile, cmd, arg)
5243 cpp_reader *pfile;
5244 char *cmd;
5245 char *arg;
5246 {
5247 struct cpp_pending *pend
5248 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
5249 pend->cmd = cmd;
5250 pend->arg = arg;
5251 pend->next = CPP_OPTIONS (pfile)->pending;
5252 CPP_OPTIONS (pfile)->pending = pend;
5253 }
5254
5255 \f
5256 static void
5257 print_help ()
5258 {
5259 printf ("Usage: %s [switches] input output\n", progname);
5260 printf ("Switches:\n");
5261 printf (" -include <file> Include the contents of <file> before other files\n");
5262 printf (" -imacros <file> Accept definition of marcos in <file>\n");
5263 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
5264 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
5265 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
5266 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
5267 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
5268 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
5269 printf (" -nostdinc Do not search the system include directories\n");
5270 printf (" -nostdinc++ Do not search the system include directories for C++\n");
5271 printf (" -o <file> Put output into <file>\n");
5272 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
5273 printf (" -traditional Follow K&R pre-processor behaviour\n");
5274 printf (" -trigraphs Support ANSI C trigraphs\n");
5275 printf (" -lang-c Assume that the input sources are in C\n");
5276 printf (" -lang-c89 Assume that the input sources are in C89\n");
5277 printf (" -lang-c++ Assume that the input sources are in C++\n");
5278 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
5279 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
5280 printf (" -lang-asm Assume that the input sources are in assembler\n");
5281 printf (" -lang-chill Assume that the input sources are in Chill\n");
5282 printf (" -+ Allow parsing of C++ style features\n");
5283 printf (" -w Inhibit warning messages\n");
5284 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
5285 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
5286 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
5287 printf (" -Wno-comment{s} Do not warn about comments\n");
5288 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
5289 printf (" a string if -traditional is specified\n");
5290 printf (" -Wno-traditional Do not warn about stringification\n");
5291 printf (" -Wundef Warn if an undefined macro is used by #if\n");
5292 printf (" -Wno-undef Do not warn about testing undefined macros\n");
5293 printf (" -Wimport Warn about the use of the #import directive\n");
5294 printf (" -Wno-import Do not warn about the use of #import\n");
5295 printf (" -Werror Treat all warnings as errors\n");
5296 printf (" -Wno-error Do not treat warnings as errors\n");
5297 printf (" -Wall Enable all preprocessor warnings\n");
5298 printf (" -M Generate make dependencies\n");
5299 printf (" -MM As -M, but ignore system header files\n");
5300 printf (" -MD As -M, but put output in a .d file\n");
5301 printf (" -MMD As -MD, but ignore system header files\n");
5302 printf (" -MG Treat missing header file as generated files\n");
5303 printf (" -g Include #define and #undef directives in the output\n");
5304 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
5305 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
5306 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
5307 printf (" -U<macro> Undefine <macro> \n");
5308 printf (" -u or -undef Do not predefine any macros\n");
5309 printf (" -v Display the version number\n");
5310 printf (" -H Print the name of header files as they are used\n");
5311 printf (" -C Do not discard comments\n");
5312 printf (" -dM Display a list of macro definitions active at end\n");
5313 printf (" -dD Preserve macro definitions in output\n");
5314 printf (" -dN As -dD except that only the names are preserved\n");
5315 printf (" -dI Include #include directives in the output\n");
5316 printf (" -ifoutput Describe skipped code blocks in output \n");
5317 printf (" -P Do not generate #line directives\n");
5318 printf (" -$ Do not include '$' in identifiers\n");
5319 printf (" -remap Remap file names when including files.\n");
5320 printf (" -h or --help Display this information\n");
5321 }
5322 \f
5323
5324 /* Handle one command-line option in (argc, argv).
5325 Can be called multiple times, to handle multiple sets of options.
5326 Returns number of strings consumed. */
5327 int
5328 cpp_handle_option (pfile, argc, argv)
5329 cpp_reader *pfile;
5330 int argc;
5331 char **argv;
5332 {
5333 struct cpp_options *opts = CPP_OPTIONS (pfile);
5334 int i = 0;
5335
5336 if (user_label_prefix == NULL)
5337 user_label_prefix = USER_LABEL_PREFIX;
5338
5339 if (argv[i][0] != '-') {
5340 if (opts->out_fname != NULL)
5341 {
5342 print_help ();
5343 cpp_fatal (pfile, "Too many arguments");
5344 }
5345 else if (opts->in_fname != NULL)
5346 opts->out_fname = argv[i];
5347 else
5348 opts->in_fname = argv[i];
5349 } else {
5350 switch (argv[i][1]) {
5351
5352 missing_filename:
5353 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
5354 return argc;
5355 missing_dirname:
5356 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
5357 return argc;
5358
5359 case 'f':
5360 if (!strcmp (argv[i], "-fleading-underscore"))
5361 user_label_prefix = "_";
5362 else if (!strcmp (argv[i], "-fno-leading-underscore"))
5363 user_label_prefix = "";
5364 break;
5365
5366 case 'I': /* Add directory to path for includes. */
5367 if (!strcmp (argv[i] + 2, "-"))
5368 {
5369 if (! opts->ignore_srcdir)
5370 {
5371 opts->ignore_srcdir = 1;
5372 /* Don't use any preceding -I directories for #include <...>. */
5373 opts->quote_include = opts->bracket_include;
5374 opts->bracket_include = 0;
5375 }
5376 }
5377 else
5378 {
5379 char *fname;
5380 if (argv[i][2] != 0)
5381 fname = argv[i] + 2;
5382 else if (i + 1 == argc)
5383 goto missing_dirname;
5384 else
5385 fname = argv[++i];
5386 append_include_chain (pfile, &opts->bracket_include, fname, 0);
5387 }
5388 break;
5389
5390 case 'i':
5391 /* Add directory to beginning of system include path, as a system
5392 include directory. */
5393 if (!strcmp (argv[i], "-isystem"))
5394 {
5395 if (i + 1 == argc)
5396 goto missing_filename;
5397 append_include_chain (pfile, &opts->system_include, argv[++i], 1);
5398 }
5399 /* Add directory to end of path for includes,
5400 with the default prefix at the front of its name. */
5401 else if (!strcmp (argv[i], "-iwithprefix"))
5402 {
5403 char *fname;
5404 if (i + 1 == argc)
5405 goto missing_dirname;
5406 ++i;
5407
5408 if (opts->include_prefix != 0)
5409 {
5410 fname = xmalloc (strlen (opts->include_prefix)
5411 + strlen (argv[i]) + 1);
5412 strcpy (fname, opts->include_prefix);
5413 strcat (fname, argv[i]);
5414 }
5415 else
5416 {
5417 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5418 + strlen (argv[i]) + 1);
5419 strcpy (fname, GCC_INCLUDE_DIR);
5420 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5421 if (!strcmp (fname + strlen (fname) - 8, "/include"))
5422 fname[strlen (fname) - 7] = 0;
5423 strcat (fname, argv[i]);
5424 }
5425
5426 append_include_chain (pfile, &opts->system_include, fname, 0);
5427 }
5428 /* Add directory to main path for includes,
5429 with the default prefix at the front of its name. */
5430 else if (!strcmp (argv[i], "-iwithprefix"))
5431 {
5432 char *fname;
5433 if (i + 1 == argc)
5434 goto missing_dirname;
5435 ++i;
5436
5437 if (opts->include_prefix != 0)
5438 {
5439 fname = xmalloc (strlen (opts->include_prefix)
5440 + strlen (argv[i]) + 1);
5441 strcpy (fname, opts->include_prefix);
5442 strcat (fname, argv[i]);
5443 }
5444 else
5445 {
5446 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5447 + strlen (argv[i]) + 1);
5448 strcpy (fname, GCC_INCLUDE_DIR);
5449 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5450 if (!strcmp (fname + strlen (fname) - 8, "/include"))
5451 fname[strlen (fname) - 7] = 0;
5452 strcat (fname, argv[i]);
5453 }
5454
5455 append_include_chain (pfile, &opts->bracket_include, fname, 0);
5456 }
5457 /* Add directory to end of path for includes. */
5458 else if (!strcmp (argv[i], "-idirafter"))
5459 {
5460 if (i + 1 == argc)
5461 goto missing_dirname;
5462 append_include_chain (pfile, &opts->after_include, argv[++i], 0);
5463 }
5464 else if (!strcmp (argv[i], "-include") || !strcmp (argv[i], "-imacros"))
5465 {
5466 if (i + 1 == argc)
5467 goto missing_filename;
5468 else
5469 push_pending (pfile, argv[i], argv[i+1]), i++;
5470 }
5471 else if (!strcmp (argv[i], "-iprefix"))
5472 {
5473 if (i + 1 == argc)
5474 goto missing_filename;
5475 else
5476 opts->include_prefix = argv[++i];
5477 }
5478 else if (!strcmp (argv[i], "-ifoutput"))
5479 opts->output_conditionals = 1;
5480
5481 break;
5482
5483 case 'o':
5484 if (opts->out_fname != NULL)
5485 {
5486 cpp_fatal (pfile, "Output filename specified twice");
5487 return argc;
5488 }
5489 if (i + 1 == argc)
5490 goto missing_filename;
5491 opts->out_fname = argv[++i];
5492 if (!strcmp (opts->out_fname, "-"))
5493 opts->out_fname = "";
5494 break;
5495
5496 case 'p':
5497 if (!strcmp (argv[i], "-pedantic"))
5498 CPP_PEDANTIC (pfile) = 1;
5499 else if (!strcmp (argv[i], "-pedantic-errors")) {
5500 CPP_PEDANTIC (pfile) = 1;
5501 opts->pedantic_errors = 1;
5502 }
5503 #if 0
5504 else if (!strcmp (argv[i], "-pcp")) {
5505 char *pcp_fname = argv[++i];
5506 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
5507 ? fopen (pcp_fname, "w")
5508 : fdopen (dup (fileno (stdout)), "w"));
5509 if (pcp_outfile == 0)
5510 cpp_pfatal_with_name (pfile, pcp_fname);
5511 no_precomp = 1;
5512 }
5513 #endif
5514 break;
5515
5516 case 't':
5517 if (!strcmp (argv[i], "-traditional")) {
5518 opts->traditional = 1;
5519 opts->cplusplus_comments = 0;
5520 } else if (!strcmp (argv[i], "-trigraphs")) {
5521 if (!opts->chill)
5522 opts->no_trigraphs = 0;
5523 }
5524 break;
5525
5526 case 'l':
5527 if (! strcmp (argv[i], "-lang-c"))
5528 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5529 opts->objc = 0;
5530 if (! strcmp (argv[i], "-lang-c89"))
5531 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
5532 opts->objc = 0;
5533 if (! strcmp (argv[i], "-lang-c++"))
5534 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5535 opts->objc = 0;
5536 if (! strcmp (argv[i], "-lang-objc"))
5537 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5538 opts->objc = 1;
5539 if (! strcmp (argv[i], "-lang-objc++"))
5540 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5541 opts->objc = 1;
5542 if (! strcmp (argv[i], "-lang-asm"))
5543 opts->lang_asm = 1;
5544 if (! strcmp (argv[i], "-lint"))
5545 opts->for_lint = 1;
5546 if (! strcmp (argv[i], "-lang-chill"))
5547 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
5548 opts->traditional = 1, opts->no_trigraphs = 1;
5549 break;
5550
5551 case '+':
5552 opts->cplusplus = 1, opts->cplusplus_comments = 1;
5553 break;
5554
5555 case 'w':
5556 opts->inhibit_warnings = 1;
5557 break;
5558
5559 case 'W':
5560 if (!strcmp (argv[i], "-Wtrigraphs"))
5561 opts->warn_trigraphs = 1;
5562 else if (!strcmp (argv[i], "-Wno-trigraphs"))
5563 opts->warn_trigraphs = 0;
5564 else if (!strcmp (argv[i], "-Wcomment"))
5565 opts->warn_comments = 1;
5566 else if (!strcmp (argv[i], "-Wno-comment"))
5567 opts->warn_comments = 0;
5568 else if (!strcmp (argv[i], "-Wcomments"))
5569 opts->warn_comments = 1;
5570 else if (!strcmp (argv[i], "-Wno-comments"))
5571 opts->warn_comments = 0;
5572 else if (!strcmp (argv[i], "-Wtraditional"))
5573 opts->warn_stringify = 1;
5574 else if (!strcmp (argv[i], "-Wno-traditional"))
5575 opts->warn_stringify = 0;
5576 else if (!strcmp (argv[i], "-Wundef"))
5577 opts->warn_undef = 1;
5578 else if (!strcmp (argv[i], "-Wno-undef"))
5579 opts->warn_undef = 0;
5580 else if (!strcmp (argv[i], "-Wimport"))
5581 opts->warn_import = 1;
5582 else if (!strcmp (argv[i], "-Wno-import"))
5583 opts->warn_import = 0;
5584 else if (!strcmp (argv[i], "-Werror"))
5585 opts->warnings_are_errors = 1;
5586 else if (!strcmp (argv[i], "-Wno-error"))
5587 opts->warnings_are_errors = 0;
5588 else if (!strcmp (argv[i], "-Wall"))
5589 {
5590 opts->warn_trigraphs = 1;
5591 opts->warn_comments = 1;
5592 }
5593 break;
5594
5595 case 'M':
5596 /* The style of the choices here is a bit mixed.
5597 The chosen scheme is a hybrid of keeping all options in one string
5598 and specifying each option in a separate argument:
5599 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
5600 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5601 -M[M][G][D file]. This is awkward to handle in specs, and is not
5602 as extensible. */
5603 /* ??? -MG must be specified in addition to one of -M or -MM.
5604 This can be relaxed in the future without breaking anything.
5605 The converse isn't true. */
5606
5607 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
5608 if (!strcmp (argv[i], "-MG"))
5609 {
5610 opts->print_deps_missing_files = 1;
5611 break;
5612 }
5613 if (!strcmp (argv[i], "-M"))
5614 opts->print_deps = 2;
5615 else if (!strcmp (argv[i], "-MM"))
5616 opts->print_deps = 1;
5617 else if (!strcmp (argv[i], "-MD"))
5618 opts->print_deps = 2;
5619 else if (!strcmp (argv[i], "-MMD"))
5620 opts->print_deps = 1;
5621 /* For -MD and -MMD options, write deps on file named by next arg. */
5622 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
5623 {
5624 if (i+1 == argc)
5625 goto missing_filename;
5626 opts->deps_file = argv[++i];
5627 }
5628 else
5629 {
5630 /* For -M and -MM, write deps on standard output
5631 and suppress the usual output. */
5632 opts->no_output = 1;
5633 }
5634 break;
5635
5636 case 'd':
5637 {
5638 char *p = argv[i] + 2;
5639 char c;
5640 while ((c = *p++) != 0) {
5641 /* Arg to -d specifies what parts of macros to dump */
5642 switch (c) {
5643 case 'M':
5644 opts->dump_macros = dump_only;
5645 opts->no_output = 1;
5646 break;
5647 case 'N':
5648 opts->dump_macros = dump_names;
5649 break;
5650 case 'D':
5651 opts->dump_macros = dump_definitions;
5652 break;
5653 case 'I':
5654 opts->dump_includes = 1;
5655 break;
5656 }
5657 }
5658 }
5659 break;
5660
5661 case 'g':
5662 if (argv[i][2] == '3')
5663 opts->debug_output = 1;
5664 break;
5665
5666 case '-':
5667 if (strcmp (argv[i], "--help") != 0)
5668 return i;
5669 print_help ();
5670 break;
5671
5672 case 'v':
5673 fprintf (stderr, "GNU CPP version %s", version_string);
5674 #ifdef TARGET_VERSION
5675 TARGET_VERSION;
5676 #endif
5677 fprintf (stderr, "\n");
5678 opts->verbose = 1;
5679 break;
5680
5681 case 'H':
5682 opts->print_include_names = 1;
5683 break;
5684
5685 case 'D':
5686 if (argv[i][2] != 0)
5687 push_pending (pfile, "-D", argv[i] + 2);
5688 else if (i + 1 == argc)
5689 {
5690 cpp_fatal (pfile, "Macro name missing after -D option");
5691 return argc;
5692 }
5693 else
5694 i++, push_pending (pfile, "-D", argv[i]);
5695 break;
5696
5697 case 'A':
5698 {
5699 char *p;
5700
5701 if (argv[i][2] != 0)
5702 p = argv[i] + 2;
5703 else if (i + 1 == argc)
5704 {
5705 cpp_fatal (pfile, "Assertion missing after -A option");
5706 return argc;
5707 }
5708 else
5709 p = argv[++i];
5710
5711 if (!strcmp (p, "-")) {
5712 struct cpp_pending **ptr;
5713 /* -A- eliminates all predefined macros and assertions.
5714 Let's include also any that were specified earlier
5715 on the command line. That way we can get rid of any
5716 that were passed automatically in from GCC. */
5717 opts->inhibit_predefs = 1;
5718 for (ptr = &opts->pending; *ptr != NULL; )
5719 {
5720 struct cpp_pending *pend = *ptr;
5721 if (pend->cmd && pend->cmd[0] == '-'
5722 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
5723 {
5724 *ptr = pend->next;
5725 free (pend);
5726 }
5727 else
5728 ptr = &pend->next;
5729 }
5730 } else {
5731 push_pending (pfile, "-A", p);
5732 }
5733 }
5734 break;
5735
5736 case 'U': /* JF #undef something */
5737 if (argv[i][2] != 0)
5738 push_pending (pfile, "-U", argv[i] + 2);
5739 else if (i + 1 == argc)
5740 {
5741 cpp_fatal (pfile, "Macro name missing after -U option");
5742 return argc;
5743 }
5744 else
5745 push_pending (pfile, "-U", argv[i+1]), i++;
5746 break;
5747
5748 case 'C':
5749 opts->put_out_comments = 1;
5750 break;
5751
5752 case 'E': /* -E comes from cc -E; ignore it. */
5753 break;
5754
5755 case 'P':
5756 opts->no_line_commands = 1;
5757 break;
5758
5759 case '$': /* Don't include $ in identifiers. */
5760 opts->dollars_in_ident = 0;
5761 break;
5762
5763 case 'n':
5764 if (!strcmp (argv[i], "-nostdinc"))
5765 /* -nostdinc causes no default include directories.
5766 You must specify all include-file directories with -I. */
5767 opts->no_standard_includes = 1;
5768 else if (!strcmp (argv[i], "-nostdinc++"))
5769 /* -nostdinc++ causes no default C++-specific include directories. */
5770 opts->no_standard_cplusplus_includes = 1;
5771 #if 0
5772 else if (!strcmp (argv[i], "-noprecomp"))
5773 no_precomp = 1;
5774 #endif
5775 break;
5776
5777 case 'r':
5778 if (!strcmp (argv[i], "-remap"))
5779 opts->remap = 1;
5780 break;
5781
5782 case 'u':
5783 /* Sun compiler passes undocumented switch "-undef".
5784 Let's assume it means to inhibit the predefined symbols. */
5785 opts->inhibit_predefs = 1;
5786 break;
5787
5788 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
5789 if (opts->in_fname == NULL) {
5790 opts->in_fname = "";
5791 break;
5792 } else if (opts->out_fname == NULL) {
5793 opts->out_fname = "";
5794 break;
5795 } /* else fall through into error */
5796
5797 default:
5798 return i;
5799 }
5800 }
5801
5802 return i + 1;
5803 }
5804
5805 /* Handle command-line options in (argc, argv).
5806 Can be called multiple times, to handle multiple sets of options.
5807 Returns if an unrecognized option is seen.
5808 Returns number of strings consumed. */
5809
5810 int
5811 cpp_handle_options (pfile, argc, argv)
5812 cpp_reader *pfile;
5813 int argc;
5814 char **argv;
5815 {
5816 int i;
5817 int strings_processed;
5818 for (i = 0; i < argc; i += strings_processed)
5819 {
5820 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
5821 if (strings_processed == 0)
5822 break;
5823 }
5824 return i;
5825 }
5826 \f
5827 void
5828 cpp_finish (pfile)
5829 cpp_reader *pfile;
5830 {
5831 struct cpp_options *opts = CPP_OPTIONS (pfile);
5832
5833 if (opts->print_deps)
5834 {
5835 /* Stream on which to print the dependency information. */
5836 FILE *deps_stream;
5837
5838 /* Don't actually write the deps file if compilation has failed. */
5839 if (pfile->errors == 0)
5840 {
5841 char *deps_mode = opts->print_deps_append ? "a" : "w";
5842 if (opts->deps_file == 0)
5843 deps_stream = stdout;
5844 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
5845 cpp_pfatal_with_name (pfile, opts->deps_file);
5846 fputs (pfile->deps_buffer, deps_stream);
5847 putc ('\n', deps_stream);
5848 if (opts->deps_file)
5849 {
5850 if (ferror (deps_stream) || fclose (deps_stream) != 0)
5851 cpp_fatal (pfile, "I/O error on output");
5852 }
5853 }
5854 }
5855
5856 #if 0
5857 /* Debugging: dump statistics on the include hash table. */
5858 {
5859 struct include_hash *x;
5860 int i, j;
5861
5862 for(i = 0; i < ALL_INCLUDE_HASHSIZE; i++)
5863 {
5864 x = pfile->all_include_files[i];
5865 j = 0;
5866 while(x)
5867 {
5868 j++;
5869 x = x->next;
5870 }
5871 fprintf(stderr, "%d/%d ", i, j);
5872 }
5873 fputc('\n', stderr);
5874 }
5875 #endif
5876
5877 }
5878
5879 /* Free resources used by PFILE.
5880 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
5881
5882 void
5883 cpp_cleanup (pfile)
5884 cpp_reader *pfile;
5885 {
5886 int i;
5887 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
5888 cpp_pop_buffer (pfile);
5889
5890 if (pfile->token_buffer)
5891 {
5892 free (pfile->token_buffer);
5893 pfile->token_buffer = NULL;
5894 }
5895
5896 if (pfile->deps_buffer)
5897 {
5898 free (pfile->deps_buffer);
5899 pfile->deps_buffer = NULL;
5900 pfile->deps_allocated_size = 0;
5901 }
5902
5903 while (pfile->if_stack)
5904 {
5905 IF_STACK_FRAME *temp = pfile->if_stack;
5906 pfile->if_stack = temp->next;
5907 free (temp);
5908 }
5909
5910 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
5911 {
5912 struct include_hash *imp = pfile->all_include_files[i];
5913 while (imp)
5914 {
5915 struct include_hash *next = imp->next;
5916 #if 0
5917 /* This gets freed elsewhere - I think. */
5918 free (imp->name);
5919 #endif
5920 free (imp);
5921 imp = next;
5922 }
5923 pfile->all_include_files[i] = 0;
5924 }
5925
5926 cpp_hash_cleanup (pfile);
5927 }
5928 \f
5929 /* Read an assertion into the token buffer, converting to
5930 canonical form: `#predicate(a n swe r)' The next non-whitespace
5931 character to read should be the first letter of the predicate.
5932 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
5933 with answer (see callers for why). In case of 0, an error has been
5934 printed. */
5935 static int
5936 parse_assertion (pfile)
5937 cpp_reader *pfile;
5938 {
5939 int c, dropwhite;
5940 cpp_skip_hspace (pfile);
5941 c = PEEKC();
5942 if (! is_idstart[c])
5943 {
5944 cpp_error (pfile, "assertion predicate is not an identifier");
5945 return 0;
5946 }
5947 CPP_PUTC(pfile, '#');
5948 FORWARD(1);
5949 parse_name(pfile, c);
5950
5951 c = PEEKC();
5952 if (c != '(')
5953 {
5954 if (is_hor_space[c])
5955 cpp_skip_hspace (pfile);
5956 c = PEEKC();
5957 }
5958 if (c != '(')
5959 return 1;
5960
5961 CPP_PUTC(pfile, '(');
5962 FORWARD(1);
5963 dropwhite = 1;
5964 while ((c = GETC()) != ')')
5965 {
5966 if (is_hor_space[c])
5967 {
5968 if (! dropwhite)
5969 {
5970 CPP_PUTC(pfile, ' ');
5971 dropwhite = 1;
5972 }
5973 }
5974 else if (c == '\\' && PEEKC() == '\n')
5975 FORWARD(1);
5976 else if (c == '\n' || c == EOF)
5977 {
5978 if (c == '\n') FORWARD(-1);
5979 cpp_error (pfile, "un-terminated assertion answer");
5980 return 0;
5981 }
5982 else
5983 {
5984 CPP_PUTC(pfile, c);
5985 dropwhite = 0;
5986 }
5987 }
5988
5989 if (pfile->limit[-1] == ' ')
5990 pfile->limit[-1] = ')';
5991 else if (pfile->limit[-1] == '(')
5992 {
5993 cpp_error (pfile, "empty token sequence in assertion");
5994 return 0;
5995 }
5996 else
5997 CPP_PUTC(pfile, ')');
5998
5999 CPP_NUL_TERMINATE(pfile);
6000 return 2;
6001 }
6002
6003 static int
6004 do_assert (pfile, keyword)
6005 cpp_reader *pfile;
6006 struct directive *keyword ATTRIBUTE_UNUSED;
6007 {
6008 char *sym;
6009 int ret, c;
6010 HASHNODE *base, *this;
6011 int baselen, thislen;
6012
6013 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6014 && !CPP_BUFFER (pfile)->system_header_p)
6015 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6016
6017 cpp_skip_hspace (pfile);
6018 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
6019 ret = parse_assertion (pfile);
6020 if (ret == 0)
6021 goto error;
6022 else if (ret == 1)
6023 {
6024 cpp_error (pfile, "missing token-sequence in `#assert'");
6025 goto error;
6026 }
6027
6028 cpp_skip_hspace (pfile);
6029 c = PEEKC();
6030 if (c != EOF && c != '\n')
6031 {
6032 cpp_error (pfile, "junk at end of `#assert'");
6033 goto error;
6034 }
6035
6036 thislen = strlen (sym);
6037 baselen = index (sym, '(') - sym;
6038 this = cpp_lookup (pfile, sym, thislen, -1);
6039 if (this)
6040 {
6041 cpp_warning (pfile, "`%s' re-asserted", sym);
6042 goto error;
6043 }
6044
6045 base = cpp_lookup (pfile, sym, baselen, -1);
6046 if (! base)
6047 base = install (sym, baselen, T_ASSERT, 0, 0, -1);
6048 else if (base->type != T_ASSERT)
6049 {
6050 /* Token clash - but with what?! */
6051 cpp_fatal (pfile,
6052 "cpp internal error: base->type != T_ASSERT in do_assert");
6053 goto error;
6054 }
6055
6056 this = install (sym, thislen, T_ASSERT, 0,
6057 (char *)base->value.aschain, -1);
6058 base->value.aschain = this;
6059
6060 pfile->limit = sym; /* Pop */
6061 return 0;
6062
6063 error:
6064 pfile->limit = sym; /* Pop */
6065 skip_rest_of_line (pfile);
6066 return 1;
6067 }
6068
6069 static int
6070 do_unassert (pfile, keyword)
6071 cpp_reader *pfile;
6072 struct directive *keyword ATTRIBUTE_UNUSED;
6073 {
6074 int c, ret;
6075 char *sym;
6076 long baselen, thislen;
6077 HASHNODE *base, *this, *next;
6078
6079 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6080 && !CPP_BUFFER (pfile)->system_header_p)
6081 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6082
6083 cpp_skip_hspace (pfile);
6084
6085 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
6086 ret = parse_assertion (pfile);
6087 if (ret == 0)
6088 goto error;
6089
6090 cpp_skip_hspace (pfile);
6091 c = PEEKC ();
6092 if (c != EOF && c != '\n')
6093 cpp_error (pfile, "junk at end of `#unassert'");
6094
6095 thislen = strlen (sym);
6096 if (ret == 1)
6097 {
6098 base = cpp_lookup (pfile, sym, thislen, -1);
6099 if (! base)
6100 goto error; /* It isn't an error to #undef what isn't #defined,
6101 so it isn't an error to #unassert what isn't
6102 #asserted either. */
6103
6104 for (this = base->value.aschain; this; this = next)
6105 {
6106 next = this->value.aschain;
6107 delete_macro (this);
6108 }
6109 delete_macro (base);
6110 }
6111 else
6112 {
6113 baselen = index (sym, '(') - sym;
6114 base = cpp_lookup (pfile, sym, baselen, -1);
6115 if (! base) goto error;
6116 this = cpp_lookup (pfile, sym, thislen, -1);
6117 if (! this) goto error;
6118
6119 next = base;
6120 while (next->value.aschain != this)
6121 next = next->value.aschain;
6122
6123 next->value.aschain = this->value.aschain;
6124 delete_macro (this);
6125
6126 if (base->value.aschain == NULL)
6127 delete_macro (base); /* Last answer for this predicate deleted. */
6128 }
6129
6130 pfile->limit = sym; /* Pop */
6131 return 0;
6132 error:
6133 pfile->limit = sym; /* Pop */
6134 skip_rest_of_line (pfile);
6135 return 1;
6136 }
6137
6138 int
6139 cpp_read_check_assertion (pfile)
6140 cpp_reader *pfile;
6141 {
6142 char *name = CPP_PWRITTEN (pfile);
6143 int result;
6144 HASHNODE *hp;
6145
6146 FORWARD (1); /* Skip '#' */
6147 cpp_skip_hspace (pfile);
6148 if (! parse_assertion (pfile))
6149 result = 0;
6150 else
6151 {
6152 hp = cpp_lookup (pfile, name, (char *)CPP_PWRITTEN (pfile) - name, -1);
6153 result = (hp != 0);
6154 }
6155
6156 pfile->limit = name;
6157 return result;
6158 }
6159
6160 /* Initialize PMARK to remember the current position of PFILE. */
6161
6162 void
6163 parse_set_mark (pmark, pfile)
6164 struct parse_marker *pmark;
6165 cpp_reader *pfile;
6166 {
6167 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6168 pmark->next = pbuf->marks;
6169 pbuf->marks = pmark;
6170 pmark->buf = pbuf;
6171 pmark->position = pbuf->cur - pbuf->buf;
6172 }
6173
6174 /* Cleanup PMARK - we no longer need it. */
6175
6176 void
6177 parse_clear_mark (pmark)
6178 struct parse_marker *pmark;
6179 {
6180 struct parse_marker **pp = &pmark->buf->marks;
6181 for (; ; pp = &(*pp)->next) {
6182 if (*pp == NULL) abort ();
6183 if (*pp == pmark) break;
6184 }
6185 *pp = pmark->next;
6186 }
6187
6188 /* Backup the current position of PFILE to that saved in PMARK. */
6189
6190 void
6191 parse_goto_mark (pmark, pfile)
6192 struct parse_marker *pmark;
6193 cpp_reader *pfile;
6194 {
6195 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6196 if (pbuf != pmark->buf)
6197 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
6198 pbuf->cur = pbuf->buf + pmark->position;
6199 }
6200
6201 /* Reset PMARK to point to the current position of PFILE. (Same
6202 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
6203
6204 void
6205 parse_move_mark (pmark, pfile)
6206 struct parse_marker *pmark;
6207 cpp_reader *pfile;
6208 {
6209 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6210 if (pbuf != pmark->buf)
6211 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
6212 pmark->position = pbuf->cur - pbuf->buf;
6213 }
6214
6215 \f
6216 void
6217 cpp_print_file_and_line (pfile)
6218 cpp_reader *pfile;
6219 {
6220 cpp_buffer *ip = cpp_file_buffer (pfile);
6221
6222 if (ip != NULL)
6223 {
6224 long line, col;
6225 cpp_buf_line_and_col (ip, &line, &col);
6226 cpp_file_line_for_message (pfile, ip->nominal_fname,
6227 line, pfile->show_column ? col : -1);
6228 }
6229 }
6230
6231 static void
6232 v_cpp_error (pfile, msg, ap)
6233 cpp_reader *pfile;
6234 const char *msg;
6235 va_list ap;
6236 {
6237 cpp_print_containing_files (pfile);
6238 cpp_print_file_and_line (pfile);
6239 v_cpp_message (pfile, 1, msg, ap);
6240 }
6241
6242 void
6243 cpp_error VPROTO ((cpp_reader * pfile, const char *msg, ...))
6244 {
6245 #ifndef ANSI_PROTOTYPES
6246 cpp_reader *pfile;
6247 const char *msg;
6248 #endif
6249 va_list ap;
6250
6251 VA_START(ap, msg);
6252
6253 #ifndef ANSI_PROTOTYPES
6254 pfile = va_arg (ap, cpp_reader *);
6255 msg = va_arg (ap, const char *);
6256 #endif
6257
6258 v_cpp_error (pfile, msg, ap);
6259 va_end(ap);
6260 }
6261
6262 /* Print error message but don't count it. */
6263
6264 static void
6265 v_cpp_warning (pfile, msg, ap)
6266 cpp_reader *pfile;
6267 const char *msg;
6268 va_list ap;
6269 {
6270 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6271 return;
6272
6273 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6274 pfile->errors++;
6275
6276 cpp_print_containing_files (pfile);
6277 cpp_print_file_and_line (pfile);
6278 v_cpp_message (pfile, 0, msg, ap);
6279 }
6280
6281 void
6282 cpp_warning VPROTO ((cpp_reader * pfile, const char *msg, ...))
6283 {
6284 #ifndef ANSI_PROTOTYPES
6285 cpp_reader *pfile;
6286 const char *msg;
6287 #endif
6288 va_list ap;
6289
6290 VA_START (ap, msg);
6291
6292 #ifndef ANSI_PROTOTYPES
6293 pfile = va_arg (ap, cpp_reader *);
6294 msg = va_arg (ap, const char *);
6295 #endif
6296
6297 v_cpp_warning (pfile, msg, ap);
6298 va_end(ap);
6299 }
6300
6301 /* Print an error message and maybe count it. */
6302
6303 void
6304 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msg, ...))
6305 {
6306 #ifndef ANSI_PROTOTYPES
6307 cpp_reader *pfile;
6308 const char *msg;
6309 #endif
6310 va_list ap;
6311
6312 VA_START (ap, msg);
6313
6314 #ifndef ANSI_PROTOTYPES
6315 pfile = va_arg (ap, cpp_reader *);
6316 msg = va_arg (ap, const char *);
6317 #endif
6318
6319 if (CPP_OPTIONS (pfile)->pedantic_errors)
6320 v_cpp_error (pfile, msg, ap);
6321 else
6322 v_cpp_warning (pfile, msg, ap);
6323 va_end(ap);
6324 }
6325
6326 static void
6327 v_cpp_error_with_line (pfile, line, column, msg, ap)
6328 cpp_reader * pfile;
6329 int line;
6330 int column;
6331 const char * msg;
6332 va_list ap;
6333 {
6334 cpp_buffer *ip = cpp_file_buffer (pfile);
6335
6336 cpp_print_containing_files (pfile);
6337
6338 if (ip != NULL)
6339 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6340
6341 v_cpp_message (pfile, 1, msg, ap);
6342 }
6343
6344 void
6345 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6346 {
6347 #ifndef ANSI_PROTOTYPES
6348 cpp_reader *pfile;
6349 int line;
6350 int column;
6351 const char *msg;
6352 #endif
6353 va_list ap;
6354
6355 VA_START (ap, msg);
6356
6357 #ifndef ANSI_PROTOTYPES
6358 pfile = va_arg (ap, cpp_reader *);
6359 line = va_arg (ap, int);
6360 column = va_arg (ap, int);
6361 msg = va_arg (ap, const char *);
6362 #endif
6363
6364 v_cpp_error_with_line(pfile, line, column, msg, ap);
6365 va_end(ap);
6366 }
6367
6368 static void
6369 v_cpp_warning_with_line (pfile, line, column, msg, ap)
6370 cpp_reader * pfile;
6371 int line;
6372 int column;
6373 const char *msg;
6374 va_list ap;
6375 {
6376 cpp_buffer *ip;
6377
6378 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6379 return;
6380
6381 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6382 pfile->errors++;
6383
6384 cpp_print_containing_files (pfile);
6385
6386 ip = cpp_file_buffer (pfile);
6387
6388 if (ip != NULL)
6389 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6390
6391 v_cpp_message (pfile, 0, msg, ap);
6392 }
6393
6394 #if 0
6395 static void
6396 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6397 {
6398 #ifndef ANSI_PROTOTYPES
6399 cpp_reader *pfile;
6400 int line;
6401 int column;
6402 const char *msg;
6403 #endif
6404 va_list ap;
6405
6406 VA_START (ap, msg);
6407
6408 #ifndef ANSI_PROTOTYPES
6409 pfile = va_arg (ap, cpp_reader *);
6410 line = va_arg (ap, int);
6411 column = va_arg (ap, int);
6412 msg = va_arg (ap, const char *);
6413 #endif
6414
6415 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6416 va_end(ap);
6417 }
6418 #endif
6419
6420 void
6421 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6422 {
6423 #ifndef ANSI_PROTOTYPES
6424 cpp_reader *pfile;
6425 int line;
6426 int column;
6427 const char *msg;
6428 #endif
6429 va_list ap;
6430
6431 VA_START (ap, msg);
6432
6433 #ifndef ANSI_PROTOTYPES
6434 pfile = va_arg (ap, cpp_reader *);
6435 line = va_arg (ap, int);
6436 column = va_arg (ap, int);
6437 msg = va_arg (ap, const char *);
6438 #endif
6439
6440 if (CPP_OPTIONS (pfile)->pedantic_errors)
6441 v_cpp_error_with_line (pfile, column, line, msg, ap);
6442 else
6443 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6444 va_end(ap);
6445 }
6446
6447 /* Report a warning (or an error if pedantic_errors)
6448 giving specified file name and line number, not current. */
6449
6450 void
6451 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line, const char *msg, ...))
6452 {
6453 #ifndef ANSI_PROTOTYPES
6454 cpp_reader *pfile;
6455 char *file;
6456 int line;
6457 const char *msg;
6458 #endif
6459 va_list ap;
6460
6461 VA_START (ap, msg);
6462
6463 #ifndef ANSI_PROTOTYPES
6464 pfile = va_arg (ap, cpp_reader *);
6465 file = va_arg (ap, char *);
6466 line = va_arg (ap, int);
6467 msg = va_arg (ap, const char *);
6468 #endif
6469
6470 if (!CPP_OPTIONS (pfile)->pedantic_errors
6471 && CPP_OPTIONS (pfile)->inhibit_warnings)
6472 return;
6473 if (file != NULL)
6474 cpp_file_line_for_message (pfile, file, line, -1);
6475 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msg, ap);
6476 va_end(ap);
6477 }
6478
6479 /* my_strerror - return the descriptive text associated with an
6480 `errno' code. */
6481
6482 static char *
6483 my_strerror (errnum)
6484 int errnum;
6485 {
6486 char *result;
6487
6488 #ifndef VMS
6489 #ifndef HAVE_STRERROR
6490 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
6491 #else
6492 result = strerror (errnum);
6493 #endif
6494 #else /* VMS */
6495 /* VAXCRTL's strerror() takes an optional second argument, which only
6496 matters when the first argument is EVMSERR. However, it's simplest
6497 just to pass it unconditionally. `vaxc$errno' is declared in
6498 <errno.h>, and maintained by the library in parallel with `errno'.
6499 We assume that caller's `errnum' either matches the last setting of
6500 `errno' by the library or else does not have the value `EVMSERR'. */
6501
6502 result = strerror (errnum, vaxc$errno);
6503 #endif
6504
6505 if (!result)
6506 result = "undocumented I/O error";
6507
6508 return result;
6509 }
6510
6511 /* Error including a message from `errno'. */
6512
6513 void
6514 cpp_error_from_errno (pfile, name)
6515 cpp_reader *pfile;
6516 const char *name;
6517 {
6518 cpp_message_from_errno (pfile, 1, name);
6519 }
6520
6521 void
6522 cpp_message_from_errno (pfile, is_error, name)
6523 cpp_reader *pfile;
6524 int is_error;
6525 const char *name;
6526 {
6527 int e = errno;
6528 cpp_buffer *ip = cpp_file_buffer (pfile);
6529
6530 cpp_print_containing_files (pfile);
6531
6532 if (ip != NULL)
6533 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
6534
6535 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
6536 }
6537
6538 void
6539 cpp_perror_with_name (pfile, name)
6540 cpp_reader *pfile;
6541 const char *name;
6542 {
6543 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
6544 }
6545
6546 /* TODO:
6547 * No pre-compiled header file support.
6548 *
6549 * Possibly different enum token codes for each C/C++ token.
6550 *
6551 * Find and cleanup remaining uses of static variables,
6552 *
6553 * Support for trigraphs.
6554 *
6555 * Support -dM flag (dump_all_macros).
6556 *
6557 * Support for_lint flag.
6558 */