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