2 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
4 Based on CCCP program by 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
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 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
27 #include "..\..\sdcc_vc.h"
38 #include "../src/config.h"
47 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
48 for the sake of machines with limited C compilers. */
53 #endif /* not EMACS */
54 #define GCC_INCLUDE_DIR "/usr/local/lib/gcc-lib/i386-unknown-freebsd_1.0/2.5.8/include"
55 #ifndef STANDARD_INCLUDE_DIR
56 #define STANDARD_INCLUDE_DIR "/usr/include"
59 #ifndef LOCAL_INCLUDE_DIR
60 #define LOCAL_INCLUDE_DIR "/usr/local/include"
63 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
65 #define PTR_INT_TYPE ptrdiff_t
67 #define PTR_INT_TYPE long
78 /* By default, colon separates directories in a path. */
79 #ifndef PATH_SEPARATOR
80 #define PATH_SEPARATOR ':'
98 #if !defined(_MSC_VER)
99 #include <sys/time.h> /* for __DATE__ and __TIME__ */
100 #include <sys/resource.h>
102 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
103 // #include <sys/times.h>
112 /* This defines "errno" properly for VMS, and gives us EACCES. */
115 extern char *index ();
116 extern char *rindex ();
124 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
125 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
127 /* Find the largest host integer type and set its size and type. */
129 #ifndef HOST_BITS_PER_WIDE_INT
131 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
132 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
133 #define HOST_WIDE_INT long
135 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
136 #define HOST_WIDE_INT int
142 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
146 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
149 /* Define a generic NULL if one hasn't already been defined. */
156 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
157 #define GENERIC_PTR void *
159 #define GENERIC_PTR char *
164 #define NULL_PTR ((GENERIC_PTR)0)
167 #ifndef INCLUDE_LEN_FUDGE
168 #define INCLUDE_LEN_FUDGE 0
171 /* Symbols to predefine. */
173 #ifdef CPP_PREDEFINES
174 static char *predefs = CPP_PREDEFINES;
176 static char *predefs = "";
179 /* We let tm.h override the types used here, to handle trivial differences
180 such as the choice of unsigned int or long unsigned int for size_t.
181 When machines start needing nontrivial differences in the size type,
182 it would be best to do something here to figure out automatically
183 from other information what type to use. */
185 /* The string value for __SIZE_TYPE__. */
188 #define SIZE_TYPE "long unsigned int"
191 /* The string value for __PTRDIFF_TYPE__. */
194 #define PTRDIFF_TYPE "long int"
197 /* The string value for __WCHAR_TYPE__. */
200 #define WCHAR_TYPE "int"
202 #define CPP_WCHAR_TYPE(PFILE) \
203 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
205 /* The string value for __USER_LABEL_PREFIX__ */
207 #ifndef USER_LABEL_PREFIX
208 #define USER_LABEL_PREFIX ""
211 /* The string value for __REGISTER_PREFIX__ */
213 #ifndef REGISTER_PREFIX
214 #define REGISTER_PREFIX ""
218 /* In the definition of a #assert name, this structure forms
219 a list of the individual values asserted.
220 Each value is itself a list of "tokens".
221 These are strings that are compared by name. */
223 struct tokenlist_list {
224 struct tokenlist_list *next;
225 struct arglist *tokens;
228 struct assertion_hashnode {
229 struct assertion_hashnode *next; /* double links for easy deletion */
230 struct assertion_hashnode *prev;
231 /* also, a back pointer to this node's hash
232 chain is kept, in case the node is the head
233 of the chain and gets deleted. */
234 struct assertion_hashnode **bucket_hdr;
235 int length; /* length of token, for quick comparison */
236 U_CHAR *name; /* the actual name */
237 /* List of token-sequences. */
238 struct tokenlist_list *value;
241 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
242 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
244 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
245 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
246 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
247 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
248 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
249 (Note that it is false while we're expanding marco *arguments*.) */
250 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
252 /* Move all backslash-newline pairs out of embarrassing places.
253 Exchange all such pairs following BP
254 with any potentially-embarrassing characters that follow them.
255 Potentially-embarrassing characters are / and *
256 (because a backslash-newline inside a comment delimiter
257 would cause it not to be recognized). */
259 #define NEWLINE_FIX \
260 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
262 /* Same, but assume we've already read the potential '\\' into C. */
263 #define NEWLINE_FIX1(C) do { \
264 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
268 struct cpp_pending *next;
273 /* Forward declarations. */
275 //extern char *Safe_malloc ();
277 static void add_import ();
278 static void append_include_chain ();
279 static void make_undef ();
280 static void make_assertion ();
281 static void path_include ();
282 static void initialize_builtins ();
283 static void initialize_char_syntax ();
284 static void dump_arg_n ();
285 static void dump_defn_1 ();
286 extern void delete_macro ();
287 static void trigraph_pcp ();
288 static int finclude ();
289 static void validate_else ();
290 static int comp_def_part ();
291 extern void fancy_abort ();
292 static void pipe_closed ();
293 static void print_containing_files ();
294 static int lookup_import ();
295 static int redundant_include_p ();
296 static is_system_include ();
297 static struct file_name_map *read_name_map ();
298 static char *read_filename_string ();
299 static int open_include_file ();
300 static int check_preconditions ();
301 static void pcfinclude ();
302 static void pcstring_used ();
303 static int check_macro_name ();
304 static int compare_defs ();
305 static int compare_token_lists ();
306 static HOST_WIDE_INT eval_if_expression ();
307 static int change_newlines ();
309 static int file_size_and_mode ();
310 static struct arglist *read_token_list ();
311 static void free_token_list ();
312 static int safe_read ();
313 static void push_macro_expansion PARAMS ((cpp_reader *,
314 U_CHAR*, int, HASHNODE*));
315 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
316 //extern char *xrealloc ();
317 //extern char *xcalloc ();
318 static char *savestring ();
320 static void conditional_skip ();
321 static void skip_if_group ();
323 /* Last arg to output_line_command. */
324 enum file_change_code {same_file, enter_file, leave_file};
326 /* External declarations. */
328 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
330 extern char *getenv ();
331 extern FILE *fdopen ();
332 extern char *version_string;
333 extern struct tm *localtime ();
335 /* These functions are declared to return int instead of void since they
336 are going to be placed in a table and some old compilers have trouble with
337 pointers to functions returning void. */
339 static int do_define ();
340 static int do_line ();
341 static int do_include ();
342 static int do_undef ();
343 static int do_error ();
344 static int do_pragma ();
345 static int do_ident ();
347 static int do_xifdef ();
348 static int do_else ();
349 static int do_elif ();
350 static int do_endif ();
351 static int do_sccs ();
352 static int do_once ();
353 static int do_assert ();
354 static int do_unassert ();
355 static int do_warning ();
357 struct file_name_list
359 struct file_name_list *next;
361 /* If the following is nonzero, it is a macro name.
362 Don't include the file again if that macro is defined. */
363 U_CHAR *control_macro;
364 /* If the following is nonzero, it is a C-language system include
366 int c_system_include_path;
367 /* Mapping of file names for this directory. */
368 struct file_name_map *name_map;
369 /* Non-zero if name_map is valid. */
373 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
374 via the same directory as the file that #included it. */
375 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
377 /* #include "file" looks in source file dir, then stack. */
378 /* #include <file> just looks in the stack. */
379 /* -I directories are added to the end, then the defaults are added. */
381 static struct default_include {
382 char *fname; /* The name of the directory. */
383 int cplusplus; /* Only look here if we're compiling C++. */
384 int cxx_aware; /* Includes in this directory don't need to
385 be wrapped in extern "C" when compiling
387 } include_defaults_array[]
388 #ifdef INCLUDE_DEFAULTS
392 /* Pick up GNU C++ specific include files. */
393 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
395 /* This is the dir for fixincludes. Put it just before
396 the files that we fix. */
397 { GCC_INCLUDE_DIR, 0, 0 },
398 /* For cross-compilation, this dir name is generated
399 automatically in Makefile.in. */
400 { CROSS_INCLUDE_DIR, 0, 0 },
401 /* This is another place that the target system's headers might be. */
402 { TOOL_INCLUDE_DIR, 0, 1 },
403 { LOCAL_INCLUDE_DIR, 0, 1 },
404 #else /* not CROSS_COMPILE */
405 /* This should be /usr/local/include and should come before
406 the fixincludes-fixed header files. */
407 { LOCAL_INCLUDE_DIR, 0, 1 },
408 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
409 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
410 { TOOL_INCLUDE_DIR, 0, 1 },
411 /* This is the dir for fixincludes. Put it just before
412 the files that we fix. */
413 { GCC_INCLUDE_DIR, 0, 0 },
414 /* Some systems have an extra dir of include files. */
415 #ifdef SYSTEM_INCLUDE_DIR
416 { SYSTEM_INCLUDE_DIR, 0, 0 },
418 { STANDARD_INCLUDE_DIR, 0, 0 },
419 #endif /* not CROSS_COMPILE */
422 #endif /* no INCLUDE_DEFAULTS */
424 /* `struct directive' defines one #-directive, including how to handle it. */
427 int length; /* Length of name */
428 int (*func)(); /* Function to handle directive */
429 char *name; /* Name of directive */
430 enum node_type type; /* Code which describes which directive. */
431 char command_reads_line; /* One if rest of line is read by func. */
432 char traditional_comments; /* Nonzero: keep comments if -traditional. */
433 char pass_thru; /* Copy preprocessed directive to output file.*/
436 /* Here is the actual list of #-directives, most-often-used first.
437 The initialize_builtins function assumes #define is the very first. */
439 static struct directive directive_table[] = {
440 { 6, do_define, "define", T_DEFINE, 0, 1},
441 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
442 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
443 { 7, do_include, "include", T_INCLUDE, 1},
444 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
445 { 6, do_include, "import", T_IMPORT, 1},
446 { 5, do_endif, "endif", T_ENDIF, 1},
447 { 4, do_else, "else", T_ELSE, 1},
448 { 2, do_if, "if", T_IF, 1},
449 { 4, do_elif, "elif", T_ELIF, 1},
450 { 5, do_undef, "undef", T_UNDEF},
451 { 5, do_error, "error", T_ERROR},
452 { 7, do_warning, "warning", T_WARNING},
453 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
454 { 4, do_line, "line", T_LINE, 1},
455 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
456 #ifdef SCCS_DIRECTIVE
457 { 4, do_sccs, "sccs", T_SCCS},
459 { 6, do_assert, "assert", T_ASSERT, 1},
460 { 8, do_unassert, "unassert", T_UNASSERT, 1},
461 { -1, 0, "", T_UNUSED},
464 /* table to tell if char can be part of a C identifier. */
465 U_CHAR is_idchar[256];
466 /* table to tell if char can be first char of a c identifier. */
467 U_CHAR is_idstart[256];
468 /* table to tell if c is horizontal space. */
469 U_CHAR is_hor_space[256];
470 /* table to tell if c is horizontal or vertical space. */
471 static U_CHAR is_space[256];
473 /* Initialize syntactic classifications of characters. */
476 initialize_char_syntax (
477 struct cpp_options *opts)
482 * Set up is_idchar and is_idstart tables. These should be
483 * faster than saying (is_alpha (c) || c == '_'), etc.
484 * Set up these things before calling any routines tthat
487 for (i = 'a'; i <= 'z'; i++) {
488 is_idchar[i - 'a' + 'A'] = 1;
490 is_idstart[i - 'a' + 'A'] = 1;
493 for (i = '0'; i <= '9'; i++)
497 is_idchar['$'] = opts->dollars_in_ident;
498 is_idstart['$'] = opts->dollars_in_ident;
500 /* horizontal space table */
501 is_hor_space[' '] = 1;
502 is_hor_space['\t'] = 1;
503 is_hor_space['\v'] = 1;
504 is_hor_space['\f'] = 1;
505 is_hor_space['\r'] = 1;
516 /* Place into PFILE a quoted string representing the string SRC.
517 Caller must reserve enough space in pfile->token_buffer. */
525 CPP_PUTC_Q (pfile, '\"');
527 switch ((c = *src++))
531 CPP_PUTC_Q (pfile, c);
534 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
535 CPP_ADJUST_WRITTEN (pfile, 4);
541 CPP_PUTC_Q (pfile, '\\');
542 CPP_PUTC_Q (pfile, c);
546 CPP_PUTC_Q (pfile, '\"');
547 CPP_NUL_TERMINATE_Q (pfile);
552 /* Make sure PFILE->token_buffer will hold at least N more chars. */
559 long old_written = CPP_WRITTEN (pfile);
560 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
561 pfile->token_buffer = (U_CHAR*) Safe_realloc(pfile->token_buffer, pfile->token_buffer_size);
562 CPP_SET_WRITTEN (pfile, old_written);
567 * process a given definition string, for initialization
568 * If STR is just an identifier, define it with value 1.
569 * If STR has anything after the identifier, then it should
570 * be identifier=definition.
584 cpp_error (pfile, "malformed option `-D %s'", str);
587 while (is_idchar[*++p])
591 buf = (U_CHAR *) alloca (p - buf + 4);
592 strcpy ((char *)buf, str);
593 strcat ((char *)buf, " 1");
597 cpp_error (pfile, "malformed option `-D %s'", str);
603 /* Copy the entire option so we can modify it. */
604 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
605 strncpy (buf, str, p - str);
606 /* Change the = to a space. */
608 /* Scan for any backslash-newline and remove it. */
613 if (*p == '\\' && p[1] == '\n')
621 do_define (pfile, NULL, buf, buf + strlen (buf));
624 /* Process the string STR as if it appeared as the body of a #assert.
625 OPTION is the option name for which STR was the argument. */
634 struct directive *kt;
637 /* Copy the entire option so we can modify it. */
638 buf = (U_CHAR *) alloca (strlen (str) + 1);
639 strcpy ((char *) buf, str);
640 /* Scan for any backslash-newline and remove it. */
644 if (*p == '\\' && p[1] == '\n')
653 if (!is_idstart[*p]) {
654 cpp_error (pfile, "malformed option `%s %s'", option, str);
657 while (is_idchar[*++p])
659 while (*p == ' ' || *p == '\t') p++;
660 if (! (*p == 0 || *p == '(')) {
661 cpp_error (pfile, "malformed option `%s %s'", option, str);
665 ip = cpp_push_buffer (pfile, buf, strlen (buf));
666 do_assert (pfile, NULL, NULL, NULL);
667 cpp_pop_buffer (pfile);
670 /* Append a chain of `struct file_name_list's
671 to the end of the main include chain.
672 FIRST is the beginning of the chain to append, and LAST is the end. */
675 append_include_chain (
677 struct file_name_list *first,struct file_name_list *last)
679 struct cpp_options *opts = CPP_OPTIONS (pfile);
680 struct file_name_list *dir;
685 if (opts->include == 0)
686 opts->include = first;
688 opts->last_include->next = first;
690 if (opts->first_bracket_include == 0)
691 opts->first_bracket_include = first;
693 for (dir = first; ; dir = dir->next) {
694 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
695 if (len > pfile->max_include_len)
696 pfile->max_include_len = len;
702 opts->last_include = last;
705 /* Add output to `deps_buffer' for the -M switch.
706 STRING points to the text to be output.
707 SPACER is ':' for targets, ' ' for dependencies, zero for text
708 to be inserted literally. */
716 int size = strlen (string);
721 #ifndef MAX_OUTPUT_COLUMNS
722 #define MAX_OUTPUT_COLUMNS 72
725 && pfile->deps_column > 0
726 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
728 deps_output (pfile, " \\\n ", 0);
729 pfile->deps_column = 0;
732 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
734 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
735 pfile->deps_buffer = (char *) Safe_realloc(pfile->deps_buffer,
736 pfile->deps_allocated_size);
738 if (spacer == ' ' && pfile->deps_column > 0)
739 pfile->deps_buffer[pfile->deps_size++] = ' ';
740 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
741 pfile->deps_size += size;
742 pfile->deps_column += size;
744 pfile->deps_buffer[pfile->deps_size++] = ':';
745 pfile->deps_buffer[pfile->deps_size] = 0;
748 /* Given a colon-separated list of file names PATH,
749 add all the names to the search path for include files. */
764 struct file_name_list *dirtmp;
766 /* Find the end of this name. */
767 while (*q != 0 && *q != PATH_SEPARATOR) q++;
769 /* An empty name in the path stands for the current directory. */
770 name = (char *) Safe_malloc (2);
774 /* Otherwise use the directory that is named. */
775 name = (char *) Safe_malloc (q - p + 1);
776 bcopy (p, name, q - p);
780 dirtmp = (struct file_name_list *)
781 Safe_malloc (sizeof (struct file_name_list));
782 dirtmp->next = 0; /* New one goes on the end */
783 dirtmp->control_macro = 0;
784 dirtmp->c_system_include_path = 0;
785 dirtmp->fname = name;
786 dirtmp->got_name_map = 0;
787 append_include_chain (pfile, dirtmp, dirtmp);
789 /* Advance past this name. */
793 /* Skip the colon. */
800 struct cpp_options *opts)
802 bzero ((char *) opts, sizeof *opts);
803 opts->in_fname = NULL;
804 opts->out_fname = NULL;
806 /* Initialize is_idchar to allow $. */
807 opts->dollars_in_ident = 1;
808 initialize_char_syntax (opts);
809 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
811 opts->no_line_commands = 0;
812 opts->no_trigraphs = 1;
813 opts->put_out_comments = 0;
814 opts->print_include_names = 0;
815 opts->dump_macros = dump_none;
818 opts->cplusplus_comments = 0;
825 opts->pedantic_errors = 0;
826 opts->inhibit_warnings = 0;
827 opts->warn_comments = 0;
828 opts->warn_import = 1;
829 opts->warnings_are_errors = 0;
852 HASHNODE *macro = (HASHNODE*)pbuf->data;
853 if (macro->type == T_DISABLED)
854 macro->type = T_MACRO;
855 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
880 register U_CHAR *p = bp;
882 /* First count the backslash-newline pairs here. */
884 while (p[0] == '\\' && p[1] == '\n')
887 /* What follows the backslash-newlines is not embarrassing. */
889 if (*p != '/' && *p != '*')
892 /* Copy all potentially embarrassing characters
893 that follow the backslash-newline pairs
894 down to where the pairs originally started. */
896 while (*p == '*' || *p == '/')
899 /* Now write the same number of pairs after the embarrassing chars. */
907 /* Assuming we have read '/'.
908 If this is the start of a comment (followed by '*' or '/'),
909 skip to the end of the comment, and return ' '.
910 Return EOF if we reached the end of file before the end of the comment.
911 If not the start of a comment, return '/'. */
919 while (PEEKC() == '\\' && PEEKN(1) == '\n')
934 while (c == '\\' && PEEKC() == '\n')
938 FORWARD(1), c = GETC();
940 if (prev_c == '*' && c == '/')
942 if (c == '\n' && linep)
946 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
953 return ' '; /* Allow // to be terminated by EOF. */
954 while (c == '\\' && PEEKC() == '\n')
963 /* Don't consider final '\n' to be part of comment. */
973 /* Skip whitespace \-newline and comments. Does not macro-expand. */
985 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
986 cpp_pedwarn (pfile, "%s in preprocessing directive",
987 c == '\f' ? "formfeed" : "vertical tab");
993 c = skip_comment (pfile, NULL);
996 if (c == EOF || c == '/')
999 else if (c == '\\' && PEEKN(1) == '\n') {
1002 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
1003 && is_hor_space[PEEKN(1)])
1009 /* Read the rest of the current line.
1010 The line is appended to PFILE's output buffer. */
1016 struct cpp_options *opts = CPP_OPTIONS (pfile);
1026 if (PEEKC() == '\n')
1033 goto scan_directive_token;
1037 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
1038 goto scan_directive_token;
1042 if (CPP_PEDANTIC (pfile))
1043 cpp_pedwarn (pfile, "%s in preprocessing directive",
1044 c == '\f' ? "formfeed" : "vertical tab");
1050 scan_directive_token:
1052 cpp_get_token (pfile);
1055 CPP_PUTC (pfile, c);
1058 CPP_NUL_TERMINATE (pfile);
1065 long old = CPP_WRITTEN (pfile);
1066 copy_rest_of_line (pfile);
1067 CPP_SET_WRITTEN (pfile, old);
1070 /* Handle a possible # directive.
1071 '#' has already been read. */
1077 register struct directive *kt;
1080 U_CHAR *ident, *line_end;
1081 long old_written = CPP_WRITTEN (pfile);
1083 cpp_skip_hspace (pfile);
1086 if (c >= '0' && c <= '9')
1088 /* Handle # followed by a line number. */
1089 if (CPP_PEDANTIC (pfile))
1090 cpp_pedwarn (pfile, "`#' followed by integer");
1091 do_line (pfile, NULL);
1092 goto done_a_directive;
1095 /* Now find the directive name. */
1096 CPP_PUTC (pfile, '#');
1097 parse_name (pfile, GETC());
1098 ident = pfile->token_buffer + old_written + 1;
1099 ident_length = CPP_PWRITTEN (pfile) - ident;
1100 if (ident_length == 0 && PEEKC() == '\n')
1102 /* A line of just `#' becomes blank. */
1103 goto done_a_directive;
1107 if (ident_length == 0 || !is_idstart[*ident]) {
1109 while (is_idchar[*p]) {
1110 if (*p < '0' || *p > '9')
1114 /* Avoid error for `###' and similar cases unless -pedantic. */
1116 while (*p == '#' || is_hor_space[*p]) p++;
1118 if (pedantic && !lang_asm)
1119 cpp_warning (pfile, "invalid preprocessor directive");
1125 cpp_error (pfile, "invalid preprocessor directive name");
1131 * Decode the keyword and call the appropriate expansion
1132 * routine, after moving the input pointer up to the next line.
1134 for (kt = directive_table; ; kt++) {
1135 if (kt->length <= 0)
1136 goto not_a_directive;
1137 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1141 if (! kt->command_reads_line)
1143 /* Nonzero means do not delete comments within the directive.
1144 #define needs this when -traditional. */
1145 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1146 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1147 CPP_OPTIONS (pfile)->put_out_comments = comments;
1148 after_ident = CPP_WRITTEN (pfile);
1149 copy_rest_of_line (pfile);
1150 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1153 /* For #pragma and #define, we may want to pass through the directive.
1154 Other directives may create output, but we don't want the directive
1155 itself out, so we pop it now. For example #include may write a
1156 command (see comment in do_include), and conditionals may emit
1157 #failed ... #endfailed stuff. But note that popping the buffer
1158 means the parameters to kt->func may point after pfile->limit
1159 so these parameters are invalid as soon as something gets appended
1160 to the token_buffer. */
1162 line_end = CPP_PWRITTEN (pfile);
1163 if (!kt->pass_thru && kt->type != T_DEFINE)
1164 CPP_SET_WRITTEN (pfile, old_written);
1166 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1168 || (kt->type == T_DEFINE
1169 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1171 /* Just leave the entire #define in the output stack. */
1173 else if (kt->type == T_DEFINE
1174 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1176 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1177 SKIP_WHITE_SPACE (p);
1178 while (is_idchar[*p]) p++;
1180 CPP_PUTC (pfile, '\n');
1182 else if (kt->type == T_DEFINE)
1183 CPP_SET_WRITTEN (pfile, old_written);
1191 /* Pass a directive through to the output file.
1192 BUF points to the contents of the directive, as a contiguous string.
1193 LIMIT points to the first character past the end of the directive.
1194 KEYWORD is the keyword-table entry for the directive. */
1197 pass_thru_directive (
1198 U_CHAR *buf, U_CHAR *limit,
1200 struct directive *keyword)
1202 register unsigned keyword_length = keyword->length;
1204 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1205 CPP_PUTC_Q (pfile, '#');
1206 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1207 if (limit != buf && buf[0] != ' ')
1208 CPP_PUTC_Q (pfile, ' ');
1209 CPP_PUTS_Q (pfile, buf, limit - buf);
1211 CPP_PUTS_Q (pfile, '\n');
1212 /* Count the line we have just made in the output,
1213 to get in sync properly. */
1218 /* The arglist structure is built by do_define to tell
1219 collect_definition where the argument names begin. That
1220 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1221 would contain pointers to the strings x, y, and z.
1222 Collect_definition would then build a DEFINITION node,
1223 with reflist nodes pointing to the places x, y, and z had
1224 appeared. So the arglist is just convenience data passed
1225 between these two routines. It is not kept around after
1226 the current #define has been processed and entered into the
1230 struct arglist *next;
1237 /* Read a replacement list for a macro with parameters.
1238 Build the DEFINITION structure.
1239 Reads characters of text starting at BUF until END.
1240 ARGLIST specifies the formal parameters to look for
1241 in the text of the definition; NARGS is the number of args
1242 in that list, or -1 for a macro name that wants no argument list.
1243 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1244 and NAMELEN is its length in characters.
1246 Note that comments, backslash-newlines, and leading white space
1247 have already been deleted from the argument. */
1252 U_CHAR *buf, U_CHAR *limit,
1254 struct arglist *arglist)
1257 register U_CHAR *p, *lastp, *exp_p;
1258 struct reflist *endpat = NULL;
1259 /* Pointer to first nonspace after last ## seen. */
1261 /* Pointer to first nonspace after last single-# seen. */
1262 U_CHAR *stringify = 0;
1264 int expected_delimiter = '\0';
1266 /* Scan thru the replacement list, ignoring comments and quoted
1267 strings, picking up on the macro calls. It does a linear search
1268 thru the arg list on every potential symbol. Profiling might say
1269 that something smarter should happen. */
1274 /* Find the beginning of the trailing whitespace. */
1276 while (p < limit && is_space[limit[-1]]) limit--;
1278 /* Allocate space for the text in the macro definition.
1279 Leading and trailing whitespace chars need 2 bytes each.
1280 Each other input char may or may not need 1 byte,
1281 so this is an upper bound. The extra 5 are for invented
1282 leading and trailing newline-marker and final null. */
1283 maxsize = (sizeof (DEFINITION)
1285 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1289 defn = (DEFINITION *) Safe_calloc(maxsize);
1291 defn->nargs = nargs;
1292 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1297 /* Add one initial space escape-marker to prevent accidental
1298 token-pasting (often removed by macroexpand). */
1302 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1303 cpp_error (pfile, "`##' at start of macro definition");
1307 /* Process the main body of the definition. */
1309 int skipped_arg = 0;
1310 register U_CHAR c = *p++;
1314 if (!CPP_TRADITIONAL (pfile)) {
1318 if (expected_delimiter != '\0') {
1319 if (c == expected_delimiter)
1320 expected_delimiter = '\0';
1322 expected_delimiter = c;
1326 if (p < limit && expected_delimiter) {
1327 /* In a string, backslash goes through
1328 and makes next char ordinary. */
1334 /* An '@' in a string or character constant stands for itself,
1335 and does not need to be escaped. */
1336 if (!expected_delimiter)
1341 /* # is ordinary inside a string. */
1342 if (expected_delimiter)
1344 if (p < limit && *p == '#') {
1345 /* ##: concatenate preceding and following tokens. */
1346 /* Take out the first #, discard preceding whitespace. */
1348 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1350 /* Skip the second #. */
1352 /* Discard following whitespace. */
1353 SKIP_WHITE_SPACE (p);
1356 cpp_error (pfile, "`##' at end of macro definition");
1357 } else if (nargs >= 0) {
1358 /* Single #: stringify following argument ref.
1359 Don't leave the # in the expansion. */
1361 SKIP_WHITE_SPACE (p);
1362 if (p == limit || ! is_idstart[*p])
1364 "`#' operator is not followed by a macro argument name");
1371 /* In -traditional mode, recognize arguments inside strings and
1372 and character constants, and ignore special properties of #.
1373 Arguments inside strings are considered "stringified", but no
1374 extra quote marks are supplied. */
1378 if (expected_delimiter != '\0') {
1379 if (c == expected_delimiter)
1380 expected_delimiter = '\0';
1382 expected_delimiter = c;
1386 /* Backslash quotes delimiters and itself, but not macro args. */
1387 if (expected_delimiter != 0 && p < limit
1388 && (*p == expected_delimiter || *p == '\\')) {
1395 if (expected_delimiter != '\0') /* No comments inside strings. */
1398 /* If we find a comment that wasn't removed by handle_directive,
1399 this must be -traditional. So replace the comment with
1403 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1406 /* Mark this as a concatenation-point, as if it had been ##. */
1410 else if (*p == '/') {
1411 /* A c++ comment. Discard to the end of line */
1419 /* Handle the start of a symbol. */
1420 if (is_idchar[c] && nargs > 0) {
1421 U_CHAR *id_beg = p - 1;
1425 while (p != limit && is_idchar[*p]) p++;
1426 id_len = p - id_beg;
1428 if (is_idstart[c]) {
1429 register struct arglist *arg;
1431 for (arg = arglist; arg != NULL; arg = arg->next) {
1432 struct reflist *tpat;
1434 if (arg->name[0] == c
1435 && arg->length == id_len
1436 && strncmp (arg->name, id_beg, id_len) == 0) {
1437 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1438 if (CPP_TRADITIONAL (pfile)) {
1439 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1443 "macro arg `%.*s' would be stringified with -traditional.",
1447 /* If ANSI, don't actually substitute inside a string. */
1448 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1450 /* make a pat node for this arg and append it to the end of
1452 tpat = (struct reflist *) Safe_malloc (sizeof (struct reflist));
1454 tpat->raw_before = concat == id_beg;
1455 tpat->raw_after = 0;
1456 tpat->rest_args = arg->rest_args;
1457 tpat->stringify = (CPP_TRADITIONAL (pfile)
1458 ? expected_delimiter != '\0'
1459 : stringify == id_beg);
1462 defn->pattern = tpat;
1464 endpat->next = tpat;
1467 tpat->argno = arg->argno;
1468 tpat->nchars = exp_p - lastp;
1470 register U_CHAR *p1 = p;
1471 SKIP_WHITE_SPACE (p1);
1472 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1473 tpat->raw_after = 1;
1475 lastp = exp_p; /* place to start copying from next time */
1482 /* If this was not a macro arg, copy it into the expansion. */
1483 if (! skipped_arg) {
1484 register U_CHAR *lim1 = p;
1488 if (stringify == id_beg)
1490 "`#' operator should be followed by a macro argument name");
1495 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1497 /* If ANSI, put in a "@ " marker to prevent token pasting.
1498 But not if "inside a string" (which in ANSI mode
1499 happens only for -D option). */
1506 defn->length = exp_p - defn->expansion;
1508 /* Crash now if we overrun the allocated size. */
1509 if (defn->length + 1 > maxsize)
1513 /* This isn't worth the time it takes. */
1514 /* give back excess storage */
1515 defn->expansion = (U_CHAR *) Safe_realloc(defn->expansion, defn->length + 1);
1522 * special extension string that can be added to the last macro argument to
1523 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1524 * #define wow(a, b...) process (b, a, b)
1525 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1526 * { wow (one, two); } -> { process (two, one, two); }
1527 * if this "rest_arg" is used with the concat token '##' and if it is not
1528 * supplied then the token attached to with ## will not be outputted. Ex:
1529 * #define wow (a, b...) process (b ## , a, ## b)
1530 * { wow (1, 2); } -> { process (2, 1, 2); }
1531 * { wow (one); } -> { process (one); {
1533 static char rest_extension[] = "...";
1534 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1536 /* Create a DEFINITION node from a #define directive. Arguments are
1537 as for do_define. */
1540 U_CHAR *buf, U_CHAR *limit,
1544 U_CHAR *bp; /* temp ptr into input buffer */
1545 U_CHAR *symname; /* remember where symbol name starts */
1546 int sym_length; /* and how long it is */
1549 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1551 int arglengths = 0; /* Accumulate lengths of arg names
1552 plus number of args. */
1554 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1558 while (is_hor_space[*bp])
1561 symname = bp; /* remember where it starts */
1563 sym_length = check_macro_name (pfile, bp, "macro");
1566 /* Lossage will occur if identifiers or control keywords are broken
1567 across lines using backslash. This is not the right place to take
1571 struct arglist *arg_ptrs = NULL;
1574 bp++; /* skip '(' */
1575 SKIP_WHITE_SPACE (bp);
1577 /* Loop over macro argument names. */
1578 while (*bp != ')') {
1579 struct arglist *temp;
1581 temp = (struct arglist *) alloca (sizeof (struct arglist));
1583 temp->next = arg_ptrs;
1584 temp->argno = argno++;
1585 temp->rest_args = 0;
1589 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1591 if (!is_idstart[*bp])
1592 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1594 /* Find the end of the arg name. */
1595 while (is_idchar[*bp]) {
1597 /* do we have a "special" rest-args extension here? */
1598 if (limit - bp > REST_EXTENSION_LENGTH &&
1599 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1601 temp->rest_args = 1;
1605 temp->length = bp - temp->name;
1607 bp += REST_EXTENSION_LENGTH;
1608 arglengths += temp->length + 2;
1609 SKIP_WHITE_SPACE (bp);
1610 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1611 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1616 SKIP_WHITE_SPACE (bp);
1619 cpp_error (pfile, "unterminated parameter list in `#define'");
1623 struct arglist *otemp;
1625 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1626 if (temp->length == otemp->length &&
1627 strncmp (temp->name, otemp->name, temp->length) == 0) {
1630 name = (U_CHAR *) alloca (temp->length + 1);
1631 (void) strncpy (name, temp->name, temp->length);
1632 name[temp->length] = '\0';
1634 "duplicate argument name `%s' in `#define'", name);
1640 ++bp; /* skip paren */
1641 SKIP_WHITE_SPACE (bp);
1642 /* now everything from bp before limit is the definition. */
1643 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1644 defn->rest_args = rest_args;
1646 /* Now set defn->args.argnames to the result of concatenating
1647 the argument names in reverse order
1648 with comma-space between them. */
1649 defn->args.argnames = (U_CHAR *) Safe_malloc (arglengths + 1);
1651 struct arglist *temp;
1653 for (temp = arg_ptrs; temp; temp = temp->next) {
1654 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1656 if (temp->next != 0) {
1657 defn->args.argnames[i++] = ',';
1658 defn->args.argnames[i++] = ' ';
1661 defn->args.argnames[i] = 0;
1664 /* Simple expansion or empty definition. */
1668 if (is_hor_space[*bp]) {
1670 SKIP_WHITE_SPACE (bp);
1673 case '!': case '"': case '#': case '%': case '&': case '\'':
1674 case ')': case '*': case '+': case ',': case '-': case '.':
1675 case '/': case ':': case ';': case '<': case '=': case '>':
1676 case '?': case '[': case '\\': case ']': case '^': case '{':
1677 case '|': case '}': case '~':
1678 cpp_warning (pfile, "missing white space after `#define %.*s'",
1679 sym_length, symname);
1683 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1684 sym_length, symname);
1689 /* now everything from bp before limit is the definition. */
1690 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1691 defn->args.argnames = (U_CHAR *) "";
1697 /* OP is null if this is a predefinition */
1698 defn->predefined = predefinition;
1700 mdef.symnam = symname;
1701 mdef.symlen = sym_length;
1710 /* Check a purported macro name SYMNAME, and yield its length.
1711 USAGE is the kind of name this is intended for. */
1722 for (p = symname; is_idchar[*p]; p++)
1724 sym_length = p - symname;
1725 if (sym_length == 0)
1726 cpp_error (pfile, "invalid %s name", usage);
1727 else if (!is_idstart[*symname]) {
1728 U_CHAR *msg; /* what pain... */
1729 msg = (U_CHAR *) alloca (sym_length + 1);
1730 bcopy (symname, msg, sym_length);
1731 msg[sym_length] = 0;
1732 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1734 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1735 cpp_error (pfile, "invalid %s name `defined'", usage);
1741 * return zero if two DEFINITIONs are isomorphic
1745 DEFINITION *d1, DEFINITION *d2)
1747 register struct reflist *a1, *a2;
1748 register U_CHAR *p1 = d1->expansion;
1749 register U_CHAR *p2 = d2->expansion;
1752 if (d1->nargs != d2->nargs)
1754 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1756 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1757 a1 = a1->next, a2 = a2->next) {
1758 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1759 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1760 || a1->argno != a2->argno
1761 || a1->stringify != a2->stringify
1762 || a1->raw_before != a2->raw_before
1763 || a1->raw_after != a2->raw_after)
1771 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1772 p2, d2->length - (p2 - d2->expansion), 1))
1777 /* Return 1 if two parts of two macro definitions are effectively different.
1778 One of the parts starts at BEG1 and has LEN1 chars;
1779 the other has LEN2 chars at BEG2.
1780 Any sequence of whitespace matches any other sequence of whitespace.
1781 FIRST means these parts are the first of a macro definition;
1782 so ignore leading whitespace entirely.
1783 LAST means these parts are the last of a macro definition;
1784 so ignore trailing whitespace entirely. */
1789 U_CHAR *beg1, int len1,
1790 U_CHAR *beg2, int len2 ,
1793 register U_CHAR *end1 = beg1 + len1;
1794 register U_CHAR *end2 = beg2 + len2;
1796 while (beg1 != end1 && is_space[*beg1]) beg1++;
1797 while (beg2 != end2 && is_space[*beg2]) beg2++;
1800 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1801 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1803 while (beg1 != end1 && beg2 != end2) {
1804 if (is_space[*beg1] && is_space[*beg2]) {
1805 while (beg1 != end1 && is_space[*beg1]) beg1++;
1806 while (beg2 != end2 && is_space[*beg2]) beg2++;
1807 } else if (*beg1 == *beg2) {
1811 return (beg1 != end1) || (beg2 != end2);
1814 /* Process a #define command.
1815 BUF points to the contents of the #define command, as a contiguous string.
1816 LIMIT points to the first character past the end of the definition.
1817 KEYWORD is the keyword-table entry for #define,
1818 or NULL for a "predefined" macro. */
1823 struct directive *keyword,
1824 U_CHAR *buf, U_CHAR *limit)
1831 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1832 if (pcp_outfile && keyword)
1833 pass_thru_directive (buf, limit, pfile, keyword);
1836 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1840 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1842 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1845 /* Redefining a precompiled key is ok. */
1846 if (hp->type == T_PCSTRING)
1848 /* Redefining a macro is ok if the definitions are the same. */
1849 else if (hp->type == T_MACRO)
1850 ok = ! compare_defs (mdef.defn, hp->value.defn);
1851 /* Redefining a constant is ok with -D. */
1852 else if (hp->type == T_CONST)
1853 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1854 /* Print the warning if it's not ok. */
1857 U_CHAR *msg; /* what pain... */
1859 /* If we are passing through #define and #undef directives, do
1860 that for this re-definition now. */
1861 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1862 pass_thru_directive (buf, limit, pfile, keyword);
1864 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1866 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1867 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1868 cpp_pedwarn (pfile, msg);
1869 if (hp->type == T_MACRO)
1870 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1871 "this is the location of the previous definition");
1873 /* Replace the old definition. */
1875 hp->value.defn = mdef.defn;
1879 /* If we are passing through #define and #undef directives, do
1880 that for this new definition now. */
1881 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1882 pass_thru_directive (buf, limit, pfile, keyword);
1883 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1884 (char *) mdef.defn, hashcode);
1894 /* This structure represents one parsed argument in a macro call.
1895 `raw' points to the argument text as written (`raw_length' is its length).
1896 `expanded' points to the argument's macro-expansion
1897 (its length is `expand_length').
1898 `stringified_length' is the length the argument would have
1900 `use_count' is the number of times this macro arg is substituted
1901 into the macro. If the actual use count exceeds 10,
1902 the value stored is 10. */
1904 /* raw and expanded are relative to ARG_BASE */
1905 #define ARG_BASE ((pfile)->token_buffer)
1908 /* Strings relative to pfile->token_buffer */
1909 long raw, expanded, stringified;
1910 int raw_length, expand_length;
1911 int stringified_length;
1923 #ifdef STATIC_BUFFERS
1924 register cpp_buffer *buf = CPP_BUFFER (pfile);
1925 if (buf == pfile->buffer_stack)
1926 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1928 bzero ((char *) buf, sizeof (cpp_buffer));
1929 CPP_BUFFER (pfile) = buf;
1931 register cpp_buffer *buf = (cpp_buffer*) Safe_malloc (sizeof(cpp_buffer));
1932 bzero ((char *) buf, sizeof (cpp_buffer));
1933 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1934 CPP_BUFFER (pfile) = buf;
1936 buf->if_stack = pfile->if_stack;
1937 buf->cleanup = null_cleanup;
1938 buf->underflow = null_underflow;
1939 buf->buf = buf->cur = buffer;
1940 buf->alimit = buf->rlimit = buffer + length;
1946 cpp_pop_buffer (pfile)
1949 cpp_buffer *buf = CPP_BUFFER (pfile);
1950 #ifdef STATIC_BUFFERS
1951 (*buf->cleanup) (buf, pfile);
1952 return ++CPP_BUFFER (pfile);
1954 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1955 (*buf->cleanup) (buf, pfile);
1956 CPP_BUFFER (pfile) = next_buf;
1962 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1963 Pop the buffer when done. */
1969 cpp_buffer *buffer = CPP_BUFFER (pfile);
1972 enum cpp_token token = cpp_get_token (pfile);
1973 if (token == CPP_EOF) /* Should not happen ... */
1975 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1977 cpp_pop_buffer (pfile);
1984 * Rescan a string (which may have escape marks) into pfile's buffer.
1985 * Place the result in pfile->token_buffer.
1987 * The input is copied before it is scanned, so it is safe to pass
1988 * it something from the token_buffer that will get overwritten
1989 * (because it follows CPP_WRITTEN). This is used by do_include.
1993 cpp_expand_to_buffer (
1998 register cpp_buffer *ip;
2000 U_CHAR *limit = buf + length;
2003 int odepth = indepth;
2009 /* Set up the input on the input stack. */
2011 buf1 = (U_CHAR *) alloca (length + 1);
2013 register U_CHAR *p1 = buf;
2014 register U_CHAR *p2 = buf1;
2021 ip = cpp_push_buffer (pfile, buf1, length);
2022 ip->has_escapes = 1;
2024 ip->lineno = obuf.lineno = 1;
2027 /* Scan the input, create the output. */
2028 cpp_scan_buffer (pfile);
2031 if (indepth != odepth)
2035 CPP_NUL_TERMINATE (pfile);
2050 (*linep)++, (*colp) = 1;
2056 /* Move line_base forward, updating lineno and colno. */
2060 register cpp_buffer *pbuf)
2062 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2063 unsigned char *new_pos = pbuf->cur;
2064 register struct parse_marker *mark;
2065 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2067 if (pbuf->buf + mark->position < new_pos)
2068 new_pos = pbuf->buf + mark->position;
2070 pbuf->line_base += new_pos - old_pos;
2071 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2075 cpp_buf_line_and_col (
2076 register cpp_buffer *pbuf,
2077 long *linep,long *colp)
2084 *linep = pbuf->lineno;
2085 *colp = pbuf->colno;
2086 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2095 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2101 cpp_buffer *ip = CPP_BUFFER (pfile);
2103 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2104 if (ip->fname != NULL)
2111 register U_CHAR *buf,
2112 register U_CHAR *limit)
2114 register long count = 0;
2125 * write out a #line command, for instance, after an #include file.
2126 * If CONDITIONAL is nonzero, we can omit the #line if it would
2127 * appear to be a no-op, and we can output a few newlines instead
2128 * if we want to increase the line number by a small amount.
2129 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2133 output_line_command (
2136 enum file_change_code file_change)
2139 char *line_cmd_buf, *line_end;
2141 cpp_buffer *ip = CPP_BUFFER (pfile);
2143 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2147 update_position (ip);
2148 line = CPP_BUFFER (pfile)->lineno;
2149 col = CPP_BUFFER (pfile)->colno;
2150 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2152 if (CPP_OPTIONS (pfile)->no_line_commands)
2156 if (line == pfile->lineno)
2159 /* If the inherited line number is a little too small,
2160 output some newlines instead of a #line command. */
2161 if (line > pfile->lineno && line < pfile->lineno + 8) {
2162 CPP_RESERVE (pfile, 20);
2163 while (line > pfile->lineno) {
2164 CPP_PUTC_Q (pfile, '\n');
2172 /* Don't output a line number of 0 if we can help it. */
2173 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2174 && *ip->bufp == '\n') {
2180 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2182 static char sharp_line[] = "#line ";
2184 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2187 sprintf (CPP_PWRITTEN (pfile), "%d ", line+2);
2188 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2190 // modification for SDC51
2191 if (*ip->nominal_fname == '\0')
2192 quote_string (pfile,"standard input");
2194 quote_string (pfile, ip->nominal_fname);
2195 if (file_change != same_file) {
2196 CPP_PUTC_Q (pfile, ' ');
2197 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2199 /* Tell cc1 if following text comes from a system header file. */
2200 if (ip->system_header_p) {
2201 CPP_PUTC_Q (pfile, ' ');
2202 CPP_PUTC_Q (pfile, '3');
2204 #ifndef NO_IMPLICIT_EXTERN_C
2205 /* Tell cc1plus if following text should be treated as C. */
2206 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2207 CPP_PUTC_Q (pfile, ' ');
2208 CPP_PUTC_Q (pfile, '4');
2211 CPP_PUTC_Q (pfile, '\n');
2212 pfile->lineno = line;
2216 * Parse a macro argument and append the info on PFILE's token_buffer.
2217 * REST_ARGS means to absorb the rest of the args.
2218 * Return nonzero to indicate a syntax error.
2221 static enum cpp_token
2227 enum cpp_token token;
2228 long arg_start = CPP_WRITTEN (pfile);
2229 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2230 CPP_OPTIONS (pfile)->put_out_comments = 0;
2232 /* Try to parse as much of the argument as exists at this
2233 input stack level. */
2234 pfile->no_macro_expand++;
2237 token = cpp_get_token (pfile);
2243 /* If we've hit end of file, it's an error (reported by caller).
2244 Ditto if it's the end of cpp_expand_to_buffer text.
2245 If we've hit end of macro, just continue. */
2246 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2257 /* if we've returned to lowest level and
2258 we aren't absorbing all args */
2259 if (paren == 0 && rest_args == 0)
2263 /* Remove ',' or ')' from argument buffer. */
2264 CPP_ADJUST_WRITTEN (pfile, -1);
2271 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2272 pfile->no_macro_expand--;
2277 /* Turn newlines to spaces in the string of length LENGTH at START,
2278 except inside of string constants.
2279 The string is copied into itself with its beginning staying fixed. */
2286 register U_CHAR *ibp;
2287 register U_CHAR *obp;
2288 register U_CHAR *limit;
2292 limit = start + length;
2295 while (ibp < limit) {
2296 *obp++ = c = *ibp++;
2301 /* Notice and skip strings, so that we don't delete newlines in them. */
2304 while (ibp < limit) {
2305 *obp++ = c = *ibp++;
2308 if (c == '\n' && quotec == '\'')
2324 if (!pfile->timebuf) {
2325 time_t t = time ((time_t *)0);
2326 pfile->timebuf = localtime (&t);
2328 return pfile->timebuf;
2331 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2332 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2336 * expand things like __FILE__. Place the expansion into the output
2337 * buffer *without* rescanning.
2348 cpp_buffer *ip = NULL;
2351 int paren = 0; /* For special `defined' keyword */
2354 if (pcp_outfile && pcp_inside_if
2355 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2357 "Predefined macro `%s' used inside `#if' during precompilation",
2361 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2365 cpp_error (pfile, "cccp error: not in any file?!");
2366 return; /* the show must go on */
2368 if (ip->fname != NULL)
2378 if (hp->type == T_BASE_FILE)
2380 while (CPP_PREV_BUFFER (ip))
2381 ip = CPP_PREV_BUFFER (ip);
2383 string = ip->nominal_fname;
2387 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2388 quote_string (pfile, string);
2392 case T_INCLUDE_LEVEL:
2394 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2395 if (ip->fname != NULL)
2398 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2399 sprintf (buf, "%d", true_indepth - 1);
2403 buf = (char *) alloca (3 + strlen (version_string));
2404 sprintf (buf, "\"%s\"", version_string);
2407 #ifndef NO_BUILTIN_SIZE_TYPE
2413 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2414 case T_PTRDIFF_TYPE:
2420 buf = CPP_WCHAR_TYPE (pfile);
2423 case T_USER_LABEL_PREFIX_TYPE:
2424 buf = USER_LABEL_PREFIX;
2427 case T_REGISTER_PREFIX_TYPE:
2428 buf = REGISTER_PREFIX;
2432 buf = (char *) alloca (4 * sizeof (int));
2433 sprintf (buf, "%d", hp->value.ival);
2435 if (pcp_inside_if && pcp_outfile)
2436 /* Output a precondition for this macro use */
2437 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2443 long line = ip->lineno;
2444 long col = ip->colno;
2445 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2447 buf = (char *) alloca (10);
2448 sprintf (buf, "%d", line);
2454 buf = (char *) alloca (20);
2455 timebuf = timestamp (pfile);
2456 if (hp->type == T_DATE)
2457 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2458 timebuf->tm_mday, timebuf->tm_year + 1900);
2460 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2464 case T_SPEC_DEFINED:
2465 buf = " 0 "; /* Assume symbol is not defined */
2466 ip = CPP_BUFFER (pfile);
2467 SKIP_WHITE_SPACE (ip->cur);
2468 if (*ip->cur == '(')
2471 ip->cur++; /* Skip over the paren */
2472 SKIP_WHITE_SPACE (ip->cur);
2475 if (!is_idstart[*ip->cur])
2477 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2480 if (pcp_outfile && pcp_inside_if
2481 && (hp->type == T_CONST
2482 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2483 /* Output a precondition for this macro use. */
2484 fprintf (pcp_outfile, "#define %s\n", hp->name);
2490 if (pcp_outfile && pcp_inside_if)
2492 /* Output a precondition for this macro use */
2493 U_CHAR *cp = ip->bufp;
2494 fprintf (pcp_outfile, "#undef ");
2495 while (is_idchar[*cp]) /* Ick! */
2496 fputc (*cp++, pcp_outfile);
2497 putc ('\n', pcp_outfile);
2500 while (is_idchar[*ip->cur])
2502 SKIP_WHITE_SPACE (ip->cur);
2505 if (*ip->cur != ')')
2513 cpp_error (pfile, "`defined' without an identifier");
2517 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2521 CPP_RESERVE (pfile, len + 1);
2522 CPP_PUTS_Q (pfile, buf, len);
2523 CPP_NUL_TERMINATE_Q (pfile);
2528 /* Initialize the built-in macros. */
2531 initialize_builtins (
2534 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2535 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2536 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2537 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2538 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2539 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2540 #ifndef NO_BUILTIN_SIZE_TYPE
2541 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2543 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2544 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2546 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2547 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2548 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2549 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2550 if (!CPP_TRADITIONAL (pfile))
2551 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2552 if (CPP_OPTIONS (pfile)->objc)
2553 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2554 /* This is supplied using a -D by the compiler driver
2555 so that it is present only when truly compiling with GNU C. */
2556 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2558 if (CPP_OPTIONS (pfile)->debug_output)
2560 char directive[2048];
2561 register struct directive *dp = &directive_table[0];
2562 struct tm *timebuf = timestamp (pfile);
2563 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2565 while (CPP_PREV_BUFFER (pbuffer))
2566 pbuffer = CPP_PREV_BUFFER (pbuffer);
2567 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2568 pbuffer->nominal_fname);
2569 output_line_command (pfile, 0, same_file);
2570 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2572 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2573 output_line_command (pfile, 0, same_file);
2574 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2576 #ifndef NO_BUILTIN_SIZE_TYPE
2577 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2578 output_line_command (pfile, 0, same_file);
2579 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2582 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2583 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2584 output_line_command (pfile, 0, same_file);
2585 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2588 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2589 output_line_command (pfile, 0, same_file);
2590 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2592 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2593 monthnames[timebuf->tm_mon],
2594 timebuf->tm_mday, timebuf->tm_year + 1900);
2595 output_line_command (pfile, 0, same_file);
2596 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2598 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2599 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2600 output_line_command (pfile, 0, same_file);
2601 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2603 if (!CPP_TRADITIONAL (pfile))
2605 sprintf (directive, " __STDC__ 1");
2606 output_line_command (pfile, 0, same_file);
2607 pass_thru_directive (directive, &directive[strlen (directive)],
2610 if (CPP_OPTIONS (pfile)->objc)
2612 sprintf (directive, " __OBJC__ 1");
2613 output_line_command (pfile, 0, same_file);
2614 pass_thru_directive (directive, &directive[strlen (directive)],
2620 /* Return 1 iff a token ending in C1 followed directly by a token C2
2621 could cause mis-tokenization. */
2630 if (c2 == c1 || c2 == '=')
2634 case '0': case '1': case '2': case '3': case '4':
2635 case '5': case '6': case '7': case '8': case '9':
2637 if (c2 == '-' || c2 == '+')
2638 return 1; /* could extend a pre-processing number */
2641 if (c2 == '\'' || c2 == '\"')
2642 return 1; /* Could turn into L"xxx" or L'xxx'. */
2646 case 'a': case 'b': case 'c': case 'd': case 'f':
2647 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2648 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2649 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2651 case 'A': case 'B': case 'C': case 'D': case 'F':
2652 case 'G': case 'H': case 'I': case 'J': case 'K':
2653 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2654 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2656 /* We're in the middle of either a name or a pre-processing number. */
2657 return (is_idchar[c2] || c2 == '.');
2658 case '<': case '>': case '!': case '%': case '#': case ':':
2659 case '^': case '&': case '|': case '*': case '/': case '=':
2660 return (c2 == c1 || c2 == '=');
2665 /* Expand a macro call.
2666 HP points to the symbol that is the macro being called.
2667 Put the result of expansion onto the input stack
2668 so that subsequent input by our caller will use it.
2670 If macro wants arguments, caller has already verified that
2671 an argument list follows; arguments come from the input stack. */
2679 DEFINITION *defn = hp->value.defn;
2680 register U_CHAR *xbuf;
2681 long start_line, start_column;
2683 struct argdata *args;
2684 long old_written = CPP_WRITTEN (pfile);
2686 int start_line = instack[indepth].lineno;
2688 int rest_args, rest_zero;
2692 CHECK_DEPTH (return;);
2696 /* This macro is being used inside a #if, which means it must be */
2697 /* recorded as a precondition. */
2698 if (pcp_inside_if && pcp_outfile && defn->predefined)
2699 dump_single_macro (hp, pcp_outfile);
2702 pfile->output_escapes++;
2703 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2705 nargs = defn->nargs;
2709 enum cpp_token token;
2711 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2713 for (i = 0; i < nargs; i++)
2715 args[i].raw = args[i].expanded = 0;
2716 args[i].raw_length = 0;
2717 args[i].expand_length = args[i].stringified_length = -1;
2718 args[i].use_count = 0;
2721 /* Parse all the macro args that are supplied. I counts them.
2722 The first NARGS args are stored in ARGS.
2723 The rest are discarded. If rest_args is set then we assume
2724 macarg absorbed the rest of the args. */
2728 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2733 if (i < nargs || (nargs == 0 && i == 0))
2735 /* if we are working on last arg which absorbs rest of args... */
2736 if (i == nargs - 1 && defn->rest_args)
2738 args[i].raw = CPP_WRITTEN (pfile);
2739 token = macarg (pfile, rest_args);
2740 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2741 args[i].newlines = 0; /* FIXME */
2744 token = macarg (pfile, 0);
2745 if (token == CPP_EOF || token == CPP_POP)
2747 cpp_error_with_line (pfile, start_line, start_column,
2748 "unterminated macro call");
2752 } while (token == CPP_COMMA);
2754 /* If we got one arg but it was just whitespace, call that 0 args. */
2757 register U_CHAR *bp = ARG_BASE + args[0].raw;
2758 register U_CHAR *lim = bp + args[0].raw_length;
2759 /* cpp.texi says for foo ( ) we provide one argument.
2760 However, if foo wants just 0 arguments, treat this as 0. */
2762 while (bp != lim && is_space[*bp]) bp++;
2767 /* Don't output an error message if we have already output one for
2768 a parse error above. */
2770 if (nargs == 0 && i > 0)
2772 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2776 /* traditional C allows foo() if foo wants one argument. */
2777 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2779 /* the rest args token is allowed to absorb 0 tokens */
2780 else if (i == nargs - 1 && defn->rest_args)
2783 cpp_error (pfile, "macro `%s' used without args", hp->name);
2785 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2787 cpp_error (pfile, "macro `%s' used with only %d args",
2793 "macro `%s' used with too many (%d) args", hp->name, i);
2797 /* If macro wants zero args, we parsed the arglist for checking only.
2798 Read directly from the macro definition. */
2801 xbuf = defn->expansion;
2802 xbuf_len = defn->length;
2806 register U_CHAR *exp = defn->expansion;
2807 register int offset; /* offset in expansion,
2808 copied a piece at a time */
2809 register int totlen; /* total amount of exp buffer filled so far */
2811 register struct reflist *ap, *last_ap;
2813 /* Macro really takes args. Compute the expansion of this call. */
2815 /* Compute length in characters of the macro's expansion.
2816 Also count number of times each arg is used. */
2817 xbuf_len = defn->length;
2818 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2822 register struct argdata *arg = &args[ap->argno];
2823 /* Stringify it it hasn't already been */
2824 if (arg->stringified_length < 0)
2826 int arglen = arg->raw_length;
2830 /* Initially need_space is -1. Otherwise, 1 means the
2831 previous character was a space, but we suppressed it;
2832 0 means the previous character was a non-space. */
2833 int need_space = -1;
2835 arg->stringified = CPP_WRITTEN (pfile);
2836 if (!CPP_TRADITIONAL (pfile))
2837 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2838 for (; i < arglen; i++)
2840 c = (ARG_BASE + arg->raw)[i];
2844 /* Internal sequences of whitespace are replaced by
2845 one space except within an string or char token.*/
2848 if (CPP_WRITTEN (pfile) > arg->stringified
2849 && (CPP_PWRITTEN (pfile))[-1] == '@')
2851 /* "@ " escape markers are removed */
2852 CPP_ADJUST_WRITTEN (pfile, -1);
2855 if (need_space == 0)
2859 else if (need_space > 0)
2860 CPP_PUTC (pfile, ' ');
2875 else if (c == '\"' || c == '\'')
2879 /* Escape these chars */
2880 if (c == '\"' || (in_string && c == '\\'))
2881 CPP_PUTC (pfile, '\\');
2883 CPP_PUTC (pfile, c);
2886 CPP_RESERVE (pfile, 4);
2887 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2889 CPP_ADJUST_WRITTEN (pfile, 4);
2892 if (!CPP_TRADITIONAL (pfile))
2893 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2894 arg->stringified_length
2895 = CPP_WRITTEN (pfile) - arg->stringified;
2897 xbuf_len += args[ap->argno].stringified_length;
2899 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2900 /* Add 4 for two newline-space markers to prevent
2901 token concatenation. */
2902 xbuf_len += args[ap->argno].raw_length + 4;
2905 /* We have an ordinary (expanded) occurrence of the arg.
2906 So compute its expansion, if we have not already. */
2907 if (args[ap->argno].expand_length < 0)
2909 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2910 cpp_expand_to_buffer (pfile,
2911 ARG_BASE + args[ap->argno].raw,
2912 args[ap->argno].raw_length);
2914 args[ap->argno].expand_length
2915 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2918 /* Add 4 for two newline-space markers to prevent
2919 token concatenation. */
2920 xbuf_len += args[ap->argno].expand_length + 4;
2922 if (args[ap->argno].use_count < 10)
2923 args[ap->argno].use_count++;
2926 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
2928 /* Generate in XBUF the complete expansion
2929 with arguments substituted in.
2930 TOTLEN is the total size generated so far.
2931 OFFSET is the index in the definition
2932 of where we are copying from. */
2933 offset = totlen = 0;
2934 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2935 last_ap = ap, ap = ap->next)
2937 register struct argdata *arg = &args[ap->argno];
2938 int count_before = totlen;
2940 /* Add chars to XBUF. */
2941 for (i = 0; i < ap->nchars; i++, offset++)
2942 xbuf[totlen++] = exp[offset];
2944 /* If followed by an empty rest arg with concatenation,
2945 delete the last run of nonwhite chars. */
2946 if (rest_zero && totlen > count_before
2947 && ((ap->rest_args && ap->raw_before)
2948 || (last_ap != NULL && last_ap->rest_args
2949 && last_ap->raw_after)))
2951 /* Delete final whitespace. */
2952 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2955 /* Delete the nonwhites before them. */
2956 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2960 if (ap->stringify != 0)
2962 bcopy (ARG_BASE + arg->stringified,
2963 xbuf + totlen, arg->stringified_length);
2964 totlen += arg->stringified_length;
2966 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2968 U_CHAR *p1 = ARG_BASE + arg->raw;
2969 U_CHAR *l1 = p1 + arg->raw_length;
2972 while (p1 != l1 && is_space[*p1]) p1++;
2973 while (p1 != l1 && is_idchar[*p1])
2974 xbuf[totlen++] = *p1++;
2978 /* Arg is concatenated after: delete trailing whitespace,
2979 whitespace markers, and no-reexpansion markers. */
2982 if (is_space[l1[-1]]) l1--;
2983 else if (l1[-1] == '-')
2985 U_CHAR *p2 = l1 - 1;
2986 /* If a `-' is preceded by an odd number of newlines then it
2987 and the last newline are a no-reexpansion marker. */
2988 while (p2 != p1 && p2[-1] == '\n') p2--;
2989 if ((l1 - 1 - p2) & 1) {
2998 bcopy (p1, xbuf + totlen, l1 - p1);
3003 U_CHAR *expanded = ARG_BASE + arg->expanded;
3004 if (!ap->raw_before && totlen > 0 && arg->expand_length
3005 && !CPP_TRADITIONAL(pfile)
3006 && unsafe_chars (xbuf[totlen-1], expanded[0]))
3008 xbuf[totlen++] = '@';
3009 xbuf[totlen++] = ' ';
3012 bcopy (expanded, xbuf + totlen, arg->expand_length);
3013 totlen += arg->expand_length;
3015 if (!ap->raw_after && totlen > 0 && offset < defn->length
3016 && !CPP_TRADITIONAL(pfile)
3017 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3019 xbuf[totlen++] = '@';
3020 xbuf[totlen++] = ' ';
3023 /* If a macro argument with newlines is used multiple times,
3024 then only expand the newlines once. This avoids creating
3025 output lines which don't correspond to any input line,
3026 which confuses gdb and gcov. */
3027 if (arg->use_count > 1 && arg->newlines > 0)
3029 /* Don't bother doing change_newlines for subsequent
3033 = change_newlines (expanded, arg->expand_length);
3037 if (totlen > xbuf_len)
3041 /* if there is anything left of the definition
3042 after handling the arg list, copy that in too. */
3044 for (i = offset; i < defn->length; i++)
3046 /* if we've reached the end of the macro */
3049 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3050 && last_ap->raw_after))
3051 xbuf[totlen++] = exp[i];
3059 pfile->output_escapes--;
3061 /* Now put the expansion on the input stack
3062 so our caller will commence reading from it. */
3063 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3064 CPP_BUFFER (pfile)->has_escapes = 1;
3066 /* Pop the space we've used in the token_buffer for argument expansion. */
3067 CPP_SET_WRITTEN (pfile, old_written);
3069 /* Recursive macro use sometimes works traditionally.
3070 #define foo(x,y) bar (x (y,0), y)
3073 if (!CPP_TRADITIONAL (pfile))
3074 hp->type = T_DISABLED;
3078 push_macro_expansion (
3080 register U_CHAR *xbuf,
3084 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3085 mbuf->cleanup = macro_cleanup;
3088 /* The first chars of the expansion should be a "@ " added by
3089 collect_expansion. This is to prevent accidental token-pasting
3090 between the text preceding the macro invocation, and the macro
3093 We would like to avoid adding unneeded spaces (for the sake of
3094 tools that use cpp, such as imake). In some common cases we can
3095 tell that it is safe to omit the space.
3097 The character before the macro invocation cannot have been an
3098 idchar (or else it would have been pasted with the idchars of
3099 the macro name). Therefore, if the first non-space character
3100 of the expansion is an idchar, we do not need the extra space
3101 to prevent token pasting.
3103 Also, we don't need the extra space if the first char is '(',
3104 or some other (less common) characters. */
3106 if (xbuf[0] == '@' && xbuf[1] == ' '
3107 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3108 || xbuf[2] == '\"'))
3112 /* Like cpp_get_token, except that it does not read past end-of-line.
3113 Also, horizontal space is skipped, and macros are popped. */
3115 static enum cpp_token
3116 get_directive_token (
3121 long old_written = CPP_WRITTEN (pfile);
3122 enum cpp_token token;
3123 cpp_skip_hspace (pfile);
3124 if (PEEKC () == '\n')
3126 token = cpp_get_token (pfile);
3130 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3132 /* ... else fall though ... */
3133 case CPP_HSPACE: case CPP_COMMENT:
3134 CPP_SET_WRITTEN (pfile, old_written);
3142 /* Handle #include and #import.
3143 This function expects to see "fname" or <fname> on the input.
3145 The input is normally in part of the output_buffer following
3146 CPP_WRITTEN, and will get overwritten by output_line_command.
3147 I.e. in input file specification has been popped by handle_directive.
3153 struct directive *keyword,
3154 U_CHAR *unused1, U_CHAR *unused2)
3156 int importing = (keyword->type == T_IMPORT);
3157 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3158 char *fname; /* Dynamically allocated fname buffer */
3161 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3162 enum cpp_token token;
3164 /* Chain of dirs to search */
3165 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3166 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3167 struct file_name_list *searchptr = 0;
3168 long old_written = CPP_WRITTEN (pfile);
3172 int f; /* file number */
3174 int retried = 0; /* Have already tried macro
3175 expanding the include line*/
3176 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3181 f= -1; /* JF we iz paranoid! */
3183 if (importing && CPP_OPTIONS (pfile)->warn_import
3184 && !CPP_OPTIONS (pfile)->inhibit_warnings
3185 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3187 pfile->import_warning = 1;
3188 cpp_warning (pfile, "using `#import' is not recommended");
3189 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3190 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3191 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3192 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3193 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3194 fprintf (stderr, " ... <real contents of file> ...\n");
3195 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3196 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3197 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3198 fprintf (stderr, "when it is equipped with such a conditional.\n");
3201 pfile->parsing_include_directive++;
3202 token = get_directive_token (pfile);
3203 pfile->parsing_include_directive--;
3205 if (token == CPP_STRING)
3207 /* FIXME - check no trailing garbage */
3208 fbeg = pfile->token_buffer + old_written + 1;
3209 fend = CPP_PWRITTEN (pfile) - 1;
3210 if (fbeg[-1] == '<')
3213 /* If -I-, start with the first -I dir after the -I-. */
3214 if (CPP_OPTIONS (pfile)->first_bracket_include)
3215 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3217 /* If -I- was specified, don't search current dir, only spec'd ones. */
3218 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3221 /* We have "filename". Figure out directory this source
3222 file is coming from and put it on the front of the list. */
3224 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3229 if ((nam = fp->nominal_fname) != NULL)
3231 /* Found a named file. Figure out dir of the file,
3232 and put it in front of the search list. */
3233 dsp[0].next = search_start;
3236 ep = rindex (nam, '/');
3238 ep = rindex (nam, ']');
3239 if (ep == NULL) ep = rindex (nam, '>');
3240 if (ep == NULL) ep = rindex (nam, ':');
3241 if (ep != NULL) ep++;
3246 dsp[0].fname = (char *) alloca (n + 1);
3247 strncpy (dsp[0].fname, nam, n);
3248 dsp[0].fname[n] = '\0';
3249 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3250 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3254 dsp[0].fname = 0; /* Current directory */
3256 dsp[0].got_name_map = 0;
3263 else if (token == CPP_NAME)
3266 * Support '#include xyz' like VAX-C to allow for easy use of all the
3267 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3268 * code from case '<' is repeated here) and generates a warning.
3271 "VAX-C-style include specification found, use '#include <filename.h>' !");
3273 /* If -I-, start with the first -I dir after the -I-. */
3274 if (CPP_OPTIONS (pfile)->first_bracket_include)
3275 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3276 fbeg = pfile->token_buffer + old_written;
3277 fend = CPP_PWRITTEN (pfile);
3283 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3284 CPP_SET_WRITTEN (pfile, old_written);
3285 skip_rest_of_line (pfile);
3291 token = get_directive_token (pfile);
3292 if (token != CPP_VSPACE)
3294 cpp_error (pfile, "junk at end of `#include'");
3295 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3296 token = get_directive_token (pfile);
3299 /* For #include_next, skip in the search path
3300 past the dir in which the containing file was found. */
3304 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3305 if (fp->fname != NULL)
3307 /* fp->dir is null if the containing file was specified with
3308 an absolute file name. In that case, don't skip anything. */
3309 if (fp->dir == SELF_DIR_DUMMY)
3310 search_start = CPP_OPTIONS (pfile)->include;
3312 search_start = fp->dir->next;
3317 CPP_SET_WRITTEN (pfile, old_written);
3323 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3327 /* Allocate this permanently, because it gets stored in the definitions
3329 fname = (char *) Safe_malloc (pfile->max_include_len + flen + 4);
3330 /* + 2 above for slash and terminating null. */
3331 /* + 2 added for '.h' on VMS (to support '#include filename') */
3333 /* If specified file name is absolute, just open it. */
3336 strncpy (fname, fbeg, flen);
3338 if (redundant_include_p (pfile, fname))
3341 f = lookup_import (pfile, fname, NULL_PTR);
3343 f = open_include_file (pfile, fname, NULL_PTR);
3345 return 0; /* Already included this file */
3347 /* Search directory path, trying to open the file.
3348 Copy each filename tried into FNAME. */
3350 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3351 if (searchptr->fname) {
3352 /* The empty string in a search path is ignored.
3353 This makes it possible to turn off entirely
3354 a standard piece of the list. */
3355 if (searchptr->fname[0] == 0)
3357 strcpy (fname, searchptr->fname);
3358 strcat (fname, "/");
3359 fname[strlen (fname) + flen] = 0;
3363 strncat (fname, fbeg, flen);
3365 /* Change this 1/2 Unix 1/2 VMS file specification into a
3366 full VMS file specification */
3367 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3368 /* Fix up the filename */
3369 hack_vms_include_specification (fname);
3371 /* This is a normal VMS filespec, so use it unchanged. */
3372 strncpy (fname, fbeg, flen);
3374 /* if it's '#include filename', add the missing .h */
3375 if (index(fname,'.')==NULL) {
3376 strcat (fname, ".h");
3380 /* ??? There are currently 3 separate mechanisms for avoiding processing
3381 of redundant include files: #import, #pragma once, and
3382 redundant_include_p. It would be nice if they were unified. */
3383 if (redundant_include_p (pfile, fname))
3386 f = lookup_import (pfile, fname, searchptr);
3388 f = open_include_file (pfile, fname, searchptr);
3390 return 0; /* Already included this file */
3392 else if (f == -1 && errno == EACCES)
3393 cpp_warning (pfile, "Header file %s exists, but is not readable",
3403 /* A file that was not found. */
3404 strncpy (fname, fbeg, flen);
3406 /* If generating dependencies and -MG was specified, we assume missing
3407 files are leaf files, living in the same directory as the source file
3408 or other similar place; these missing files may be generated from
3409 other files and may not exist yet (eg: y.tab.h). */
3411 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3412 && CPP_PRINT_DEPS (pfile)
3413 > (angle_brackets || (pfile->system_include_depth > 0)))
3415 /* If it was requested as a system header file,
3416 then assume it belongs in the first place to look for such. */
3419 for (searchptr = search_start; searchptr;
3420 searchptr = searchptr->next)
3422 if (searchptr->fname)
3426 if (searchptr->fname[0] == 0)
3428 p = (char *) alloca (strlen (searchptr->fname)
3429 + strlen (fname) + 2);
3430 strcpy (p, searchptr->fname);
3433 deps_output (pfile, p, ' ');
3440 /* Otherwise, omit the directory, as if the file existed
3441 in the directory with the source. */
3442 deps_output (pfile, fname, ' ');
3445 /* If -M was specified, and this header file won't be added to the
3446 dependency list, then don't count this as an error, because we can
3447 still produce correct output. Otherwise, we can't produce correct
3448 output, because there may be dependencies we need inside the missing
3449 file, and we don't know what directory this missing file exists in.*/
3450 else if (CPP_PRINT_DEPS (pfile)
3451 && (CPP_PRINT_DEPS (pfile)
3452 <= (angle_brackets || (pfile->system_include_depth > 0))))
3453 cpp_warning (pfile, "No include path in which to find %s", fname);
3454 else if (search_start)
3455 cpp_error_from_errno (pfile, fname);
3457 cpp_error (pfile, "No include path in which to find %s", fname);
3460 /* Check to see if this include file is a once-only include file.
3463 struct file_name_list* ptr;
3465 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3466 if (!strcmp (ptr->fname, fname)) {
3468 return 0; /* This file was once'd. */
3472 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3473 if (!strcmp (ptr->fname, fname))
3474 break; /* This file was included before. */
3478 /* This is the first time for this file. */
3479 /* Add it to list of files included. */
3481 ptr = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3482 ptr->control_macro = 0;
3483 ptr->c_system_include_path = 0;
3484 ptr->next = pfile->all_include_files;
3485 pfile->all_include_files = ptr;
3486 ptr->fname = savestring (fname);
3487 ptr->got_name_map = 0;
3489 /* For -M, add this file to the dependencies. */
3490 if (CPP_PRINT_DEPS (pfile)
3491 > (angle_brackets || (pfile->system_include_depth > 0)))
3492 deps_output (pfile, fname, ' ');
3495 /* Handle -H option. */
3496 if (CPP_OPTIONS(pfile)->print_include_names)
3498 cpp_buffer *buf = CPP_BUFFER (pfile);
3499 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3501 fprintf (stderr, "%s\n", fname);
3505 pfile->system_include_depth++;
3507 /* Actually process the file. */
3509 /* Record file on "seen" list for #import. */
3510 add_import (pfile, f, fname);
3512 pcftry = (char *) alloca (strlen (fname) + 30);
3524 sprintf (pcftry, "%s%d", fname, pcfnum++);
3526 pcf = open (pcftry, O_RDONLY, 0666);
3532 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3534 || stat_f.st_dev != s.st_dev)
3536 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3537 /* Don't need it any more. */
3542 /* Don't need it at all. */
3547 } while (pcf != -1 && !pcfbuf);
3551 /* Actually process the file */
3552 cpp_push_buffer (pfile, NULL, 0);
3553 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3554 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3556 output_line_command (pfile, 0, enter_file);
3557 pfile->only_seen_white = 2;
3561 pfile->system_include_depth--;
3566 /* Return nonzero if there is no need to include file NAME
3567 because it has already been included and it contains a conditional
3568 to make a repeated include do nothing. */
3571 redundant_include_p (
3575 struct file_name_list *l = pfile->all_include_files;
3576 for (; l; l = l->next)
3577 if (! strcmp (name, l->fname)
3579 && cpp_lookup (pfile, l->control_macro, -1, -1))
3584 /* Return nonzero if the given FILENAME is an absolute pathname which
3585 designates a file within one of the known "system" include file
3586 directories. We assume here that if the given FILENAME looks like
3587 it is the name of a file which resides either directly in a "system"
3588 include file directory, or within any subdirectory thereof, then the
3589 given file must be a "system" include file. This function tells us
3590 if we should suppress pedantic errors/warnings for the given FILENAME.
3592 The value is 2 if the file is a C-language system header file
3593 for which C++ should (on most systems) assume `extern "C"'. */
3598 register char *filename)
3600 struct file_name_list *searchptr;
3602 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3603 searchptr = searchptr->next)
3604 if (searchptr->fname) {
3605 register char *sys_dir = searchptr->fname;
3606 register unsigned length = strlen (sys_dir);
3608 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3610 if (searchptr->c_system_include_path)
3621 * Install a name in the assertion hash table.
3623 * If LEN is >= 0, it is the length of the name.
3624 * Otherwise, compute the length by scanning the entire name.
3626 * If HASH is >= 0, it is the precomputed hash code.
3627 * Otherwise, compute the hash code.
3629 static ASSERTION_HASHNODE *
3636 register ASSERTION_HASHNODE *hp;
3637 register int i, bucket;
3638 register U_CHAR *p, *q;
3640 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3641 hp = (ASSERTION_HASHNODE *) Safe_malloc (i);
3643 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3644 hp->next = pfile->assertion_hashtab[bucket];
3645 pfile->assertion_hashtab[bucket] = hp;
3647 if (hp->next != NULL)
3648 hp->next->prev = hp;
3651 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3654 for (i = 0; i < len; i++)
3660 * find the most recent hash node for name name (ending with first
3661 * non-identifier char) installed by install
3663 * If LEN is >= 0, it is the length of the name.
3664 * Otherwise, compute the length by scanning the entire name.
3666 * If HASH is >= 0, it is the precomputed hash code.
3667 * Otherwise, compute the hash code.
3670 static ASSERTION_HASHNODE *
3677 register ASSERTION_HASHNODE *bucket;
3679 bucket = pfile->assertion_hashtab[hash];
3681 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3683 bucket = bucket->next;
3690 ASSERTION_HASHNODE *hp)
3692 struct tokenlist_list *tail;
3693 if (hp->prev != NULL)
3694 hp->prev->next = hp->next;
3695 if (hp->next != NULL)
3696 hp->next->prev = hp->prev;
3698 for (tail = hp->value; tail; )
3700 struct tokenlist_list *next = tail->next;
3701 free_token_list (tail->tokens);
3706 /* make sure that the bucket chain header that
3707 the deleted guy was on points to the right thing afterwards. */
3708 if (hp == *hp->bucket_hdr)
3709 *hp->bucket_hdr = hp->next;
3714 /* Convert a character string literal into a nul-terminated string.
3715 The input string is [IN ... LIMIT).
3716 The result is placed in RESULT. RESULT can be the same as IN.
3717 The value returned in the end of the string written to RESULT,
3718 or NULL on error. */
3723 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3743 char *bpc = (char *) in;
3744 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3745 in = (U_CHAR *) bpc;
3747 *result++ = (U_CHAR)c;
3750 /* else fall through */
3760 * interpret #line command. Remembers previously seen fnames
3761 * in its very own hash table.
3763 #define FNAME_HASHSIZE 37
3768 struct directive *keyword)
3770 cpp_buffer *ip = CPP_BUFFER (pfile);
3772 long old_written = CPP_WRITTEN (pfile);
3773 enum file_change_code file_change = same_file;
3774 enum cpp_token token;
3777 token = get_directive_token (pfile);
3779 if (token != CPP_NUMBER
3780 || !isdigit(pfile->token_buffer[old_written]))
3782 cpp_error (pfile, "invalid format `#line' command");
3783 goto bad_line_directive;
3786 /* The Newline at the end of this line remains to be processed.
3787 To put the next line at the specified line number,
3788 we must store a line number now that is one less. */
3789 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3790 CPP_SET_WRITTEN (pfile, old_written);
3792 /* NEW_LINENO is one less than the actual line number here. */
3793 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3794 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3796 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3797 if (PEEKC() && !is_space[PEEKC()]) {
3798 cpp_error (pfile, "invalid format `#line' command");
3799 goto bad_line_directive;
3803 token = get_directive_token (pfile);
3805 if (token == CPP_STRING) {
3806 U_CHAR *fname = pfile->token_buffer + old_written;
3808 static HASHNODE *fname_table[FNAME_HASHSIZE];
3809 HASHNODE *hp, **hash_bucket;
3814 /* Turn the file name, which is a character string literal,
3815 into a null-terminated string. Do this in place. */
3816 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3817 if (end_name == NULL)
3819 cpp_error (pfile, "invalid format `#line' command");
3820 goto bad_line_directive;
3823 fname_length = end_name - fname;
3825 num_start = CPP_WRITTEN (pfile);
3826 token = get_directive_token (pfile);
3827 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3828 p = pfile->token_buffer + num_start;
3829 if (CPP_PEDANTIC (pfile))
3830 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3832 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3834 cpp_error (pfile, "invalid format `#line' command");
3835 goto bad_line_directive;
3838 file_change = enter_file;
3840 file_change = leave_file;
3842 ip->system_header_p = 1;
3843 else /* if (*p == 4) */
3844 ip->system_header_p = 2;
3846 CPP_SET_WRITTEN (pfile, num_start);
3847 token = get_directive_token (pfile);
3848 p = pfile->token_buffer + num_start;
3849 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3850 ip->system_header_p = *p == 3 ? 1 : 2;
3851 token = get_directive_token (pfile);
3853 if (token != CPP_VSPACE) {
3854 cpp_error (pfile, "invalid format `#line' command");
3855 goto bad_line_directive;
3860 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3861 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3862 if (hp->length == fname_length &&
3863 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3864 ip->nominal_fname = hp->value.cpval;
3868 /* Didn't find it; cons up a new one. */
3869 hp = (HASHNODE *) Safe_calloc(sizeof (HASHNODE) + fname_length + 1);
3870 hp->next = *hash_bucket;
3873 hp->length = fname_length;
3874 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3875 bcopy (fname, hp->value.cpval, fname_length);
3878 else if (token != CPP_VSPACE && token != CPP_EOF) {
3879 cpp_error (pfile, "invalid format `#line' command");
3880 goto bad_line_directive;
3883 ip->lineno = new_lineno;
3885 skip_rest_of_line (pfile);
3886 CPP_SET_WRITTEN (pfile, old_written);
3887 output_line_command (pfile, 0, file_change);
3892 * remove the definition of a symbol from the symbol table.
3893 * according to un*x /lib/cpp, it is not an error to undef
3894 * something that has no definitions, so it isn't one here either.
3900 struct directive *keyword,
3901 U_CHAR *buf, U_CHAR *limit)
3905 U_CHAR *orig_buf = buf;
3908 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3909 if (pcp_outfile && keyword)
3910 pass_thru_directive (buf, limit, pfile, keyword);
3913 SKIP_WHITE_SPACE (buf);
3914 sym_length = check_macro_name (pfile, buf, "macro");
3916 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3918 /* If we are generating additional info for debugging (with -g) we
3919 need to pass through all effective #undef commands. */
3920 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3921 pass_thru_directive (orig_buf, limit, pfile, keyword);
3922 if (hp->type != T_MACRO)
3923 cpp_warning (pfile, "undefining `%s'", hp->name);
3927 if (CPP_PEDANTIC (pfile)) {
3929 SKIP_WHITE_SPACE (buf);
3931 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3937 * Report an error detected by the program we are processing.
3938 * Use the text of the line in the error message.
3939 * (We use error because it prints the filename & line#.)
3945 struct directive *keyword,
3946 U_CHAR *buf,U_CHAR *limit)
3948 int length = limit - buf;
3949 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3950 bcopy (buf, copy, length);
3952 SKIP_WHITE_SPACE (copy);
3953 cpp_error (pfile, "#error %s", copy);
3958 * Report a warning detected by the program we are processing.
3959 * Use the text of the line in the warning message, then continue.
3960 * (We use error because it prints the filename & line#.)
3966 struct directive *keyword,
3967 U_CHAR *buf,U_CHAR *limit)
3969 int length = limit - buf;
3970 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3971 bcopy (buf, copy, length);
3973 SKIP_WHITE_SPACE (copy);
3974 cpp_warning (pfile, "#warning %s", copy);
3978 /* Remember the name of the current file being read from so that we can
3979 avoid ever including it again. */
3985 cpp_buffer *ip = NULL;
3986 struct file_name_list *new;
3988 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3992 if (ip->fname != NULL)
3997 new = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3998 new->next = pfile->dont_repeat_files;
3999 pfile->dont_repeat_files = new;
4000 new->fname = savestring (ip->fname);
4001 new->control_macro = 0;
4002 new->got_name_map = 0;
4003 new->c_system_include_path = 0;
4008 /* #ident has already been copied to the output file, so just ignore it. */
4013 struct directive *keyword,
4014 U_CHAR *buf, U_CHAR *limit)
4016 /* long old_written = CPP_WRITTEN (pfile);*/
4019 /* Allow #ident in system headers, since that's not user's fault. */
4020 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
4021 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
4023 /* Leave rest of line to be read by later calls to cpp_get_token. */
4028 /* #pragma and its argument line have already been copied to the output file.
4029 Just check for some recognized pragmas that need validation here. */
4034 struct directive *keyword,
4035 U_CHAR *buf,U_CHAR *limit)
4037 while (*buf == ' ' || *buf == '\t')
4039 if (!strncmp (buf, "once", 4)) {
4040 /* Allow #pragma once in system headers, since that's not the user's
4042 if (!CPP_BUFFER (pfile)->system_header_p)
4043 cpp_warning (pfile, "`#pragma once' is obsolete");
4047 if (!strncmp (buf, "implementation", 14)) {
4048 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4049 been included yet. */
4050 struct file_name_list *ptr;
4051 U_CHAR *p = buf + 14, *fname, *inc_fname;
4053 SKIP_WHITE_SPACE (p);
4054 if (*p == '\n' || *p != '\"')
4058 p = (U_CHAR *) index (fname, '\"');
4059 fname_len = p != NULL ? p - fname : strlen (fname);
4061 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4062 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4063 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4064 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4066 "`#pragma implementation' for `%s' appears after file is included",
4075 /* This was a fun hack, but #pragma seems to start to be useful.
4076 By failing to recognize it, we pass it through unchanged to cc1. */
4079 * the behavior of the #pragma directive is implementation defined.
4080 * this implementation defines it as follows.
4087 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4090 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4092 execl ("/usr/games/hack", "#pragma", 0);
4093 execl ("/usr/games/rogue", "#pragma", 0);
4094 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4095 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4097 fatal ("You are in a maze of twisty compiler features, all different");
4101 /* Just ignore #sccs, on systems where we define it at all. */
4106 struct directive *keyword,
4107 U_CHAR *buf,U_CHAR *limit)
4109 if (CPP_PEDANTIC (pfile))
4110 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4115 * handle #if command by
4116 * 1) inserting special `defined' keyword into the hash table
4117 * that gets turned into 0 or 1 by special_symbol (thus,
4118 * if the luser has a symbol called `defined' already, it won't
4119 * work inside the #if command)
4120 * 2) rescan the input into a temporary output buffer
4121 * 3) pass the output buffer to the yacc parser and collect a value
4122 * 4) clean up the mess left from steps 1 and 2.
4123 * 5) call conditional_skip to skip til the next #endif (etc.),
4124 * or not, depending on the value from step 3.
4130 struct directive *keyword,
4131 U_CHAR *buf,U_CHAR *limit)
4133 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4134 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4139 * handle a #elif directive by not changing if_stack either.
4140 * see the comment above do_else.
4146 struct directive *keyword,
4147 U_CHAR *buf,U_CHAR *limit)
4149 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4150 cpp_error (pfile, "`#elif' not within a conditional");
4153 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4154 cpp_error (pfile, "`#elif' after `#else'");
4156 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4158 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4159 && strcmp (pfile->if_stack->fname,
4160 CPP_BUFFER (pfile)->nominal_fname) != 0)
4161 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4162 fprintf (stderr, ")\n");
4164 pfile->if_stack->type = T_ELIF;
4167 if (pfile->if_stack->if_succeeded)
4168 skip_if_group (pfile, 0);
4170 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4172 skip_if_group (pfile, 0);
4174 ++pfile->if_stack->if_succeeded; /* continue processing input */
4175 output_line_command (pfile, 1, same_file);
4182 * evaluate a #if expression in BUF, of length LENGTH,
4183 * then parse the result as a C expression and return the value as an int.
4185 static HOST_WIDE_INT
4186 eval_if_expression (
4191 HASHNODE *save_defined;
4192 HOST_WIDE_INT value;
4193 long old_written = CPP_WRITTEN (pfile);
4195 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4196 pfile->pcp_inside_if = 1;
4198 value = cpp_parse_expr (pfile);
4199 pfile->pcp_inside_if = 0;
4200 delete_macro (save_defined); /* clean up special symbol */
4202 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4208 * routine to handle ifdef/ifndef. Try to look up the symbol,
4209 * then do or don't skip to the #endif/#else/#elif depending
4210 * on what directive is actually being processed.
4216 struct directive *keyword,
4217 U_CHAR *unused1, U_CHAR *unused2)
4220 cpp_buffer *ip = CPP_BUFFER (pfile);
4223 enum cpp_token token;
4224 int start_of_file = 0;
4225 U_CHAR *control_macro = 0;
4226 int old_written = CPP_WRITTEN (pfile);
4228 /* Detect a #ifndef at start of file (not counting comments). */
4229 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4230 start_of_file = pfile->only_seen_white == 2;
4232 pfile->no_macro_expand++;
4233 token = get_directive_token (pfile);
4234 pfile->no_macro_expand--;
4236 ident = pfile->token_buffer + old_written;
4237 ident_length = CPP_WRITTEN (pfile) - old_written;
4238 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4240 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4242 skip = (keyword->type == T_IFDEF);
4243 if (! CPP_TRADITIONAL (pfile))
4244 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4246 else if (token == CPP_NAME)
4248 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4249 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4250 if (start_of_file && !skip)
4252 control_macro = (U_CHAR *) Safe_malloc (ident_length + 1);
4253 bcopy (ident, control_macro, ident_length + 1);
4258 skip = (keyword->type == T_IFDEF);
4259 if (! CPP_TRADITIONAL (pfile))
4260 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4263 if (!CPP_TRADITIONAL (pfile))
4265 cpp_skip_hspace (pfile);
4267 if (c != EOF && c != '\n')
4268 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4270 skip_rest_of_line (pfile);
4274 /* Output a precondition for this macro. */
4275 if (hp && hp->value.defn->predefined)
4276 fprintf (pcp_outfile, "#define %s\n", hp->name);
4279 fprintf (pcp_outfile, "#undef ");
4280 while (is_idchar[*cp]) /* Ick! */
4281 fputc (*cp++, pcp_outfile);
4282 putc ('\n', pcp_outfile);
4286 conditional_skip (pfile, skip, T_IF, control_macro);
4290 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4291 If this is a #ifndef starting at the beginning of a file,
4292 CONTROL_MACRO is the macro name tested by the #ifndef.
4293 Otherwise, CONTROL_MACRO is 0. */
4299 enum node_type type,
4300 U_CHAR *control_macro)
4302 IF_STACK_FRAME *temp;
4304 temp = (IF_STACK_FRAME *) Safe_calloc(sizeof (IF_STACK_FRAME));
4305 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4307 temp->lineno = CPP_BUFFER (pfile)->lineno;
4309 temp->next = pfile->if_stack;
4310 temp->control_macro = control_macro;
4311 pfile->if_stack = temp;
4313 pfile->if_stack->type = type;
4316 skip_if_group (pfile, 0);
4319 ++pfile->if_stack->if_succeeded;
4320 output_line_command (pfile, 1, same_file);
4325 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4326 * leaves input ptr at the sharp sign found.
4327 * If ANY is nonzero, return at next directive of any sort.
4335 int at_beg_of_line = 1;
4336 struct directive *kt;
4337 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4339 U_CHAR *beg_of_line = bp;
4341 register int ident_length;
4342 U_CHAR *ident, *after_ident;
4343 struct parse_marker line_start_mark;
4345 parse_set_mark (&line_start_mark, pfile);
4347 if (CPP_OPTIONS (pfile)->output_conditionals) {
4348 static char failed[] = "#failed\n";
4349 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4351 output_line_command (pfile, 1, same_file);
4355 if (CPP_OPTIONS (pfile)->output_conditionals)
4357 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4358 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4359 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4361 parse_move_mark (&line_start_mark, pfile);
4362 if (!CPP_TRADITIONAL (pfile))
4363 cpp_skip_hspace (pfile);
4367 int old_written = CPP_WRITTEN (pfile);
4368 cpp_skip_hspace (pfile);
4370 parse_name (pfile, GETC());
4371 ident_length = CPP_WRITTEN (pfile) - old_written;
4372 ident = pfile->token_buffer + old_written;
4373 pfile->limit = ident;
4375 if (ident_length == 0)
4376 goto not_a_directive;
4378 /* Handle # followed by a line number. */
4380 /* Avoid error for `###' and similar cases unless -pedantic. */
4383 for (kt = directive_table; kt->length >= 0; kt++)
4385 IF_STACK_FRAME *temp;
4386 if (ident_length == kt->length
4387 && strncmp (ident, kt->name, kt->length) == 0)
4389 /* If we are asked to return on next directive, do so now. */
4399 = (IF_STACK_FRAME *) Safe_calloc(sizeof (IF_STACK_FRAME));
4400 temp->next = pfile->if_stack;
4401 pfile->if_stack = temp;
4403 temp->lineno = CPP_BUFFER(pfile)->lineno;
4405 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4406 temp->type = kt->type;
4410 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4411 validate_else (pfile,
4412 kt->type == T_ELSE ? "#else" : "#endif");
4414 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4417 "`#%s' not within a conditional", kt->name);
4420 else if (pfile->if_stack == save_if_stack)
4421 goto done; /* found what we came for */
4423 if (kt->type != T_ENDIF)
4425 if (pfile->if_stack->type == T_ELSE)
4426 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4427 pfile->if_stack->type = kt->type;
4431 temp = pfile->if_stack;
4432 pfile->if_stack = temp->next;
4439 /* Don't let erroneous code go by. */
4440 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4441 && CPP_PEDANTIC (pfile))
4442 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4446 /* We're in the middle of a line. Skip the rest of it. */
4453 case '/': /* possible comment */
4454 c = skip_comment (pfile, NULL);
4461 old = CPP_WRITTEN (pfile);
4462 cpp_get_token (pfile);
4463 CPP_SET_WRITTEN (pfile, old);
4466 /* Char after backslash loses its special meaning. */
4467 if (PEEKC() == '\n')
4477 if (CPP_OPTIONS (pfile)->output_conditionals) {
4478 static char end_failed[] = "#endfailed\n";
4479 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4482 pfile->only_seen_white = 1;
4483 parse_goto_mark (&line_start_mark, pfile);
4484 parse_clear_mark (&line_start_mark);
4488 * handle a #else directive. Do this by just continuing processing
4489 * without changing if_stack ; this is so that the error message
4490 * for missing #endif's etc. will point to the original #if. It
4491 * is possible that something different would be better.
4497 struct directive *keyword,
4498 U_CHAR *buf, U_CHAR *limit)
4500 cpp_buffer *ip = CPP_BUFFER (pfile);
4502 if (CPP_PEDANTIC (pfile))
4503 validate_else (pfile, "#else");
4504 skip_rest_of_line (pfile);
4506 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4507 cpp_error (pfile, "`#else' not within a conditional");
4510 /* #ifndef can't have its special treatment for containing the whole file
4511 if it has a #else clause. */
4512 pfile->if_stack->control_macro = 0;
4514 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4515 cpp_error (pfile, "`#else' after `#else'");
4516 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4517 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4518 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4519 fprintf (stderr, ")\n");
4521 pfile->if_stack->type = T_ELSE;
4524 if (pfile->if_stack->if_succeeded)
4525 skip_if_group (pfile, 0);
4527 ++pfile->if_stack->if_succeeded; /* continue processing input */
4528 output_line_command (pfile, 1, same_file);
4534 * unstack after #endif command
4540 struct directive *keyword,
4541 U_CHAR *buf, U_CHAR *limit)
4543 if (CPP_PEDANTIC (pfile))
4544 validate_else (pfile, "#endif");
4545 skip_rest_of_line (pfile);
4547 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4548 cpp_error (pfile, "unbalanced `#endif'");
4551 IF_STACK_FRAME *temp = pfile->if_stack;
4552 pfile->if_stack = temp->next;
4553 if (temp->control_macro != 0)
4555 /* This #endif matched a #ifndef at the start of the file.
4556 See if it is at the end of the file. */
4557 struct parse_marker start_mark;
4560 parse_set_mark (&start_mark, pfile);
4564 cpp_skip_hspace (pfile);
4569 parse_goto_mark (&start_mark, pfile);
4570 parse_clear_mark (&start_mark);
4574 /* If we get here, this #endif ends a #ifndef
4575 that contains all of the file (aside from whitespace).
4576 Arrange not to include the file again
4577 if the macro that was tested is defined.
4579 Do not do this for the top-level file in a -include or any
4580 file in a -imacros. */
4584 && ! (indepth == 1 && pfile->no_record_file)
4585 && ! (pfile->no_record_file && no_output))
4588 struct file_name_list *ifile = pfile->all_include_files;
4590 for ( ; ifile != NULL; ifile = ifile->next)
4592 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4594 ifile->control_macro = temp->control_macro;
4602 output_line_command (pfile, 1, same_file);
4607 /* When an #else or #endif is found while skipping failed conditional,
4608 if -pedantic was specified, this is called to warn about text after
4609 the command name. P points to the first char after the command name. */
4617 cpp_skip_hspace (pfile);
4619 if (c != EOF && c != '\n')
4621 "text following `%s' violates ANSI standard", directive);
4624 /* Get the next token, and add it to the text in pfile->token_buffer.
4625 Return the kind of token we got. */
4632 register int c, c2, c3;
4634 long start_line, start_column;
4635 enum cpp_token token;
4636 struct cpp_options *opts = CPP_OPTIONS (pfile);
4637 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4643 if (CPP_BUFFER (pfile)->seen_eof)
4645 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4652 cpp_buffer *next_buf
4653 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4654 CPP_BUFFER (pfile)->seen_eof = 1;
4655 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4657 /* We're about to return from an #include file.
4658 Emit #line information now (as part of the CPP_POP) result.
4659 But the #line refers to the file we will pop to. */
4660 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4661 CPP_BUFFER (pfile) = next_buf;
4662 pfile->input_stack_listing_current = 0;
4663 output_line_command (pfile, 0, leave_file);
4664 CPP_BUFFER (pfile) = cur_buffer;
4674 struct parse_marker start_mark;
4676 if (PEEKC () == '=')
4678 if (opts->put_out_comments)
4679 parse_set_mark (&start_mark, pfile);
4681 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4682 &start_line, &start_column);
4683 c = skip_comment (pfile, &newlines);
4684 if (opts->put_out_comments && (c == '/' || c == EOF))
4685 parse_clear_mark (&start_mark);
4690 cpp_error_with_line (pfile, start_line, start_column,
4691 "unterminated comment");
4694 c = '/'; /* Initial letter of comment. */
4696 /* Comments are equivalent to spaces.
4697 For -traditional, a comment is equivalent to nothing. */
4698 if (opts->put_out_comments)
4700 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4702 U_CHAR *start = pbuf->buf + start_mark.position;
4703 int len = pbuf->cur - start;
4704 CPP_RESERVE(pfile, 1 + len);
4705 CPP_PUTC_Q (pfile, c);
4706 CPP_PUTS_Q (pfile, start, len);
4707 pfile->lineno += newlines;
4708 parse_clear_mark (&start_mark);
4711 else if (CPP_TRADITIONAL (pfile))
4718 /* This may not work if cpp_get_token is called recursively,
4719 since many places look for horizontal space. */
4722 /* Copy the newlines into the output buffer, in order to
4723 avoid the pain of a #line every time a multiline comment
4725 CPP_RESERVE(pfile, newlines);
4726 while (--newlines >= 0)
4728 CPP_PUTC_Q (pfile, '\n');
4734 CPP_RESERVE(pfile, 1);
4735 CPP_PUTC_Q (pfile, ' ');
4739 if (opts->for_lint) {
4742 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4744 if (lintcmd != NULL) {
4745 /* I believe it is always safe to emit this newline: */
4747 bcopy ("#pragma lint ", (char *) obp, 13);
4749 bcopy (lintcmd, (char *) obp, cmdlen);
4754 bcopy (argbp, (char *) obp, arglen);
4758 /* OK, now bring us back to the state we were in before we entered
4759 this branch. We need #line b/c the newline for the pragma
4760 could fuck things up. */
4761 output_line_command (pfile, 0, same_file);
4762 *(obp++) = ' '; /* just in case, if comments are copied thru */
4770 /* If this is expanding a macro definition, don't recognize
4771 preprocessor directives. */
4774 /* If this is expand_into_temp_buffer, recognize them
4775 only after an actual newline at this level,
4776 not at the beginning of the input level. */
4777 if (ip->fname == 0 && beg_of_line == ip->buf)
4783 if (!pfile->only_seen_white)
4785 if (handle_directive (pfile))
4786 return CPP_DIRECTIVE;
4787 pfile->only_seen_white = 0;
4792 /* A single quoted string is treated like a double -- some
4793 programs (e.g., troff) are perverse this way */
4794 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4795 &start_line, &start_column);
4796 old_written = CPP_WRITTEN (pfile);
4798 CPP_PUTC (pfile, c);
4804 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4806 /* try harder: this string crosses a macro expansion
4807 boundary. This can happen naturally if -traditional.
4808 Otherwise, only -D can make a macro with an unmatched
4810 cpp_buffer *next_buf
4811 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4812 (*CPP_BUFFER (pfile)->cleanup)
4813 (CPP_BUFFER (pfile), pfile);
4814 CPP_BUFFER (pfile) = next_buf;
4817 if (!CPP_TRADITIONAL (pfile))
4819 cpp_error_with_line (pfile, start_line, start_column,
4820 "unterminated string or character constant");
4821 if (pfile->multiline_string_line != start_line
4822 && pfile->multiline_string_line != 0)
4823 cpp_error_with_line (pfile,
4824 pfile->multiline_string_line, -1,
4825 "possible real start of unterminated constant");
4826 pfile->multiline_string_line = 0;
4830 CPP_PUTC (pfile, cc);
4834 /* Traditionally, end of line ends a string constant with
4835 no error. So exit the loop and record the new line. */
4836 if (CPP_TRADITIONAL (pfile))
4840 cpp_error_with_line (pfile, start_line, start_column,
4841 "unterminated character constant");
4844 if (CPP_PEDANTIC (pfile)
4845 && pfile->multiline_string_line == 0)
4847 cpp_pedwarn_with_line (pfile, start_line, start_column,
4848 "string constant runs past end of line");
4850 if (pfile->multiline_string_line == 0)
4851 pfile->multiline_string_line = start_line;
4858 /* Backslash newline is replaced by nothing at all. */
4859 CPP_ADJUST_WRITTEN (pfile, -1);
4864 /* ANSI stupidly requires that in \\ the second \
4865 is *not* prevented from combining with a newline. */
4868 CPP_PUTC (pfile, cc);
4880 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4881 CPP_PWRITTEN (pfile));
4882 pfile->only_seen_white = 0;
4883 return c == '\'' ? CPP_CHAR : CPP_STRING;
4886 if (!opts->dollars_in_ident)
4891 if (opts->cplusplus && PEEKC () == ':')
4900 if (c2 == c || c2 == '=')
4910 if (PEEKC () == '=')
4917 if (c2 == '-' && opts->chill)
4919 /* Chill style comment */
4920 if (opts->put_out_comments)
4921 parse_set_mark (&start_mark, pfile);
4922 FORWARD(1); /* Skip second '-'. */
4930 /* Don't consider final '\n' to be part of comment. */
4936 goto return_comment;
4938 if (c2 == '-' || c2 == '=' || c2 == '>')
4943 if (pfile->parsing_include_directive)
4947 CPP_PUTC (pfile, c);
4952 if (c == '\n' || c == EOF)
4955 "missing '>' in `#include <FILENAME>'");
4961 /* else fall through */
4970 CPP_RESERVE (pfile, 4);
4971 CPP_PUTC (pfile, c);
4972 CPP_PUTC (pfile, c2);
4976 CPP_PUTC_Q (pfile, GETC ());
4977 CPP_NUL_TERMINATE_Q (pfile);
4978 pfile->only_seen_white = 0;
4982 if (CPP_BUFFER (pfile)->has_escapes)
4987 if (pfile->output_escapes)
4988 CPP_PUTS (pfile, "@-", 2);
4989 parse_name (pfile, GETC ());
4992 else if (is_space [c])
4994 CPP_RESERVE (pfile, 2);
4995 if (pfile->output_escapes)
4996 CPP_PUTC_Q (pfile, '@');
4997 CPP_PUTC_Q (pfile, c);
5001 if (pfile->output_escapes)
5003 CPP_PUTS (pfile, "@@", 2);
5013 CPP_RESERVE(pfile, 2);
5014 CPP_PUTC_Q (pfile, '.');
5018 /* FIXME - misses the case "..\\\n." */
5019 if (c2 == '.' && PEEKN(1) == '.')
5021 CPP_RESERVE(pfile, 4);
5022 CPP_PUTC_Q (pfile, '.');
5023 CPP_PUTC_Q (pfile, '.');
5024 CPP_PUTC_Q (pfile, '.');
5026 CPP_NUL_TERMINATE_Q (pfile);
5027 pfile->only_seen_white = 0;
5034 pfile->only_seen_white = 0;
5036 CPP_RESERVE(pfile, 3);
5037 CPP_PUTC_Q (pfile, c);
5038 CPP_PUTC_Q (pfile, GETC ());
5039 CPP_NUL_TERMINATE_Q (pfile);
5045 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5047 CPP_PUTC (pfile, c);
5053 case '0': case '1': case '2': case '3': case '4':
5054 case '5': case '6': case '7': case '8': case '9':
5059 CPP_RESERVE (pfile, 2);
5060 CPP_PUTC_Q (pfile, c);
5065 if (!is_idchar[c] && c != '.'
5066 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5071 CPP_NUL_TERMINATE_Q (pfile);
5072 pfile->only_seen_white = 0;
5074 case 'b': case 'c': case 'd': case 'h': case 'o':
5075 case 'B': case 'C': case 'D': case 'H': case 'O':
5076 if (opts->chill && PEEKC () == '\'')
5078 pfile->only_seen_white = 0;
5079 CPP_RESERVE (pfile, 2);
5080 CPP_PUTC_Q (pfile, c);
5081 CPP_PUTC_Q (pfile, '\'');
5087 goto chill_number_eof;
5090 if (c == '\\' && PEEKC() == '\n')
5097 CPP_PUTC (pfile, c);
5101 CPP_RESERVE (pfile, 2);
5102 CPP_PUTC_Q (pfile, c);
5103 CPP_NUL_TERMINATE_Q (pfile);
5110 CPP_NUL_TERMINATE (pfile);
5117 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5118 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5119 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5120 case 'x': case 'y': case 'z':
5121 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5122 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5123 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5128 unsigned char *ident;
5129 int before_name_written = CPP_WRITTEN (pfile);
5131 parse_name (pfile, c);
5132 pfile->only_seen_white = 0;
5133 if (pfile->no_macro_expand)
5135 ident = pfile->token_buffer + before_name_written;
5136 ident_len = CPP_PWRITTEN (pfile) - ident;
5137 hp = cpp_lookup (pfile, ident, ident_len, -1);
5140 if (hp->type == T_DISABLED)
5142 if (pfile->output_escapes)
5143 { /* Return "@-IDENT", followed by '\0'. */
5145 CPP_RESERVE (pfile, 3);
5146 ident = pfile->token_buffer + before_name_written;
5147 CPP_ADJUST_WRITTEN (pfile, 2);
5148 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5155 /* If macro wants an arglist, verify that a '(' follows.
5156 first skip all whitespace, copying it to the output
5157 after the macro name. Then, if there is no '(',
5158 decide this is not a macro call and leave things that way. */
5159 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5161 struct parse_marker macro_mark;
5163 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5165 cpp_buffer *next_buf;
5166 cpp_skip_hspace (pfile);
5167 if (PEEKC () != EOF)
5169 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5170 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5171 CPP_BUFFER (pfile) = next_buf;
5173 parse_set_mark (¯o_mark, pfile);
5176 cpp_skip_hspace (pfile);
5178 is_macro_call = c == '(';
5184 parse_goto_mark (¯o_mark, pfile);
5185 parse_clear_mark (¯o_mark);
5189 /* This is now known to be a macro call. */
5191 /* it might not actually be a macro. */
5192 if (hp->type != T_MACRO) {
5193 int xbuf_len; U_CHAR *xbuf;
5194 CPP_SET_WRITTEN (pfile, before_name_written);
5195 special_symbol (hp, pfile);
5196 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5197 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
5198 CPP_SET_WRITTEN (pfile, before_name_written);
5199 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5200 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5204 /* Expand the macro, reading arguments as needed,
5205 and push the expansion on the input stack. */
5206 macroexpand (pfile, hp);
5207 CPP_SET_WRITTEN (pfile, before_name_written);
5210 /* An extra "@ " is added to the end of a macro expansion
5211 to prevent accidental token pasting. We prefer to avoid
5212 unneeded extra spaces (for the sake of cpp-using tools like
5213 imake). Here we remove the space if it is safe to do so. */
5214 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5215 && pfile->buffer->rlimit[-2] == '@'
5216 && pfile->buffer->rlimit[-1] == ' ')
5218 int c1 = pfile->buffer->rlimit[-3];
5219 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5220 if (c2 == EOF || ! unsafe_chars (c1, c2))
5221 pfile->buffer->rlimit -= 2;
5226 case ' ': case '\t': case '\v': case '\r':
5229 CPP_PUTC (pfile, c);
5231 if (c == EOF || !is_hor_space[c])
5245 CPP_PUTC (pfile, c);
5246 if (pfile->only_seen_white == 0)
5247 pfile->only_seen_white = 1;
5249 output_line_command (pfile, 1, same_file);
5252 case '(': token = CPP_LPAREN; goto char1;
5253 case ')': token = CPP_RPAREN; goto char1;
5254 case '{': token = CPP_LBRACE; goto char1;
5255 case '}': token = CPP_RBRACE; goto char1;
5256 case ',': token = CPP_COMMA; goto char1;
5257 case ';': token = CPP_SEMICOLON; goto char1;
5263 pfile->only_seen_white = 0;
5264 CPP_PUTC (pfile, c);
5270 /* Like cpp_get_token, but skip spaces and comments. */
5272 cpp_get_non_space_token (
5275 int old_written = CPP_WRITTEN (pfile);
5278 enum cpp_token token = cpp_get_token (pfile);
5279 if (token != CPP_COMMENT && token != CPP_POP
5280 && token != CPP_HSPACE && token != CPP_VSPACE)
5282 CPP_SET_WRITTEN (pfile, old_written);
5286 /* Parse an identifier starting with C. */
5290 cpp_reader *pfile, int c)
5296 if (c == '\\' && PEEKC() == '\n')
5305 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5306 CPP_PUTC_Q (pfile, c);
5311 CPP_NUL_TERMINATE_Q (pfile);
5316 /* Maintain and search list of included files, for #import. */
5318 /* Hash a file name for import_hash_table. */
5326 while (*f) val += *f++;
5327 return (val%IMPORT_HASH_SIZE);
5330 /* Search for file FILENAME in import_hash_table.
5331 Return -2 if found, either a matching name or a matching inode.
5332 Otherwise, open the file and return a file descriptor if successful
5333 or -1 if unsuccessful. */
5339 struct file_name_list *searchptr)
5341 struct import_file *i;
5347 hashval = import_hash (filename);
5349 /* Attempt to find file in list of already included files */
5350 i = pfile->import_hash_table[hashval];
5353 if (!strcmp (filename, i->name))
5354 return -2; /* return found */
5357 /* Open it and try a match on inode/dev */
5358 fd = open_include_file (pfile, filename, searchptr);
5362 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5363 i = pfile->import_hash_table[h];
5365 /* Compare the inode and the device.
5366 Supposedly on some systems the inode is not a scalar. */
5367 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5368 && i->dev == sb.st_dev) {
5370 return -2; /* return found */
5375 return fd; /* Not found, return open file */
5378 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5386 struct import_file *i;
5390 hashval = import_hash (fname);
5392 i = (struct import_file *)Safe_malloc (sizeof (struct import_file));
5393 i->name = (char *)Safe_malloc (strlen (fname)+1);
5394 strcpy (i->name, fname);
5395 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5397 i->next = pfile->import_hash_table[hashval];
5398 pfile->import_hash_table[hashval] = i;
5401 /* The file_name_map structure holds a mapping of file names for a
5402 particular directory. This mapping is read from the file named
5403 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5404 map filenames on a file system with severe filename restrictions,
5405 such as DOS. The format of the file name map file is just a series
5406 of lines with two tokens on each line. The first token is the name
5407 to map, and the second token is the actual name to use. */
5409 struct file_name_map
5411 struct file_name_map *map_next;
5416 #define FILE_NAME_MAP_FILE "header.gcc"
5418 /* Read a space delimited string of unlimited length from a stdio
5422 read_filename_string (
5430 set = alloc = Safe_malloc (len + 1);
5434 while ((ch = getc (f)) != EOF && ! is_space[ch])
5436 if (set - alloc == len)
5439 alloc = Safe_realloc(alloc, len + 1);
5440 set = alloc + len / 2;
5450 /* This structure holds a linked list of file name maps, one per directory. */
5451 struct file_name_map_list
5453 struct file_name_map_list *map_list_next;
5454 char *map_list_name;
5455 struct file_name_map *map_list_map;
5458 /* Read the file name map file for DIRNAME. */
5460 static struct file_name_map *
5465 register struct file_name_map_list *map_list_ptr;
5469 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5470 map_list_ptr = map_list_ptr->map_list_next)
5471 if (! strcmp (map_list_ptr->map_list_name, dirname))
5472 return map_list_ptr->map_list_map;
5474 map_list_ptr = ((struct file_name_map_list *)
5475 Safe_malloc (sizeof (struct file_name_map_list)));
5476 map_list_ptr->map_list_name = savestring (dirname);
5477 map_list_ptr->map_list_map = NULL;
5479 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5480 strcpy (name, dirname);
5483 strcat (name, FILE_NAME_MAP_FILE);
5484 f = fopen (name, "r");
5486 map_list_ptr->map_list_map = NULL;
5490 int dirlen = strlen (dirname);
5492 while ((ch = getc (f)) != EOF)
5495 struct file_name_map *ptr;
5499 from = read_filename_string (ch, f);
5500 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5502 to = read_filename_string (ch, f);
5504 ptr = ((struct file_name_map *)
5505 Safe_malloc (sizeof (struct file_name_map)));
5506 ptr->map_from = from;
5508 /* Make the real filename absolute. */
5513 ptr->map_to = Safe_malloc (dirlen + strlen (to) + 2);
5514 strcpy (ptr->map_to, dirname);
5515 ptr->map_to[dirlen] = '/';
5516 strcpy (ptr->map_to + dirlen + 1, to);
5520 ptr->map_next = map_list_ptr->map_list_map;
5521 map_list_ptr->map_list_map = ptr;
5523 while ((ch = getc (f)) != '\n')
5530 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5531 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5533 return map_list_ptr->map_list_map;
5536 /* Try to open include file FILENAME. SEARCHPTR is the directory
5537 being tried from the include file search path. This function maps
5538 filenames on file systems based on information read by
5545 struct file_name_list *searchptr)
5547 register struct file_name_map *map;
5548 register char *from;
5551 if (searchptr && ! searchptr->got_name_map)
5553 searchptr->name_map = read_name_map (pfile,
5555 ? searchptr->fname : ".");
5556 searchptr->got_name_map = 1;
5559 /* First check the mapping for the directory we are using. */
5560 if (searchptr && searchptr->name_map)
5563 if (searchptr->fname)
5564 from += strlen (searchptr->fname) + 1;
5565 for (map = searchptr->name_map; map; map = map->map_next)
5567 if (! strcmp (map->map_from, from))
5569 /* Found a match. */
5570 return open (map->map_to, O_RDONLY, 0666);
5575 /* Try to find a mapping file for the particular directory we are
5576 looking in. Thus #include <sys/types.h> will look up sys/types.h
5577 in /usr/include/header.gcc and look up types.h in
5578 /usr/include/sys/header.gcc. */
5579 p = rindex (filename, '/');
5584 && strlen (searchptr->fname) == p - filename
5585 && ! strncmp (searchptr->fname, filename, p - filename))
5587 /* FILENAME is in SEARCHPTR, which we've already checked. */
5588 return open (filename, O_RDONLY, 0666);
5598 dir = (char *) alloca (p - filename + 1);
5599 bcopy (filename, dir, p - filename);
5600 dir[p - filename] = '\0';
5603 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5604 if (! strcmp (map->map_from, from))
5605 return open (map->map_to, O_RDONLY, 0666);
5607 return open (filename, O_RDONLY, 0666);
5610 /* Process the contents of include file FNAME, already open on descriptor F,
5612 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5613 "system" include directories (as decided by the `is_system_include'
5615 DIRPTR is the link in the dir path through which this file was found,
5616 or 0 if the file name was absolute or via the current directory.
5617 Return 1 on success, 0 on failure.
5619 The caller is responsible for the cpp_push_buffer. */
5626 int system_header_p,
5627 struct file_name_list *dirptr)
5633 cpp_buffer *fp; /* For input stack frame */
5634 int missing_newline = 0;
5636 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5638 cpp_perror_with_name (pfile, fname);
5640 cpp_pop_buffer (pfile);
5644 fp = CPP_BUFFER (pfile);
5645 fp->nominal_fname = fp->fname = fname;
5650 fp->system_header_p = system_header_p;
5653 fp->cleanup = file_cleanup;
5655 if (S_ISREG (st_mode)) {
5656 fp->buf = (U_CHAR *) Safe_malloc (st_size + 2);
5657 fp->alimit = fp->buf + st_size + 2;
5660 /* Read the file contents, knowing that st_size is an upper bound
5661 on the number of bytes we can read. */
5662 length = safe_read (f, fp->buf, st_size);
5663 fp->rlimit = fp->buf + length;
5664 if (length < 0) goto nope;
5666 else if (S_ISDIR (st_mode)) {
5667 cpp_error (pfile, "directory `%s' specified in #include", fname);
5671 /* Cannot count its file size before reading.
5672 First read the entire file into heap and
5673 copy them into buffer on stack. */
5678 fp->buf = (U_CHAR *) Safe_malloc (bsize + 2);
5681 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5683 goto nope; /* error! */
5685 if (st_size != bsize)
5686 break; /* End of file */
5688 fp->buf = (U_CHAR *) Safe_realloc(fp->buf, bsize + 2);
5694 if ((length > 0 && fp->buf[length - 1] != '\n')
5695 /* Backslash-newline at end is not good enough. */
5696 || (length > 1 && fp->buf[length - 2] == '\\')) {
5697 fp->buf[length++] = '\n';
5699 missing_newline = 1;
5702 fp->buf[length] = '\0';
5703 fp->rlimit = fp->buf + length;
5705 /* Close descriptor now, so nesting does not use lots of descriptors. */
5708 /* Must do this before calling trigraph_pcp, so that the correct file name
5709 will be printed in warning messages. */
5711 pfile->input_stack_listing_current = 0;
5721 if (missing_newline)
5724 if (CPP_PEDANTIC (pfile) && missing_newline)
5725 pedwarn ("file does not end in newline");
5728 input_file_stack_tick++;
5735 cpp_perror_with_name (pfile, fname);
5746 struct cpp_options *opts = CPP_OPTIONS (pfile);
5747 struct cpp_pending *pend;
5752 /* The code looks at the defaults through this pointer, rather than through
5753 the constant structure above. This pointer gets changed if an environment
5754 variable specifies other defaults. */
5755 struct default_include *include_defaults = include_defaults_array;
5757 /* Add dirs from CPATH after dirs from -I. */
5758 /* There seems to be confusion about what CPATH should do,
5759 so for the moment it is not documented. */
5760 /* Some people say that CPATH should replace the standard include dirs,
5761 but that seems pointless: it comes before them, so it overrides them
5763 p = (char *) getenv ("CPATH");
5764 if (p != 0 && ! opts->no_standard_includes)
5765 path_include (pfile, p);
5767 /* Now that dollars_in_ident is known, initialize is_idchar. */
5768 initialize_char_syntax (opts);
5770 /* Do partial setup of input buffer for the sake of generating
5771 early #line directives (when -g is in effect). */
5772 fp = cpp_push_buffer (pfile, NULL, 0);
5773 if (opts->in_fname == NULL)
5774 opts->in_fname = "";
5775 fp->nominal_fname = fp->fname = opts->in_fname;
5778 /* Install __LINE__, etc. Must follow initialize_char_syntax
5779 and option processing. */
5780 initialize_builtins (pfile);
5782 /* Do standard #defines and assertions
5783 that identify system and machine type. */
5785 if (!opts->inhibit_predefs) {
5786 char *p = (char *) alloca (strlen (predefs) + 1);
5787 strcpy (p, predefs);
5790 while (*p == ' ' || *p == '\t')
5792 /* Handle -D options. */
5793 if (p[0] == '-' && p[1] == 'D') {
5795 while (*p && *p != ' ' && *p != '\t')
5799 if (opts->debug_output)
5800 output_line_command (pfile, 0, same_file);
5801 cpp_define (pfile, q);
5802 while (*p == ' ' || *p == '\t')
5804 } else if (p[0] == '-' && p[1] == 'A') {
5805 /* Handle -A options (assertions). */
5814 past_name = assertion;
5815 /* Locate end of name. */
5816 while (*past_name && *past_name != ' '
5817 && *past_name != '\t' && *past_name != '(')
5819 /* Locate `(' at start of value. */
5821 while (*value && (*value == ' ' || *value == '\t'))
5823 if (*value++ != '(')
5825 while (*value && (*value == ' ' || *value == '\t'))
5828 /* Locate end of value. */
5829 while (*past_value && *past_value != ' '
5830 && *past_value != '\t' && *past_value != ')')
5832 termination = past_value;
5833 while (*termination && (*termination == ' ' || *termination == '\t'))
5835 if (*termination++ != ')')
5837 if (*termination && *termination != ' ' && *termination != '\t')
5839 /* Temporarily null-terminate the value. */
5840 save_char = *termination;
5841 *termination = '\0';
5842 /* Install the assertion. */
5843 make_assertion (pfile, "-A", assertion);
5844 *termination = (char) save_char;
5846 while (*p == ' ' || *p == '\t')
5854 /* Now handle the command line options. */
5856 /* Do -U's, -D's and -A's in the order they were seen. */
5857 /* First reverse the list. */
5858 opts->pending = nreverse_pending (opts->pending);
5860 for (pend = opts->pending; pend; pend = pend->next)
5862 if (pend->cmd != NULL && pend->cmd[0] == '-')
5864 switch (pend->cmd[1])
5867 if (opts->debug_output)
5868 output_line_command (pfile, 0, same_file);
5869 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5872 if (opts->debug_output)
5873 output_line_command (pfile, 0, same_file);
5874 cpp_define (pfile, pend->arg);
5877 make_assertion (pfile, "-A", pend->arg);
5883 opts->done_initializing = 1;
5885 { /* read the appropriate environment variable and if it exists
5886 replace include_defaults with the listed path. */
5888 switch ((opts->objc << 1) + opts->cplusplus)
5891 epath = getenv ("C_INCLUDE_PATH");
5894 epath = getenv ("CPLUS_INCLUDE_PATH");
5897 epath = getenv ("OBJC_INCLUDE_PATH");
5900 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5903 /* If the environment var for this language is set,
5904 add to the default list of include directories. */
5906 char *nstore = (char *) alloca (strlen (epath) + 2);
5908 char *startp, *endp;
5910 for (num_dirs = 1, startp = epath; *startp; startp++)
5911 if (*startp == PATH_SEPARATOR)
5914 = (struct default_include *) Safe_malloc ((num_dirs
5915 * sizeof (struct default_include))
5916 + sizeof (include_defaults_array));
5917 startp = endp = epath;
5920 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5921 if ((*endp == PATH_SEPARATOR)
5923 strncpy (nstore, startp, endp-startp);
5925 strcpy (nstore, ".");
5927 nstore[endp-startp] = '\0';
5929 include_defaults[num_dirs].fname = savestring (nstore);
5930 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5931 include_defaults[num_dirs].cxx_aware = 1;
5935 endp = startp = endp + 1;
5939 /* Put the usual defaults back in at the end. */
5940 bcopy ((char *) include_defaults_array,
5941 (char *) &include_defaults[num_dirs],
5942 sizeof (include_defaults_array));
5946 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5947 opts->first_system_include = opts->before_system;
5949 /* Unless -fnostdinc,
5950 tack on the standard include file dirs to the specified list */
5951 if (!opts->no_standard_includes) {
5952 struct default_include *p = include_defaults;
5953 char *specd_prefix = opts->include_prefix;
5954 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5955 int default_len = 0;
5956 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5957 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5958 default_len = strlen (default_prefix) - 7;
5959 default_prefix[default_len] = 0;
5961 /* Search "translated" versions of GNU directories.
5962 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5963 if (specd_prefix != 0 && default_len != 0)
5964 for (p = include_defaults; p->fname; p++) {
5965 /* Some standard dirs are only for C++. */
5967 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5968 /* Does this dir start with the prefix? */
5969 if (!strncmp (p->fname, default_prefix, default_len)) {
5970 /* Yes; change prefix and add to search list. */
5971 struct file_name_list *new
5972 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5973 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5974 char *str = (char *) Safe_malloc (this_len + 1);
5975 strcpy (str, specd_prefix);
5976 strcat (str, p->fname + default_len);
5978 new->control_macro = 0;
5979 new->c_system_include_path = !p->cxx_aware;
5980 new->got_name_map = 0;
5981 append_include_chain (pfile, new, new);
5982 if (opts->first_system_include == 0)
5983 opts->first_system_include = new;
5987 /* Search ordinary names for GNU include directories. */
5988 for (p = include_defaults; p->fname; p++) {
5989 /* Some standard dirs are only for C++. */
5991 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5992 struct file_name_list *new
5993 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5994 new->control_macro = 0;
5995 new->c_system_include_path = !p->cxx_aware;
5996 new->fname = p->fname;
5997 new->got_name_map = 0;
5998 append_include_chain (pfile, new, new);
5999 if (opts->first_system_include == 0)
6000 opts->first_system_include = new;
6005 /* Tack the after_include chain at the end of the include chain. */
6006 append_include_chain (pfile, opts->after_include, opts->last_after_include);
6007 if (opts->first_system_include == 0)
6008 opts->first_system_include = opts->after_include;
6010 /* With -v, print the list of dirs to search. */
6011 if (opts->verbose) {
6012 struct file_name_list *p;
6013 fprintf (stderr, "#include \"...\" search starts here:\n");
6014 for (p = opts->include; p; p = p->next) {
6015 if (p == opts->first_bracket_include)
6016 fprintf (stderr, "#include <...> search starts here:\n");
6017 fprintf (stderr, " %s\n", p->fname);
6019 fprintf (stderr, "End of search list.\n");
6022 /* Scan the -imacros files before the main input.
6023 Much like #including them, but with no_output set
6024 so that only their macro definitions matter. */
6026 opts->no_output++; pfile->no_record_file++;
6027 for (pend = opts->pending; pend; pend = pend->next)
6029 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6031 int fd = open (pend->arg, O_RDONLY, 0666);
6034 cpp_perror_with_name (pfile, pend->arg);
6035 return FATAL_EXIT_CODE;
6037 cpp_push_buffer (pfile, NULL, 0);
6038 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6039 cpp_scan_buffer (pfile);
6042 opts->no_output--; pfile->no_record_file--;
6044 /* Copy the entire contents of the main input file into
6045 the stacked input buffer previously allocated for it. */
6046 if (fname == NULL || *fname == 0) {
6049 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6050 cpp_pfatal_with_name (pfile, fname);
6052 /* -MG doesn't select the form of output and must be specified with one of
6053 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6054 inhibit compilation. */
6055 if (opts->print_deps_missing_files
6056 && (opts->print_deps == 0 || !opts->no_output))
6057 fatal (pfile, "-MG must be specified with one of -M or -MM");
6059 /* Either of two environment variables can specify output of deps.
6060 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6061 where OUTPUT_FILE is the file to write deps info to
6062 and DEPS_TARGET is the target to mention in the deps. */
6064 if (opts->print_deps == 0
6065 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6066 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6067 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6073 spec = getenv ("SUNPRO_DEPENDENCIES");
6074 opts->print_deps = 2;
6077 opts->print_deps = 1;
6080 /* Find the space before the DEPS_TARGET, if there is one. */
6081 /* This should use index. (mrs) */
6082 while (*s != 0 && *s != ' ') s++;
6085 opts->deps_target = s + 1;
6086 output_file = (char *) Safe_malloc (s - spec + 1);
6087 bcopy (spec, output_file, s - spec);
6088 output_file[s - spec] = 0;
6092 opts->deps_target = 0;
6096 opts->deps_file = output_file;
6097 opts->print_deps_append = 1;
6100 /* For -M, print the expected object file name
6101 as the target of this Make-rule. */
6102 if (opts->print_deps)
6104 pfile->deps_allocated_size = 200;
6105 pfile->deps_buffer = (char *) Safe_malloc (pfile->deps_allocated_size);
6106 pfile->deps_buffer[0] = 0;
6107 pfile->deps_size = 0;
6108 pfile->deps_column = 0;
6110 if (opts->deps_target)
6111 deps_output (pfile, opts->deps_target, ':');
6112 else if (*opts->in_fname == 0)
6113 deps_output (pfile, "-", ':');
6119 /* Discard all directory prefixes from filename. */
6120 if ((q = rindex (opts->in_fname, '/')) != NULL
6121 #ifdef DIR_SEPARATOR
6122 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6129 /* Copy remainder to mungable area. */
6130 p = (char *) alloca (strlen(q) + 8);
6133 /* Output P, but remove known suffixes. */
6137 && p[len - 2] == '.'
6138 && index("cCsSm", p[len - 1]))
6141 && p[len - 3] == '.'
6142 && p[len - 2] == 'c'
6143 && p[len - 1] == 'c')
6146 && p[len - 4] == '.'
6147 && p[len - 3] == 'c'
6148 && p[len - 2] == 'x'
6149 && p[len - 1] == 'x')
6152 && p[len - 4] == '.'
6153 && p[len - 3] == 'c'
6154 && p[len - 2] == 'p'
6155 && p[len - 1] == 'p')
6158 /* Supply our own suffix. */
6169 deps_output (pfile, p, ':');
6170 deps_output (pfile, opts->in_fname, ' ');
6175 /* Make sure data ends with a newline. And put a null after it. */
6177 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6178 /* Backslash-newline at end is not good enough. */
6179 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6180 fp->buf[fp->length++] = '\n';
6181 missing_newline = 1;
6183 fp->buf[fp->length] = '\0';
6185 /* Unless inhibited, convert trigraphs in the input. */
6191 /* Scan the -include files before the main input.
6192 We push these in reverse order, so that the first one is handled first. */
6194 pfile->no_record_file++;
6195 opts->pending = nreverse_pending (opts->pending);
6196 for (pend = opts->pending; pend; pend = pend->next)
6198 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6200 int fd = open (pend->arg, O_RDONLY, 0666);
6203 cpp_perror_with_name (pfile, pend->arg);
6204 return FATAL_EXIT_CODE;
6206 cpp_push_buffer (pfile, NULL, 0);
6207 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6210 pfile->no_record_file--;
6212 /* Free the pending list. */
6213 for (pend = opts->pending; pend; )
6215 struct cpp_pending *next = pend->next;
6219 opts->pending = NULL;
6222 /* Scan the input, processing macros and directives. */
6224 rescan (&outbuf, 0);
6226 if (missing_newline)
6229 if (CPP_PEDANTIC (pfile) && missing_newline)
6230 pedwarn ("file does not end in newline");
6233 if (finclude (pfile, f, fname, 0, NULL_PTR))
6234 output_line_command (pfile, 0, same_file);
6235 return SUCCESS_EXIT_CODE;
6242 bzero ((char *) pfile, sizeof (cpp_reader));
6243 pfile->get_token = cpp_get_token;
6245 pfile->token_buffer_size = 200;
6246 pfile->token_buffer = (U_CHAR*)Safe_malloc (pfile->token_buffer_size);
6247 CPP_SET_WRITTEN (pfile, 0);
6249 pfile->system_include_depth = 0;
6250 pfile->dont_repeat_files = 0;
6251 pfile->all_include_files = 0;
6252 pfile->max_include_len = 0;
6253 pfile->timebuf = NULL;
6254 pfile->only_seen_white = 1;
6255 pfile->buffer = CPP_NULL_BUFFER(pfile);
6258 static struct cpp_pending *
6260 struct cpp_pending *list)
6263 register struct cpp_pending *prev = 0, *next, *pend;
6264 for (pend = list; pend; pend = next)
6279 struct cpp_pending *pend
6280 = (struct cpp_pending*)Safe_malloc (sizeof (struct cpp_pending));
6283 pend->next = CPP_OPTIONS (pfile)->pending;
6284 CPP_OPTIONS (pfile)->pending = pend;
6287 /* Handle command-line options in (argc, argv).
6288 Can be called multiple times, to handle multiple sets of options.
6289 Returns if an unrecognized option is seen.
6290 Returns number of handled arguments. */
6293 cpp_handle_options (
6299 struct cpp_options *opts = CPP_OPTIONS (pfile);
6300 for (i = 0; i < argc; i++) {
6301 if (argv[i][0] != '-') {
6302 if (opts->out_fname != NULL)
6303 fatal ("Usage: %s [switches] input output", argv[0]);
6304 else if (opts->in_fname != NULL)
6305 opts->out_fname = argv[i];
6307 opts->in_fname = argv[i];
6309 switch (argv[i][1]) {
6312 if (!strcmp (argv[i], "-include")
6313 || !strcmp (argv[i], "-imacros")) {
6315 fatal ("Filename missing after `%s' option", argv[i]);
6317 push_pending (pfile, argv[i], argv[i+1]), i++;
6319 if (!strcmp (argv[i], "-iprefix")) {
6321 fatal ("Filename missing after `-iprefix' option");
6323 opts->include_prefix = argv[++i];
6325 if (!strcmp (argv[i], "-ifoutput")) {
6326 opts->output_conditionals = 1;
6328 if (!strcmp (argv[i], "-isystem")) {
6329 struct file_name_list *dirtmp;
6332 fatal ("Filename missing after `-isystem' option");
6334 dirtmp = (struct file_name_list *)
6335 Safe_malloc (sizeof (struct file_name_list));
6337 dirtmp->control_macro = 0;
6338 dirtmp->c_system_include_path = 1;
6339 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1]) + 1);
6340 strcpy (dirtmp->fname, argv[++i]);
6341 dirtmp->got_name_map = 0;
6343 if (opts->before_system == 0)
6344 opts->before_system = dirtmp;
6346 opts->last_before_system->next = dirtmp;
6347 opts->last_before_system = dirtmp; /* Tail follows the last one */
6349 /* Add directory to end of path for includes,
6350 with the default prefix at the front of its name. */
6351 if (!strcmp (argv[i], "-iwithprefix")) {
6352 struct file_name_list *dirtmp;
6355 if (opts->include_prefix != 0)
6356 prefix = opts->include_prefix;
6358 prefix = savestring (GCC_INCLUDE_DIR);
6359 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6360 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6361 prefix[strlen (prefix) - 7] = 0;
6364 dirtmp = (struct file_name_list *)
6365 Safe_malloc (sizeof (struct file_name_list));
6366 dirtmp->next = 0; /* New one goes on the end */
6367 dirtmp->control_macro = 0;
6368 dirtmp->c_system_include_path = 0;
6370 fatal ("Directory name missing after `-iwithprefix' option");
6372 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6373 + strlen (prefix) + 1);
6374 strcpy (dirtmp->fname, prefix);
6375 strcat (dirtmp->fname, argv[++i]);
6376 dirtmp->got_name_map = 0;
6378 if (opts->after_include == 0)
6379 opts->after_include = dirtmp;
6381 opts->last_after_include->next = dirtmp;
6382 opts->last_after_include = dirtmp; /* Tail follows the last one */
6384 /* Add directory to main path for includes,
6385 with the default prefix at the front of its name. */
6386 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6387 struct file_name_list *dirtmp;
6390 if (opts->include_prefix != 0)
6391 prefix = opts->include_prefix;
6393 prefix = savestring (GCC_INCLUDE_DIR);
6394 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6395 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6396 prefix[strlen (prefix) - 7] = 0;
6399 dirtmp = (struct file_name_list *)
6400 Safe_malloc (sizeof (struct file_name_list));
6401 dirtmp->next = 0; /* New one goes on the end */
6402 dirtmp->control_macro = 0;
6403 dirtmp->c_system_include_path = 0;
6405 fatal ("Directory name missing after `-iwithprefixbefore' option");
6407 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6408 + strlen (prefix) + 1);
6409 strcpy (dirtmp->fname, prefix);
6410 strcat (dirtmp->fname, argv[++i]);
6411 dirtmp->got_name_map = 0;
6413 append_include_chain (pfile, dirtmp, dirtmp);
6415 /* Add directory to end of path for includes. */
6416 if (!strcmp (argv[i], "-idirafter")) {
6417 struct file_name_list *dirtmp;
6419 dirtmp = (struct file_name_list *)
6420 Safe_malloc (sizeof (struct file_name_list));
6421 dirtmp->next = 0; /* New one goes on the end */
6422 dirtmp->control_macro = 0;
6423 dirtmp->c_system_include_path = 0;
6425 fatal ("Directory name missing after `-idirafter' option");
6427 dirtmp->fname = argv[++i];
6428 dirtmp->got_name_map = 0;
6430 if (opts->after_include == 0)
6431 opts->after_include = dirtmp;
6433 opts->last_after_include->next = dirtmp;
6434 opts->last_after_include = dirtmp; /* Tail follows the last one */
6439 if (opts->out_fname != NULL)
6440 fatal ("Output filename specified twice");
6442 fatal ("Filename missing after -o option");
6443 opts->out_fname = argv[++i];
6444 if (!strcmp (opts->out_fname, "-"))
6445 opts->out_fname = "";
6449 if (!strcmp (argv[i], "-pedantic"))
6450 CPP_PEDANTIC (pfile) = 1;
6451 else if (!strcmp (argv[i], "-pedantic-errors")) {
6452 CPP_PEDANTIC (pfile) = 1;
6453 opts->pedantic_errors = 1;
6456 else if (!strcmp (argv[i], "-pcp")) {
6457 char *pcp_fname = argv[++i];
6459 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6460 ? fopen (pcp_fname, "w")
6461 : fdopen (dup (fileno (stdout)), "w"));
6462 if (pcp_outfile == 0)
6463 cpp_pfatal_with_name (pfile, pcp_fname);
6470 if (!strcmp (argv[i], "-traditional")) {
6471 opts->traditional = 1;
6472 if (opts->dollars_in_ident > 0)
6473 opts->dollars_in_ident = 1;
6474 } else if (!strcmp (argv[i], "-trigraphs")) {
6476 opts->no_trigraphs = 0;
6481 if (! strcmp (argv[i], "-lang-c"))
6482 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6483 if (! strcmp (argv[i], "-lang-c++"))
6484 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6485 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6486 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6487 if (! strcmp (argv[i], "-lang-objc"))
6488 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6489 if (! strcmp (argv[i], "-lang-objc++"))
6490 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6491 if (! strcmp (argv[i], "-lang-asm"))
6493 if (! strcmp (argv[i], "-lint"))
6495 if (! strcmp (argv[i], "-lang-chill"))
6496 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6497 opts->traditional = 1, opts->no_trigraphs = 1;
6501 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6505 opts->inhibit_warnings = 1;
6509 if (!strcmp (argv[i], "-Wtrigraphs"))
6510 opts->warn_trigraphs = 1;
6511 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6512 opts->warn_trigraphs = 0;
6513 else if (!strcmp (argv[i], "-Wcomment"))
6514 opts->warn_comments = 1;
6515 else if (!strcmp (argv[i], "-Wno-comment"))
6516 opts->warn_comments = 0;
6517 else if (!strcmp (argv[i], "-Wcomments"))
6518 opts->warn_comments = 1;
6519 else if (!strcmp (argv[i], "-Wno-comments"))
6520 opts->warn_comments = 0;
6521 else if (!strcmp (argv[i], "-Wtraditional"))
6522 opts->warn_stringify = 1;
6523 else if (!strcmp (argv[i], "-Wno-traditional"))
6524 opts->warn_stringify = 0;
6525 else if (!strcmp (argv[i], "-Wimport"))
6526 opts->warn_import = 1;
6527 else if (!strcmp (argv[i], "-Wno-import"))
6528 opts->warn_import = 0;
6529 else if (!strcmp (argv[i], "-Werror"))
6530 opts->warnings_are_errors = 1;
6531 else if (!strcmp (argv[i], "-Wno-error"))
6532 opts->warnings_are_errors = 0;
6533 else if (!strcmp (argv[i], "-Wall"))
6535 opts->warn_trigraphs = 1;
6536 opts->warn_comments = 1;
6541 /* The style of the choices here is a bit mixed.
6542 The chosen scheme is a hybrid of keeping all options in one string
6543 and specifying each option in a separate argument:
6544 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6545 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6546 -M[M][G][D file]. This is awkward to handle in specs, and is not
6548 /* ??? -MG must be specified in addition to one of -M or -MM.
6549 This can be relaxed in the future without breaking anything.
6550 The converse isn't true. */
6552 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6553 if (!strcmp (argv[i], "-MG"))
6555 opts->print_deps_missing_files = 1;
6558 if (!strcmp (argv[i], "-M"))
6559 opts->print_deps = 2;
6560 else if (!strcmp (argv[i], "-MM"))
6561 opts->print_deps = 1;
6562 else if (!strcmp (argv[i], "-MD"))
6563 opts->print_deps = 2;
6564 else if (!strcmp (argv[i], "-MMD"))
6565 opts->print_deps = 1;
6566 /* For -MD and -MMD options, write deps on file named by next arg. */
6567 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6570 fatal ("Filename missing after %s option", argv[i]);
6571 opts->deps_file = argv[++i];
6575 /* For -M and -MM, write deps on standard output
6576 and suppress the usual output. */
6577 opts->no_output = 1;
6583 char *p = argv[i] + 2;
6585 while ((c = *p++) != 0) {
6586 /* Arg to -d specifies what parts of macros to dump */
6589 opts->dump_macros = dump_only;
6590 opts->no_output = 1;
6593 opts->dump_macros = dump_names;
6596 opts->dump_macros = dump_definitions;
6604 if (argv[i][2] == '3')
6605 opts->debug_output = 1;
6609 fprintf (stderr, "GNU CPP version %s", version_string);
6610 #ifdef TARGET_VERSION
6613 fprintf (stderr, "\n");
6614 // opts->verbose = 1;
6618 opts->print_include_names = 1;
6622 if (argv[i][2] != 0)
6623 push_pending (pfile, "-D", argv[i] + 2);
6624 else if (i + 1 == argc)
6625 fatal ("Macro name missing after -D option");
6627 i++, push_pending (pfile, "-D", argv[i]);
6634 if (argv[i][2] != 0)
6636 else if (i + 1 == argc)
6637 fatal ("Assertion missing after -A option");
6641 if (!strcmp (p, "-")) {
6642 struct cpp_pending **ptr;
6643 /* -A- eliminates all predefined macros and assertions.
6644 Let's include also any that were specified earlier
6645 on the command line. That way we can get rid of any
6646 that were passed automatically in from GCC. */
6648 opts->inhibit_predefs = 1;
6649 for (ptr = &opts->pending; *ptr != NULL; )
6651 struct cpp_pending *pend = *ptr;
6652 if (pend->cmd && pend->cmd[0] == '-'
6653 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6662 push_pending (pfile, "-A", p);
6667 case 'U': /* JF #undef something */
6668 if (argv[i][2] != 0)
6669 push_pending (pfile, "-U", argv[i] + 2);
6670 else if (i + 1 == argc)
6671 fatal ("Macro name missing after -U option");
6673 push_pending (pfile, "-U", argv[i+1]), i++;
6677 opts->put_out_comments = 1;
6680 case 'E': /* -E comes from cc -E; ignore it. */
6684 opts->no_line_commands = 1;
6687 case '$': /* Don't include $ in identifiers. */
6688 opts->dollars_in_ident = 0;
6691 case 'I': /* Add directory to path for includes. */
6693 struct file_name_list *dirtmp;
6695 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6696 && !strcmp (argv[i] + 2, "-")) {
6697 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6698 /* Don't use any preceding -I directories for #include <...>. */
6699 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6702 dirtmp = (struct file_name_list *)
6703 Safe_malloc (sizeof (struct file_name_list));
6704 dirtmp->next = 0; /* New one goes on the end */
6705 dirtmp->control_macro = 0;
6706 dirtmp->c_system_include_path = 0;
6707 if (argv[i][2] != 0)
6708 dirtmp->fname = argv[i] + 2;
6709 else if (i + 1 == argc)
6710 fatal ("Directory name missing after -I option");
6712 dirtmp->fname = argv[++i];
6713 dirtmp->got_name_map = 0;
6714 append_include_chain (pfile, dirtmp, dirtmp);
6720 if (!strcmp (argv[i], "-nostdinc"))
6721 /* -nostdinc causes no default include directories.
6722 You must specify all include-file directories with -I. */
6723 opts->no_standard_includes = 1;
6724 else if (!strcmp (argv[i], "-nostdinc++"))
6725 /* -nostdinc++ causes no default C++-specific include directories. */
6726 opts->no_standard_cplusplus_includes = 1;
6728 else if (!strcmp (argv[i], "-noprecomp"))
6734 /* Sun compiler passes undocumented switch "-undef".
6735 Let's assume it means to inhibit the predefined symbols. */
6736 opts->inhibit_predefs = 1;
6739 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6740 if (opts->in_fname == NULL) {
6741 opts->in_fname = "";
6743 } else if (opts->out_fname == NULL) {
6744 opts->out_fname = "";
6746 } /* else fall through into error */
6760 struct cpp_options *opts = CPP_OPTIONS (pfile);
6762 if (opts->print_deps)
6764 /* Stream on which to print the dependency information. */
6767 /* Don't actually write the deps file if compilation has failed. */
6768 if (pfile->errors == 0)
6770 char *deps_mode = opts->print_deps_append ? "a" : "w";
6771 if (opts->deps_file == 0)
6772 deps_stream = stdout;
6773 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6774 cpp_pfatal_with_name (pfile, opts->deps_file);
6775 fputs (pfile->deps_buffer, deps_stream);
6776 putc ('\n', deps_stream);
6777 if (opts->deps_file)
6779 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6780 fatal ("I/O error on output");
6786 /* Free resources used by PFILE. */
6793 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6794 cpp_pop_buffer (pfile);
6796 if (pfile->token_buffer)
6798 free (pfile->token_buffer);
6799 pfile->token_buffer = NULL;
6802 if (pfile->deps_buffer)
6804 free (pfile->deps_buffer);
6805 pfile->deps_buffer = NULL;
6806 pfile->deps_allocated_size = 0;
6809 while (pfile->if_stack)
6811 IF_STACK_FRAME *temp = pfile->if_stack;
6812 pfile->if_stack = temp->next;
6816 while (pfile->dont_repeat_files)
6818 struct file_name_list *temp = pfile->dont_repeat_files;
6819 pfile->dont_repeat_files = temp->next;
6824 while (pfile->all_include_files)
6826 struct file_name_list *temp = pfile->all_include_files;
6827 pfile->all_include_files = temp->next;
6832 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6834 register struct import_file *imp = pfile->import_hash_table[i];
6837 struct import_file *next = imp->next;
6842 pfile->import_hash_table[i] = 0;
6845 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6847 while (pfile->assertion_hashtab[i])
6848 delete_assertion (pfile->assertion_hashtab[i]);
6851 cpp_hash_cleanup (pfile);
6857 struct directive *keyword,
6858 U_CHAR *buf, U_CHAR *limit)
6860 long symstart; /* remember where symbol name starts */
6862 int sym_length; /* and how long it is */
6863 struct arglist *tokens = NULL;
6865 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6866 && !CPP_BUFFER (pfile)->system_header_p)
6867 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6869 cpp_skip_hspace (pfile);
6870 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6871 parse_name (pfile, GETC());
6872 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6875 cpp_skip_hspace (pfile);
6876 if (PEEKC() != '(') {
6877 cpp_error (pfile, "missing token-sequence in `#assert'");
6883 tokens = read_token_list (pfile, &error_flag);
6887 cpp_error (pfile, "empty token-sequence in `#assert'");
6890 cpp_skip_hspace (pfile);
6892 if (c != EOF && c != '\n')
6893 cpp_pedwarn (pfile, "junk at end of `#assert'");
6894 skip_rest_of_line (pfile);
6897 /* If this name isn't already an assertion name, make it one.
6898 Error if it was already in use in some other way. */
6901 ASSERTION_HASHNODE *hp;
6902 U_CHAR *symname = pfile->token_buffer + symstart;
6903 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6904 struct tokenlist_list *value
6905 = (struct tokenlist_list *) Safe_malloc (sizeof (struct tokenlist_list));
6907 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6909 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6910 cpp_error (pfile, "`defined' redefined as assertion");
6911 hp = assertion_install (pfile, symname, sym_length, hashcode);
6914 /* Add the spec'd token-sequence to the list of such. */
6915 value->tokens = tokens;
6916 value->next = hp->value;
6919 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6922 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6923 skip_rest_of_line (pfile);
6930 struct directive *keyword,
6931 U_CHAR *buf, U_CHAR *limit)
6933 long symstart; /* remember where symbol name starts */
6934 int sym_length; /* and how long it is */
6937 struct arglist *tokens = NULL;
6938 int tokens_specified = 0;
6940 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6941 && !CPP_BUFFER (pfile)->system_header_p)
6942 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6944 cpp_skip_hspace (pfile);
6946 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6947 parse_name (pfile, GETC());
6948 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6951 cpp_skip_hspace (pfile);
6952 if (PEEKC() == '(') {
6955 tokens = read_token_list (pfile, &error_flag);
6959 cpp_error (pfile, "empty token list in `#unassert'");
6963 tokens_specified = 1;
6966 cpp_skip_hspace (pfile);
6968 if (c != EOF && c != '\n')
6969 cpp_error (pfile, "junk at end of `#unassert'");
6970 skip_rest_of_line (pfile);
6973 ASSERTION_HASHNODE *hp;
6974 U_CHAR *symname = pfile->token_buffer + symstart;
6975 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6976 struct tokenlist_list *tail, *prev;
6978 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6982 /* If no token list was specified, then eliminate this assertion
6984 if (! tokens_specified)
6985 delete_assertion (hp);
6987 /* If a list of tokens was given, then delete any matching list. */
6992 struct tokenlist_list *next = tail->next;
6993 if (compare_token_lists (tail->tokens, tokens)) {
6997 hp->value = tail->next;
6998 free_token_list (tail->tokens);
7008 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7011 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7012 skip_rest_of_line (pfile);
7016 /* Test whether there is an assertion named NAME
7017 and optionally whether it has an asserted token list TOKENS.
7018 NAME is not null terminated; its length is SYM_LENGTH.
7019 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7026 int tokens_specified,
7027 struct arglist *tokens)
7029 ASSERTION_HASHNODE *hp;
7030 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7032 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7033 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7035 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7037 /* It is not an assertion; just return false. */
7040 /* If no token list was specified, then value is 1. */
7041 if (! tokens_specified)
7045 struct tokenlist_list *tail;
7049 /* If a list of tokens was given,
7050 then succeed if the assertion records a matching list. */
7053 if (compare_token_lists (tail->tokens, tokens))
7058 /* Fail if the assertion has no matching list. */
7063 /* Compare two lists of tokens for equality including order of tokens. */
7066 compare_token_lists (
7067 struct arglist *l1, struct arglist *l2 )
7070 if (l1->length != l2->length)
7072 if (strncmp (l1->name, l2->name, l1->length))
7078 /* Succeed if both lists end at the same time. */
7083 reverse_token_list (
7084 struct arglist *tokens)
7086 register struct arglist *prev = 0, *this, *next;
7087 for (this = tokens; this; this = next)
7096 /* Read a space-separated list of tokens ending in a close parenthesis.
7097 Return a list of strings, in the order they were written.
7098 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7100 static struct arglist *
7105 struct arglist *token_ptrs = 0;
7110 FORWARD (1); /* Skip '(' */
7112 /* Loop over the assertion value tokens. */
7115 struct arglist *temp;
7116 long name_written = CPP_WRITTEN (pfile);
7117 int eofp = 0; int c;
7119 cpp_skip_hspace (pfile);
7123 /* Find the end of the token. */
7126 CPP_PUTC (pfile, c);
7134 CPP_PUTC (pfile, c);
7136 else if (c == '"' || c == '\'')
7139 cpp_get_token (pfile);
7145 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7146 && c != '"' && c != '\'')
7148 CPP_PUTC (pfile, c);
7151 if (c != EOF) FORWARD(-1);
7154 length = CPP_WRITTEN (pfile) - name_written;
7155 temp = (struct arglist *)
7156 Safe_malloc (sizeof (struct arglist) + length + 1);
7157 temp->name = (U_CHAR *) (temp + 1);
7158 bcopy ((char *) (pfile->token_buffer + name_written),
7159 (char *) temp->name, length);
7160 temp->name[length] = 0;
7161 temp->next = token_ptrs;
7163 temp->length = length;
7165 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7167 if (c == EOF || c == '\n')
7170 "unterminated token sequence following `#' operator");
7175 /* We accumulated the names in reverse order.
7176 Now reverse them to get the proper order. */
7177 return reverse_token_list (token_ptrs);
7182 struct arglist *tokens)
7185 struct arglist *next = tokens->next;
7186 free (tokens->name);
7192 /* Get the file-mode and data size of the file open on FD
7193 and store them in *MODE_POINTER and *SIZE_POINTER. */
7196 file_size_and_mode (
7199 long int *size_pointer)
7203 if (fstat (fd, &sbuf) < 0) return (-1);
7204 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7205 if (size_pointer) *size_pointer = sbuf.st_size;
7209 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7210 retrying if necessary. Return a negative value if an error occurs,
7211 otherwise return the actual number of bytes read,
7212 which must be LEN unless end-of-file was reached. */
7222 int nchars = read (desc, ptr, left);
7243 unsigned size = strlen (input);
7244 char *output = Safe_malloc (size + 1);
7245 strcpy (output, input);
7249 /* Initialize PMARK to remember the current position of PFILE. */
7252 struct parse_marker *pmark,
7255 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7256 pmark->next = pbuf->marks;
7257 pbuf->marks = pmark;
7259 pmark->position = pbuf->cur - pbuf->buf;
7262 /* Cleanup PMARK - we no longer need it. */
7265 struct parse_marker *pmark)
7267 struct parse_marker **pp = &pmark->buf->marks;
7268 for (; ; pp = &(*pp)->next) {
7269 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7270 if (*pp == pmark) break;
7275 /* Backup the current position of PFILE to that saved in PMARK. */
7279 struct parse_marker *pmark,
7282 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7283 if (pbuf != pmark->buf)
7284 fatal ("internal error %s", "parse_goto_mark");
7285 pbuf->cur = pbuf->buf + pmark->position;
7288 /* Reset PMARK to point to the current position of PFILE. (Same
7289 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7293 struct parse_marker *pmark,
7296 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7297 if (pbuf != pmark->buf)
7298 fatal ("internal error %s", "parse_move_mark");
7299 pmark->position = pbuf->cur - pbuf->buf;
7303 cpp_read_check_assertion (
7306 int name_start = CPP_WRITTEN (pfile);
7307 int name_length, name_written;
7309 FORWARD (1); /* Skip '#' */
7310 cpp_skip_hspace (pfile);
7311 parse_name (pfile, GETC ());
7312 name_written = CPP_WRITTEN (pfile);
7313 name_length = name_written - name_start;
7314 cpp_skip_hspace (pfile);
7315 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7318 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7319 result = check_assertion (pfile,
7320 pfile->token_buffer + name_start, name_length,
7324 result = check_assertion (pfile,
7325 pfile->token_buffer + name_start, name_length,
7327 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7332 cpp_print_file_and_line (pfile)
7335 cpp_buffer *ip = cpp_file_buffer (pfile);
7340 cpp_buf_line_and_col (ip, &line, &col);
7341 cpp_file_line_for_message (pfile, ip->nominal_fname,
7342 line, pfile->show_column ? col : -1);
7347 cpp_error (pfile, msg, arg1, arg2, arg3)
7350 char *arg1, *arg2, *arg3;
7352 cpp_print_containing_files (pfile);
7353 cpp_print_file_and_line (pfile);
7354 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7357 /* Print error message but don't count it. */
7360 cpp_warning (pfile, msg, arg1, arg2, arg3)
7363 char *arg1, *arg2, *arg3;
7365 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7368 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7371 cpp_print_containing_files (pfile);
7372 cpp_print_file_and_line (pfile);
7373 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7376 /* Print an error message and maybe count it. */
7379 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7382 char *arg1, *arg2, *arg3;
7384 if (CPP_OPTIONS (pfile)->pedantic_errors)
7385 cpp_error (pfile, msg, arg1, arg2, arg3);
7387 cpp_warning (pfile, msg, arg1, arg2, arg3);
7391 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7395 char *arg1, *arg2, *arg3;
7398 cpp_buffer *ip = cpp_file_buffer (pfile);
7400 cpp_print_containing_files (pfile);
7403 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7405 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7409 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7413 char *arg1, *arg2, *arg3;
7418 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7421 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7424 cpp_print_containing_files (pfile);
7426 ip = cpp_file_buffer (pfile);
7429 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7431 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7435 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7439 char *arg1, *arg2, *arg3;
7441 if (CPP_OPTIONS (pfile)->pedantic_errors)
7442 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7444 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7447 /* Report a warning (or an error if pedantic_errors)
7448 giving specified file name and line number, not current. */
7451 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7456 char *arg1, *arg2, *arg3;
7458 if (!CPP_OPTIONS (pfile)->pedantic_errors
7459 && CPP_OPTIONS (pfile)->inhibit_warnings)
7462 cpp_file_line_for_message (pfile, file, line, -1);
7463 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7464 msg, arg1, arg2, arg3);
7467 /* This defines "errno" properly for VMS, and gives us EACCES. */
7474 #ifndef HAVE_STRERROR
7475 extern int sys_nerr;
7477 extern const char *const sys_errlist[];
7480 extern char *sys_errlist[];
7483 #else /* HAVE_STRERROR */
7487 char *strerror (int,...);
7491 * my_strerror - return the descriptive text associated with an `errno' code.
7495 my_strerror (errnum)
7501 #ifndef HAVE_STRERROR
7502 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7504 result = strerror (errnum);
7507 /* VAXCRTL's strerror() takes an optional second argument, which only
7508 matters when the first argument is EVMSERR. However, it's simplest
7509 just to pass it unconditionally. `vaxc$errno' is declared in
7510 <errno.h>, and maintained by the library in parallel with `errno'.
7511 We assume that caller's `errnum' either matches the last setting of
7512 `errno' by the library or else does not have the value `EVMSERR'. */
7514 result = strerror (errnum, vaxc$errno);
7518 result = "undocumented I/O error";
7523 /* Error including a message from `errno'. */
7526 cpp_error_from_errno (
7533 cpp_buffer *ip = cpp_file_buffer (pfile);
7535 cpp_print_containing_files (pfile);
7538 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7540 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno),NULL);
7544 cpp_perror_with_name (
7548 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7552 * No pre-compiled header file support.
7554 * Possibly different enum token codes for each C/C++ token.
7556 * Should clean up remaining directives to that do_XXX functions
7557 * only take two arguments and all have command_reads_line.
7559 * Find and cleanup remaining uses of static variables,
7561 * Support for trigraphs.
7563 * Support -dM flag (dump_all_macros).
7565 * Support for_lint flag.