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