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 *xmalloc ();
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*)
562 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
563 CPP_SET_WRITTEN (pfile, old_written);
568 * process a given definition string, for initialization
569 * If STR is just an identifier, define it with value 1.
570 * If STR has anything after the identifier, then it should
571 * be identifier=definition.
585 cpp_error (pfile, "malformed option `-D %s'", str);
588 while (is_idchar[*++p])
592 buf = (U_CHAR *) alloca (p - buf + 4);
593 strcpy ((char *)buf, str);
594 strcat ((char *)buf, " 1");
598 cpp_error (pfile, "malformed option `-D %s'", str);
604 /* Copy the entire option so we can modify it. */
605 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
606 strncpy (buf, str, p - str);
607 /* Change the = to a space. */
609 /* Scan for any backslash-newline and remove it. */
614 if (*p == '\\' && p[1] == '\n')
622 do_define (pfile, NULL, buf, buf + strlen (buf));
625 /* Process the string STR as if it appeared as the body of a #assert.
626 OPTION is the option name for which STR was the argument. */
635 struct directive *kt;
638 /* Copy the entire option so we can modify it. */
639 buf = (U_CHAR *) alloca (strlen (str) + 1);
640 strcpy ((char *) buf, str);
641 /* Scan for any backslash-newline and remove it. */
645 if (*p == '\\' && p[1] == '\n')
654 if (!is_idstart[*p]) {
655 cpp_error (pfile, "malformed option `%s %s'", option, str);
658 while (is_idchar[*++p])
660 while (*p == ' ' || *p == '\t') p++;
661 if (! (*p == 0 || *p == '(')) {
662 cpp_error (pfile, "malformed option `%s %s'", option, str);
666 ip = cpp_push_buffer (pfile, buf, strlen (buf));
667 do_assert (pfile, NULL, NULL, NULL);
668 cpp_pop_buffer (pfile);
671 /* Append a chain of `struct file_name_list's
672 to the end of the main include chain.
673 FIRST is the beginning of the chain to append, and LAST is the end. */
676 append_include_chain (
678 struct file_name_list *first,struct file_name_list *last)
680 struct cpp_options *opts = CPP_OPTIONS (pfile);
681 struct file_name_list *dir;
686 if (opts->include == 0)
687 opts->include = first;
689 opts->last_include->next = first;
691 if (opts->first_bracket_include == 0)
692 opts->first_bracket_include = first;
694 for (dir = first; ; dir = dir->next) {
695 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
696 if (len > pfile->max_include_len)
697 pfile->max_include_len = len;
703 opts->last_include = last;
706 /* Add output to `deps_buffer' for the -M switch.
707 STRING points to the text to be output.
708 SPACER is ':' for targets, ' ' for dependencies, zero for text
709 to be inserted literally. */
717 int size = strlen (string);
722 #ifndef MAX_OUTPUT_COLUMNS
723 #define MAX_OUTPUT_COLUMNS 72
726 && pfile->deps_column > 0
727 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
729 deps_output (pfile, " \\\n ", 0);
730 pfile->deps_column = 0;
733 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
735 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
736 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
737 pfile->deps_allocated_size);
739 if (spacer == ' ' && pfile->deps_column > 0)
740 pfile->deps_buffer[pfile->deps_size++] = ' ';
741 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
742 pfile->deps_size += size;
743 pfile->deps_column += size;
745 pfile->deps_buffer[pfile->deps_size++] = ':';
746 pfile->deps_buffer[pfile->deps_size] = 0;
749 /* Given a colon-separated list of file names PATH,
750 add all the names to the search path for include files. */
765 struct file_name_list *dirtmp;
767 /* Find the end of this name. */
768 while (*q != 0 && *q != PATH_SEPARATOR) q++;
770 /* An empty name in the path stands for the current directory. */
771 name = (char *) xmalloc (2);
775 /* Otherwise use the directory that is named. */
776 name = (char *) xmalloc (q - p + 1);
777 bcopy (p, name, q - p);
781 dirtmp = (struct file_name_list *)
782 xmalloc (sizeof (struct file_name_list));
783 dirtmp->next = 0; /* New one goes on the end */
784 dirtmp->control_macro = 0;
785 dirtmp->c_system_include_path = 0;
786 dirtmp->fname = name;
787 dirtmp->got_name_map = 0;
788 append_include_chain (pfile, dirtmp, dirtmp);
790 /* Advance past this name. */
794 /* Skip the colon. */
801 struct cpp_options *opts)
803 bzero ((char *) opts, sizeof *opts);
804 opts->in_fname = NULL;
805 opts->out_fname = NULL;
807 /* Initialize is_idchar to allow $. */
808 opts->dollars_in_ident = 1;
809 initialize_char_syntax (opts);
810 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
812 opts->no_line_commands = 0;
813 opts->no_trigraphs = 1;
814 opts->put_out_comments = 0;
815 opts->print_include_names = 0;
816 opts->dump_macros = dump_none;
819 opts->cplusplus_comments = 0;
826 opts->pedantic_errors = 0;
827 opts->inhibit_warnings = 0;
828 opts->warn_comments = 0;
829 opts->warn_import = 1;
830 opts->warnings_are_errors = 0;
853 HASHNODE *macro = (HASHNODE*)pbuf->data;
854 if (macro->type == T_DISABLED)
855 macro->type = T_MACRO;
856 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
881 register U_CHAR *p = bp;
883 /* First count the backslash-newline pairs here. */
885 while (p[0] == '\\' && p[1] == '\n')
888 /* What follows the backslash-newlines is not embarrassing. */
890 if (*p != '/' && *p != '*')
893 /* Copy all potentially embarrassing characters
894 that follow the backslash-newline pairs
895 down to where the pairs originally started. */
897 while (*p == '*' || *p == '/')
900 /* Now write the same number of pairs after the embarrassing chars. */
908 /* Assuming we have read '/'.
909 If this is the start of a comment (followed by '*' or '/'),
910 skip to the end of the comment, and return ' '.
911 Return EOF if we reached the end of file before the end of the comment.
912 If not the start of a comment, return '/'. */
920 while (PEEKC() == '\\' && PEEKN(1) == '\n')
935 while (c == '\\' && PEEKC() == '\n')
939 FORWARD(1), c = GETC();
941 if (prev_c == '*' && c == '/')
943 if (c == '\n' && linep)
947 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
954 return ' '; /* Allow // to be terminated by EOF. */
955 while (c == '\\' && PEEKC() == '\n')
964 /* Don't consider final '\n' to be part of comment. */
974 /* Skip whitespace \-newline and comments. Does not macro-expand. */
986 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
987 cpp_pedwarn (pfile, "%s in preprocessing directive",
988 c == '\f' ? "formfeed" : "vertical tab");
994 c = skip_comment (pfile, NULL);
997 if (c == EOF || c == '/')
1000 else if (c == '\\' && PEEKN(1) == '\n') {
1003 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
1004 && is_hor_space[PEEKN(1)])
1010 /* Read the rest of the current line.
1011 The line is appended to PFILE's output buffer. */
1017 struct cpp_options *opts = CPP_OPTIONS (pfile);
1027 if (PEEKC() == '\n')
1034 goto scan_directive_token;
1038 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
1039 goto scan_directive_token;
1043 if (CPP_PEDANTIC (pfile))
1044 cpp_pedwarn (pfile, "%s in preprocessing directive",
1045 c == '\f' ? "formfeed" : "vertical tab");
1051 scan_directive_token:
1053 cpp_get_token (pfile);
1056 CPP_PUTC (pfile, c);
1059 CPP_NUL_TERMINATE (pfile);
1066 long old = CPP_WRITTEN (pfile);
1067 copy_rest_of_line (pfile);
1068 CPP_SET_WRITTEN (pfile, old);
1071 /* Handle a possible # directive.
1072 '#' has already been read. */
1078 register struct directive *kt;
1081 U_CHAR *ident, *line_end;
1082 long old_written = CPP_WRITTEN (pfile);
1084 cpp_skip_hspace (pfile);
1087 if (c >= '0' && c <= '9')
1089 /* Handle # followed by a line number. */
1090 if (CPP_PEDANTIC (pfile))
1091 cpp_pedwarn (pfile, "`#' followed by integer");
1092 do_line (pfile, NULL);
1093 goto done_a_directive;
1096 /* Now find the directive name. */
1097 CPP_PUTC (pfile, '#');
1098 parse_name (pfile, GETC());
1099 ident = pfile->token_buffer + old_written + 1;
1100 ident_length = CPP_PWRITTEN (pfile) - ident;
1101 if (ident_length == 0 && PEEKC() == '\n')
1103 /* A line of just `#' becomes blank. */
1104 goto done_a_directive;
1108 if (ident_length == 0 || !is_idstart[*ident]) {
1110 while (is_idchar[*p]) {
1111 if (*p < '0' || *p > '9')
1115 /* Avoid error for `###' and similar cases unless -pedantic. */
1117 while (*p == '#' || is_hor_space[*p]) p++;
1119 if (pedantic && !lang_asm)
1120 cpp_warning (pfile, "invalid preprocessor directive");
1126 cpp_error (pfile, "invalid preprocessor directive name");
1132 * Decode the keyword and call the appropriate expansion
1133 * routine, after moving the input pointer up to the next line.
1135 for (kt = directive_table; ; kt++) {
1136 if (kt->length <= 0)
1137 goto not_a_directive;
1138 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1142 if (! kt->command_reads_line)
1144 /* Nonzero means do not delete comments within the directive.
1145 #define needs this when -traditional. */
1146 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1147 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1148 CPP_OPTIONS (pfile)->put_out_comments = comments;
1149 after_ident = CPP_WRITTEN (pfile);
1150 copy_rest_of_line (pfile);
1151 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1154 /* For #pragma and #define, we may want to pass through the directive.
1155 Other directives may create output, but we don't want the directive
1156 itself out, so we pop it now. For example #include may write a
1157 command (see comment in do_include), and conditionals may emit
1158 #failed ... #endfailed stuff. But note that popping the buffer
1159 means the parameters to kt->func may point after pfile->limit
1160 so these parameters are invalid as soon as something gets appended
1161 to the token_buffer. */
1163 line_end = CPP_PWRITTEN (pfile);
1164 if (!kt->pass_thru && kt->type != T_DEFINE)
1165 CPP_SET_WRITTEN (pfile, old_written);
1167 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1169 || (kt->type == T_DEFINE
1170 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1172 /* Just leave the entire #define in the output stack. */
1174 else if (kt->type == T_DEFINE
1175 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1177 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1178 SKIP_WHITE_SPACE (p);
1179 while (is_idchar[*p]) p++;
1181 CPP_PUTC (pfile, '\n');
1183 else if (kt->type == T_DEFINE)
1184 CPP_SET_WRITTEN (pfile, old_written);
1192 /* Pass a directive through to the output file.
1193 BUF points to the contents of the directive, as a contiguous string.
1194 LIMIT points to the first character past the end of the directive.
1195 KEYWORD is the keyword-table entry for the directive. */
1198 pass_thru_directive (
1199 U_CHAR *buf, U_CHAR *limit,
1201 struct directive *keyword)
1203 register unsigned keyword_length = keyword->length;
1205 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1206 CPP_PUTC_Q (pfile, '#');
1207 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1208 if (limit != buf && buf[0] != ' ')
1209 CPP_PUTC_Q (pfile, ' ');
1210 CPP_PUTS_Q (pfile, buf, limit - buf);
1212 CPP_PUTS_Q (pfile, '\n');
1213 /* Count the line we have just made in the output,
1214 to get in sync properly. */
1219 /* The arglist structure is built by do_define to tell
1220 collect_definition where the argument names begin. That
1221 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1222 would contain pointers to the strings x, y, and z.
1223 Collect_definition would then build a DEFINITION node,
1224 with reflist nodes pointing to the places x, y, and z had
1225 appeared. So the arglist is just convenience data passed
1226 between these two routines. It is not kept around after
1227 the current #define has been processed and entered into the
1231 struct arglist *next;
1238 /* Read a replacement list for a macro with parameters.
1239 Build the DEFINITION structure.
1240 Reads characters of text starting at BUF until END.
1241 ARGLIST specifies the formal parameters to look for
1242 in the text of the definition; NARGS is the number of args
1243 in that list, or -1 for a macro name that wants no argument list.
1244 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1245 and NAMELEN is its length in characters.
1247 Note that comments, backslash-newlines, and leading white space
1248 have already been deleted from the argument. */
1253 U_CHAR *buf, U_CHAR *limit,
1255 struct arglist *arglist)
1258 register U_CHAR *p, *lastp, *exp_p;
1259 struct reflist *endpat = NULL;
1260 /* Pointer to first nonspace after last ## seen. */
1262 /* Pointer to first nonspace after last single-# seen. */
1263 U_CHAR *stringify = 0;
1265 int expected_delimiter = '\0';
1267 /* Scan thru the replacement list, ignoring comments and quoted
1268 strings, picking up on the macro calls. It does a linear search
1269 thru the arg list on every potential symbol. Profiling might say
1270 that something smarter should happen. */
1275 /* Find the beginning of the trailing whitespace. */
1277 while (p < limit && is_space[limit[-1]]) limit--;
1279 /* Allocate space for the text in the macro definition.
1280 Leading and trailing whitespace chars need 2 bytes each.
1281 Each other input char may or may not need 1 byte,
1282 so this is an upper bound. The extra 5 are for invented
1283 leading and trailing newline-marker and final null. */
1284 maxsize = (sizeof (DEFINITION)
1286 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1290 defn = (DEFINITION *) xcalloc (1, maxsize);
1292 defn->nargs = nargs;
1293 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1298 /* Add one initial space escape-marker to prevent accidental
1299 token-pasting (often removed by macroexpand). */
1303 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1304 cpp_error (pfile, "`##' at start of macro definition");
1308 /* Process the main body of the definition. */
1310 int skipped_arg = 0;
1311 register U_CHAR c = *p++;
1315 if (!CPP_TRADITIONAL (pfile)) {
1319 if (expected_delimiter != '\0') {
1320 if (c == expected_delimiter)
1321 expected_delimiter = '\0';
1323 expected_delimiter = c;
1327 if (p < limit && expected_delimiter) {
1328 /* In a string, backslash goes through
1329 and makes next char ordinary. */
1335 /* An '@' in a string or character constant stands for itself,
1336 and does not need to be escaped. */
1337 if (!expected_delimiter)
1342 /* # is ordinary inside a string. */
1343 if (expected_delimiter)
1345 if (p < limit && *p == '#') {
1346 /* ##: concatenate preceding and following tokens. */
1347 /* Take out the first #, discard preceding whitespace. */
1349 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1351 /* Skip the second #. */
1353 /* Discard following whitespace. */
1354 SKIP_WHITE_SPACE (p);
1357 cpp_error (pfile, "`##' at end of macro definition");
1358 } else if (nargs >= 0) {
1359 /* Single #: stringify following argument ref.
1360 Don't leave the # in the expansion. */
1362 SKIP_WHITE_SPACE (p);
1363 if (p == limit || ! is_idstart[*p])
1365 "`#' operator is not followed by a macro argument name");
1372 /* In -traditional mode, recognize arguments inside strings and
1373 and character constants, and ignore special properties of #.
1374 Arguments inside strings are considered "stringified", but no
1375 extra quote marks are supplied. */
1379 if (expected_delimiter != '\0') {
1380 if (c == expected_delimiter)
1381 expected_delimiter = '\0';
1383 expected_delimiter = c;
1387 /* Backslash quotes delimiters and itself, but not macro args. */
1388 if (expected_delimiter != 0 && p < limit
1389 && (*p == expected_delimiter || *p == '\\')) {
1396 if (expected_delimiter != '\0') /* No comments inside strings. */
1399 /* If we find a comment that wasn't removed by handle_directive,
1400 this must be -traditional. So replace the comment with
1404 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1407 /* Mark this as a concatenation-point, as if it had been ##. */
1411 else if (*p == '/') {
1412 /* A c++ comment. Discard to the end of line */
1420 /* Handle the start of a symbol. */
1421 if (is_idchar[c] && nargs > 0) {
1422 U_CHAR *id_beg = p - 1;
1426 while (p != limit && is_idchar[*p]) p++;
1427 id_len = p - id_beg;
1429 if (is_idstart[c]) {
1430 register struct arglist *arg;
1432 for (arg = arglist; arg != NULL; arg = arg->next) {
1433 struct reflist *tpat;
1435 if (arg->name[0] == c
1436 && arg->length == id_len
1437 && strncmp (arg->name, id_beg, id_len) == 0) {
1438 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1439 if (CPP_TRADITIONAL (pfile)) {
1440 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1444 "macro arg `%.*s' would be stringified with -traditional.",
1448 /* If ANSI, don't actually substitute inside a string. */
1449 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1451 /* make a pat node for this arg and append it to the end of
1453 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1455 tpat->raw_before = concat == id_beg;
1456 tpat->raw_after = 0;
1457 tpat->rest_args = arg->rest_args;
1458 tpat->stringify = (CPP_TRADITIONAL (pfile)
1459 ? expected_delimiter != '\0'
1460 : stringify == id_beg);
1463 defn->pattern = tpat;
1465 endpat->next = tpat;
1468 tpat->argno = arg->argno;
1469 tpat->nchars = exp_p - lastp;
1471 register U_CHAR *p1 = p;
1472 SKIP_WHITE_SPACE (p1);
1473 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1474 tpat->raw_after = 1;
1476 lastp = exp_p; /* place to start copying from next time */
1483 /* If this was not a macro arg, copy it into the expansion. */
1484 if (! skipped_arg) {
1485 register U_CHAR *lim1 = p;
1489 if (stringify == id_beg)
1491 "`#' operator should be followed by a macro argument name");
1496 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1498 /* If ANSI, put in a "@ " marker to prevent token pasting.
1499 But not if "inside a string" (which in ANSI mode
1500 happens only for -D option). */
1507 defn->length = exp_p - defn->expansion;
1509 /* Crash now if we overrun the allocated size. */
1510 if (defn->length + 1 > maxsize)
1514 /* This isn't worth the time it takes. */
1515 /* give back excess storage */
1516 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1523 * special extension string that can be added to the last macro argument to
1524 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1525 * #define wow(a, b...) process (b, a, b)
1526 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1527 * { wow (one, two); } -> { process (two, one, two); }
1528 * if this "rest_arg" is used with the concat token '##' and if it is not
1529 * supplied then the token attached to with ## will not be outputted. Ex:
1530 * #define wow (a, b...) process (b ## , a, ## b)
1531 * { wow (1, 2); } -> { process (2, 1, 2); }
1532 * { wow (one); } -> { process (one); {
1534 static char rest_extension[] = "...";
1535 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1537 /* Create a DEFINITION node from a #define directive. Arguments are
1538 as for do_define. */
1541 U_CHAR *buf, U_CHAR *limit,
1545 U_CHAR *bp; /* temp ptr into input buffer */
1546 U_CHAR *symname; /* remember where symbol name starts */
1547 int sym_length; /* and how long it is */
1550 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1552 int arglengths = 0; /* Accumulate lengths of arg names
1553 plus number of args. */
1555 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1559 while (is_hor_space[*bp])
1562 symname = bp; /* remember where it starts */
1564 sym_length = check_macro_name (pfile, bp, "macro");
1567 /* Lossage will occur if identifiers or control keywords are broken
1568 across lines using backslash. This is not the right place to take
1572 struct arglist *arg_ptrs = NULL;
1575 bp++; /* skip '(' */
1576 SKIP_WHITE_SPACE (bp);
1578 /* Loop over macro argument names. */
1579 while (*bp != ')') {
1580 struct arglist *temp;
1582 temp = (struct arglist *) alloca (sizeof (struct arglist));
1584 temp->next = arg_ptrs;
1585 temp->argno = argno++;
1586 temp->rest_args = 0;
1590 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1592 if (!is_idstart[*bp])
1593 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1595 /* Find the end of the arg name. */
1596 while (is_idchar[*bp]) {
1598 /* do we have a "special" rest-args extension here? */
1599 if (limit - bp > REST_EXTENSION_LENGTH &&
1600 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1602 temp->rest_args = 1;
1606 temp->length = bp - temp->name;
1608 bp += REST_EXTENSION_LENGTH;
1609 arglengths += temp->length + 2;
1610 SKIP_WHITE_SPACE (bp);
1611 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1612 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1617 SKIP_WHITE_SPACE (bp);
1620 cpp_error (pfile, "unterminated parameter list in `#define'");
1624 struct arglist *otemp;
1626 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1627 if (temp->length == otemp->length &&
1628 strncmp (temp->name, otemp->name, temp->length) == 0) {
1631 name = (U_CHAR *) alloca (temp->length + 1);
1632 (void) strncpy (name, temp->name, temp->length);
1633 name[temp->length] = '\0';
1635 "duplicate argument name `%s' in `#define'", name);
1641 ++bp; /* skip paren */
1642 SKIP_WHITE_SPACE (bp);
1643 /* now everything from bp before limit is the definition. */
1644 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1645 defn->rest_args = rest_args;
1647 /* Now set defn->args.argnames to the result of concatenating
1648 the argument names in reverse order
1649 with comma-space between them. */
1650 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1652 struct arglist *temp;
1654 for (temp = arg_ptrs; temp; temp = temp->next) {
1655 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1657 if (temp->next != 0) {
1658 defn->args.argnames[i++] = ',';
1659 defn->args.argnames[i++] = ' ';
1662 defn->args.argnames[i] = 0;
1665 /* Simple expansion or empty definition. */
1669 if (is_hor_space[*bp]) {
1671 SKIP_WHITE_SPACE (bp);
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 '\\': case ']': case '^': case '{':
1678 case '|': case '}': case '~':
1679 cpp_warning (pfile, "missing white space after `#define %.*s'",
1680 sym_length, symname);
1684 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1685 sym_length, symname);
1690 /* now everything from bp before limit is the definition. */
1691 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1692 defn->args.argnames = (U_CHAR *) "";
1698 /* OP is null if this is a predefinition */
1699 defn->predefined = predefinition;
1701 mdef.symnam = symname;
1702 mdef.symlen = sym_length;
1711 /* Check a purported macro name SYMNAME, and yield its length.
1712 USAGE is the kind of name this is intended for. */
1723 for (p = symname; is_idchar[*p]; p++)
1725 sym_length = p - symname;
1726 if (sym_length == 0)
1727 cpp_error (pfile, "invalid %s name", usage);
1728 else if (!is_idstart[*symname]) {
1729 U_CHAR *msg; /* what pain... */
1730 msg = (U_CHAR *) alloca (sym_length + 1);
1731 bcopy (symname, msg, sym_length);
1732 msg[sym_length] = 0;
1733 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1735 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1736 cpp_error (pfile, "invalid %s name `defined'", usage);
1742 * return zero if two DEFINITIONs are isomorphic
1746 DEFINITION *d1, DEFINITION *d2)
1748 register struct reflist *a1, *a2;
1749 register U_CHAR *p1 = d1->expansion;
1750 register U_CHAR *p2 = d2->expansion;
1753 if (d1->nargs != d2->nargs)
1755 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1757 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1758 a1 = a1->next, a2 = a2->next) {
1759 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1760 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1761 || a1->argno != a2->argno
1762 || a1->stringify != a2->stringify
1763 || a1->raw_before != a2->raw_before
1764 || a1->raw_after != a2->raw_after)
1772 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1773 p2, d2->length - (p2 - d2->expansion), 1))
1778 /* Return 1 if two parts of two macro definitions are effectively different.
1779 One of the parts starts at BEG1 and has LEN1 chars;
1780 the other has LEN2 chars at BEG2.
1781 Any sequence of whitespace matches any other sequence of whitespace.
1782 FIRST means these parts are the first of a macro definition;
1783 so ignore leading whitespace entirely.
1784 LAST means these parts are the last of a macro definition;
1785 so ignore trailing whitespace entirely. */
1790 U_CHAR *beg1, int len1,
1791 U_CHAR *beg2, int len2 ,
1794 register U_CHAR *end1 = beg1 + len1;
1795 register U_CHAR *end2 = beg2 + len2;
1797 while (beg1 != end1 && is_space[*beg1]) beg1++;
1798 while (beg2 != end2 && is_space[*beg2]) beg2++;
1801 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1802 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1804 while (beg1 != end1 && beg2 != end2) {
1805 if (is_space[*beg1] && is_space[*beg2]) {
1806 while (beg1 != end1 && is_space[*beg1]) beg1++;
1807 while (beg2 != end2 && is_space[*beg2]) beg2++;
1808 } else if (*beg1 == *beg2) {
1812 return (beg1 != end1) || (beg2 != end2);
1815 /* Process a #define command.
1816 BUF points to the contents of the #define command, as a contiguous string.
1817 LIMIT points to the first character past the end of the definition.
1818 KEYWORD is the keyword-table entry for #define,
1819 or NULL for a "predefined" macro. */
1824 struct directive *keyword,
1825 U_CHAR *buf, U_CHAR *limit)
1832 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1833 if (pcp_outfile && keyword)
1834 pass_thru_directive (buf, limit, pfile, keyword);
1837 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1841 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1843 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1846 /* Redefining a precompiled key is ok. */
1847 if (hp->type == T_PCSTRING)
1849 /* Redefining a macro is ok if the definitions are the same. */
1850 else if (hp->type == T_MACRO)
1851 ok = ! compare_defs (mdef.defn, hp->value.defn);
1852 /* Redefining a constant is ok with -D. */
1853 else if (hp->type == T_CONST)
1854 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1855 /* Print the warning if it's not ok. */
1858 U_CHAR *msg; /* what pain... */
1860 /* If we are passing through #define and #undef directives, do
1861 that for this re-definition now. */
1862 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1863 pass_thru_directive (buf, limit, pfile, keyword);
1865 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1867 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1868 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1869 cpp_pedwarn (pfile, msg);
1870 if (hp->type == T_MACRO)
1871 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1872 "this is the location of the previous definition");
1874 /* Replace the old definition. */
1876 hp->value.defn = mdef.defn;
1880 /* If we are passing through #define and #undef directives, do
1881 that for this new definition now. */
1882 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1883 pass_thru_directive (buf, limit, pfile, keyword);
1884 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1885 (char *) mdef.defn, hashcode);
1895 /* This structure represents one parsed argument in a macro call.
1896 `raw' points to the argument text as written (`raw_length' is its length).
1897 `expanded' points to the argument's macro-expansion
1898 (its length is `expand_length').
1899 `stringified_length' is the length the argument would have
1901 `use_count' is the number of times this macro arg is substituted
1902 into the macro. If the actual use count exceeds 10,
1903 the value stored is 10. */
1905 /* raw and expanded are relative to ARG_BASE */
1906 #define ARG_BASE ((pfile)->token_buffer)
1909 /* Strings relative to pfile->token_buffer */
1910 long raw, expanded, stringified;
1911 int raw_length, expand_length;
1912 int stringified_length;
1924 #ifdef STATIC_BUFFERS
1925 register cpp_buffer *buf = CPP_BUFFER (pfile);
1926 if (buf == pfile->buffer_stack)
1927 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1929 bzero ((char *) buf, sizeof (cpp_buffer));
1930 CPP_BUFFER (pfile) = buf;
1932 register cpp_buffer *buf = (cpp_buffer*) xmalloc (sizeof(cpp_buffer));
1933 bzero ((char *) buf, sizeof (cpp_buffer));
1934 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1935 CPP_BUFFER (pfile) = buf;
1937 buf->if_stack = pfile->if_stack;
1938 buf->cleanup = null_cleanup;
1939 buf->underflow = null_underflow;
1940 buf->buf = buf->cur = buffer;
1941 buf->alimit = buf->rlimit = buffer + length;
1947 cpp_pop_buffer (pfile)
1950 cpp_buffer *buf = CPP_BUFFER (pfile);
1951 #ifdef STATIC_BUFFERS
1952 (*buf->cleanup) (buf, pfile);
1953 return ++CPP_BUFFER (pfile);
1955 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1956 (*buf->cleanup) (buf, pfile);
1957 CPP_BUFFER (pfile) = next_buf;
1963 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1964 Pop the buffer when done. */
1970 cpp_buffer *buffer = CPP_BUFFER (pfile);
1973 enum cpp_token token = cpp_get_token (pfile);
1974 if (token == CPP_EOF) /* Should not happen ... */
1976 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1978 cpp_pop_buffer (pfile);
1985 * Rescan a string (which may have escape marks) into pfile's buffer.
1986 * Place the result in pfile->token_buffer.
1988 * The input is copied before it is scanned, so it is safe to pass
1989 * it something from the token_buffer that will get overwritten
1990 * (because it follows CPP_WRITTEN). This is used by do_include.
1994 cpp_expand_to_buffer (
1999 register cpp_buffer *ip;
2001 U_CHAR *limit = buf + length;
2004 int odepth = indepth;
2010 /* Set up the input on the input stack. */
2012 buf1 = (U_CHAR *) alloca (length + 1);
2014 register U_CHAR *p1 = buf;
2015 register U_CHAR *p2 = buf1;
2022 ip = cpp_push_buffer (pfile, buf1, length);
2023 ip->has_escapes = 1;
2025 ip->lineno = obuf.lineno = 1;
2028 /* Scan the input, create the output. */
2029 cpp_scan_buffer (pfile);
2032 if (indepth != odepth)
2036 CPP_NUL_TERMINATE (pfile);
2051 (*linep)++, (*colp) = 1;
2057 /* Move line_base forward, updating lineno and colno. */
2061 register cpp_buffer *pbuf)
2063 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2064 unsigned char *new_pos = pbuf->cur;
2065 register struct parse_marker *mark;
2066 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2068 if (pbuf->buf + mark->position < new_pos)
2069 new_pos = pbuf->buf + mark->position;
2071 pbuf->line_base += new_pos - old_pos;
2072 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2076 cpp_buf_line_and_col (
2077 register cpp_buffer *pbuf,
2078 long *linep,long *colp)
2085 *linep = pbuf->lineno;
2086 *colp = pbuf->colno;
2087 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2096 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2102 cpp_buffer *ip = CPP_BUFFER (pfile);
2104 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2105 if (ip->fname != NULL)
2112 register U_CHAR *buf,
2113 register U_CHAR *limit)
2115 register long count = 0;
2126 * write out a #line command, for instance, after an #include file.
2127 * If CONDITIONAL is nonzero, we can omit the #line if it would
2128 * appear to be a no-op, and we can output a few newlines instead
2129 * if we want to increase the line number by a small amount.
2130 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2134 output_line_command (
2137 enum file_change_code file_change)
2140 char *line_cmd_buf, *line_end;
2142 cpp_buffer *ip = CPP_BUFFER (pfile);
2144 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2148 update_position (ip);
2149 line = CPP_BUFFER (pfile)->lineno;
2150 col = CPP_BUFFER (pfile)->colno;
2151 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2153 if (CPP_OPTIONS (pfile)->no_line_commands)
2157 if (line == pfile->lineno)
2160 /* If the inherited line number is a little too small,
2161 output some newlines instead of a #line command. */
2162 if (line > pfile->lineno && line < pfile->lineno + 8) {
2163 CPP_RESERVE (pfile, 20);
2164 while (line > pfile->lineno) {
2165 CPP_PUTC_Q (pfile, '\n');
2173 /* Don't output a line number of 0 if we can help it. */
2174 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2175 && *ip->bufp == '\n') {
2181 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2183 static char sharp_line[] = "#line ";
2185 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2188 sprintf (CPP_PWRITTEN (pfile), "%d ", line+2);
2189 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2191 // modification for SDC51
2192 if (*ip->nominal_fname == '\0')
2193 quote_string (pfile,"standard input");
2195 quote_string (pfile, ip->nominal_fname);
2196 if (file_change != same_file) {
2197 CPP_PUTC_Q (pfile, ' ');
2198 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2200 /* Tell cc1 if following text comes from a system header file. */
2201 if (ip->system_header_p) {
2202 CPP_PUTC_Q (pfile, ' ');
2203 CPP_PUTC_Q (pfile, '3');
2205 #ifndef NO_IMPLICIT_EXTERN_C
2206 /* Tell cc1plus if following text should be treated as C. */
2207 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2208 CPP_PUTC_Q (pfile, ' ');
2209 CPP_PUTC_Q (pfile, '4');
2212 CPP_PUTC_Q (pfile, '\n');
2213 pfile->lineno = line;
2217 * Parse a macro argument and append the info on PFILE's token_buffer.
2218 * REST_ARGS means to absorb the rest of the args.
2219 * Return nonzero to indicate a syntax error.
2222 static enum cpp_token
2228 enum cpp_token token;
2229 long arg_start = CPP_WRITTEN (pfile);
2230 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2231 CPP_OPTIONS (pfile)->put_out_comments = 0;
2233 /* Try to parse as much of the argument as exists at this
2234 input stack level. */
2235 pfile->no_macro_expand++;
2238 token = cpp_get_token (pfile);
2244 /* If we've hit end of file, it's an error (reported by caller).
2245 Ditto if it's the end of cpp_expand_to_buffer text.
2246 If we've hit end of macro, just continue. */
2247 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2258 /* if we've returned to lowest level and
2259 we aren't absorbing all args */
2260 if (paren == 0 && rest_args == 0)
2264 /* Remove ',' or ')' from argument buffer. */
2265 CPP_ADJUST_WRITTEN (pfile, -1);
2272 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2273 pfile->no_macro_expand--;
2278 /* Turn newlines to spaces in the string of length LENGTH at START,
2279 except inside of string constants.
2280 The string is copied into itself with its beginning staying fixed. */
2287 register U_CHAR *ibp;
2288 register U_CHAR *obp;
2289 register U_CHAR *limit;
2293 limit = start + length;
2296 while (ibp < limit) {
2297 *obp++ = c = *ibp++;
2302 /* Notice and skip strings, so that we don't delete newlines in them. */
2305 while (ibp < limit) {
2306 *obp++ = c = *ibp++;
2309 if (c == '\n' && quotec == '\'')
2325 if (!pfile->timebuf) {
2326 time_t t = time ((time_t *)0);
2327 pfile->timebuf = localtime (&t);
2329 return pfile->timebuf;
2332 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2333 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2337 * expand things like __FILE__. Place the expansion into the output
2338 * buffer *without* rescanning.
2349 cpp_buffer *ip = NULL;
2352 int paren = 0; /* For special `defined' keyword */
2355 if (pcp_outfile && pcp_inside_if
2356 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2358 "Predefined macro `%s' used inside `#if' during precompilation",
2362 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2366 cpp_error (pfile, "cccp error: not in any file?!");
2367 return; /* the show must go on */
2369 if (ip->fname != NULL)
2379 if (hp->type == T_BASE_FILE)
2381 while (CPP_PREV_BUFFER (ip))
2382 ip = CPP_PREV_BUFFER (ip);
2384 string = ip->nominal_fname;
2388 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2389 quote_string (pfile, string);
2393 case T_INCLUDE_LEVEL:
2395 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2396 if (ip->fname != NULL)
2399 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2400 sprintf (buf, "%d", true_indepth - 1);
2404 buf = (char *) alloca (3 + strlen (version_string));
2405 sprintf (buf, "\"%s\"", version_string);
2408 #ifndef NO_BUILTIN_SIZE_TYPE
2414 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2415 case T_PTRDIFF_TYPE:
2421 buf = CPP_WCHAR_TYPE (pfile);
2424 case T_USER_LABEL_PREFIX_TYPE:
2425 buf = USER_LABEL_PREFIX;
2428 case T_REGISTER_PREFIX_TYPE:
2429 buf = REGISTER_PREFIX;
2433 buf = (char *) alloca (4 * sizeof (int));
2434 sprintf (buf, "%d", hp->value.ival);
2436 if (pcp_inside_if && pcp_outfile)
2437 /* Output a precondition for this macro use */
2438 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2444 long line = ip->lineno;
2445 long col = ip->colno;
2446 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2448 buf = (char *) alloca (10);
2449 sprintf (buf, "%d", line);
2455 buf = (char *) alloca (20);
2456 timebuf = timestamp (pfile);
2457 if (hp->type == T_DATE)
2458 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2459 timebuf->tm_mday, timebuf->tm_year + 1900);
2461 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2465 case T_SPEC_DEFINED:
2466 buf = " 0 "; /* Assume symbol is not defined */
2467 ip = CPP_BUFFER (pfile);
2468 SKIP_WHITE_SPACE (ip->cur);
2469 if (*ip->cur == '(')
2472 ip->cur++; /* Skip over the paren */
2473 SKIP_WHITE_SPACE (ip->cur);
2476 if (!is_idstart[*ip->cur])
2478 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2481 if (pcp_outfile && pcp_inside_if
2482 && (hp->type == T_CONST
2483 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2484 /* Output a precondition for this macro use. */
2485 fprintf (pcp_outfile, "#define %s\n", hp->name);
2491 if (pcp_outfile && pcp_inside_if)
2493 /* Output a precondition for this macro use */
2494 U_CHAR *cp = ip->bufp;
2495 fprintf (pcp_outfile, "#undef ");
2496 while (is_idchar[*cp]) /* Ick! */
2497 fputc (*cp++, pcp_outfile);
2498 putc ('\n', pcp_outfile);
2501 while (is_idchar[*ip->cur])
2503 SKIP_WHITE_SPACE (ip->cur);
2506 if (*ip->cur != ')')
2514 cpp_error (pfile, "`defined' without an identifier");
2518 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2522 CPP_RESERVE (pfile, len + 1);
2523 CPP_PUTS_Q (pfile, buf, len);
2524 CPP_NUL_TERMINATE_Q (pfile);
2529 /* Initialize the built-in macros. */
2532 initialize_builtins (
2535 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2536 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2537 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2538 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2539 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2540 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2541 #ifndef NO_BUILTIN_SIZE_TYPE
2542 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2544 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2545 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2547 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2548 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2549 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2550 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2551 if (!CPP_TRADITIONAL (pfile))
2552 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2553 if (CPP_OPTIONS (pfile)->objc)
2554 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2555 /* This is supplied using a -D by the compiler driver
2556 so that it is present only when truly compiling with GNU C. */
2557 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2559 if (CPP_OPTIONS (pfile)->debug_output)
2561 char directive[2048];
2562 register struct directive *dp = &directive_table[0];
2563 struct tm *timebuf = timestamp (pfile);
2564 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2566 while (CPP_PREV_BUFFER (pbuffer))
2567 pbuffer = CPP_PREV_BUFFER (pbuffer);
2568 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2569 pbuffer->nominal_fname);
2570 output_line_command (pfile, 0, same_file);
2571 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2573 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2574 output_line_command (pfile, 0, same_file);
2575 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2577 #ifndef NO_BUILTIN_SIZE_TYPE
2578 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2579 output_line_command (pfile, 0, same_file);
2580 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2583 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2584 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2585 output_line_command (pfile, 0, same_file);
2586 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2589 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2590 output_line_command (pfile, 0, same_file);
2591 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2593 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2594 monthnames[timebuf->tm_mon],
2595 timebuf->tm_mday, timebuf->tm_year + 1900);
2596 output_line_command (pfile, 0, same_file);
2597 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2599 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2600 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2601 output_line_command (pfile, 0, same_file);
2602 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2604 if (!CPP_TRADITIONAL (pfile))
2606 sprintf (directive, " __STDC__ 1");
2607 output_line_command (pfile, 0, same_file);
2608 pass_thru_directive (directive, &directive[strlen (directive)],
2611 if (CPP_OPTIONS (pfile)->objc)
2613 sprintf (directive, " __OBJC__ 1");
2614 output_line_command (pfile, 0, same_file);
2615 pass_thru_directive (directive, &directive[strlen (directive)],
2621 /* Return 1 iff a token ending in C1 followed directly by a token C2
2622 could cause mis-tokenization. */
2631 if (c2 == c1 || c2 == '=')
2635 case '0': case '1': case '2': case '3': case '4':
2636 case '5': case '6': case '7': case '8': case '9':
2638 if (c2 == '-' || c2 == '+')
2639 return 1; /* could extend a pre-processing number */
2642 if (c2 == '\'' || c2 == '\"')
2643 return 1; /* Could turn into L"xxx" or L'xxx'. */
2647 case 'a': case 'b': case 'c': case 'd': case 'f':
2648 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2649 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2650 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2652 case 'A': case 'B': case 'C': case 'D': case 'F':
2653 case 'G': case 'H': case 'I': case 'J': case 'K':
2654 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2655 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2657 /* We're in the middle of either a name or a pre-processing number. */
2658 return (is_idchar[c2] || c2 == '.');
2659 case '<': case '>': case '!': case '%': case '#': case ':':
2660 case '^': case '&': case '|': case '*': case '/': case '=':
2661 return (c2 == c1 || c2 == '=');
2666 /* Expand a macro call.
2667 HP points to the symbol that is the macro being called.
2668 Put the result of expansion onto the input stack
2669 so that subsequent input by our caller will use it.
2671 If macro wants arguments, caller has already verified that
2672 an argument list follows; arguments come from the input stack. */
2680 DEFINITION *defn = hp->value.defn;
2681 register U_CHAR *xbuf;
2682 long start_line, start_column;
2684 struct argdata *args;
2685 long old_written = CPP_WRITTEN (pfile);
2687 int start_line = instack[indepth].lineno;
2689 int rest_args, rest_zero;
2693 CHECK_DEPTH (return;);
2697 /* This macro is being used inside a #if, which means it must be */
2698 /* recorded as a precondition. */
2699 if (pcp_inside_if && pcp_outfile && defn->predefined)
2700 dump_single_macro (hp, pcp_outfile);
2703 pfile->output_escapes++;
2704 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2706 nargs = defn->nargs;
2710 enum cpp_token token;
2712 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2714 for (i = 0; i < nargs; i++)
2716 args[i].raw = args[i].expanded = 0;
2717 args[i].raw_length = 0;
2718 args[i].expand_length = args[i].stringified_length = -1;
2719 args[i].use_count = 0;
2722 /* Parse all the macro args that are supplied. I counts them.
2723 The first NARGS args are stored in ARGS.
2724 The rest are discarded. If rest_args is set then we assume
2725 macarg absorbed the rest of the args. */
2729 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2734 if (i < nargs || (nargs == 0 && i == 0))
2736 /* if we are working on last arg which absorbs rest of args... */
2737 if (i == nargs - 1 && defn->rest_args)
2739 args[i].raw = CPP_WRITTEN (pfile);
2740 token = macarg (pfile, rest_args);
2741 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2742 args[i].newlines = 0; /* FIXME */
2745 token = macarg (pfile, 0);
2746 if (token == CPP_EOF || token == CPP_POP)
2748 cpp_error_with_line (pfile, start_line, start_column,
2749 "unterminated macro call");
2753 } while (token == CPP_COMMA);
2755 /* If we got one arg but it was just whitespace, call that 0 args. */
2758 register U_CHAR *bp = ARG_BASE + args[0].raw;
2759 register U_CHAR *lim = bp + args[0].raw_length;
2760 /* cpp.texi says for foo ( ) we provide one argument.
2761 However, if foo wants just 0 arguments, treat this as 0. */
2763 while (bp != lim && is_space[*bp]) bp++;
2768 /* Don't output an error message if we have already output one for
2769 a parse error above. */
2771 if (nargs == 0 && i > 0)
2773 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2777 /* traditional C allows foo() if foo wants one argument. */
2778 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2780 /* the rest args token is allowed to absorb 0 tokens */
2781 else if (i == nargs - 1 && defn->rest_args)
2784 cpp_error (pfile, "macro `%s' used without args", hp->name);
2786 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2788 cpp_error (pfile, "macro `%s' used with only %d args",
2794 "macro `%s' used with too many (%d) args", hp->name, i);
2798 /* If macro wants zero args, we parsed the arglist for checking only.
2799 Read directly from the macro definition. */
2802 xbuf = defn->expansion;
2803 xbuf_len = defn->length;
2807 register U_CHAR *exp = defn->expansion;
2808 register int offset; /* offset in expansion,
2809 copied a piece at a time */
2810 register int totlen; /* total amount of exp buffer filled so far */
2812 register struct reflist *ap, *last_ap;
2814 /* Macro really takes args. Compute the expansion of this call. */
2816 /* Compute length in characters of the macro's expansion.
2817 Also count number of times each arg is used. */
2818 xbuf_len = defn->length;
2819 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2823 register struct argdata *arg = &args[ap->argno];
2824 /* Stringify it it hasn't already been */
2825 if (arg->stringified_length < 0)
2827 int arglen = arg->raw_length;
2831 /* Initially need_space is -1. Otherwise, 1 means the
2832 previous character was a space, but we suppressed it;
2833 0 means the previous character was a non-space. */
2834 int need_space = -1;
2836 arg->stringified = CPP_WRITTEN (pfile);
2837 if (!CPP_TRADITIONAL (pfile))
2838 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2839 for (; i < arglen; i++)
2841 c = (ARG_BASE + arg->raw)[i];
2845 /* Internal sequences of whitespace are replaced by
2846 one space except within an string or char token.*/
2849 if (CPP_WRITTEN (pfile) > arg->stringified
2850 && (CPP_PWRITTEN (pfile))[-1] == '@')
2852 /* "@ " escape markers are removed */
2853 CPP_ADJUST_WRITTEN (pfile, -1);
2856 if (need_space == 0)
2860 else if (need_space > 0)
2861 CPP_PUTC (pfile, ' ');
2876 else if (c == '\"' || c == '\'')
2880 /* Escape these chars */
2881 if (c == '\"' || (in_string && c == '\\'))
2882 CPP_PUTC (pfile, '\\');
2884 CPP_PUTC (pfile, c);
2887 CPP_RESERVE (pfile, 4);
2888 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2890 CPP_ADJUST_WRITTEN (pfile, 4);
2893 if (!CPP_TRADITIONAL (pfile))
2894 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2895 arg->stringified_length
2896 = CPP_WRITTEN (pfile) - arg->stringified;
2898 xbuf_len += args[ap->argno].stringified_length;
2900 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2901 /* Add 4 for two newline-space markers to prevent
2902 token concatenation. */
2903 xbuf_len += args[ap->argno].raw_length + 4;
2906 /* We have an ordinary (expanded) occurrence of the arg.
2907 So compute its expansion, if we have not already. */
2908 if (args[ap->argno].expand_length < 0)
2910 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2911 cpp_expand_to_buffer (pfile,
2912 ARG_BASE + args[ap->argno].raw,
2913 args[ap->argno].raw_length);
2915 args[ap->argno].expand_length
2916 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2919 /* Add 4 for two newline-space markers to prevent
2920 token concatenation. */
2921 xbuf_len += args[ap->argno].expand_length + 4;
2923 if (args[ap->argno].use_count < 10)
2924 args[ap->argno].use_count++;
2927 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2929 /* Generate in XBUF the complete expansion
2930 with arguments substituted in.
2931 TOTLEN is the total size generated so far.
2932 OFFSET is the index in the definition
2933 of where we are copying from. */
2934 offset = totlen = 0;
2935 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2936 last_ap = ap, ap = ap->next)
2938 register struct argdata *arg = &args[ap->argno];
2939 int count_before = totlen;
2941 /* Add chars to XBUF. */
2942 for (i = 0; i < ap->nchars; i++, offset++)
2943 xbuf[totlen++] = exp[offset];
2945 /* If followed by an empty rest arg with concatenation,
2946 delete the last run of nonwhite chars. */
2947 if (rest_zero && totlen > count_before
2948 && ((ap->rest_args && ap->raw_before)
2949 || (last_ap != NULL && last_ap->rest_args
2950 && last_ap->raw_after)))
2952 /* Delete final whitespace. */
2953 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2956 /* Delete the nonwhites before them. */
2957 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2961 if (ap->stringify != 0)
2963 bcopy (ARG_BASE + arg->stringified,
2964 xbuf + totlen, arg->stringified_length);
2965 totlen += arg->stringified_length;
2967 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2969 U_CHAR *p1 = ARG_BASE + arg->raw;
2970 U_CHAR *l1 = p1 + arg->raw_length;
2973 while (p1 != l1 && is_space[*p1]) p1++;
2974 while (p1 != l1 && is_idchar[*p1])
2975 xbuf[totlen++] = *p1++;
2979 /* Arg is concatenated after: delete trailing whitespace,
2980 whitespace markers, and no-reexpansion markers. */
2983 if (is_space[l1[-1]]) l1--;
2984 else if (l1[-1] == '-')
2986 U_CHAR *p2 = l1 - 1;
2987 /* If a `-' is preceded by an odd number of newlines then it
2988 and the last newline are a no-reexpansion marker. */
2989 while (p2 != p1 && p2[-1] == '\n') p2--;
2990 if ((l1 - 1 - p2) & 1) {
2999 bcopy (p1, xbuf + totlen, l1 - p1);
3004 U_CHAR *expanded = ARG_BASE + arg->expanded;
3005 if (!ap->raw_before && totlen > 0 && arg->expand_length
3006 && !CPP_TRADITIONAL(pfile)
3007 && unsafe_chars (xbuf[totlen-1], expanded[0]))
3009 xbuf[totlen++] = '@';
3010 xbuf[totlen++] = ' ';
3013 bcopy (expanded, xbuf + totlen, arg->expand_length);
3014 totlen += arg->expand_length;
3016 if (!ap->raw_after && totlen > 0 && offset < defn->length
3017 && !CPP_TRADITIONAL(pfile)
3018 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3020 xbuf[totlen++] = '@';
3021 xbuf[totlen++] = ' ';
3024 /* If a macro argument with newlines is used multiple times,
3025 then only expand the newlines once. This avoids creating
3026 output lines which don't correspond to any input line,
3027 which confuses gdb and gcov. */
3028 if (arg->use_count > 1 && arg->newlines > 0)
3030 /* Don't bother doing change_newlines for subsequent
3034 = change_newlines (expanded, arg->expand_length);
3038 if (totlen > xbuf_len)
3042 /* if there is anything left of the definition
3043 after handling the arg list, copy that in too. */
3045 for (i = offset; i < defn->length; i++)
3047 /* if we've reached the end of the macro */
3050 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3051 && last_ap->raw_after))
3052 xbuf[totlen++] = exp[i];
3060 pfile->output_escapes--;
3062 /* Now put the expansion on the input stack
3063 so our caller will commence reading from it. */
3064 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3065 CPP_BUFFER (pfile)->has_escapes = 1;
3067 /* Pop the space we've used in the token_buffer for argument expansion. */
3068 CPP_SET_WRITTEN (pfile, old_written);
3070 /* Recursive macro use sometimes works traditionally.
3071 #define foo(x,y) bar (x (y,0), y)
3074 if (!CPP_TRADITIONAL (pfile))
3075 hp->type = T_DISABLED;
3079 push_macro_expansion (
3081 register U_CHAR *xbuf,
3085 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3086 mbuf->cleanup = macro_cleanup;
3089 /* The first chars of the expansion should be a "@ " added by
3090 collect_expansion. This is to prevent accidental token-pasting
3091 between the text preceding the macro invocation, and the macro
3094 We would like to avoid adding unneeded spaces (for the sake of
3095 tools that use cpp, such as imake). In some common cases we can
3096 tell that it is safe to omit the space.
3098 The character before the macro invocation cannot have been an
3099 idchar (or else it would have been pasted with the idchars of
3100 the macro name). Therefore, if the first non-space character
3101 of the expansion is an idchar, we do not need the extra space
3102 to prevent token pasting.
3104 Also, we don't need the extra space if the first char is '(',
3105 or some other (less common) characters. */
3107 if (xbuf[0] == '@' && xbuf[1] == ' '
3108 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3109 || xbuf[2] == '\"'))
3113 /* Like cpp_get_token, except that it does not read past end-of-line.
3114 Also, horizontal space is skipped, and macros are popped. */
3116 static enum cpp_token
3117 get_directive_token (
3122 long old_written = CPP_WRITTEN (pfile);
3123 enum cpp_token token;
3124 cpp_skip_hspace (pfile);
3125 if (PEEKC () == '\n')
3127 token = cpp_get_token (pfile);
3131 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3133 /* ... else fall though ... */
3134 case CPP_HSPACE: case CPP_COMMENT:
3135 CPP_SET_WRITTEN (pfile, old_written);
3143 /* Handle #include and #import.
3144 This function expects to see "fname" or <fname> on the input.
3146 The input is normally in part of the output_buffer following
3147 CPP_WRITTEN, and will get overwritten by output_line_command.
3148 I.e. in input file specification has been popped by handle_directive.
3154 struct directive *keyword,
3155 U_CHAR *unused1, U_CHAR *unused2)
3157 int importing = (keyword->type == T_IMPORT);
3158 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3159 char *fname; /* Dynamically allocated fname buffer */
3162 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3163 enum cpp_token token;
3165 /* Chain of dirs to search */
3166 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3167 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3168 struct file_name_list *searchptr = 0;
3169 long old_written = CPP_WRITTEN (pfile);
3173 int f; /* file number */
3175 int retried = 0; /* Have already tried macro
3176 expanding the include line*/
3177 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3182 f= -1; /* JF we iz paranoid! */
3184 if (importing && CPP_OPTIONS (pfile)->warn_import
3185 && !CPP_OPTIONS (pfile)->inhibit_warnings
3186 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3188 pfile->import_warning = 1;
3189 cpp_warning (pfile, "using `#import' is not recommended");
3190 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3191 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3192 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3193 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3194 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3195 fprintf (stderr, " ... <real contents of file> ...\n");
3196 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3197 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3198 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3199 fprintf (stderr, "when it is equipped with such a conditional.\n");
3202 pfile->parsing_include_directive++;
3203 token = get_directive_token (pfile);
3204 pfile->parsing_include_directive--;
3206 if (token == CPP_STRING)
3208 /* FIXME - check no trailing garbage */
3209 fbeg = pfile->token_buffer + old_written + 1;
3210 fend = CPP_PWRITTEN (pfile) - 1;
3211 if (fbeg[-1] == '<')
3214 /* If -I-, start with the first -I dir after the -I-. */
3215 if (CPP_OPTIONS (pfile)->first_bracket_include)
3216 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3218 /* If -I- was specified, don't search current dir, only spec'd ones. */
3219 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3222 /* We have "filename". Figure out directory this source
3223 file is coming from and put it on the front of the list. */
3225 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3230 if ((nam = fp->nominal_fname) != NULL)
3232 /* Found a named file. Figure out dir of the file,
3233 and put it in front of the search list. */
3234 dsp[0].next = search_start;
3237 ep = rindex (nam, '/');
3239 ep = rindex (nam, ']');
3240 if (ep == NULL) ep = rindex (nam, '>');
3241 if (ep == NULL) ep = rindex (nam, ':');
3242 if (ep != NULL) ep++;
3247 dsp[0].fname = (char *) alloca (n + 1);
3248 strncpy (dsp[0].fname, nam, n);
3249 dsp[0].fname[n] = '\0';
3250 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3251 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3255 dsp[0].fname = 0; /* Current directory */
3257 dsp[0].got_name_map = 0;
3264 else if (token == CPP_NAME)
3267 * Support '#include xyz' like VAX-C to allow for easy use of all the
3268 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3269 * code from case '<' is repeated here) and generates a warning.
3272 "VAX-C-style include specification found, use '#include <filename.h>' !");
3274 /* If -I-, start with the first -I dir after the -I-. */
3275 if (CPP_OPTIONS (pfile)->first_bracket_include)
3276 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3277 fbeg = pfile->token_buffer + old_written;
3278 fend = CPP_PWRITTEN (pfile);
3284 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3285 CPP_SET_WRITTEN (pfile, old_written);
3286 skip_rest_of_line (pfile);
3292 token = get_directive_token (pfile);
3293 if (token != CPP_VSPACE)
3295 cpp_error (pfile, "junk at end of `#include'");
3296 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3297 token = get_directive_token (pfile);
3300 /* For #include_next, skip in the search path
3301 past the dir in which the containing file was found. */
3305 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3306 if (fp->fname != NULL)
3308 /* fp->dir is null if the containing file was specified with
3309 an absolute file name. In that case, don't skip anything. */
3310 if (fp->dir == SELF_DIR_DUMMY)
3311 search_start = CPP_OPTIONS (pfile)->include;
3313 search_start = fp->dir->next;
3318 CPP_SET_WRITTEN (pfile, old_written);
3324 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3328 /* Allocate this permanently, because it gets stored in the definitions
3330 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3331 /* + 2 above for slash and terminating null. */
3332 /* + 2 added for '.h' on VMS (to support '#include filename') */
3334 /* If specified file name is absolute, just open it. */
3337 strncpy (fname, fbeg, flen);
3339 if (redundant_include_p (pfile, fname))
3342 f = lookup_import (pfile, fname, NULL_PTR);
3344 f = open_include_file (pfile, fname, NULL_PTR);
3346 return 0; /* Already included this file */
3348 /* Search directory path, trying to open the file.
3349 Copy each filename tried into FNAME. */
3351 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3352 if (searchptr->fname) {
3353 /* The empty string in a search path is ignored.
3354 This makes it possible to turn off entirely
3355 a standard piece of the list. */
3356 if (searchptr->fname[0] == 0)
3358 strcpy (fname, searchptr->fname);
3359 strcat (fname, "/");
3360 fname[strlen (fname) + flen] = 0;
3364 strncat (fname, fbeg, flen);
3366 /* Change this 1/2 Unix 1/2 VMS file specification into a
3367 full VMS file specification */
3368 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3369 /* Fix up the filename */
3370 hack_vms_include_specification (fname);
3372 /* This is a normal VMS filespec, so use it unchanged. */
3373 strncpy (fname, fbeg, flen);
3375 /* if it's '#include filename', add the missing .h */
3376 if (index(fname,'.')==NULL) {
3377 strcat (fname, ".h");
3381 /* ??? There are currently 3 separate mechanisms for avoiding processing
3382 of redundant include files: #import, #pragma once, and
3383 redundant_include_p. It would be nice if they were unified. */
3384 if (redundant_include_p (pfile, fname))
3387 f = lookup_import (pfile, fname, searchptr);
3389 f = open_include_file (pfile, fname, searchptr);
3391 return 0; /* Already included this file */
3393 else if (f == -1 && errno == EACCES)
3394 cpp_warning (pfile, "Header file %s exists, but is not readable",
3404 /* A file that was not found. */
3405 strncpy (fname, fbeg, flen);
3407 /* If generating dependencies and -MG was specified, we assume missing
3408 files are leaf files, living in the same directory as the source file
3409 or other similar place; these missing files may be generated from
3410 other files and may not exist yet (eg: y.tab.h). */
3412 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3413 && CPP_PRINT_DEPS (pfile)
3414 > (angle_brackets || (pfile->system_include_depth > 0)))
3416 /* If it was requested as a system header file,
3417 then assume it belongs in the first place to look for such. */
3420 for (searchptr = search_start; searchptr;
3421 searchptr = searchptr->next)
3423 if (searchptr->fname)
3427 if (searchptr->fname[0] == 0)
3429 p = (char *) alloca (strlen (searchptr->fname)
3430 + strlen (fname) + 2);
3431 strcpy (p, searchptr->fname);
3434 deps_output (pfile, p, ' ');
3441 /* Otherwise, omit the directory, as if the file existed
3442 in the directory with the source. */
3443 deps_output (pfile, fname, ' ');
3446 /* If -M was specified, and this header file won't be added to the
3447 dependency list, then don't count this as an error, because we can
3448 still produce correct output. Otherwise, we can't produce correct
3449 output, because there may be dependencies we need inside the missing
3450 file, and we don't know what directory this missing file exists in.*/
3451 else if (CPP_PRINT_DEPS (pfile)
3452 && (CPP_PRINT_DEPS (pfile)
3453 <= (angle_brackets || (pfile->system_include_depth > 0))))
3454 cpp_warning (pfile, "No include path in which to find %s", fname);
3455 else if (search_start)
3456 cpp_error_from_errno (pfile, fname);
3458 cpp_error (pfile, "No include path in which to find %s", fname);
3461 /* Check to see if this include file is a once-only include file.
3464 struct file_name_list* ptr;
3466 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3467 if (!strcmp (ptr->fname, fname)) {
3469 return 0; /* This file was once'd. */
3473 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3474 if (!strcmp (ptr->fname, fname))
3475 break; /* This file was included before. */
3479 /* This is the first time for this file. */
3480 /* Add it to list of files included. */
3482 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3483 ptr->control_macro = 0;
3484 ptr->c_system_include_path = 0;
3485 ptr->next = pfile->all_include_files;
3486 pfile->all_include_files = ptr;
3487 ptr->fname = savestring (fname);
3488 ptr->got_name_map = 0;
3490 /* For -M, add this file to the dependencies. */
3491 if (CPP_PRINT_DEPS (pfile)
3492 > (angle_brackets || (pfile->system_include_depth > 0)))
3493 deps_output (pfile, fname, ' ');
3496 /* Handle -H option. */
3497 if (CPP_OPTIONS(pfile)->print_include_names)
3499 cpp_buffer *buf = CPP_BUFFER (pfile);
3500 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3502 fprintf (stderr, "%s\n", fname);
3506 pfile->system_include_depth++;
3508 /* Actually process the file. */
3510 /* Record file on "seen" list for #import. */
3511 add_import (pfile, f, fname);
3513 pcftry = (char *) alloca (strlen (fname) + 30);
3525 sprintf (pcftry, "%s%d", fname, pcfnum++);
3527 pcf = open (pcftry, O_RDONLY, 0666);
3533 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3535 || stat_f.st_dev != s.st_dev)
3537 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3538 /* Don't need it any more. */
3543 /* Don't need it at all. */
3548 } while (pcf != -1 && !pcfbuf);
3552 /* Actually process the file */
3553 cpp_push_buffer (pfile, NULL, 0);
3554 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3555 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3557 output_line_command (pfile, 0, enter_file);
3558 pfile->only_seen_white = 2;
3562 pfile->system_include_depth--;
3567 /* Return nonzero if there is no need to include file NAME
3568 because it has already been included and it contains a conditional
3569 to make a repeated include do nothing. */
3572 redundant_include_p (
3576 struct file_name_list *l = pfile->all_include_files;
3577 for (; l; l = l->next)
3578 if (! strcmp (name, l->fname)
3580 && cpp_lookup (pfile, l->control_macro, -1, -1))
3585 /* Return nonzero if the given FILENAME is an absolute pathname which
3586 designates a file within one of the known "system" include file
3587 directories. We assume here that if the given FILENAME looks like
3588 it is the name of a file which resides either directly in a "system"
3589 include file directory, or within any subdirectory thereof, then the
3590 given file must be a "system" include file. This function tells us
3591 if we should suppress pedantic errors/warnings for the given FILENAME.
3593 The value is 2 if the file is a C-language system header file
3594 for which C++ should (on most systems) assume `extern "C"'. */
3599 register char *filename)
3601 struct file_name_list *searchptr;
3603 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3604 searchptr = searchptr->next)
3605 if (searchptr->fname) {
3606 register char *sys_dir = searchptr->fname;
3607 register unsigned length = strlen (sys_dir);
3609 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3611 if (searchptr->c_system_include_path)
3622 * Install a name in the assertion hash table.
3624 * If LEN is >= 0, it is the length of the name.
3625 * Otherwise, compute the length by scanning the entire name.
3627 * If HASH is >= 0, it is the precomputed hash code.
3628 * Otherwise, compute the hash code.
3630 static ASSERTION_HASHNODE *
3637 register ASSERTION_HASHNODE *hp;
3638 register int i, bucket;
3639 register U_CHAR *p, *q;
3641 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3642 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3644 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3645 hp->next = pfile->assertion_hashtab[bucket];
3646 pfile->assertion_hashtab[bucket] = hp;
3648 if (hp->next != NULL)
3649 hp->next->prev = hp;
3652 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3655 for (i = 0; i < len; i++)
3661 * find the most recent hash node for name name (ending with first
3662 * non-identifier char) installed by install
3664 * If LEN is >= 0, it is the length of the name.
3665 * Otherwise, compute the length by scanning the entire name.
3667 * If HASH is >= 0, it is the precomputed hash code.
3668 * Otherwise, compute the hash code.
3671 static ASSERTION_HASHNODE *
3678 register ASSERTION_HASHNODE *bucket;
3680 bucket = pfile->assertion_hashtab[hash];
3682 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3684 bucket = bucket->next;
3691 ASSERTION_HASHNODE *hp)
3693 struct tokenlist_list *tail;
3694 if (hp->prev != NULL)
3695 hp->prev->next = hp->next;
3696 if (hp->next != NULL)
3697 hp->next->prev = hp->prev;
3699 for (tail = hp->value; tail; )
3701 struct tokenlist_list *next = tail->next;
3702 free_token_list (tail->tokens);
3707 /* make sure that the bucket chain header that
3708 the deleted guy was on points to the right thing afterwards. */
3709 if (hp == *hp->bucket_hdr)
3710 *hp->bucket_hdr = hp->next;
3715 /* Convert a character string literal into a nul-terminated string.
3716 The input string is [IN ... LIMIT).
3717 The result is placed in RESULT. RESULT can be the same as IN.
3718 The value returned in the end of the string written to RESULT,
3719 or NULL on error. */
3724 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3744 char *bpc = (char *) in;
3745 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3746 in = (U_CHAR *) bpc;
3748 *result++ = (U_CHAR)c;
3751 /* else fall through */
3761 * interpret #line command. Remembers previously seen fnames
3762 * in its very own hash table.
3764 #define FNAME_HASHSIZE 37
3769 struct directive *keyword)
3771 cpp_buffer *ip = CPP_BUFFER (pfile);
3773 long old_written = CPP_WRITTEN (pfile);
3774 enum file_change_code file_change = same_file;
3775 enum cpp_token token;
3778 token = get_directive_token (pfile);
3780 if (token != CPP_NUMBER
3781 || !isdigit(pfile->token_buffer[old_written]))
3783 cpp_error (pfile, "invalid format `#line' command");
3784 goto bad_line_directive;
3787 /* The Newline at the end of this line remains to be processed.
3788 To put the next line at the specified line number,
3789 we must store a line number now that is one less. */
3790 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3791 CPP_SET_WRITTEN (pfile, old_written);
3793 /* NEW_LINENO is one less than the actual line number here. */
3794 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3795 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3797 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3798 if (PEEKC() && !is_space[PEEKC()]) {
3799 cpp_error (pfile, "invalid format `#line' command");
3800 goto bad_line_directive;
3804 token = get_directive_token (pfile);
3806 if (token == CPP_STRING) {
3807 U_CHAR *fname = pfile->token_buffer + old_written;
3809 static HASHNODE *fname_table[FNAME_HASHSIZE];
3810 HASHNODE *hp, **hash_bucket;
3815 /* Turn the file name, which is a character string literal,
3816 into a null-terminated string. Do this in place. */
3817 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3818 if (end_name == NULL)
3820 cpp_error (pfile, "invalid format `#line' command");
3821 goto bad_line_directive;
3824 fname_length = end_name - fname;
3826 num_start = CPP_WRITTEN (pfile);
3827 token = get_directive_token (pfile);
3828 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3829 p = pfile->token_buffer + num_start;
3830 if (CPP_PEDANTIC (pfile))
3831 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3833 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3835 cpp_error (pfile, "invalid format `#line' command");
3836 goto bad_line_directive;
3839 file_change = enter_file;
3841 file_change = leave_file;
3843 ip->system_header_p = 1;
3844 else /* if (*p == 4) */
3845 ip->system_header_p = 2;
3847 CPP_SET_WRITTEN (pfile, num_start);
3848 token = get_directive_token (pfile);
3849 p = pfile->token_buffer + num_start;
3850 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3851 ip->system_header_p = *p == 3 ? 1 : 2;
3852 token = get_directive_token (pfile);
3854 if (token != CPP_VSPACE) {
3855 cpp_error (pfile, "invalid format `#line' command");
3856 goto bad_line_directive;
3861 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3862 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3863 if (hp->length == fname_length &&
3864 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3865 ip->nominal_fname = hp->value.cpval;
3869 /* Didn't find it; cons up a new one. */
3870 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3871 hp->next = *hash_bucket;
3874 hp->length = fname_length;
3875 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3876 bcopy (fname, hp->value.cpval, fname_length);
3879 else if (token != CPP_VSPACE && token != CPP_EOF) {
3880 cpp_error (pfile, "invalid format `#line' command");
3881 goto bad_line_directive;
3884 ip->lineno = new_lineno;
3886 skip_rest_of_line (pfile);
3887 CPP_SET_WRITTEN (pfile, old_written);
3888 output_line_command (pfile, 0, file_change);
3893 * remove the definition of a symbol from the symbol table.
3894 * according to un*x /lib/cpp, it is not an error to undef
3895 * something that has no definitions, so it isn't one here either.
3901 struct directive *keyword,
3902 U_CHAR *buf, U_CHAR *limit)
3906 U_CHAR *orig_buf = buf;
3909 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3910 if (pcp_outfile && keyword)
3911 pass_thru_directive (buf, limit, pfile, keyword);
3914 SKIP_WHITE_SPACE (buf);
3915 sym_length = check_macro_name (pfile, buf, "macro");
3917 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3919 /* If we are generating additional info for debugging (with -g) we
3920 need to pass through all effective #undef commands. */
3921 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3922 pass_thru_directive (orig_buf, limit, pfile, keyword);
3923 if (hp->type != T_MACRO)
3924 cpp_warning (pfile, "undefining `%s'", hp->name);
3928 if (CPP_PEDANTIC (pfile)) {
3930 SKIP_WHITE_SPACE (buf);
3932 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3938 * Report an error detected by the program we are processing.
3939 * Use the text of the line in the error message.
3940 * (We use error because it prints the filename & line#.)
3946 struct directive *keyword,
3947 U_CHAR *buf,U_CHAR *limit)
3949 int length = limit - buf;
3950 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3951 bcopy (buf, copy, length);
3953 SKIP_WHITE_SPACE (copy);
3954 cpp_error (pfile, "#error %s", copy);
3959 * Report a warning detected by the program we are processing.
3960 * Use the text of the line in the warning message, then continue.
3961 * (We use error because it prints the filename & line#.)
3967 struct directive *keyword,
3968 U_CHAR *buf,U_CHAR *limit)
3970 int length = limit - buf;
3971 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3972 bcopy (buf, copy, length);
3974 SKIP_WHITE_SPACE (copy);
3975 cpp_warning (pfile, "#warning %s", copy);
3979 /* Remember the name of the current file being read from so that we can
3980 avoid ever including it again. */
3986 cpp_buffer *ip = NULL;
3987 struct file_name_list *new;
3989 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3993 if (ip->fname != NULL)
3998 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3999 new->next = pfile->dont_repeat_files;
4000 pfile->dont_repeat_files = new;
4001 new->fname = savestring (ip->fname);
4002 new->control_macro = 0;
4003 new->got_name_map = 0;
4004 new->c_system_include_path = 0;
4009 /* #ident has already been copied to the output file, so just ignore it. */
4014 struct directive *keyword,
4015 U_CHAR *buf, U_CHAR *limit)
4017 /* long old_written = CPP_WRITTEN (pfile);*/
4020 /* Allow #ident in system headers, since that's not user's fault. */
4021 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
4022 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
4024 /* Leave rest of line to be read by later calls to cpp_get_token. */
4029 /* #pragma and its argument line have already been copied to the output file.
4030 Just check for some recognized pragmas that need validation here. */
4035 struct directive *keyword,
4036 U_CHAR *buf,U_CHAR *limit)
4038 while (*buf == ' ' || *buf == '\t')
4040 if (!strncmp (buf, "once", 4)) {
4041 /* Allow #pragma once in system headers, since that's not the user's
4043 if (!CPP_BUFFER (pfile)->system_header_p)
4044 cpp_warning (pfile, "`#pragma once' is obsolete");
4048 if (!strncmp (buf, "implementation", 14)) {
4049 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4050 been included yet. */
4051 struct file_name_list *ptr;
4052 U_CHAR *p = buf + 14, *fname, *inc_fname;
4054 SKIP_WHITE_SPACE (p);
4055 if (*p == '\n' || *p != '\"')
4059 p = (U_CHAR *) index (fname, '\"');
4060 fname_len = p != NULL ? p - fname : strlen (fname);
4062 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4063 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4064 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4065 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4067 "`#pragma implementation' for `%s' appears after file is included",
4076 /* This was a fun hack, but #pragma seems to start to be useful.
4077 By failing to recognize it, we pass it through unchanged to cc1. */
4080 * the behavior of the #pragma directive is implementation defined.
4081 * this implementation defines it as follows.
4088 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4091 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4093 execl ("/usr/games/hack", "#pragma", 0);
4094 execl ("/usr/games/rogue", "#pragma", 0);
4095 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4096 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4098 fatal ("You are in a maze of twisty compiler features, all different");
4102 /* Just ignore #sccs, on systems where we define it at all. */
4107 struct directive *keyword,
4108 U_CHAR *buf,U_CHAR *limit)
4110 if (CPP_PEDANTIC (pfile))
4111 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4116 * handle #if command by
4117 * 1) inserting special `defined' keyword into the hash table
4118 * that gets turned into 0 or 1 by special_symbol (thus,
4119 * if the luser has a symbol called `defined' already, it won't
4120 * work inside the #if command)
4121 * 2) rescan the input into a temporary output buffer
4122 * 3) pass the output buffer to the yacc parser and collect a value
4123 * 4) clean up the mess left from steps 1 and 2.
4124 * 5) call conditional_skip to skip til the next #endif (etc.),
4125 * or not, depending on the value from step 3.
4131 struct directive *keyword,
4132 U_CHAR *buf,U_CHAR *limit)
4134 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4135 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4140 * handle a #elif directive by not changing if_stack either.
4141 * see the comment above do_else.
4147 struct directive *keyword,
4148 U_CHAR *buf,U_CHAR *limit)
4150 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4151 cpp_error (pfile, "`#elif' not within a conditional");
4154 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4155 cpp_error (pfile, "`#elif' after `#else'");
4157 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4159 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4160 && strcmp (pfile->if_stack->fname,
4161 CPP_BUFFER (pfile)->nominal_fname) != 0)
4162 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4163 fprintf (stderr, ")\n");
4165 pfile->if_stack->type = T_ELIF;
4168 if (pfile->if_stack->if_succeeded)
4169 skip_if_group (pfile, 0);
4171 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4173 skip_if_group (pfile, 0);
4175 ++pfile->if_stack->if_succeeded; /* continue processing input */
4176 output_line_command (pfile, 1, same_file);
4183 * evaluate a #if expression in BUF, of length LENGTH,
4184 * then parse the result as a C expression and return the value as an int.
4186 static HOST_WIDE_INT
4187 eval_if_expression (
4192 HASHNODE *save_defined;
4193 HOST_WIDE_INT value;
4194 long old_written = CPP_WRITTEN (pfile);
4196 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4197 pfile->pcp_inside_if = 1;
4199 value = cpp_parse_expr (pfile);
4200 pfile->pcp_inside_if = 0;
4201 delete_macro (save_defined); /* clean up special symbol */
4203 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4209 * routine to handle ifdef/ifndef. Try to look up the symbol,
4210 * then do or don't skip to the #endif/#else/#elif depending
4211 * on what directive is actually being processed.
4217 struct directive *keyword,
4218 U_CHAR *unused1, U_CHAR *unused2)
4221 cpp_buffer *ip = CPP_BUFFER (pfile);
4224 enum cpp_token token;
4225 int start_of_file = 0;
4226 U_CHAR *control_macro = 0;
4227 int old_written = CPP_WRITTEN (pfile);
4229 /* Detect a #ifndef at start of file (not counting comments). */
4230 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4231 start_of_file = pfile->only_seen_white == 2;
4233 pfile->no_macro_expand++;
4234 token = get_directive_token (pfile);
4235 pfile->no_macro_expand--;
4237 ident = pfile->token_buffer + old_written;
4238 ident_length = CPP_WRITTEN (pfile) - old_written;
4239 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4241 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4243 skip = (keyword->type == T_IFDEF);
4244 if (! CPP_TRADITIONAL (pfile))
4245 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4247 else if (token == CPP_NAME)
4249 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4250 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4251 if (start_of_file && !skip)
4253 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4254 bcopy (ident, control_macro, ident_length + 1);
4259 skip = (keyword->type == T_IFDEF);
4260 if (! CPP_TRADITIONAL (pfile))
4261 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4264 if (!CPP_TRADITIONAL (pfile))
4266 cpp_skip_hspace (pfile);
4268 if (c != EOF && c != '\n')
4269 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4271 skip_rest_of_line (pfile);
4275 /* Output a precondition for this macro. */
4276 if (hp && hp->value.defn->predefined)
4277 fprintf (pcp_outfile, "#define %s\n", hp->name);
4280 fprintf (pcp_outfile, "#undef ");
4281 while (is_idchar[*cp]) /* Ick! */
4282 fputc (*cp++, pcp_outfile);
4283 putc ('\n', pcp_outfile);
4287 conditional_skip (pfile, skip, T_IF, control_macro);
4291 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4292 If this is a #ifndef starting at the beginning of a file,
4293 CONTROL_MACRO is the macro name tested by the #ifndef.
4294 Otherwise, CONTROL_MACRO is 0. */
4300 enum node_type type,
4301 U_CHAR *control_macro)
4303 IF_STACK_FRAME *temp;
4305 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4306 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4308 temp->lineno = CPP_BUFFER (pfile)->lineno;
4310 temp->next = pfile->if_stack;
4311 temp->control_macro = control_macro;
4312 pfile->if_stack = temp;
4314 pfile->if_stack->type = type;
4317 skip_if_group (pfile, 0);
4320 ++pfile->if_stack->if_succeeded;
4321 output_line_command (pfile, 1, same_file);
4326 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4327 * leaves input ptr at the sharp sign found.
4328 * If ANY is nonzero, return at next directive of any sort.
4336 int at_beg_of_line = 1;
4337 struct directive *kt;
4338 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4340 U_CHAR *beg_of_line = bp;
4342 register int ident_length;
4343 U_CHAR *ident, *after_ident;
4344 struct parse_marker line_start_mark;
4346 parse_set_mark (&line_start_mark, pfile);
4348 if (CPP_OPTIONS (pfile)->output_conditionals) {
4349 static char failed[] = "#failed\n";
4350 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4352 output_line_command (pfile, 1, same_file);
4356 if (CPP_OPTIONS (pfile)->output_conditionals)
4358 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4359 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4360 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4362 parse_move_mark (&line_start_mark, pfile);
4363 if (!CPP_TRADITIONAL (pfile))
4364 cpp_skip_hspace (pfile);
4368 int old_written = CPP_WRITTEN (pfile);
4369 cpp_skip_hspace (pfile);
4371 parse_name (pfile, GETC());
4372 ident_length = CPP_WRITTEN (pfile) - old_written;
4373 ident = pfile->token_buffer + old_written;
4374 pfile->limit = ident;
4376 if (ident_length == 0)
4377 goto not_a_directive;
4379 /* Handle # followed by a line number. */
4381 /* Avoid error for `###' and similar cases unless -pedantic. */
4384 for (kt = directive_table; kt->length >= 0; kt++)
4386 IF_STACK_FRAME *temp;
4387 if (ident_length == kt->length
4388 && strncmp (ident, kt->name, kt->length) == 0)
4390 /* If we are asked to return on next directive, do so now. */
4400 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4401 temp->next = pfile->if_stack;
4402 pfile->if_stack = temp;
4404 temp->lineno = CPP_BUFFER(pfile)->lineno;
4406 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4407 temp->type = kt->type;
4411 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4412 validate_else (pfile,
4413 kt->type == T_ELSE ? "#else" : "#endif");
4415 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4418 "`#%s' not within a conditional", kt->name);
4421 else if (pfile->if_stack == save_if_stack)
4422 goto done; /* found what we came for */
4424 if (kt->type != T_ENDIF)
4426 if (pfile->if_stack->type == T_ELSE)
4427 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4428 pfile->if_stack->type = kt->type;
4432 temp = pfile->if_stack;
4433 pfile->if_stack = temp->next;
4440 /* Don't let erroneous code go by. */
4441 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4442 && CPP_PEDANTIC (pfile))
4443 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4447 /* We're in the middle of a line. Skip the rest of it. */
4454 case '/': /* possible comment */
4455 c = skip_comment (pfile, NULL);
4462 old = CPP_WRITTEN (pfile);
4463 cpp_get_token (pfile);
4464 CPP_SET_WRITTEN (pfile, old);
4467 /* Char after backslash loses its special meaning. */
4468 if (PEEKC() == '\n')
4478 if (CPP_OPTIONS (pfile)->output_conditionals) {
4479 static char end_failed[] = "#endfailed\n";
4480 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4483 pfile->only_seen_white = 1;
4484 parse_goto_mark (&line_start_mark, pfile);
4485 parse_clear_mark (&line_start_mark);
4489 * handle a #else directive. Do this by just continuing processing
4490 * without changing if_stack ; this is so that the error message
4491 * for missing #endif's etc. will point to the original #if. It
4492 * is possible that something different would be better.
4498 struct directive *keyword,
4499 U_CHAR *buf, U_CHAR *limit)
4501 cpp_buffer *ip = CPP_BUFFER (pfile);
4503 if (CPP_PEDANTIC (pfile))
4504 validate_else (pfile, "#else");
4505 skip_rest_of_line (pfile);
4507 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4508 cpp_error (pfile, "`#else' not within a conditional");
4511 /* #ifndef can't have its special treatment for containing the whole file
4512 if it has a #else clause. */
4513 pfile->if_stack->control_macro = 0;
4515 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4516 cpp_error (pfile, "`#else' after `#else'");
4517 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4518 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4519 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4520 fprintf (stderr, ")\n");
4522 pfile->if_stack->type = T_ELSE;
4525 if (pfile->if_stack->if_succeeded)
4526 skip_if_group (pfile, 0);
4528 ++pfile->if_stack->if_succeeded; /* continue processing input */
4529 output_line_command (pfile, 1, same_file);
4535 * unstack after #endif command
4541 struct directive *keyword,
4542 U_CHAR *buf, U_CHAR *limit)
4544 if (CPP_PEDANTIC (pfile))
4545 validate_else (pfile, "#endif");
4546 skip_rest_of_line (pfile);
4548 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4549 cpp_error (pfile, "unbalanced `#endif'");
4552 IF_STACK_FRAME *temp = pfile->if_stack;
4553 pfile->if_stack = temp->next;
4554 if (temp->control_macro != 0)
4556 /* This #endif matched a #ifndef at the start of the file.
4557 See if it is at the end of the file. */
4558 struct parse_marker start_mark;
4561 parse_set_mark (&start_mark, pfile);
4565 cpp_skip_hspace (pfile);
4570 parse_goto_mark (&start_mark, pfile);
4571 parse_clear_mark (&start_mark);
4575 /* If we get here, this #endif ends a #ifndef
4576 that contains all of the file (aside from whitespace).
4577 Arrange not to include the file again
4578 if the macro that was tested is defined.
4580 Do not do this for the top-level file in a -include or any
4581 file in a -imacros. */
4585 && ! (indepth == 1 && pfile->no_record_file)
4586 && ! (pfile->no_record_file && no_output))
4589 struct file_name_list *ifile = pfile->all_include_files;
4591 for ( ; ifile != NULL; ifile = ifile->next)
4593 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4595 ifile->control_macro = temp->control_macro;
4603 output_line_command (pfile, 1, same_file);
4608 /* When an #else or #endif is found while skipping failed conditional,
4609 if -pedantic was specified, this is called to warn about text after
4610 the command name. P points to the first char after the command name. */
4618 cpp_skip_hspace (pfile);
4620 if (c != EOF && c != '\n')
4622 "text following `%s' violates ANSI standard", directive);
4625 /* Get the next token, and add it to the text in pfile->token_buffer.
4626 Return the kind of token we got. */
4633 register int c, c2, c3;
4635 long start_line, start_column;
4636 enum cpp_token token;
4637 struct cpp_options *opts = CPP_OPTIONS (pfile);
4638 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4644 if (CPP_BUFFER (pfile)->seen_eof)
4646 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4653 cpp_buffer *next_buf
4654 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4655 CPP_BUFFER (pfile)->seen_eof = 1;
4656 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4658 /* We're about to return from an #include file.
4659 Emit #line information now (as part of the CPP_POP) result.
4660 But the #line refers to the file we will pop to. */
4661 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4662 CPP_BUFFER (pfile) = next_buf;
4663 pfile->input_stack_listing_current = 0;
4664 output_line_command (pfile, 0, leave_file);
4665 CPP_BUFFER (pfile) = cur_buffer;
4675 struct parse_marker start_mark;
4677 if (PEEKC () == '=')
4679 if (opts->put_out_comments)
4680 parse_set_mark (&start_mark, pfile);
4682 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4683 &start_line, &start_column);
4684 c = skip_comment (pfile, &newlines);
4685 if (opts->put_out_comments && (c == '/' || c == EOF))
4686 parse_clear_mark (&start_mark);
4691 cpp_error_with_line (pfile, start_line, start_column,
4692 "unterminated comment");
4695 c = '/'; /* Initial letter of comment. */
4697 /* Comments are equivalent to spaces.
4698 For -traditional, a comment is equivalent to nothing. */
4699 if (opts->put_out_comments)
4701 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4703 U_CHAR *start = pbuf->buf + start_mark.position;
4704 int len = pbuf->cur - start;
4705 CPP_RESERVE(pfile, 1 + len);
4706 CPP_PUTC_Q (pfile, c);
4707 CPP_PUTS_Q (pfile, start, len);
4708 pfile->lineno += newlines;
4709 parse_clear_mark (&start_mark);
4712 else if (CPP_TRADITIONAL (pfile))
4719 /* This may not work if cpp_get_token is called recursively,
4720 since many places look for horizontal space. */
4723 /* Copy the newlines into the output buffer, in order to
4724 avoid the pain of a #line every time a multiline comment
4726 CPP_RESERVE(pfile, newlines);
4727 while (--newlines >= 0)
4729 CPP_PUTC_Q (pfile, '\n');
4735 CPP_RESERVE(pfile, 1);
4736 CPP_PUTC_Q (pfile, ' ');
4740 if (opts->for_lint) {
4743 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4745 if (lintcmd != NULL) {
4746 /* I believe it is always safe to emit this newline: */
4748 bcopy ("#pragma lint ", (char *) obp, 13);
4750 bcopy (lintcmd, (char *) obp, cmdlen);
4755 bcopy (argbp, (char *) obp, arglen);
4759 /* OK, now bring us back to the state we were in before we entered
4760 this branch. We need #line b/c the newline for the pragma
4761 could fuck things up. */
4762 output_line_command (pfile, 0, same_file);
4763 *(obp++) = ' '; /* just in case, if comments are copied thru */
4771 /* If this is expanding a macro definition, don't recognize
4772 preprocessor directives. */
4775 /* If this is expand_into_temp_buffer, recognize them
4776 only after an actual newline at this level,
4777 not at the beginning of the input level. */
4778 if (ip->fname == 0 && beg_of_line == ip->buf)
4784 if (!pfile->only_seen_white)
4786 if (handle_directive (pfile))
4787 return CPP_DIRECTIVE;
4788 pfile->only_seen_white = 0;
4793 /* A single quoted string is treated like a double -- some
4794 programs (e.g., troff) are perverse this way */
4795 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4796 &start_line, &start_column);
4797 old_written = CPP_WRITTEN (pfile);
4799 CPP_PUTC (pfile, c);
4805 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4807 /* try harder: this string crosses a macro expansion
4808 boundary. This can happen naturally if -traditional.
4809 Otherwise, only -D can make a macro with an unmatched
4811 cpp_buffer *next_buf
4812 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4813 (*CPP_BUFFER (pfile)->cleanup)
4814 (CPP_BUFFER (pfile), pfile);
4815 CPP_BUFFER (pfile) = next_buf;
4818 if (!CPP_TRADITIONAL (pfile))
4820 cpp_error_with_line (pfile, start_line, start_column,
4821 "unterminated string or character constant");
4822 if (pfile->multiline_string_line != start_line
4823 && pfile->multiline_string_line != 0)
4824 cpp_error_with_line (pfile,
4825 pfile->multiline_string_line, -1,
4826 "possible real start of unterminated constant");
4827 pfile->multiline_string_line = 0;
4831 CPP_PUTC (pfile, cc);
4835 /* Traditionally, end of line ends a string constant with
4836 no error. So exit the loop and record the new line. */
4837 if (CPP_TRADITIONAL (pfile))
4841 cpp_error_with_line (pfile, start_line, start_column,
4842 "unterminated character constant");
4845 if (CPP_PEDANTIC (pfile)
4846 && pfile->multiline_string_line == 0)
4848 cpp_pedwarn_with_line (pfile, start_line, start_column,
4849 "string constant runs past end of line");
4851 if (pfile->multiline_string_line == 0)
4852 pfile->multiline_string_line = start_line;
4859 /* Backslash newline is replaced by nothing at all. */
4860 CPP_ADJUST_WRITTEN (pfile, -1);
4865 /* ANSI stupidly requires that in \\ the second \
4866 is *not* prevented from combining with a newline. */
4869 CPP_PUTC (pfile, cc);
4881 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4882 CPP_PWRITTEN (pfile));
4883 pfile->only_seen_white = 0;
4884 return c == '\'' ? CPP_CHAR : CPP_STRING;
4887 if (!opts->dollars_in_ident)
4892 if (opts->cplusplus && PEEKC () == ':')
4901 if (c2 == c || c2 == '=')
4911 if (PEEKC () == '=')
4918 if (c2 == '-' && opts->chill)
4920 /* Chill style comment */
4921 if (opts->put_out_comments)
4922 parse_set_mark (&start_mark, pfile);
4923 FORWARD(1); /* Skip second '-'. */
4931 /* Don't consider final '\n' to be part of comment. */
4937 goto return_comment;
4939 if (c2 == '-' || c2 == '=' || c2 == '>')
4944 if (pfile->parsing_include_directive)
4948 CPP_PUTC (pfile, c);
4953 if (c == '\n' || c == EOF)
4956 "missing '>' in `#include <FILENAME>'");
4962 /* else fall through */
4971 CPP_RESERVE (pfile, 4);
4972 CPP_PUTC (pfile, c);
4973 CPP_PUTC (pfile, c2);
4977 CPP_PUTC_Q (pfile, GETC ());
4978 CPP_NUL_TERMINATE_Q (pfile);
4979 pfile->only_seen_white = 0;
4983 if (CPP_BUFFER (pfile)->has_escapes)
4988 if (pfile->output_escapes)
4989 CPP_PUTS (pfile, "@-", 2);
4990 parse_name (pfile, GETC ());
4993 else if (is_space [c])
4995 CPP_RESERVE (pfile, 2);
4996 if (pfile->output_escapes)
4997 CPP_PUTC_Q (pfile, '@');
4998 CPP_PUTC_Q (pfile, c);
5002 if (pfile->output_escapes)
5004 CPP_PUTS (pfile, "@@", 2);
5014 CPP_RESERVE(pfile, 2);
5015 CPP_PUTC_Q (pfile, '.');
5019 /* FIXME - misses the case "..\\\n." */
5020 if (c2 == '.' && PEEKN(1) == '.')
5022 CPP_RESERVE(pfile, 4);
5023 CPP_PUTC_Q (pfile, '.');
5024 CPP_PUTC_Q (pfile, '.');
5025 CPP_PUTC_Q (pfile, '.');
5027 CPP_NUL_TERMINATE_Q (pfile);
5028 pfile->only_seen_white = 0;
5035 pfile->only_seen_white = 0;
5037 CPP_RESERVE(pfile, 3);
5038 CPP_PUTC_Q (pfile, c);
5039 CPP_PUTC_Q (pfile, GETC ());
5040 CPP_NUL_TERMINATE_Q (pfile);
5046 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5048 CPP_PUTC (pfile, c);
5054 case '0': case '1': case '2': case '3': case '4':
5055 case '5': case '6': case '7': case '8': case '9':
5060 CPP_RESERVE (pfile, 2);
5061 CPP_PUTC_Q (pfile, c);
5066 if (!is_idchar[c] && c != '.'
5067 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5072 CPP_NUL_TERMINATE_Q (pfile);
5073 pfile->only_seen_white = 0;
5075 case 'b': case 'c': case 'd': case 'h': case 'o':
5076 case 'B': case 'C': case 'D': case 'H': case 'O':
5077 if (opts->chill && PEEKC () == '\'')
5079 pfile->only_seen_white = 0;
5080 CPP_RESERVE (pfile, 2);
5081 CPP_PUTC_Q (pfile, c);
5082 CPP_PUTC_Q (pfile, '\'');
5088 goto chill_number_eof;
5091 if (c == '\\' && PEEKC() == '\n')
5098 CPP_PUTC (pfile, c);
5102 CPP_RESERVE (pfile, 2);
5103 CPP_PUTC_Q (pfile, c);
5104 CPP_NUL_TERMINATE_Q (pfile);
5111 CPP_NUL_TERMINATE (pfile);
5118 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5119 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5120 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5121 case 'x': case 'y': case 'z':
5122 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5123 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5124 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5129 unsigned char *ident;
5130 int before_name_written = CPP_WRITTEN (pfile);
5132 parse_name (pfile, c);
5133 pfile->only_seen_white = 0;
5134 if (pfile->no_macro_expand)
5136 ident = pfile->token_buffer + before_name_written;
5137 ident_len = CPP_PWRITTEN (pfile) - ident;
5138 hp = cpp_lookup (pfile, ident, ident_len, -1);
5141 if (hp->type == T_DISABLED)
5143 if (pfile->output_escapes)
5144 { /* Return "@-IDENT", followed by '\0'. */
5146 CPP_RESERVE (pfile, 3);
5147 ident = pfile->token_buffer + before_name_written;
5148 CPP_ADJUST_WRITTEN (pfile, 2);
5149 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5156 /* If macro wants an arglist, verify that a '(' follows.
5157 first skip all whitespace, copying it to the output
5158 after the macro name. Then, if there is no '(',
5159 decide this is not a macro call and leave things that way. */
5160 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5162 struct parse_marker macro_mark;
5164 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5166 cpp_buffer *next_buf;
5167 cpp_skip_hspace (pfile);
5168 if (PEEKC () != EOF)
5170 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5171 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5172 CPP_BUFFER (pfile) = next_buf;
5174 parse_set_mark (¯o_mark, pfile);
5177 cpp_skip_hspace (pfile);
5179 is_macro_call = c == '(';
5185 parse_goto_mark (¯o_mark, pfile);
5186 parse_clear_mark (¯o_mark);
5190 /* This is now known to be a macro call. */
5192 /* it might not actually be a macro. */
5193 if (hp->type != T_MACRO) {
5194 int xbuf_len; U_CHAR *xbuf;
5195 CPP_SET_WRITTEN (pfile, before_name_written);
5196 special_symbol (hp, pfile);
5197 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5198 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5199 CPP_SET_WRITTEN (pfile, before_name_written);
5200 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5201 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5205 /* Expand the macro, reading arguments as needed,
5206 and push the expansion on the input stack. */
5207 macroexpand (pfile, hp);
5208 CPP_SET_WRITTEN (pfile, before_name_written);
5211 /* An extra "@ " is added to the end of a macro expansion
5212 to prevent accidental token pasting. We prefer to avoid
5213 unneeded extra spaces (for the sake of cpp-using tools like
5214 imake). Here we remove the space if it is safe to do so. */
5215 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5216 && pfile->buffer->rlimit[-2] == '@'
5217 && pfile->buffer->rlimit[-1] == ' ')
5219 int c1 = pfile->buffer->rlimit[-3];
5220 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5221 if (c2 == EOF || ! unsafe_chars (c1, c2))
5222 pfile->buffer->rlimit -= 2;
5227 case ' ': case '\t': case '\v': case '\r':
5230 CPP_PUTC (pfile, c);
5232 if (c == EOF || !is_hor_space[c])
5246 CPP_PUTC (pfile, c);
5247 if (pfile->only_seen_white == 0)
5248 pfile->only_seen_white = 1;
5250 output_line_command (pfile, 1, same_file);
5253 case '(': token = CPP_LPAREN; goto char1;
5254 case ')': token = CPP_RPAREN; goto char1;
5255 case '{': token = CPP_LBRACE; goto char1;
5256 case '}': token = CPP_RBRACE; goto char1;
5257 case ',': token = CPP_COMMA; goto char1;
5258 case ';': token = CPP_SEMICOLON; goto char1;
5264 pfile->only_seen_white = 0;
5265 CPP_PUTC (pfile, c);
5271 /* Like cpp_get_token, but skip spaces and comments. */
5273 cpp_get_non_space_token (
5276 int old_written = CPP_WRITTEN (pfile);
5279 enum cpp_token token = cpp_get_token (pfile);
5280 if (token != CPP_COMMENT && token != CPP_POP
5281 && token != CPP_HSPACE && token != CPP_VSPACE)
5283 CPP_SET_WRITTEN (pfile, old_written);
5287 /* Parse an identifier starting with C. */
5291 cpp_reader *pfile, int c)
5297 if (c == '\\' && PEEKC() == '\n')
5306 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5307 CPP_PUTC_Q (pfile, c);
5312 CPP_NUL_TERMINATE_Q (pfile);
5317 /* Maintain and search list of included files, for #import. */
5319 /* Hash a file name for import_hash_table. */
5327 while (*f) val += *f++;
5328 return (val%IMPORT_HASH_SIZE);
5331 /* Search for file FILENAME in import_hash_table.
5332 Return -2 if found, either a matching name or a matching inode.
5333 Otherwise, open the file and return a file descriptor if successful
5334 or -1 if unsuccessful. */
5340 struct file_name_list *searchptr)
5342 struct import_file *i;
5348 hashval = import_hash (filename);
5350 /* Attempt to find file in list of already included files */
5351 i = pfile->import_hash_table[hashval];
5354 if (!strcmp (filename, i->name))
5355 return -2; /* return found */
5358 /* Open it and try a match on inode/dev */
5359 fd = open_include_file (pfile, filename, searchptr);
5363 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5364 i = pfile->import_hash_table[h];
5366 /* Compare the inode and the device.
5367 Supposedly on some systems the inode is not a scalar. */
5368 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5369 && i->dev == sb.st_dev) {
5371 return -2; /* return found */
5376 return fd; /* Not found, return open file */
5379 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5387 struct import_file *i;
5391 hashval = import_hash (fname);
5393 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5394 i->name = (char *)xmalloc (strlen (fname)+1);
5395 strcpy (i->name, fname);
5396 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5398 i->next = pfile->import_hash_table[hashval];
5399 pfile->import_hash_table[hashval] = i;
5402 /* The file_name_map structure holds a mapping of file names for a
5403 particular directory. This mapping is read from the file named
5404 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5405 map filenames on a file system with severe filename restrictions,
5406 such as DOS. The format of the file name map file is just a series
5407 of lines with two tokens on each line. The first token is the name
5408 to map, and the second token is the actual name to use. */
5410 struct file_name_map
5412 struct file_name_map *map_next;
5417 #define FILE_NAME_MAP_FILE "header.gcc"
5419 /* Read a space delimited string of unlimited length from a stdio
5423 read_filename_string (
5431 set = alloc = xmalloc (len + 1);
5435 while ((ch = getc (f)) != EOF && ! is_space[ch])
5437 if (set - alloc == len)
5440 alloc = xrealloc (alloc, len + 1);
5441 set = alloc + len / 2;
5451 /* This structure holds a linked list of file name maps, one per directory. */
5452 struct file_name_map_list
5454 struct file_name_map_list *map_list_next;
5455 char *map_list_name;
5456 struct file_name_map *map_list_map;
5459 /* Read the file name map file for DIRNAME. */
5461 static struct file_name_map *
5466 register struct file_name_map_list *map_list_ptr;
5470 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5471 map_list_ptr = map_list_ptr->map_list_next)
5472 if (! strcmp (map_list_ptr->map_list_name, dirname))
5473 return map_list_ptr->map_list_map;
5475 map_list_ptr = ((struct file_name_map_list *)
5476 xmalloc (sizeof (struct file_name_map_list)));
5477 map_list_ptr->map_list_name = savestring (dirname);
5478 map_list_ptr->map_list_map = NULL;
5480 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5481 strcpy (name, dirname);
5484 strcat (name, FILE_NAME_MAP_FILE);
5485 f = fopen (name, "r");
5487 map_list_ptr->map_list_map = NULL;
5491 int dirlen = strlen (dirname);
5493 while ((ch = getc (f)) != EOF)
5496 struct file_name_map *ptr;
5500 from = read_filename_string (ch, f);
5501 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5503 to = read_filename_string (ch, f);
5505 ptr = ((struct file_name_map *)
5506 xmalloc (sizeof (struct file_name_map)));
5507 ptr->map_from = from;
5509 /* Make the real filename absolute. */
5514 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5515 strcpy (ptr->map_to, dirname);
5516 ptr->map_to[dirlen] = '/';
5517 strcpy (ptr->map_to + dirlen + 1, to);
5521 ptr->map_next = map_list_ptr->map_list_map;
5522 map_list_ptr->map_list_map = ptr;
5524 while ((ch = getc (f)) != '\n')
5531 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5532 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5534 return map_list_ptr->map_list_map;
5537 /* Try to open include file FILENAME. SEARCHPTR is the directory
5538 being tried from the include file search path. This function maps
5539 filenames on file systems based on information read by
5546 struct file_name_list *searchptr)
5548 register struct file_name_map *map;
5549 register char *from;
5552 if (searchptr && ! searchptr->got_name_map)
5554 searchptr->name_map = read_name_map (pfile,
5556 ? searchptr->fname : ".");
5557 searchptr->got_name_map = 1;
5560 /* First check the mapping for the directory we are using. */
5561 if (searchptr && searchptr->name_map)
5564 if (searchptr->fname)
5565 from += strlen (searchptr->fname) + 1;
5566 for (map = searchptr->name_map; map; map = map->map_next)
5568 if (! strcmp (map->map_from, from))
5570 /* Found a match. */
5571 return open (map->map_to, O_RDONLY, 0666);
5576 /* Try to find a mapping file for the particular directory we are
5577 looking in. Thus #include <sys/types.h> will look up sys/types.h
5578 in /usr/include/header.gcc and look up types.h in
5579 /usr/include/sys/header.gcc. */
5580 p = rindex (filename, '/');
5585 && strlen (searchptr->fname) == p - filename
5586 && ! strncmp (searchptr->fname, filename, p - filename))
5588 /* FILENAME is in SEARCHPTR, which we've already checked. */
5589 return open (filename, O_RDONLY, 0666);
5599 dir = (char *) alloca (p - filename + 1);
5600 bcopy (filename, dir, p - filename);
5601 dir[p - filename] = '\0';
5604 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5605 if (! strcmp (map->map_from, from))
5606 return open (map->map_to, O_RDONLY, 0666);
5608 return open (filename, O_RDONLY, 0666);
5611 /* Process the contents of include file FNAME, already open on descriptor F,
5613 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5614 "system" include directories (as decided by the `is_system_include'
5616 DIRPTR is the link in the dir path through which this file was found,
5617 or 0 if the file name was absolute or via the current directory.
5618 Return 1 on success, 0 on failure.
5620 The caller is responsible for the cpp_push_buffer. */
5627 int system_header_p,
5628 struct file_name_list *dirptr)
5634 cpp_buffer *fp; /* For input stack frame */
5635 int missing_newline = 0;
5637 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5639 cpp_perror_with_name (pfile, fname);
5641 cpp_pop_buffer (pfile);
5645 fp = CPP_BUFFER (pfile);
5646 fp->nominal_fname = fp->fname = fname;
5651 fp->system_header_p = system_header_p;
5654 fp->cleanup = file_cleanup;
5656 if (S_ISREG (st_mode)) {
5657 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5658 fp->alimit = fp->buf + st_size + 2;
5661 /* Read the file contents, knowing that st_size is an upper bound
5662 on the number of bytes we can read. */
5663 length = safe_read (f, fp->buf, st_size);
5664 fp->rlimit = fp->buf + length;
5665 if (length < 0) goto nope;
5667 else if (S_ISDIR (st_mode)) {
5668 cpp_error (pfile, "directory `%s' specified in #include", fname);
5672 /* Cannot count its file size before reading.
5673 First read the entire file into heap and
5674 copy them into buffer on stack. */
5679 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5682 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5684 goto nope; /* error! */
5686 if (st_size != bsize)
5687 break; /* End of file */
5689 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5695 if ((length > 0 && fp->buf[length - 1] != '\n')
5696 /* Backslash-newline at end is not good enough. */
5697 || (length > 1 && fp->buf[length - 2] == '\\')) {
5698 fp->buf[length++] = '\n';
5700 missing_newline = 1;
5703 fp->buf[length] = '\0';
5704 fp->rlimit = fp->buf + length;
5706 /* Close descriptor now, so nesting does not use lots of descriptors. */
5709 /* Must do this before calling trigraph_pcp, so that the correct file name
5710 will be printed in warning messages. */
5712 pfile->input_stack_listing_current = 0;
5722 if (missing_newline)
5725 if (CPP_PEDANTIC (pfile) && missing_newline)
5726 pedwarn ("file does not end in newline");
5729 input_file_stack_tick++;
5736 cpp_perror_with_name (pfile, fname);
5747 struct cpp_options *opts = CPP_OPTIONS (pfile);
5748 struct cpp_pending *pend;
5753 /* The code looks at the defaults through this pointer, rather than through
5754 the constant structure above. This pointer gets changed if an environment
5755 variable specifies other defaults. */
5756 struct default_include *include_defaults = include_defaults_array;
5758 /* Add dirs from CPATH after dirs from -I. */
5759 /* There seems to be confusion about what CPATH should do,
5760 so for the moment it is not documented. */
5761 /* Some people say that CPATH should replace the standard include dirs,
5762 but that seems pointless: it comes before them, so it overrides them
5764 p = (char *) getenv ("CPATH");
5765 if (p != 0 && ! opts->no_standard_includes)
5766 path_include (pfile, p);
5768 /* Now that dollars_in_ident is known, initialize is_idchar. */
5769 initialize_char_syntax (opts);
5771 /* Do partial setup of input buffer for the sake of generating
5772 early #line directives (when -g is in effect). */
5773 fp = cpp_push_buffer (pfile, NULL, 0);
5774 if (opts->in_fname == NULL)
5775 opts->in_fname = "";
5776 fp->nominal_fname = fp->fname = opts->in_fname;
5779 /* Install __LINE__, etc. Must follow initialize_char_syntax
5780 and option processing. */
5781 initialize_builtins (pfile);
5783 /* Do standard #defines and assertions
5784 that identify system and machine type. */
5786 if (!opts->inhibit_predefs) {
5787 char *p = (char *) alloca (strlen (predefs) + 1);
5788 strcpy (p, predefs);
5791 while (*p == ' ' || *p == '\t')
5793 /* Handle -D options. */
5794 if (p[0] == '-' && p[1] == 'D') {
5796 while (*p && *p != ' ' && *p != '\t')
5800 if (opts->debug_output)
5801 output_line_command (pfile, 0, same_file);
5802 cpp_define (pfile, q);
5803 while (*p == ' ' || *p == '\t')
5805 } else if (p[0] == '-' && p[1] == 'A') {
5806 /* Handle -A options (assertions). */
5815 past_name = assertion;
5816 /* Locate end of name. */
5817 while (*past_name && *past_name != ' '
5818 && *past_name != '\t' && *past_name != '(')
5820 /* Locate `(' at start of value. */
5822 while (*value && (*value == ' ' || *value == '\t'))
5824 if (*value++ != '(')
5826 while (*value && (*value == ' ' || *value == '\t'))
5829 /* Locate end of value. */
5830 while (*past_value && *past_value != ' '
5831 && *past_value != '\t' && *past_value != ')')
5833 termination = past_value;
5834 while (*termination && (*termination == ' ' || *termination == '\t'))
5836 if (*termination++ != ')')
5838 if (*termination && *termination != ' ' && *termination != '\t')
5840 /* Temporarily null-terminate the value. */
5841 save_char = *termination;
5842 *termination = '\0';
5843 /* Install the assertion. */
5844 make_assertion (pfile, "-A", assertion);
5845 *termination = (char) save_char;
5847 while (*p == ' ' || *p == '\t')
5855 /* Now handle the command line options. */
5857 /* Do -U's, -D's and -A's in the order they were seen. */
5858 /* First reverse the list. */
5859 opts->pending = nreverse_pending (opts->pending);
5861 for (pend = opts->pending; pend; pend = pend->next)
5863 if (pend->cmd != NULL && pend->cmd[0] == '-')
5865 switch (pend->cmd[1])
5868 if (opts->debug_output)
5869 output_line_command (pfile, 0, same_file);
5870 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5873 if (opts->debug_output)
5874 output_line_command (pfile, 0, same_file);
5875 cpp_define (pfile, pend->arg);
5878 make_assertion (pfile, "-A", pend->arg);
5884 opts->done_initializing = 1;
5886 { /* read the appropriate environment variable and if it exists
5887 replace include_defaults with the listed path. */
5889 switch ((opts->objc << 1) + opts->cplusplus)
5892 epath = getenv ("C_INCLUDE_PATH");
5895 epath = getenv ("CPLUS_INCLUDE_PATH");
5898 epath = getenv ("OBJC_INCLUDE_PATH");
5901 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5904 /* If the environment var for this language is set,
5905 add to the default list of include directories. */
5907 char *nstore = (char *) alloca (strlen (epath) + 2);
5909 char *startp, *endp;
5911 for (num_dirs = 1, startp = epath; *startp; startp++)
5912 if (*startp == PATH_SEPARATOR)
5915 = (struct default_include *) xmalloc ((num_dirs
5916 * sizeof (struct default_include))
5917 + sizeof (include_defaults_array));
5918 startp = endp = epath;
5921 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5922 if ((*endp == PATH_SEPARATOR)
5924 strncpy (nstore, startp, endp-startp);
5926 strcpy (nstore, ".");
5928 nstore[endp-startp] = '\0';
5930 include_defaults[num_dirs].fname = savestring (nstore);
5931 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5932 include_defaults[num_dirs].cxx_aware = 1;
5936 endp = startp = endp + 1;
5940 /* Put the usual defaults back in at the end. */
5941 bcopy ((char *) include_defaults_array,
5942 (char *) &include_defaults[num_dirs],
5943 sizeof (include_defaults_array));
5947 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5948 opts->first_system_include = opts->before_system;
5950 /* Unless -fnostdinc,
5951 tack on the standard include file dirs to the specified list */
5952 if (!opts->no_standard_includes) {
5953 struct default_include *p = include_defaults;
5954 char *specd_prefix = opts->include_prefix;
5955 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5956 int default_len = 0;
5957 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5958 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5959 default_len = strlen (default_prefix) - 7;
5960 default_prefix[default_len] = 0;
5962 /* Search "translated" versions of GNU directories.
5963 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5964 if (specd_prefix != 0 && default_len != 0)
5965 for (p = include_defaults; p->fname; p++) {
5966 /* Some standard dirs are only for C++. */
5968 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5969 /* Does this dir start with the prefix? */
5970 if (!strncmp (p->fname, default_prefix, default_len)) {
5971 /* Yes; change prefix and add to search list. */
5972 struct file_name_list *new
5973 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5974 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5975 char *str = (char *) xmalloc (this_len + 1);
5976 strcpy (str, specd_prefix);
5977 strcat (str, p->fname + default_len);
5979 new->control_macro = 0;
5980 new->c_system_include_path = !p->cxx_aware;
5981 new->got_name_map = 0;
5982 append_include_chain (pfile, new, new);
5983 if (opts->first_system_include == 0)
5984 opts->first_system_include = new;
5988 /* Search ordinary names for GNU include directories. */
5989 for (p = include_defaults; p->fname; p++) {
5990 /* Some standard dirs are only for C++. */
5992 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5993 struct file_name_list *new
5994 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5995 new->control_macro = 0;
5996 new->c_system_include_path = !p->cxx_aware;
5997 new->fname = p->fname;
5998 new->got_name_map = 0;
5999 append_include_chain (pfile, new, new);
6000 if (opts->first_system_include == 0)
6001 opts->first_system_include = new;
6006 /* Tack the after_include chain at the end of the include chain. */
6007 append_include_chain (pfile, opts->after_include, opts->last_after_include);
6008 if (opts->first_system_include == 0)
6009 opts->first_system_include = opts->after_include;
6011 /* With -v, print the list of dirs to search. */
6012 if (opts->verbose) {
6013 struct file_name_list *p;
6014 fprintf (stderr, "#include \"...\" search starts here:\n");
6015 for (p = opts->include; p; p = p->next) {
6016 if (p == opts->first_bracket_include)
6017 fprintf (stderr, "#include <...> search starts here:\n");
6018 fprintf (stderr, " %s\n", p->fname);
6020 fprintf (stderr, "End of search list.\n");
6023 /* Scan the -imacros files before the main input.
6024 Much like #including them, but with no_output set
6025 so that only their macro definitions matter. */
6027 opts->no_output++; pfile->no_record_file++;
6028 for (pend = opts->pending; pend; pend = pend->next)
6030 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6032 int fd = open (pend->arg, O_RDONLY, 0666);
6035 cpp_perror_with_name (pfile, pend->arg);
6036 return FATAL_EXIT_CODE;
6038 cpp_push_buffer (pfile, NULL, 0);
6039 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6040 cpp_scan_buffer (pfile);
6043 opts->no_output--; pfile->no_record_file--;
6045 /* Copy the entire contents of the main input file into
6046 the stacked input buffer previously allocated for it. */
6047 if (fname == NULL || *fname == 0) {
6050 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6051 cpp_pfatal_with_name (pfile, fname);
6053 /* -MG doesn't select the form of output and must be specified with one of
6054 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6055 inhibit compilation. */
6056 if (opts->print_deps_missing_files
6057 && (opts->print_deps == 0 || !opts->no_output))
6058 fatal (pfile, "-MG must be specified with one of -M or -MM");
6060 /* Either of two environment variables can specify output of deps.
6061 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6062 where OUTPUT_FILE is the file to write deps info to
6063 and DEPS_TARGET is the target to mention in the deps. */
6065 if (opts->print_deps == 0
6066 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6067 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6068 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6074 spec = getenv ("SUNPRO_DEPENDENCIES");
6075 opts->print_deps = 2;
6078 opts->print_deps = 1;
6081 /* Find the space before the DEPS_TARGET, if there is one. */
6082 /* This should use index. (mrs) */
6083 while (*s != 0 && *s != ' ') s++;
6086 opts->deps_target = s + 1;
6087 output_file = (char *) xmalloc (s - spec + 1);
6088 bcopy (spec, output_file, s - spec);
6089 output_file[s - spec] = 0;
6093 opts->deps_target = 0;
6097 opts->deps_file = output_file;
6098 opts->print_deps_append = 1;
6101 /* For -M, print the expected object file name
6102 as the target of this Make-rule. */
6103 if (opts->print_deps)
6105 pfile->deps_allocated_size = 200;
6106 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6107 pfile->deps_buffer[0] = 0;
6108 pfile->deps_size = 0;
6109 pfile->deps_column = 0;
6111 if (opts->deps_target)
6112 deps_output (pfile, opts->deps_target, ':');
6113 else if (*opts->in_fname == 0)
6114 deps_output (pfile, "-", ':');
6120 /* Discard all directory prefixes from filename. */
6121 if ((q = rindex (opts->in_fname, '/')) != NULL
6122 #ifdef DIR_SEPARATOR
6123 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6130 /* Copy remainder to mungable area. */
6131 p = (char *) alloca (strlen(q) + 8);
6134 /* Output P, but remove known suffixes. */
6138 && p[len - 2] == '.'
6139 && index("cCsSm", p[len - 1]))
6142 && p[len - 3] == '.'
6143 && p[len - 2] == 'c'
6144 && p[len - 1] == 'c')
6147 && p[len - 4] == '.'
6148 && p[len - 3] == 'c'
6149 && p[len - 2] == 'x'
6150 && p[len - 1] == 'x')
6153 && p[len - 4] == '.'
6154 && p[len - 3] == 'c'
6155 && p[len - 2] == 'p'
6156 && p[len - 1] == 'p')
6159 /* Supply our own suffix. */
6170 deps_output (pfile, p, ':');
6171 deps_output (pfile, opts->in_fname, ' ');
6176 /* Make sure data ends with a newline. And put a null after it. */
6178 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6179 /* Backslash-newline at end is not good enough. */
6180 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6181 fp->buf[fp->length++] = '\n';
6182 missing_newline = 1;
6184 fp->buf[fp->length] = '\0';
6186 /* Unless inhibited, convert trigraphs in the input. */
6192 /* Scan the -include files before the main input.
6193 We push these in reverse order, so that the first one is handled first. */
6195 pfile->no_record_file++;
6196 opts->pending = nreverse_pending (opts->pending);
6197 for (pend = opts->pending; pend; pend = pend->next)
6199 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6201 int fd = open (pend->arg, O_RDONLY, 0666);
6204 cpp_perror_with_name (pfile, pend->arg);
6205 return FATAL_EXIT_CODE;
6207 cpp_push_buffer (pfile, NULL, 0);
6208 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6211 pfile->no_record_file--;
6213 /* Free the pending list. */
6214 for (pend = opts->pending; pend; )
6216 struct cpp_pending *next = pend->next;
6220 opts->pending = NULL;
6223 /* Scan the input, processing macros and directives. */
6225 rescan (&outbuf, 0);
6227 if (missing_newline)
6230 if (CPP_PEDANTIC (pfile) && missing_newline)
6231 pedwarn ("file does not end in newline");
6234 if (finclude (pfile, f, fname, 0, NULL_PTR))
6235 output_line_command (pfile, 0, same_file);
6236 return SUCCESS_EXIT_CODE;
6243 bzero ((char *) pfile, sizeof (cpp_reader));
6244 pfile->get_token = cpp_get_token;
6246 pfile->token_buffer_size = 200;
6247 pfile->token_buffer = (U_CHAR*)xmalloc (pfile->token_buffer_size);
6248 CPP_SET_WRITTEN (pfile, 0);
6250 pfile->system_include_depth = 0;
6251 pfile->dont_repeat_files = 0;
6252 pfile->all_include_files = 0;
6253 pfile->max_include_len = 0;
6254 pfile->timebuf = NULL;
6255 pfile->only_seen_white = 1;
6256 pfile->buffer = CPP_NULL_BUFFER(pfile);
6259 static struct cpp_pending *
6261 struct cpp_pending *list)
6264 register struct cpp_pending *prev = 0, *next, *pend;
6265 for (pend = list; pend; pend = next)
6280 struct cpp_pending *pend
6281 = (struct cpp_pending*)xmalloc (sizeof (struct cpp_pending));
6284 pend->next = CPP_OPTIONS (pfile)->pending;
6285 CPP_OPTIONS (pfile)->pending = pend;
6288 /* Handle command-line options in (argc, argv).
6289 Can be called multiple times, to handle multiple sets of options.
6290 Returns if an unrecognized option is seen.
6291 Returns number of handled arguments. */
6294 cpp_handle_options (
6300 struct cpp_options *opts = CPP_OPTIONS (pfile);
6301 for (i = 0; i < argc; i++) {
6302 if (argv[i][0] != '-') {
6303 if (opts->out_fname != NULL)
6304 fatal ("Usage: %s [switches] input output", argv[0]);
6305 else if (opts->in_fname != NULL)
6306 opts->out_fname = argv[i];
6308 opts->in_fname = argv[i];
6310 switch (argv[i][1]) {
6313 if (!strcmp (argv[i], "-include")
6314 || !strcmp (argv[i], "-imacros")) {
6316 fatal ("Filename missing after `%s' option", argv[i]);
6318 push_pending (pfile, argv[i], argv[i+1]), i++;
6320 if (!strcmp (argv[i], "-iprefix")) {
6322 fatal ("Filename missing after `-iprefix' option");
6324 opts->include_prefix = argv[++i];
6326 if (!strcmp (argv[i], "-ifoutput")) {
6327 opts->output_conditionals = 1;
6329 if (!strcmp (argv[i], "-isystem")) {
6330 struct file_name_list *dirtmp;
6333 fatal ("Filename missing after `-isystem' option");
6335 dirtmp = (struct file_name_list *)
6336 xmalloc (sizeof (struct file_name_list));
6338 dirtmp->control_macro = 0;
6339 dirtmp->c_system_include_path = 1;
6340 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6341 strcpy (dirtmp->fname, argv[++i]);
6342 dirtmp->got_name_map = 0;
6344 if (opts->before_system == 0)
6345 opts->before_system = dirtmp;
6347 opts->last_before_system->next = dirtmp;
6348 opts->last_before_system = dirtmp; /* Tail follows the last one */
6350 /* Add directory to end of path for includes,
6351 with the default prefix at the front of its name. */
6352 if (!strcmp (argv[i], "-iwithprefix")) {
6353 struct file_name_list *dirtmp;
6356 if (opts->include_prefix != 0)
6357 prefix = opts->include_prefix;
6359 prefix = savestring (GCC_INCLUDE_DIR);
6360 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6361 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6362 prefix[strlen (prefix) - 7] = 0;
6365 dirtmp = (struct file_name_list *)
6366 xmalloc (sizeof (struct file_name_list));
6367 dirtmp->next = 0; /* New one goes on the end */
6368 dirtmp->control_macro = 0;
6369 dirtmp->c_system_include_path = 0;
6371 fatal ("Directory name missing after `-iwithprefix' option");
6373 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6374 + strlen (prefix) + 1);
6375 strcpy (dirtmp->fname, prefix);
6376 strcat (dirtmp->fname, argv[++i]);
6377 dirtmp->got_name_map = 0;
6379 if (opts->after_include == 0)
6380 opts->after_include = dirtmp;
6382 opts->last_after_include->next = dirtmp;
6383 opts->last_after_include = dirtmp; /* Tail follows the last one */
6385 /* Add directory to main path for includes,
6386 with the default prefix at the front of its name. */
6387 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6388 struct file_name_list *dirtmp;
6391 if (opts->include_prefix != 0)
6392 prefix = opts->include_prefix;
6394 prefix = savestring (GCC_INCLUDE_DIR);
6395 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6396 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6397 prefix[strlen (prefix) - 7] = 0;
6400 dirtmp = (struct file_name_list *)
6401 xmalloc (sizeof (struct file_name_list));
6402 dirtmp->next = 0; /* New one goes on the end */
6403 dirtmp->control_macro = 0;
6404 dirtmp->c_system_include_path = 0;
6406 fatal ("Directory name missing after `-iwithprefixbefore' option");
6408 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6409 + strlen (prefix) + 1);
6410 strcpy (dirtmp->fname, prefix);
6411 strcat (dirtmp->fname, argv[++i]);
6412 dirtmp->got_name_map = 0;
6414 append_include_chain (pfile, dirtmp, dirtmp);
6416 /* Add directory to end of path for includes. */
6417 if (!strcmp (argv[i], "-idirafter")) {
6418 struct file_name_list *dirtmp;
6420 dirtmp = (struct file_name_list *)
6421 xmalloc (sizeof (struct file_name_list));
6422 dirtmp->next = 0; /* New one goes on the end */
6423 dirtmp->control_macro = 0;
6424 dirtmp->c_system_include_path = 0;
6426 fatal ("Directory name missing after `-idirafter' option");
6428 dirtmp->fname = argv[++i];
6429 dirtmp->got_name_map = 0;
6431 if (opts->after_include == 0)
6432 opts->after_include = dirtmp;
6434 opts->last_after_include->next = dirtmp;
6435 opts->last_after_include = dirtmp; /* Tail follows the last one */
6440 if (opts->out_fname != NULL)
6441 fatal ("Output filename specified twice");
6443 fatal ("Filename missing after -o option");
6444 opts->out_fname = argv[++i];
6445 if (!strcmp (opts->out_fname, "-"))
6446 opts->out_fname = "";
6450 if (!strcmp (argv[i], "-pedantic"))
6451 CPP_PEDANTIC (pfile) = 1;
6452 else if (!strcmp (argv[i], "-pedantic-errors")) {
6453 CPP_PEDANTIC (pfile) = 1;
6454 opts->pedantic_errors = 1;
6457 else if (!strcmp (argv[i], "-pcp")) {
6458 char *pcp_fname = argv[++i];
6460 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6461 ? fopen (pcp_fname, "w")
6462 : fdopen (dup (fileno (stdout)), "w"));
6463 if (pcp_outfile == 0)
6464 cpp_pfatal_with_name (pfile, pcp_fname);
6471 if (!strcmp (argv[i], "-traditional")) {
6472 opts->traditional = 1;
6473 if (opts->dollars_in_ident > 0)
6474 opts->dollars_in_ident = 1;
6475 } else if (!strcmp (argv[i], "-trigraphs")) {
6477 opts->no_trigraphs = 0;
6482 if (! strcmp (argv[i], "-lang-c"))
6483 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6484 if (! strcmp (argv[i], "-lang-c++"))
6485 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6486 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6487 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6488 if (! strcmp (argv[i], "-lang-objc"))
6489 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6490 if (! strcmp (argv[i], "-lang-objc++"))
6491 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6492 if (! strcmp (argv[i], "-lang-asm"))
6494 if (! strcmp (argv[i], "-lint"))
6496 if (! strcmp (argv[i], "-lang-chill"))
6497 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6498 opts->traditional = 1, opts->no_trigraphs = 1;
6502 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6506 opts->inhibit_warnings = 1;
6510 if (!strcmp (argv[i], "-Wtrigraphs"))
6511 opts->warn_trigraphs = 1;
6512 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6513 opts->warn_trigraphs = 0;
6514 else if (!strcmp (argv[i], "-Wcomment"))
6515 opts->warn_comments = 1;
6516 else if (!strcmp (argv[i], "-Wno-comment"))
6517 opts->warn_comments = 0;
6518 else if (!strcmp (argv[i], "-Wcomments"))
6519 opts->warn_comments = 1;
6520 else if (!strcmp (argv[i], "-Wno-comments"))
6521 opts->warn_comments = 0;
6522 else if (!strcmp (argv[i], "-Wtraditional"))
6523 opts->warn_stringify = 1;
6524 else if (!strcmp (argv[i], "-Wno-traditional"))
6525 opts->warn_stringify = 0;
6526 else if (!strcmp (argv[i], "-Wimport"))
6527 opts->warn_import = 1;
6528 else if (!strcmp (argv[i], "-Wno-import"))
6529 opts->warn_import = 0;
6530 else if (!strcmp (argv[i], "-Werror"))
6531 opts->warnings_are_errors = 1;
6532 else if (!strcmp (argv[i], "-Wno-error"))
6533 opts->warnings_are_errors = 0;
6534 else if (!strcmp (argv[i], "-Wall"))
6536 opts->warn_trigraphs = 1;
6537 opts->warn_comments = 1;
6542 /* The style of the choices here is a bit mixed.
6543 The chosen scheme is a hybrid of keeping all options in one string
6544 and specifying each option in a separate argument:
6545 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6546 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6547 -M[M][G][D file]. This is awkward to handle in specs, and is not
6549 /* ??? -MG must be specified in addition to one of -M or -MM.
6550 This can be relaxed in the future without breaking anything.
6551 The converse isn't true. */
6553 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6554 if (!strcmp (argv[i], "-MG"))
6556 opts->print_deps_missing_files = 1;
6559 if (!strcmp (argv[i], "-M"))
6560 opts->print_deps = 2;
6561 else if (!strcmp (argv[i], "-MM"))
6562 opts->print_deps = 1;
6563 else if (!strcmp (argv[i], "-MD"))
6564 opts->print_deps = 2;
6565 else if (!strcmp (argv[i], "-MMD"))
6566 opts->print_deps = 1;
6567 /* For -MD and -MMD options, write deps on file named by next arg. */
6568 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6571 fatal ("Filename missing after %s option", argv[i]);
6572 opts->deps_file = argv[++i];
6576 /* For -M and -MM, write deps on standard output
6577 and suppress the usual output. */
6578 opts->no_output = 1;
6584 char *p = argv[i] + 2;
6586 while ((c = *p++) != 0) {
6587 /* Arg to -d specifies what parts of macros to dump */
6590 opts->dump_macros = dump_only;
6591 opts->no_output = 1;
6594 opts->dump_macros = dump_names;
6597 opts->dump_macros = dump_definitions;
6605 if (argv[i][2] == '3')
6606 opts->debug_output = 1;
6610 fprintf (stderr, "GNU CPP version %s", version_string);
6611 #ifdef TARGET_VERSION
6614 fprintf (stderr, "\n");
6615 // opts->verbose = 1;
6619 opts->print_include_names = 1;
6623 if (argv[i][2] != 0)
6624 push_pending (pfile, "-D", argv[i] + 2);
6625 else if (i + 1 == argc)
6626 fatal ("Macro name missing after -D option");
6628 i++, push_pending (pfile, "-D", argv[i]);
6635 if (argv[i][2] != 0)
6637 else if (i + 1 == argc)
6638 fatal ("Assertion missing after -A option");
6642 if (!strcmp (p, "-")) {
6643 struct cpp_pending **ptr;
6644 /* -A- eliminates all predefined macros and assertions.
6645 Let's include also any that were specified earlier
6646 on the command line. That way we can get rid of any
6647 that were passed automatically in from GCC. */
6649 opts->inhibit_predefs = 1;
6650 for (ptr = &opts->pending; *ptr != NULL; )
6652 struct cpp_pending *pend = *ptr;
6653 if (pend->cmd && pend->cmd[0] == '-'
6654 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6663 push_pending (pfile, "-A", p);
6668 case 'U': /* JF #undef something */
6669 if (argv[i][2] != 0)
6670 push_pending (pfile, "-U", argv[i] + 2);
6671 else if (i + 1 == argc)
6672 fatal ("Macro name missing after -U option");
6674 push_pending (pfile, "-U", argv[i+1]), i++;
6678 opts->put_out_comments = 1;
6681 case 'E': /* -E comes from cc -E; ignore it. */
6685 opts->no_line_commands = 1;
6688 case '$': /* Don't include $ in identifiers. */
6689 opts->dollars_in_ident = 0;
6692 case 'I': /* Add directory to path for includes. */
6694 struct file_name_list *dirtmp;
6696 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6697 && !strcmp (argv[i] + 2, "-")) {
6698 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6699 /* Don't use any preceding -I directories for #include <...>. */
6700 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6703 dirtmp = (struct file_name_list *)
6704 xmalloc (sizeof (struct file_name_list));
6705 dirtmp->next = 0; /* New one goes on the end */
6706 dirtmp->control_macro = 0;
6707 dirtmp->c_system_include_path = 0;
6708 if (argv[i][2] != 0)
6709 dirtmp->fname = argv[i] + 2;
6710 else if (i + 1 == argc)
6711 fatal ("Directory name missing after -I option");
6713 dirtmp->fname = argv[++i];
6714 dirtmp->got_name_map = 0;
6715 append_include_chain (pfile, dirtmp, dirtmp);
6721 if (!strcmp (argv[i], "-nostdinc"))
6722 /* -nostdinc causes no default include directories.
6723 You must specify all include-file directories with -I. */
6724 opts->no_standard_includes = 1;
6725 else if (!strcmp (argv[i], "-nostdinc++"))
6726 /* -nostdinc++ causes no default C++-specific include directories. */
6727 opts->no_standard_cplusplus_includes = 1;
6729 else if (!strcmp (argv[i], "-noprecomp"))
6735 /* Sun compiler passes undocumented switch "-undef".
6736 Let's assume it means to inhibit the predefined symbols. */
6737 opts->inhibit_predefs = 1;
6740 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6741 if (opts->in_fname == NULL) {
6742 opts->in_fname = "";
6744 } else if (opts->out_fname == NULL) {
6745 opts->out_fname = "";
6747 } /* else fall through into error */
6761 struct cpp_options *opts = CPP_OPTIONS (pfile);
6763 if (opts->print_deps)
6765 /* Stream on which to print the dependency information. */
6768 /* Don't actually write the deps file if compilation has failed. */
6769 if (pfile->errors == 0)
6771 char *deps_mode = opts->print_deps_append ? "a" : "w";
6772 if (opts->deps_file == 0)
6773 deps_stream = stdout;
6774 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6775 cpp_pfatal_with_name (pfile, opts->deps_file);
6776 fputs (pfile->deps_buffer, deps_stream);
6777 putc ('\n', deps_stream);
6778 if (opts->deps_file)
6780 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6781 fatal ("I/O error on output");
6787 /* Free resources used by PFILE. */
6794 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6795 cpp_pop_buffer (pfile);
6797 if (pfile->token_buffer)
6799 free (pfile->token_buffer);
6800 pfile->token_buffer = NULL;
6803 if (pfile->deps_buffer)
6805 free (pfile->deps_buffer);
6806 pfile->deps_buffer = NULL;
6807 pfile->deps_allocated_size = 0;
6810 while (pfile->if_stack)
6812 IF_STACK_FRAME *temp = pfile->if_stack;
6813 pfile->if_stack = temp->next;
6817 while (pfile->dont_repeat_files)
6819 struct file_name_list *temp = pfile->dont_repeat_files;
6820 pfile->dont_repeat_files = temp->next;
6825 while (pfile->all_include_files)
6827 struct file_name_list *temp = pfile->all_include_files;
6828 pfile->all_include_files = temp->next;
6833 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6835 register struct import_file *imp = pfile->import_hash_table[i];
6838 struct import_file *next = imp->next;
6843 pfile->import_hash_table[i] = 0;
6846 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6848 while (pfile->assertion_hashtab[i])
6849 delete_assertion (pfile->assertion_hashtab[i]);
6852 cpp_hash_cleanup (pfile);
6858 struct directive *keyword,
6859 U_CHAR *buf, U_CHAR *limit)
6861 long symstart; /* remember where symbol name starts */
6863 int sym_length; /* and how long it is */
6864 struct arglist *tokens = NULL;
6866 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6867 && !CPP_BUFFER (pfile)->system_header_p)
6868 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6870 cpp_skip_hspace (pfile);
6871 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6872 parse_name (pfile, GETC());
6873 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6876 cpp_skip_hspace (pfile);
6877 if (PEEKC() != '(') {
6878 cpp_error (pfile, "missing token-sequence in `#assert'");
6884 tokens = read_token_list (pfile, &error_flag);
6888 cpp_error (pfile, "empty token-sequence in `#assert'");
6891 cpp_skip_hspace (pfile);
6893 if (c != EOF && c != '\n')
6894 cpp_pedwarn (pfile, "junk at end of `#assert'");
6895 skip_rest_of_line (pfile);
6898 /* If this name isn't already an assertion name, make it one.
6899 Error if it was already in use in some other way. */
6902 ASSERTION_HASHNODE *hp;
6903 U_CHAR *symname = pfile->token_buffer + symstart;
6904 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6905 struct tokenlist_list *value
6906 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6908 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6910 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6911 cpp_error (pfile, "`defined' redefined as assertion");
6912 hp = assertion_install (pfile, symname, sym_length, hashcode);
6915 /* Add the spec'd token-sequence to the list of such. */
6916 value->tokens = tokens;
6917 value->next = hp->value;
6920 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6923 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6924 skip_rest_of_line (pfile);
6931 struct directive *keyword,
6932 U_CHAR *buf, U_CHAR *limit)
6934 long symstart; /* remember where symbol name starts */
6935 int sym_length; /* and how long it is */
6938 struct arglist *tokens = NULL;
6939 int tokens_specified = 0;
6941 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6942 && !CPP_BUFFER (pfile)->system_header_p)
6943 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6945 cpp_skip_hspace (pfile);
6947 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6948 parse_name (pfile, GETC());
6949 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6952 cpp_skip_hspace (pfile);
6953 if (PEEKC() == '(') {
6956 tokens = read_token_list (pfile, &error_flag);
6960 cpp_error (pfile, "empty token list in `#unassert'");
6964 tokens_specified = 1;
6967 cpp_skip_hspace (pfile);
6969 if (c != EOF && c != '\n')
6970 cpp_error (pfile, "junk at end of `#unassert'");
6971 skip_rest_of_line (pfile);
6974 ASSERTION_HASHNODE *hp;
6975 U_CHAR *symname = pfile->token_buffer + symstart;
6976 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6977 struct tokenlist_list *tail, *prev;
6979 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6983 /* If no token list was specified, then eliminate this assertion
6985 if (! tokens_specified)
6986 delete_assertion (hp);
6988 /* If a list of tokens was given, then delete any matching list. */
6993 struct tokenlist_list *next = tail->next;
6994 if (compare_token_lists (tail->tokens, tokens)) {
6998 hp->value = tail->next;
6999 free_token_list (tail->tokens);
7009 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7012 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7013 skip_rest_of_line (pfile);
7017 /* Test whether there is an assertion named NAME
7018 and optionally whether it has an asserted token list TOKENS.
7019 NAME is not null terminated; its length is SYM_LENGTH.
7020 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7027 int tokens_specified,
7028 struct arglist *tokens)
7030 ASSERTION_HASHNODE *hp;
7031 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7033 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7034 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7036 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7038 /* It is not an assertion; just return false. */
7041 /* If no token list was specified, then value is 1. */
7042 if (! tokens_specified)
7046 struct tokenlist_list *tail;
7050 /* If a list of tokens was given,
7051 then succeed if the assertion records a matching list. */
7054 if (compare_token_lists (tail->tokens, tokens))
7059 /* Fail if the assertion has no matching list. */
7064 /* Compare two lists of tokens for equality including order of tokens. */
7067 compare_token_lists (
7068 struct arglist *l1, struct arglist *l2 )
7071 if (l1->length != l2->length)
7073 if (strncmp (l1->name, l2->name, l1->length))
7079 /* Succeed if both lists end at the same time. */
7084 reverse_token_list (
7085 struct arglist *tokens)
7087 register struct arglist *prev = 0, *this, *next;
7088 for (this = tokens; this; this = next)
7097 /* Read a space-separated list of tokens ending in a close parenthesis.
7098 Return a list of strings, in the order they were written.
7099 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7101 static struct arglist *
7106 struct arglist *token_ptrs = 0;
7111 FORWARD (1); /* Skip '(' */
7113 /* Loop over the assertion value tokens. */
7116 struct arglist *temp;
7117 long name_written = CPP_WRITTEN (pfile);
7118 int eofp = 0; int c;
7120 cpp_skip_hspace (pfile);
7124 /* Find the end of the token. */
7127 CPP_PUTC (pfile, c);
7135 CPP_PUTC (pfile, c);
7137 else if (c == '"' || c == '\'')
7140 cpp_get_token (pfile);
7146 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7147 && c != '"' && c != '\'')
7149 CPP_PUTC (pfile, c);
7152 if (c != EOF) FORWARD(-1);
7155 length = CPP_WRITTEN (pfile) - name_written;
7156 temp = (struct arglist *)
7157 xmalloc (sizeof (struct arglist) + length + 1);
7158 temp->name = (U_CHAR *) (temp + 1);
7159 bcopy ((char *) (pfile->token_buffer + name_written),
7160 (char *) temp->name, length);
7161 temp->name[length] = 0;
7162 temp->next = token_ptrs;
7164 temp->length = length;
7166 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7168 if (c == EOF || c == '\n')
7171 "unterminated token sequence following `#' operator");
7176 /* We accumulated the names in reverse order.
7177 Now reverse them to get the proper order. */
7178 return reverse_token_list (token_ptrs);
7183 struct arglist *tokens)
7186 struct arglist *next = tokens->next;
7187 free (tokens->name);
7193 /* Get the file-mode and data size of the file open on FD
7194 and store them in *MODE_POINTER and *SIZE_POINTER. */
7197 file_size_and_mode (
7200 long int *size_pointer)
7204 if (fstat (fd, &sbuf) < 0) return (-1);
7205 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7206 if (size_pointer) *size_pointer = sbuf.st_size;
7210 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7211 retrying if necessary. Return a negative value if an error occurs,
7212 otherwise return the actual number of bytes read,
7213 which must be LEN unless end-of-file was reached. */
7223 int nchars = read (desc, ptr, left);
7244 unsigned size = strlen (input);
7245 char *output = xmalloc (size + 1);
7246 strcpy (output, input);
7250 /* Initialize PMARK to remember the current position of PFILE. */
7253 struct parse_marker *pmark,
7256 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7257 pmark->next = pbuf->marks;
7258 pbuf->marks = pmark;
7260 pmark->position = pbuf->cur - pbuf->buf;
7263 /* Cleanup PMARK - we no longer need it. */
7266 struct parse_marker *pmark)
7268 struct parse_marker **pp = &pmark->buf->marks;
7269 for (; ; pp = &(*pp)->next) {
7270 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7271 if (*pp == pmark) break;
7276 /* Backup the current position of PFILE to that saved in PMARK. */
7280 struct parse_marker *pmark,
7283 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7284 if (pbuf != pmark->buf)
7285 fatal ("internal error %s", "parse_goto_mark");
7286 pbuf->cur = pbuf->buf + pmark->position;
7289 /* Reset PMARK to point to the current position of PFILE. (Same
7290 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7294 struct parse_marker *pmark,
7297 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7298 if (pbuf != pmark->buf)
7299 fatal ("internal error %s", "parse_move_mark");
7300 pmark->position = pbuf->cur - pbuf->buf;
7304 cpp_read_check_assertion (
7307 int name_start = CPP_WRITTEN (pfile);
7308 int name_length, name_written;
7310 FORWARD (1); /* Skip '#' */
7311 cpp_skip_hspace (pfile);
7312 parse_name (pfile, GETC ());
7313 name_written = CPP_WRITTEN (pfile);
7314 name_length = name_written - name_start;
7315 cpp_skip_hspace (pfile);
7316 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7319 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7320 result = check_assertion (pfile,
7321 pfile->token_buffer + name_start, name_length,
7325 result = check_assertion (pfile,
7326 pfile->token_buffer + name_start, name_length,
7328 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7333 cpp_print_file_and_line (pfile)
7336 cpp_buffer *ip = cpp_file_buffer (pfile);
7341 cpp_buf_line_and_col (ip, &line, &col);
7342 cpp_file_line_for_message (pfile, ip->nominal_fname,
7343 line, pfile->show_column ? col : -1);
7348 cpp_error (pfile, msg, arg1, arg2, arg3)
7351 char *arg1, *arg2, *arg3;
7353 cpp_print_containing_files (pfile);
7354 cpp_print_file_and_line (pfile);
7355 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7358 /* Print error message but don't count it. */
7361 cpp_warning (pfile, msg, arg1, arg2, arg3)
7364 char *arg1, *arg2, *arg3;
7366 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7369 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7372 cpp_print_containing_files (pfile);
7373 cpp_print_file_and_line (pfile);
7374 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7377 /* Print an error message and maybe count it. */
7380 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7383 char *arg1, *arg2, *arg3;
7385 if (CPP_OPTIONS (pfile)->pedantic_errors)
7386 cpp_error (pfile, msg, arg1, arg2, arg3);
7388 cpp_warning (pfile, msg, arg1, arg2, arg3);
7392 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7396 char *arg1, *arg2, *arg3;
7399 cpp_buffer *ip = cpp_file_buffer (pfile);
7401 cpp_print_containing_files (pfile);
7404 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7406 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7410 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7414 char *arg1, *arg2, *arg3;
7419 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7422 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7425 cpp_print_containing_files (pfile);
7427 ip = cpp_file_buffer (pfile);
7430 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7432 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7436 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7440 char *arg1, *arg2, *arg3;
7442 if (CPP_OPTIONS (pfile)->pedantic_errors)
7443 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7445 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7448 /* Report a warning (or an error if pedantic_errors)
7449 giving specified file name and line number, not current. */
7452 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7457 char *arg1, *arg2, *arg3;
7459 if (!CPP_OPTIONS (pfile)->pedantic_errors
7460 && CPP_OPTIONS (pfile)->inhibit_warnings)
7463 cpp_file_line_for_message (pfile, file, line, -1);
7464 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7465 msg, arg1, arg2, arg3);
7468 /* This defines "errno" properly for VMS, and gives us EACCES. */
7475 #ifndef HAVE_STRERROR
7476 extern int sys_nerr;
7478 extern const char *const sys_errlist[];
7481 extern char *sys_errlist[];
7484 #else /* HAVE_STRERROR */
7488 char *strerror (int,...);
7492 * my_strerror - return the descriptive text associated with an `errno' code.
7496 my_strerror (errnum)
7502 #ifndef HAVE_STRERROR
7503 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7505 result = strerror (errnum);
7508 /* VAXCRTL's strerror() takes an optional second argument, which only
7509 matters when the first argument is EVMSERR. However, it's simplest
7510 just to pass it unconditionally. `vaxc$errno' is declared in
7511 <errno.h>, and maintained by the library in parallel with `errno'.
7512 We assume that caller's `errnum' either matches the last setting of
7513 `errno' by the library or else does not have the value `EVMSERR'. */
7515 result = strerror (errnum, vaxc$errno);
7519 result = "undocumented I/O error";
7524 /* Error including a message from `errno'. */
7527 cpp_error_from_errno (
7534 cpp_buffer *ip = cpp_file_buffer (pfile);
7536 cpp_print_containing_files (pfile);
7539 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7541 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7545 cpp_perror_with_name (
7549 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7553 * No pre-compiled header file support.
7555 * Possibly different enum token codes for each C/C++ token.
7557 * Should clean up remaining directives to that do_XXX functions
7558 * only take two arguments and all have command_reads_line.
7560 * Find and cleanup remaining uses of static variables,
7562 * Support for trigraphs.
7564 * Support -dM flag (dump_all_macros).
7566 * Support for_lint flag.