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