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