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