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 ':'
91 // PENDING: Straighten this out into configure
92 #include <time.h> // all compilers need this
101 #if !defined(_MSC_VER)
104 #include <sys/time.h> /* for __DATE__ and __TIME__ */
105 #include <sys/resource.h>
107 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
108 // #include <sys/times.h>
117 /* This defines "errno" properly for VMS, and gives us EACCES. */
120 extern char *index ();
121 extern char *rindex ();
129 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
130 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
132 /* Find the largest host integer type and set its size and type. */
134 #ifndef HOST_BITS_PER_WIDE_INT
136 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
137 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
138 #define HOST_WIDE_INT long
140 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
141 #define HOST_WIDE_INT int
147 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
151 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
154 /* Define a generic NULL if one hasn't already been defined. */
161 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
162 #define GENERIC_PTR void *
164 #define GENERIC_PTR char *
169 #define NULL_PTR ((GENERIC_PTR)0)
172 #ifndef INCLUDE_LEN_FUDGE
173 #define INCLUDE_LEN_FUDGE 0
176 /* Symbols to predefine. */
178 #ifdef CPP_PREDEFINES
179 static char *predefs = CPP_PREDEFINES;
181 static char *predefs = "";
184 /* We let tm.h override the types used here, to handle trivial differences
185 such as the choice of unsigned int or long unsigned int for size_t.
186 When machines start needing nontrivial differences in the size type,
187 it would be best to do something here to figure out automatically
188 from other information what type to use. */
190 /* The string value for __SIZE_TYPE__. */
193 #define SIZE_TYPE "long unsigned int"
196 /* The string value for __PTRDIFF_TYPE__. */
199 #define PTRDIFF_TYPE "long int"
202 /* The string value for __WCHAR_TYPE__. */
205 #define WCHAR_TYPE "int"
207 #define CPP_WCHAR_TYPE(PFILE) \
208 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
210 /* The string value for __USER_LABEL_PREFIX__ */
212 #ifndef USER_LABEL_PREFIX
213 #define USER_LABEL_PREFIX ""
216 /* The string value for __REGISTER_PREFIX__ */
218 #ifndef REGISTER_PREFIX
219 #define REGISTER_PREFIX ""
223 /* In the definition of a #assert name, this structure forms
224 a list of the individual values asserted.
225 Each value is itself a list of "tokens".
226 These are strings that are compared by name. */
228 struct tokenlist_list {
229 struct tokenlist_list *next;
230 struct arglist *tokens;
233 struct assertion_hashnode {
234 struct assertion_hashnode *next; /* double links for easy deletion */
235 struct assertion_hashnode *prev;
236 /* also, a back pointer to this node's hash
237 chain is kept, in case the node is the head
238 of the chain and gets deleted. */
239 struct assertion_hashnode **bucket_hdr;
240 int length; /* length of token, for quick comparison */
241 U_CHAR *name; /* the actual name */
242 /* List of token-sequences. */
243 struct tokenlist_list *value;
246 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
247 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
249 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
250 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
251 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
252 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
253 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
254 (Note that it is false while we're expanding marco *arguments*.) */
255 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
257 /* Move all backslash-newline pairs out of embarrassing places.
258 Exchange all such pairs following BP
259 with any potentially-embarrassing characters that follow them.
260 Potentially-embarrassing characters are / and *
261 (because a backslash-newline inside a comment delimiter
262 would cause it not to be recognized). */
264 #define NEWLINE_FIX \
265 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
267 /* Same, but assume we've already read the potential '\\' into C. */
268 #define NEWLINE_FIX1(C) do { \
269 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
273 struct cpp_pending *next;
278 /* Forward declarations. */
280 //extern char *Safe_malloc ();
282 static void add_import ();
283 static void append_include_chain ();
284 static void make_assertion ();
285 static void path_include ();
286 static void initialize_builtins ();
287 static void initialize_char_syntax ();
288 extern void delete_macro ();
289 static int finclude ();
290 static void validate_else ();
291 static int comp_def_part ();
292 extern void fancy_abort ();
293 static int lookup_import ();
294 static int redundant_include_p ();
295 static int is_system_include (cpp_reader *, char *);
296 static struct file_name_map *read_name_map ();
297 static char *read_filename_string ();
298 static int open_include_file ();
299 static int check_macro_name ();
300 static int compare_defs ();
301 static int compare_token_lists ();
302 static HOST_WIDE_INT eval_if_expression ();
303 static int change_newlines ();
305 static int file_size_and_mode ();
306 static struct arglist *read_token_list ();
307 static void free_token_list ();
308 static int safe_read ();
309 static void push_macro_expansion PARAMS ((cpp_reader *,
310 U_CHAR*, int, HASHNODE*));
311 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
312 //extern char *xrealloc ();
313 //extern char *xcalloc ();
314 static char *savestring ();
316 static void conditional_skip ();
317 static void skip_if_group ();
319 /* Last arg to output_line_command. */
320 enum file_change_code {same_file, enter_file, leave_file};
322 /* External declarations. */
324 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
326 extern char *getenv ();
327 extern FILE *fdopen ();
328 extern char *version_string;
329 extern struct tm *localtime ();
331 /* These functions are declared to return int instead of void since they
332 are going to be placed in a table and some old compilers have trouble with
333 pointers to functions returning void. */
335 static int do_define ();
336 static int do_line ();
337 static int do_include ();
338 static int do_undef ();
339 static int do_error ();
340 static int do_pragma ();
341 static int do_ident ();
343 static int do_xifdef ();
344 static int do_else ();
345 static int do_elif ();
346 static int do_endif ();
347 static int do_sccs ();
348 static int do_once ();
349 static int do_assert ();
350 static int do_unassert ();
351 static int do_warning ();
353 struct file_name_list
355 struct file_name_list *next;
357 /* If the following is nonzero, it is a macro name.
358 Don't include the file again if that macro is defined. */
359 U_CHAR *control_macro;
360 /* If the following is nonzero, it is a C-language system include
362 int c_system_include_path;
363 /* Mapping of file names for this directory. */
364 struct file_name_map *name_map;
365 /* Non-zero if name_map is valid. */
369 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
370 via the same directory as the file that #included it. */
371 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
373 /* #include "file" looks in source file dir, then stack. */
374 /* #include <file> just looks in the stack. */
375 /* -I directories are added to the end, then the defaults are added. */
377 static struct default_include {
378 char *fname; /* The name of the directory. */
379 int cplusplus; /* Only look here if we're compiling C++. */
380 int cxx_aware; /* Includes in this directory don't need to
381 be wrapped in extern "C" when compiling
383 } include_defaults_array[]
384 #ifdef INCLUDE_DEFAULTS
388 /* Pick up GNU C++ specific include files. */
389 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
391 /* This is the dir for fixincludes. Put it just before
392 the files that we fix. */
393 { GCC_INCLUDE_DIR, 0, 0 },
394 /* For cross-compilation, this dir name is generated
395 automatically in Makefile.in. */
396 { CROSS_INCLUDE_DIR, 0, 0 },
397 /* This is another place that the target system's headers might be. */
398 { TOOL_INCLUDE_DIR, 0, 1 },
399 { LOCAL_INCLUDE_DIR, 0, 1 },
400 #else /* not CROSS_COMPILE */
401 /* This should be /usr/local/include and should come before
402 the fixincludes-fixed header files. */
403 { LOCAL_INCLUDE_DIR, 0, 1 },
404 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
405 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
406 { TOOL_INCLUDE_DIR, 0, 1 },
407 /* This is the dir for fixincludes. Put it just before
408 the files that we fix. */
409 { GCC_INCLUDE_DIR, 0, 0 },
410 /* Some systems have an extra dir of include files. */
411 #ifdef SYSTEM_INCLUDE_DIR
412 { SYSTEM_INCLUDE_DIR, 0, 0 },
414 { STANDARD_INCLUDE_DIR, 0, 0 },
415 #endif /* not CROSS_COMPILE */
418 #endif /* no INCLUDE_DEFAULTS */
420 /* `struct directive' defines one #-directive, including how to handle it. */
423 int length; /* Length of name */
424 int (*func)(); /* Function to handle directive */
425 char *name; /* Name of directive */
426 enum node_type type; /* Code which describes which directive. */
427 char command_reads_line; /* One if rest of line is read by func. */
428 char traditional_comments; /* Nonzero: keep comments if -traditional. */
429 char pass_thru; /* Copy preprocessed directive to output file.*/
432 /* Here is the actual list of #-directives, most-often-used first.
433 The initialize_builtins function assumes #define is the very first. */
435 static struct directive directive_table[] = {
436 { 6, do_define, "define", T_DEFINE, 0, 1},
437 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
438 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
439 { 7, do_include, "include", T_INCLUDE, 1},
440 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
441 { 6, do_include, "import", T_IMPORT, 1},
442 { 5, do_endif, "endif", T_ENDIF, 1},
443 { 4, do_else, "else", T_ELSE, 1},
444 { 2, do_if, "if", T_IF, 1},
445 { 4, do_elif, "elif", T_ELIF, 1},
446 { 5, do_undef, "undef", T_UNDEF},
447 { 5, do_error, "error", T_ERROR},
448 { 7, do_warning, "warning", T_WARNING},
449 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
450 { 4, do_line, "line", T_LINE, 1},
451 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
452 #ifdef SCCS_DIRECTIVE
453 { 4, do_sccs, "sccs", T_SCCS},
455 { 6, do_assert, "assert", T_ASSERT, 1},
456 { 8, do_unassert, "unassert", T_UNASSERT, 1},
457 { -1, 0, "", T_UNUSED},
460 /* table to tell if char can be part of a C identifier. */
461 U_CHAR is_idchar[256];
462 /* table to tell if char can be first char of a c identifier. */
463 U_CHAR is_idstart[256];
464 /* table to tell if c is horizontal space. */
465 U_CHAR is_hor_space[256];
466 /* table to tell if c is horizontal or vertical space. */
467 static U_CHAR is_space[256];
469 /* Initialize syntactic classifications of characters. */
472 initialize_char_syntax (
473 struct cpp_options *opts)
478 * Set up is_idchar and is_idstart tables. These should be
479 * faster than saying (is_alpha (c) || c == '_'), etc.
480 * Set up these things before calling any routines tthat
483 for (i = 'a'; i <= 'z'; i++) {
484 is_idchar[i - 'a' + 'A'] = 1;
486 is_idstart[i - 'a' + 'A'] = 1;
489 for (i = '0'; i <= '9'; i++)
493 is_idchar['$'] = opts->dollars_in_ident;
494 is_idstart['$'] = opts->dollars_in_ident;
496 /* horizontal space table */
497 is_hor_space[' '] = 1;
498 is_hor_space['\t'] = 1;
499 is_hor_space['\v'] = 1;
500 is_hor_space['\f'] = 1;
501 is_hor_space['\r'] = 1;
512 /* Place into PFILE a quoted string representing the string SRC.
513 Caller must reserve enough space in pfile->token_buffer. */
521 CPP_PUTC_Q (pfile, '\"');
523 switch ((c = *src++))
527 CPP_PUTC_Q (pfile, c);
530 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
531 CPP_ADJUST_WRITTEN (pfile, 4);
537 CPP_PUTC_Q (pfile, '\\');
538 CPP_PUTC_Q (pfile, c);
542 CPP_PUTC_Q (pfile, '\"');
543 CPP_NUL_TERMINATE_Q (pfile);
548 /* Make sure PFILE->token_buffer will hold at least N more chars. */
555 long old_written = CPP_WRITTEN (pfile);
556 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
557 pfile->token_buffer = (U_CHAR*) Safe_realloc(pfile->token_buffer, pfile->token_buffer_size);
558 CPP_SET_WRITTEN (pfile, old_written);
563 * process a given definition string, for initialization
564 * If STR is just an identifier, define it with value 1.
565 * If STR has anything after the identifier, then it should
566 * be identifier=definition.
580 cpp_error (pfile, "malformed option `-D %s'", str);
583 while (is_idchar[*++p])
587 buf = (U_CHAR *) alloca (p - buf + 4);
588 strcpy ((char *)buf, str);
589 strcat ((char *)buf, " 1");
593 cpp_error (pfile, "malformed option `-D %s'", str);
599 /* Copy the entire option so we can modify it. */
600 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
601 strncpy (buf, str, p - str);
602 /* Change the = to a space. */
604 /* Scan for any backslash-newline and remove it. */
609 if (*p == '\\' && p[1] == '\n')
617 do_define (pfile, NULL, buf, buf + strlen (buf));
620 /* Process the string STR as if it appeared as the body of a #assert.
621 OPTION is the option name for which STR was the argument. */
630 /* struct directive *kt; */
631 U_CHAR *buf, *p /*, *q */ ;
633 /* Copy the entire option so we can modify it. */
634 buf = (U_CHAR *) alloca (strlen (str) + 1);
635 strcpy ((char *) buf, str);
638 /* Scan for any backslash-newline and remove it. */
641 if (*p == '\\' && p[1] == '\n')
650 if (!is_idstart[*p]) {
651 cpp_error (pfile, "malformed option `%s %s'", option, str);
654 while (is_idchar[*++p])
656 while (*p == ' ' || *p == '\t') p++;
657 if (! (*p == 0 || *p == '(')) {
658 cpp_error (pfile, "malformed option `%s %s'", option, str);
662 ip = cpp_push_buffer (pfile, buf, strlen (buf));
663 do_assert (pfile, NULL, NULL, NULL);
664 cpp_pop_buffer (pfile);
667 /* Append a chain of `struct file_name_list's
668 to the end of the main include chain.
669 FIRST is the beginning of the chain to append, and LAST is the end. */
672 append_include_chain (
674 struct file_name_list *first,struct file_name_list *last)
676 struct cpp_options *opts = CPP_OPTIONS (pfile);
677 struct file_name_list *dir;
682 if (opts->include == 0)
683 opts->include = first;
685 opts->last_include->next = first;
687 if (opts->first_bracket_include == 0)
688 opts->first_bracket_include = first;
690 for (dir = first; ; dir = dir->next) {
691 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
692 if (len > pfile->max_include_len)
693 pfile->max_include_len = len;
699 opts->last_include = last;
702 /* Add output to `deps_buffer' for the -M switch.
703 STRING points to the text to be output.
704 SPACER is ':' for targets, ' ' for dependencies, zero for text
705 to be inserted literally. */
713 int size = strlen (string);
718 #ifndef MAX_OUTPUT_COLUMNS
719 #define MAX_OUTPUT_COLUMNS 72
722 && pfile->deps_column > 0
723 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
725 deps_output (pfile, " \\\n ", 0);
726 pfile->deps_column = 0;
729 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
731 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
732 pfile->deps_buffer = (char *) Safe_realloc(pfile->deps_buffer,
733 pfile->deps_allocated_size);
735 if (spacer == ' ' && pfile->deps_column > 0)
736 pfile->deps_buffer[pfile->deps_size++] = ' ';
737 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
738 pfile->deps_size += size;
739 pfile->deps_column += size;
741 pfile->deps_buffer[pfile->deps_size++] = ':';
742 pfile->deps_buffer[pfile->deps_size] = 0;
745 /* Given a colon-separated list of file names PATH,
746 add all the names to the search path for include files. */
761 struct file_name_list *dirtmp;
763 /* Find the end of this name. */
764 while (*q != 0 && *q != PATH_SEPARATOR) q++;
766 /* An empty name in the path stands for the current directory. */
767 name = (char *) Safe_malloc (2);
771 /* Otherwise use the directory that is named. */
772 name = (char *) Safe_malloc (q - p + 1);
773 bcopy (p, name, q - p);
777 dirtmp = (struct file_name_list *)
778 Safe_malloc (sizeof (struct file_name_list));
779 dirtmp->next = 0; /* New one goes on the end */
780 dirtmp->control_macro = 0;
781 dirtmp->c_system_include_path = 0;
782 dirtmp->fname = name;
783 dirtmp->got_name_map = 0;
784 append_include_chain (pfile, dirtmp, dirtmp);
786 /* Advance past this name. */
790 /* Skip the colon. */
797 struct cpp_options *opts)
799 bzero ((char *) opts, sizeof *opts);
800 opts->in_fname = NULL;
801 opts->out_fname = NULL;
803 /* Initialize is_idchar to allow $. */
804 opts->dollars_in_ident = 1;
805 initialize_char_syntax (opts);
806 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
808 opts->no_line_commands = 0;
809 opts->no_trigraphs = 1;
810 opts->put_out_comments = 0;
811 opts->print_include_names = 0;
812 opts->dump_macros = dump_none;
815 opts->cplusplus_comments = 0;
822 opts->pedantic_errors = 0;
823 opts->inhibit_warnings = 0;
824 opts->warn_comments = 0;
825 opts->warn_import = 1;
826 opts->warnings_are_errors = 0;
849 HASHNODE *macro = (HASHNODE*)pbuf->data;
850 if (macro->type == T_DISABLED)
851 macro->type = T_MACRO;
852 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
870 /* Assuming we have read '/'.
871 If this is the start of a comment (followed by '*' or '/'),
872 skip to the end of the comment, and return ' '.
873 Return EOF if we reached the end of file before the end of the comment.
874 If not the start of a comment, return '/'. */
882 while (PEEKC() == '\\' && PEEKN(1) == '\n')
897 while (c == '\\' && PEEKC() == '\n')
901 FORWARD(1), c = GETC();
903 if (prev_c == '*' && c == '/')
905 if (c == '\n' && linep)
909 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
916 return ' '; /* Allow // to be terminated by EOF. */
917 while (c == '\\' && PEEKC() == '\n')
926 /* Don't consider final '\n' to be part of comment. */
936 /* Skip whitespace \-newline and comments. Does not macro-expand. */
948 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
949 cpp_pedwarn (pfile, "%s in preprocessing directive",
950 c == '\f' ? "formfeed" : "vertical tab");
956 c = skip_comment (pfile, NULL);
959 if (c == EOF || c == '/')
962 else if (c == '\\' && PEEKN(1) == '\n') {
965 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
966 && is_hor_space[PEEKN(1)])
972 /* Read the rest of the current line.
973 The line is appended to PFILE's output buffer. */
979 struct cpp_options *opts = CPP_OPTIONS (pfile);
996 goto scan_directive_token;
1000 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
1001 goto scan_directive_token;
1005 if (CPP_PEDANTIC (pfile))
1006 cpp_pedwarn (pfile, "%s in preprocessing directive",
1007 c == '\f' ? "formfeed" : "vertical tab");
1013 scan_directive_token:
1015 cpp_get_token (pfile);
1018 CPP_PUTC (pfile, c);
1021 CPP_NUL_TERMINATE (pfile);
1028 long old = CPP_WRITTEN (pfile);
1029 copy_rest_of_line (pfile);
1030 CPP_SET_WRITTEN (pfile, old);
1033 /* Handle a possible # directive.
1034 '#' has already been read. */
1040 register struct directive *kt;
1042 long after_ident = 0;
1043 U_CHAR *ident, *line_end;
1044 long old_written = CPP_WRITTEN (pfile);
1046 cpp_skip_hspace (pfile);
1049 if (c >= '0' && c <= '9')
1051 /* Handle # followed by a line number. */
1052 if (CPP_PEDANTIC (pfile))
1053 cpp_pedwarn (pfile, "`#' followed by integer");
1054 do_line (pfile, NULL);
1055 goto done_a_directive;
1058 /* Now find the directive name. */
1059 CPP_PUTC (pfile, '#');
1060 parse_name (pfile, GETC());
1061 ident = pfile->token_buffer + old_written + 1;
1062 ident_length = CPP_PWRITTEN (pfile) - ident;
1063 if (ident_length == 0 && PEEKC() == '\n')
1065 /* A line of just `#' becomes blank. */
1066 goto done_a_directive;
1070 if (ident_length == 0 || !is_idstart[*ident]) {
1072 while (is_idchar[*p]) {
1073 if (*p < '0' || *p > '9')
1077 /* Avoid error for `###' and similar cases unless -pedantic. */
1079 while (*p == '#' || is_hor_space[*p]) p++;
1081 if (pedantic && !lang_asm)
1082 cpp_warning (pfile, "invalid preprocessor directive");
1088 cpp_error (pfile, "invalid preprocessor directive name");
1094 * Decode the keyword and call the appropriate expansion
1095 * routine, after moving the input pointer up to the next line.
1097 for (kt = directive_table; ; kt++) {
1098 if (kt->length <= 0)
1099 goto not_a_directive;
1100 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1104 if (! kt->command_reads_line)
1106 /* Nonzero means do not delete comments within the directive.
1107 #define needs this when -traditional. */
1108 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1109 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1110 CPP_OPTIONS (pfile)->put_out_comments = comments;
1111 after_ident = CPP_WRITTEN (pfile);
1112 copy_rest_of_line (pfile);
1113 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1116 /* For #pragma and #define, we may want to pass through the directive.
1117 Other directives may create output, but we don't want the directive
1118 itself out, so we pop it now. For example #include may write a
1119 command (see comment in do_include), and conditionals may emit
1120 #failed ... #endfailed stuff. But note that popping the buffer
1121 means the parameters to kt->func may point after pfile->limit
1122 so these parameters are invalid as soon as something gets appended
1123 to the token_buffer. */
1125 line_end = CPP_PWRITTEN (pfile);
1126 if (!kt->pass_thru && kt->type != T_DEFINE)
1127 CPP_SET_WRITTEN (pfile, old_written);
1129 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1131 || (kt->type == T_DEFINE
1132 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1134 /* Just leave the entire #define in the output stack. */
1136 else if (kt->type == T_DEFINE
1137 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1139 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1140 SKIP_WHITE_SPACE (p);
1141 while (is_idchar[*p]) p++;
1143 CPP_PUTC (pfile, '\n');
1145 else if (kt->type == T_DEFINE)
1146 CPP_SET_WRITTEN (pfile, old_written);
1154 /* Pass a directive through to the output file.
1155 BUF points to the contents of the directive, as a contiguous string.
1156 LIMIT points to the first character past the end of the directive.
1157 KEYWORD is the keyword-table entry for the directive. */
1160 pass_thru_directive (
1161 U_CHAR *buf, U_CHAR *limit,
1163 struct directive *keyword)
1165 register unsigned keyword_length = keyword->length;
1167 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1168 CPP_PUTC_Q (pfile, '#');
1169 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1170 if (limit != buf && buf[0] != ' ')
1171 CPP_PUTC_Q (pfile, ' ');
1172 CPP_PUTS_Q (pfile, buf, limit - buf);
1174 CPP_PUTS_Q (pfile, '\n');
1175 /* Count the line we have just made in the output,
1176 to get in sync properly. */
1181 /* The arglist structure is built by do_define to tell
1182 collect_definition where the argument names begin. That
1183 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1184 would contain pointers to the strings x, y, and z.
1185 Collect_definition would then build a DEFINITION node,
1186 with reflist nodes pointing to the places x, y, and z had
1187 appeared. So the arglist is just convenience data passed
1188 between these two routines. It is not kept around after
1189 the current #define has been processed and entered into the
1193 struct arglist *next;
1200 /* Read a replacement list for a macro with parameters.
1201 Build the DEFINITION structure.
1202 Reads characters of text starting at BUF until END.
1203 ARGLIST specifies the formal parameters to look for
1204 in the text of the definition; NARGS is the number of args
1205 in that list, or -1 for a macro name that wants no argument list.
1206 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1207 and NAMELEN is its length in characters.
1209 Note that comments, backslash-newlines, and leading white space
1210 have already been deleted from the argument. */
1215 U_CHAR *buf, U_CHAR *limit,
1217 struct arglist *arglist)
1220 register U_CHAR *p, *lastp, *exp_p;
1221 struct reflist *endpat = NULL;
1222 /* Pointer to first nonspace after last ## seen. */
1224 /* Pointer to first nonspace after last single-# seen. */
1225 U_CHAR *stringify = 0;
1227 int expected_delimiter = '\0';
1229 /* Scan thru the replacement list, ignoring comments and quoted
1230 strings, picking up on the macro calls. It does a linear search
1231 thru the arg list on every potential symbol. Profiling might say
1232 that something smarter should happen. */
1237 /* Find the beginning of the trailing whitespace. */
1239 while (p < limit && is_space[limit[-1]]) limit--;
1241 /* Allocate space for the text in the macro definition.
1242 Leading and trailing whitespace chars need 2 bytes each.
1243 Each other input char may or may not need 1 byte,
1244 so this is an upper bound. The extra 5 are for invented
1245 leading and trailing newline-marker and final null. */
1246 maxsize = (sizeof (DEFINITION)
1248 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1252 defn = (DEFINITION *) Safe_calloc(1,maxsize);
1254 defn->nargs = nargs;
1255 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1260 /* Add one initial space escape-marker to prevent accidental
1261 token-pasting (often removed by macroexpand). */
1265 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1266 cpp_error (pfile, "`##' at start of macro definition");
1270 /* Process the main body of the definition. */
1272 int skipped_arg = 0;
1273 register U_CHAR c = *p++;
1277 if (!CPP_TRADITIONAL (pfile)) {
1281 if (expected_delimiter != '\0') {
1282 if (c == expected_delimiter)
1283 expected_delimiter = '\0';
1285 expected_delimiter = c;
1289 if (p < limit && expected_delimiter) {
1290 /* In a string, backslash goes through
1291 and makes next char ordinary. */
1297 /* An '@' in a string or character constant stands for itself,
1298 and does not need to be escaped. */
1299 if (!expected_delimiter)
1304 /* # is ordinary inside a string. */
1305 if (expected_delimiter)
1307 if (p < limit && *p == '#') {
1308 /* ##: concatenate preceding and following tokens. */
1309 /* Take out the first #, discard preceding whitespace. */
1311 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1313 /* Skip the second #. */
1315 /* Discard following whitespace. */
1316 SKIP_WHITE_SPACE (p);
1319 cpp_error (pfile, "`##' at end of macro definition");
1320 } else if (nargs >= 0) {
1321 /* Single #: stringify following argument ref.
1322 Don't leave the # in the expansion. */
1324 SKIP_WHITE_SPACE (p);
1325 if (p == limit || ! is_idstart[*p])
1327 "`#' operator is not followed by a macro argument name");
1334 /* In -traditional mode, recognize arguments inside strings and
1335 and character constants, and ignore special properties of #.
1336 Arguments inside strings are considered "stringified", but no
1337 extra quote marks are supplied. */
1341 if (expected_delimiter != '\0') {
1342 if (c == expected_delimiter)
1343 expected_delimiter = '\0';
1345 expected_delimiter = c;
1349 /* Backslash quotes delimiters and itself, but not macro args. */
1350 if (expected_delimiter != 0 && p < limit
1351 && (*p == expected_delimiter || *p == '\\')) {
1358 if (expected_delimiter != '\0') /* No comments inside strings. */
1361 /* If we find a comment that wasn't removed by handle_directive,
1362 this must be -traditional. So replace the comment with
1366 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1369 /* Mark this as a concatenation-point, as if it had been ##. */
1373 else if (*p == '/') {
1374 /* A c++ comment. Discard to the end of line */
1382 /* Handle the start of a symbol. */
1383 if (is_idchar[c] && nargs > 0) {
1384 U_CHAR *id_beg = p - 1;
1388 while (p != limit && is_idchar[*p]) p++;
1389 id_len = p - id_beg;
1391 if (is_idstart[c]) {
1392 register struct arglist *arg;
1394 for (arg = arglist; arg != NULL; arg = arg->next) {
1395 struct reflist *tpat;
1397 if (arg->name[0] == c
1398 && arg->length == id_len
1399 && strncmp (arg->name, id_beg, id_len) == 0) {
1400 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1401 if (CPP_TRADITIONAL (pfile)) {
1402 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1406 "macro arg `%.*s' would be stringified with -traditional.",
1410 /* If ANSI, don't actually substitute inside a string. */
1411 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1413 /* make a pat node for this arg and append it to the end of
1415 tpat = (struct reflist *) Safe_malloc (sizeof (struct reflist));
1417 tpat->raw_before = concat == id_beg;
1418 tpat->raw_after = 0;
1419 tpat->rest_args = arg->rest_args;
1420 tpat->stringify = (CPP_TRADITIONAL (pfile)
1421 ? expected_delimiter != '\0'
1422 : stringify == id_beg);
1425 defn->pattern = tpat;
1427 endpat->next = tpat;
1430 tpat->argno = arg->argno;
1431 tpat->nchars = exp_p - lastp;
1433 register U_CHAR *p1 = p;
1434 SKIP_WHITE_SPACE (p1);
1435 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1436 tpat->raw_after = 1;
1438 lastp = exp_p; /* place to start copying from next time */
1445 /* If this was not a macro arg, copy it into the expansion. */
1446 if (! skipped_arg) {
1447 register U_CHAR *lim1 = p;
1451 if (stringify == id_beg)
1453 "`#' operator should be followed by a macro argument name");
1458 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1460 /* If ANSI, put in a "@ " marker to prevent token pasting.
1461 But not if "inside a string" (which in ANSI mode
1462 happens only for -D option). */
1469 defn->length = exp_p - defn->expansion;
1471 /* Crash now if we overrun the allocated size. */
1472 if (defn->length + 1 > maxsize)
1476 /* This isn't worth the time it takes. */
1477 /* give back excess storage */
1478 defn->expansion = (U_CHAR *) Safe_realloc(defn->expansion, defn->length + 1);
1485 * special extension string that can be added to the last macro argument to
1486 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1487 * #define wow(a, b...) process (b, a, b)
1488 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1489 * { wow (one, two); } -> { process (two, one, two); }
1490 * if this "rest_arg" is used with the concat token '##' and if it is not
1491 * supplied then the token attached to with ## will not be outputted. Ex:
1492 * #define wow (a, b...) process (b ## , a, ## b)
1493 * { wow (1, 2); } -> { process (2, 1, 2); }
1494 * { wow (one); } -> { process (one); {
1496 static char rest_extension[] = "...";
1497 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1499 /* Create a DEFINITION node from a #define directive. Arguments are
1500 as for do_define. */
1503 U_CHAR *buf, U_CHAR *limit,
1507 U_CHAR *bp; /* temp ptr into input buffer */
1508 U_CHAR *symname; /* remember where symbol name starts */
1509 int sym_length; /* and how long it is */
1512 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1514 int arglengths = 0; /* Accumulate lengths of arg names
1515 plus number of args. */
1517 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1521 while (is_hor_space[*bp])
1524 symname = bp; /* remember where it starts */
1526 sym_length = check_macro_name (pfile, bp, "macro");
1529 /* Lossage will occur if identifiers or control keywords are broken
1530 across lines using backslash. This is not the right place to take
1534 struct arglist *arg_ptrs = NULL;
1537 bp++; /* skip '(' */
1538 SKIP_WHITE_SPACE (bp);
1540 /* Loop over macro argument names. */
1541 while (*bp != ')') {
1542 struct arglist *temp;
1544 temp = (struct arglist *) alloca (sizeof (struct arglist));
1546 temp->next = arg_ptrs;
1547 temp->argno = argno++;
1548 temp->rest_args = 0;
1552 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1554 if (!is_idstart[*bp])
1555 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1557 /* Find the end of the arg name. */
1558 while (is_idchar[*bp]) {
1560 /* do we have a "special" rest-args extension here? */
1561 if (limit - bp > REST_EXTENSION_LENGTH &&
1562 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1564 temp->rest_args = 1;
1568 temp->length = bp - temp->name;
1570 bp += REST_EXTENSION_LENGTH;
1571 arglengths += temp->length + 2;
1572 SKIP_WHITE_SPACE (bp);
1573 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1574 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1579 SKIP_WHITE_SPACE (bp);
1582 cpp_error (pfile, "unterminated parameter list in `#define'");
1586 struct arglist *otemp;
1588 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1589 if (temp->length == otemp->length &&
1590 strncmp (temp->name, otemp->name, temp->length) == 0) {
1593 name = (U_CHAR *) alloca (temp->length + 1);
1594 (void) strncpy (name, temp->name, temp->length);
1595 name[temp->length] = '\0';
1597 "duplicate argument name `%s' in `#define'", name);
1603 ++bp; /* skip paren */
1604 SKIP_WHITE_SPACE (bp);
1605 /* now everything from bp before limit is the definition. */
1606 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1607 defn->rest_args = rest_args;
1609 /* Now set defn->args.argnames to the result of concatenating
1610 the argument names in reverse order
1611 with comma-space between them. */
1612 defn->args.argnames = (U_CHAR *) Safe_malloc (arglengths + 1);
1614 struct arglist *temp;
1616 for (temp = arg_ptrs; temp; temp = temp->next) {
1617 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1619 if (temp->next != 0) {
1620 defn->args.argnames[i++] = ',';
1621 defn->args.argnames[i++] = ' ';
1624 defn->args.argnames[i] = 0;
1627 /* Simple expansion or empty definition. */
1631 if (is_hor_space[*bp]) {
1633 SKIP_WHITE_SPACE (bp);
1636 case '!': case '"': case '#': case '%': case '&': case '\'':
1637 case ')': case '*': case '+': case ',': case '-': case '.':
1638 case '/': case ':': case ';': case '<': case '=': case '>':
1639 case '?': case '[': case '\\': case ']': case '^': case '{':
1640 case '|': case '}': case '~':
1641 cpp_warning (pfile, "missing white space after `#define %.*s'",
1642 sym_length, symname);
1646 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1647 sym_length, symname);
1652 /* now everything from bp before limit is the definition. */
1653 defn = collect_expansion (pfile, bp, limit, -1,(struct arglist *) NULL_PTR);
1654 defn->args.argnames = (U_CHAR *) "";
1660 /* OP is null if this is a predefinition */
1661 defn->predefined = predefinition;
1663 mdef.symnam = symname;
1664 mdef.symlen = sym_length;
1673 /* Check a purported macro name SYMNAME, and yield its length.
1674 USAGE is the kind of name this is intended for. */
1685 for (p = symname; is_idchar[*p]; p++)
1687 sym_length = p - symname;
1688 if (sym_length == 0)
1689 cpp_error (pfile, "invalid %s name", usage);
1690 else if (!is_idstart[*symname]) {
1691 U_CHAR *msg; /* what pain... */
1692 msg = (U_CHAR *) alloca (sym_length + 1);
1693 bcopy (symname, msg, sym_length);
1694 msg[sym_length] = 0;
1695 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1697 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1698 cpp_error (pfile, "invalid %s name `defined'", usage);
1704 * return zero if two DEFINITIONs are isomorphic
1708 DEFINITION *d1, DEFINITION *d2)
1710 register struct reflist *a1, *a2;
1711 register U_CHAR *p1 = d1->expansion;
1712 register U_CHAR *p2 = d2->expansion;
1715 if (d1->nargs != d2->nargs)
1717 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1719 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1720 a1 = a1->next, a2 = a2->next) {
1721 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1722 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1723 || a1->argno != a2->argno
1724 || a1->stringify != a2->stringify
1725 || a1->raw_before != a2->raw_before
1726 || a1->raw_after != a2->raw_after)
1734 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1735 p2, d2->length - (p2 - d2->expansion), 1))
1740 /* Return 1 if two parts of two macro definitions are effectively different.
1741 One of the parts starts at BEG1 and has LEN1 chars;
1742 the other has LEN2 chars at BEG2.
1743 Any sequence of whitespace matches any other sequence of whitespace.
1744 FIRST means these parts are the first of a macro definition;
1745 so ignore leading whitespace entirely.
1746 LAST means these parts are the last of a macro definition;
1747 so ignore trailing whitespace entirely. */
1752 U_CHAR *beg1, int len1,
1753 U_CHAR *beg2, int len2 ,
1756 register U_CHAR *end1 = beg1 + len1;
1757 register U_CHAR *end2 = beg2 + len2;
1759 while (beg1 != end1 && is_space[*beg1]) beg1++;
1760 while (beg2 != end2 && is_space[*beg2]) beg2++;
1763 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1764 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1766 while (beg1 != end1 && beg2 != end2) {
1767 if (is_space[*beg1] && is_space[*beg2]) {
1768 while (beg1 != end1 && is_space[*beg1]) beg1++;
1769 while (beg2 != end2 && is_space[*beg2]) beg2++;
1770 } else if (*beg1 == *beg2) {
1774 return (beg1 != end1) || (beg2 != end2);
1777 /* Process a #define command.
1778 BUF points to the contents of the #define command, as a contiguous string.
1779 LIMIT points to the first character past the end of the definition.
1780 KEYWORD is the keyword-table entry for #define,
1781 or NULL for a "predefined" macro. */
1786 struct directive *keyword,
1787 U_CHAR *buf, U_CHAR *limit)
1794 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1795 if (pcp_outfile && keyword)
1796 pass_thru_directive (buf, limit, pfile, keyword);
1799 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1803 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1805 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1808 /* Redefining a precompiled key is ok. */
1809 if (hp->type == T_PCSTRING)
1811 /* Redefining a macro is ok if the definitions are the same. */
1812 else if (hp->type == T_MACRO)
1813 ok = ! compare_defs (mdef.defn, hp->value.defn);
1814 /* Redefining a constant is ok with -D. */
1815 else if (hp->type == T_CONST)
1816 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1817 /* Print the warning if it's not ok. */
1820 U_CHAR *msg; /* what pain... */
1822 /* If we are passing through #define and #undef directives, do
1823 that for this re-definition now. */
1824 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1825 pass_thru_directive (buf, limit, pfile, keyword);
1827 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1829 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1830 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1831 cpp_pedwarn (pfile, msg);
1832 if (hp->type == T_MACRO)
1833 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1834 "this is the location of the previous definition");
1836 /* Replace the old definition. */
1838 hp->value.defn = mdef.defn;
1842 /* If we are passing through #define and #undef directives, do
1843 that for this new definition now. */
1844 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1845 pass_thru_directive (buf, limit, pfile, keyword);
1846 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1847 (char *) mdef.defn, hashcode);
1857 /* This structure represents one parsed argument in a macro call.
1858 `raw' points to the argument text as written (`raw_length' is its length).
1859 `expanded' points to the argument's macro-expansion
1860 (its length is `expand_length').
1861 `stringified_length' is the length the argument would have
1863 `use_count' is the number of times this macro arg is substituted
1864 into the macro. If the actual use count exceeds 10,
1865 the value stored is 10. */
1867 /* raw and expanded are relative to ARG_BASE */
1868 #define ARG_BASE ((pfile)->token_buffer)
1871 /* Strings relative to pfile->token_buffer */
1872 long raw, expanded, stringified;
1873 int raw_length, expand_length;
1874 int stringified_length;
1886 #ifdef STATIC_BUFFERS
1887 register cpp_buffer *buf = CPP_BUFFER (pfile);
1888 if (buf == pfile->buffer_stack)
1889 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1891 bzero ((char *) buf, sizeof (cpp_buffer));
1892 CPP_BUFFER (pfile) = buf;
1894 register cpp_buffer *buf = (cpp_buffer*) Safe_malloc (sizeof(cpp_buffer));
1895 bzero ((char *) buf, sizeof (cpp_buffer));
1896 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1897 CPP_BUFFER (pfile) = buf;
1899 buf->if_stack = pfile->if_stack;
1900 buf->cleanup = null_cleanup;
1901 buf->underflow = null_underflow;
1902 buf->buf = buf->cur = buffer;
1903 buf->alimit = buf->rlimit = buffer + length;
1909 cpp_pop_buffer (pfile)
1912 cpp_buffer *buf = CPP_BUFFER (pfile);
1913 #ifdef STATIC_BUFFERS
1914 (*buf->cleanup) (buf, pfile);
1915 return ++CPP_BUFFER (pfile);
1917 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1918 (*buf->cleanup) (buf, pfile);
1919 CPP_BUFFER (pfile) = next_buf;
1925 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1926 Pop the buffer when done. */
1932 cpp_buffer *buffer = CPP_BUFFER (pfile);
1935 enum cpp_token token = cpp_get_token (pfile);
1936 if (token == CPP_EOF) /* Should not happen ... */
1938 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1940 cpp_pop_buffer (pfile);
1947 * Rescan a string (which may have escape marks) into pfile's buffer.
1948 * Place the result in pfile->token_buffer.
1950 * The input is copied before it is scanned, so it is safe to pass
1951 * it something from the token_buffer that will get overwritten
1952 * (because it follows CPP_WRITTEN). This is used by do_include.
1956 cpp_expand_to_buffer (
1961 register cpp_buffer *ip;
1962 U_CHAR *limit = buf + length;
1966 int odepth = indepth;
1972 /* Set up the input on the input stack. */
1974 buf1 = (U_CHAR *) alloca (length + 1);
1976 register U_CHAR *p1 = buf;
1977 register U_CHAR *p2 = buf1;
1984 ip = cpp_push_buffer (pfile, buf1, length);
1985 ip->has_escapes = 1;
1987 ip->lineno = obuf.lineno = 1;
1990 /* Scan the input, create the output. */
1991 cpp_scan_buffer (pfile);
1994 if (indepth != odepth)
1998 CPP_NUL_TERMINATE (pfile);
2013 (*linep)++, (*colp) = 1;
2019 /* Move line_base forward, updating lineno and colno. */
2023 register cpp_buffer *pbuf)
2025 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2026 unsigned char *new_pos = pbuf->cur;
2027 register struct parse_marker *mark;
2028 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2030 if (pbuf->buf + mark->position < new_pos)
2031 new_pos = pbuf->buf + mark->position;
2033 pbuf->line_base += new_pos - old_pos;
2034 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2038 cpp_buf_line_and_col (
2039 register cpp_buffer *pbuf,
2040 long *linep,long *colp)
2047 *linep = pbuf->lineno;
2048 *colp = pbuf->colno;
2049 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2058 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2064 cpp_buffer *ip = CPP_BUFFER (pfile);
2066 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2067 if (ip->fname != NULL)
2074 register U_CHAR *buf,
2075 register U_CHAR *limit)
2077 register long count = 0;
2088 * write out a #line command, for instance, after an #include file.
2089 * If CONDITIONAL is nonzero, we can omit the #line if it would
2090 * appear to be a no-op, and we can output a few newlines instead
2091 * if we want to increase the line number by a small amount.
2092 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2096 output_line_command (
2099 enum file_change_code file_change)
2102 /* char *line_cmd_buf, *line_end;*/
2104 cpp_buffer *ip = CPP_BUFFER (pfile);
2106 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2110 update_position (ip);
2111 line = CPP_BUFFER (pfile)->lineno;
2112 col = CPP_BUFFER (pfile)->colno;
2113 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2115 if (CPP_OPTIONS (pfile)->no_line_commands)
2119 if (line == pfile->lineno)
2122 /* If the inherited line number is a little too small,
2123 output some newlines instead of a #line command. */
2124 if (line > pfile->lineno && line < pfile->lineno + 8) {
2125 CPP_RESERVE (pfile, 20);
2126 while (line > pfile->lineno) {
2127 CPP_PUTC_Q (pfile, '\n');
2135 /* Don't output a line number of 0 if we can help it. */
2136 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2137 && *ip->bufp == '\n') {
2143 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2145 static char sharp_line[] = "#line ";
2147 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2150 sprintf (CPP_PWRITTEN (pfile), "%ld ", line+2);
2151 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2153 // modification for SDC51
2154 if (*ip->nominal_fname == '\0')
2155 quote_string (pfile,"standard input");
2157 quote_string (pfile, ip->nominal_fname);
2158 if (file_change != same_file) {
2159 CPP_PUTC_Q (pfile, ' ');
2160 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2162 /* Tell cc1 if following text comes from a system header file. */
2163 if (ip->system_header_p) {
2164 CPP_PUTC_Q (pfile, ' ');
2165 CPP_PUTC_Q (pfile, '3');
2167 #ifndef NO_IMPLICIT_EXTERN_C
2168 /* Tell cc1plus if following text should be treated as C. */
2169 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2170 CPP_PUTC_Q (pfile, ' ');
2171 CPP_PUTC_Q (pfile, '4');
2174 CPP_PUTC_Q (pfile, '\n');
2175 pfile->lineno = line;
2179 * Parse a macro argument and append the info on PFILE's token_buffer.
2180 * REST_ARGS means to absorb the rest of the args.
2181 * Return nonzero to indicate a syntax error.
2184 static enum cpp_token
2190 enum cpp_token token = CPP_EOF;
2191 /* long arg_start = CPP_WRITTEN (pfile); */
2192 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2193 CPP_OPTIONS (pfile)->put_out_comments = 0;
2195 /* Try to parse as much of the argument as exists at this
2196 input stack level. */
2197 pfile->no_macro_expand++;
2200 token = cpp_get_token (pfile);
2206 /* If we've hit end of file, it's an error (reported by caller).
2207 Ditto if it's the end of cpp_expand_to_buffer text.
2208 If we've hit end of macro, just continue. */
2209 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2220 /* if we've returned to lowest level and
2221 we aren't absorbing all args */
2222 if (paren == 0 && rest_args == 0)
2226 /* Remove ',' or ')' from argument buffer. */
2227 CPP_ADJUST_WRITTEN (pfile, -1);
2234 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2235 pfile->no_macro_expand--;
2240 /* Turn newlines to spaces in the string of length LENGTH at START,
2241 except inside of string constants.
2242 The string is copied into itself with its beginning staying fixed. */
2249 register U_CHAR *ibp;
2250 register U_CHAR *obp;
2251 register U_CHAR *limit;
2255 limit = start + length;
2258 while (ibp < limit) {
2259 *obp++ = c = *ibp++;
2264 /* Notice and skip strings, so that we don't delete newlines in them. */
2267 while (ibp < limit) {
2268 *obp++ = c = *ibp++;
2271 if (c == '\n' && quotec == '\'')
2287 if (!pfile->timebuf) {
2288 time_t t = time ((time_t *)0);
2289 pfile->timebuf = localtime (&t);
2291 return pfile->timebuf;
2294 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2295 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2299 * expand things like __FILE__. Place the expansion into the output
2300 * buffer *without* rescanning.
2311 cpp_buffer *ip = NULL;
2314 int paren = 0; /* For special `defined' keyword */
2317 if (pcp_outfile && pcp_inside_if
2318 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2320 "Predefined macro `%s' used inside `#if' during precompilation",
2324 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2328 cpp_error (pfile, "cccp error: not in any file?!");
2329 return; /* the show must go on */
2331 if (ip->fname != NULL)
2341 if (hp->type == T_BASE_FILE)
2343 while (CPP_PREV_BUFFER (ip))
2344 ip = CPP_PREV_BUFFER (ip);
2346 string = ip->nominal_fname;
2350 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2351 quote_string (pfile, string);
2355 case T_INCLUDE_LEVEL:
2357 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2358 if (ip->fname != NULL)
2361 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2362 sprintf (buf, "%d", true_indepth - 1);
2366 buf = (char *) alloca (3 + strlen (version_string));
2367 sprintf (buf, "\"%s\"", version_string);
2370 #ifndef NO_BUILTIN_SIZE_TYPE
2376 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2377 case T_PTRDIFF_TYPE:
2383 buf = CPP_WCHAR_TYPE (pfile);
2386 case T_USER_LABEL_PREFIX_TYPE:
2387 buf = USER_LABEL_PREFIX;
2390 case T_REGISTER_PREFIX_TYPE:
2391 buf = REGISTER_PREFIX;
2395 buf = (char *) alloca (4 * sizeof (int));
2396 sprintf (buf, "%d", hp->value.ival);
2398 if (pcp_inside_if && pcp_outfile)
2399 /* Output a precondition for this macro use */
2400 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2406 long line = ip->lineno;
2407 long col = ip->colno;
2408 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2410 buf = (char *) alloca (10);
2411 sprintf (buf, "%ld", line);
2417 buf = (char *) alloca (20);
2418 timebuf = timestamp (pfile);
2419 if (hp->type == T_DATE)
2420 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2421 timebuf->tm_mday, timebuf->tm_year + 1900);
2423 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2427 case T_SPEC_DEFINED:
2428 buf = " 0 "; /* Assume symbol is not defined */
2429 ip = CPP_BUFFER (pfile);
2430 SKIP_WHITE_SPACE (ip->cur);
2431 if (*ip->cur == '(')
2434 ip->cur++; /* Skip over the paren */
2435 SKIP_WHITE_SPACE (ip->cur);
2438 if (!is_idstart[*ip->cur])
2440 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)) != 0)
2443 if (pcp_outfile && pcp_inside_if
2444 && (hp->type == T_CONST
2445 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2446 /* Output a precondition for this macro use. */
2447 fprintf (pcp_outfile, "#define %s\n", hp->name);
2453 if (pcp_outfile && pcp_inside_if)
2455 /* Output a precondition for this macro use */
2456 U_CHAR *cp = ip->bufp;
2457 fprintf (pcp_outfile, "#undef ");
2458 while (is_idchar[*cp]) /* Ick! */
2459 fputc (*cp++, pcp_outfile);
2460 putc ('\n', pcp_outfile);
2463 while (is_idchar[*ip->cur])
2465 SKIP_WHITE_SPACE (ip->cur);
2468 if (*ip->cur != ')')
2476 cpp_error (pfile, "`defined' without an identifier");
2480 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2484 CPP_RESERVE (pfile, len + 1);
2485 CPP_PUTS_Q (pfile, buf, len);
2486 CPP_NUL_TERMINATE_Q (pfile);
2491 /* Initialize the built-in macros. */
2494 initialize_builtins (
2497 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2498 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2499 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2500 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2501 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2502 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2503 #ifndef NO_BUILTIN_SIZE_TYPE
2504 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2506 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2507 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2509 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2510 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2511 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2512 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2513 if (!CPP_TRADITIONAL (pfile))
2514 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2515 if (CPP_OPTIONS (pfile)->objc)
2516 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2517 /* This is supplied using a -D by the compiler driver
2518 so that it is present only when truly compiling with GNU C. */
2519 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2521 if (CPP_OPTIONS (pfile)->debug_output)
2523 char directive[2048];
2524 register struct directive *dp = &directive_table[0];
2525 struct tm *timebuf = timestamp (pfile);
2526 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2528 while (CPP_PREV_BUFFER (pbuffer))
2529 pbuffer = CPP_PREV_BUFFER (pbuffer);
2530 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2531 pbuffer->nominal_fname);
2532 output_line_command (pfile, 0, same_file);
2533 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2535 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2536 output_line_command (pfile, 0, same_file);
2537 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2539 #ifndef NO_BUILTIN_SIZE_TYPE
2540 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2541 output_line_command (pfile, 0, same_file);
2542 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2545 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2546 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2547 output_line_command (pfile, 0, same_file);
2548 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2551 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2552 output_line_command (pfile, 0, same_file);
2553 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2555 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2556 monthnames[timebuf->tm_mon],
2557 timebuf->tm_mday, timebuf->tm_year + 1900);
2558 output_line_command (pfile, 0, same_file);
2559 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2561 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2562 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2563 output_line_command (pfile, 0, same_file);
2564 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2566 if (!CPP_TRADITIONAL (pfile))
2568 sprintf (directive, " __STDC__ 1");
2569 output_line_command (pfile, 0, same_file);
2570 pass_thru_directive (directive, &directive[strlen (directive)],
2573 if (CPP_OPTIONS (pfile)->objc)
2575 sprintf (directive, " __OBJC__ 1");
2576 output_line_command (pfile, 0, same_file);
2577 pass_thru_directive (directive, &directive[strlen (directive)],
2583 /* Return 1 iff a token ending in C1 followed directly by a token C2
2584 could cause mis-tokenization. */
2593 if (c2 == c1 || c2 == '=')
2597 case '0': case '1': case '2': case '3': case '4':
2598 case '5': case '6': case '7': case '8': case '9':
2600 if (c2 == '-' || c2 == '+')
2601 return 1; /* could extend a pre-processing number */
2604 if (c2 == '\'' || c2 == '\"')
2605 return 1; /* Could turn into L"xxx" or L'xxx'. */
2609 case 'a': case 'b': case 'c': case 'd': case 'f':
2610 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2611 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2612 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2614 case 'A': case 'B': case 'C': case 'D': case 'F':
2615 case 'G': case 'H': case 'I': case 'J': case 'K':
2616 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2617 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2619 /* We're in the middle of either a name or a pre-processing number. */
2620 return (is_idchar[c2] || c2 == '.');
2621 case '<': case '>': case '!': case '%': case '#': case ':':
2622 case '^': case '&': case '|': case '*': case '/': case '=':
2623 return (c2 == c1 || c2 == '=');
2628 /* Expand a macro call.
2629 HP points to the symbol that is the macro being called.
2630 Put the result of expansion onto the input stack
2631 so that subsequent input by our caller will use it.
2633 If macro wants arguments, caller has already verified that
2634 an argument list follows; arguments come from the input stack. */
2642 DEFINITION *defn = hp->value.defn;
2643 register U_CHAR *xbuf;
2644 long start_line, start_column;
2646 struct argdata *args;
2647 long old_written = CPP_WRITTEN (pfile);
2649 int start_line = instack[indepth].lineno;
2651 int rest_args, rest_zero;
2655 CHECK_DEPTH (return;);
2659 /* This macro is being used inside a #if, which means it must be */
2660 /* recorded as a precondition. */
2661 if (pcp_inside_if && pcp_outfile && defn->predefined)
2662 dump_single_macro (hp, pcp_outfile);
2665 pfile->output_escapes++;
2666 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2668 nargs = defn->nargs;
2672 enum cpp_token token = CPP_EOF;
2674 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2676 for (i = 0; i < nargs; i++)
2678 args[i].raw = args[i].expanded = 0;
2679 args[i].raw_length = 0;
2680 args[i].expand_length = args[i].stringified_length = -1;
2681 args[i].use_count = 0;
2684 /* Parse all the macro args that are supplied. I counts them.
2685 The first NARGS args are stored in ARGS.
2686 The rest are discarded. If rest_args is set then we assume
2687 macarg absorbed the rest of the args. */
2691 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2696 if (i < nargs || (nargs == 0 && i == 0))
2698 /* if we are working on last arg which absorbs rest of args... */
2699 if (i == nargs - 1 && defn->rest_args)
2701 args[i].raw = CPP_WRITTEN (pfile);
2702 token = macarg (pfile, rest_args);
2703 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2704 args[i].newlines = 0; /* FIXME */
2707 token = macarg (pfile, 0);
2708 if (token == CPP_EOF || token == CPP_POP)
2710 cpp_error_with_line (pfile, start_line, start_column,
2711 "unterminated macro call");
2715 } while (token == CPP_COMMA);
2717 /* If we got one arg but it was just whitespace, call that 0 args. */
2720 register U_CHAR *bp = ARG_BASE + args[0].raw;
2721 register U_CHAR *lim = bp + args[0].raw_length;
2722 /* cpp.texi says for foo ( ) we provide one argument.
2723 However, if foo wants just 0 arguments, treat this as 0. */
2725 while (bp != lim && is_space[*bp]) bp++;
2730 /* Don't output an error message if we have already output one for
2731 a parse error above. */
2733 if (nargs == 0 && i > 0)
2735 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2739 /* traditional C allows foo() if foo wants one argument. */
2740 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2742 /* the rest args token is allowed to absorb 0 tokens */
2743 else if (i == nargs - 1 && defn->rest_args)
2746 cpp_error (pfile, "macro `%s' used without args", hp->name);
2748 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2750 cpp_error (pfile, "macro `%s' used with only %d args",
2756 "macro `%s' used with too many (%d) args", hp->name, i);
2760 /* If macro wants zero args, we parsed the arglist for checking only.
2761 Read directly from the macro definition. */
2764 xbuf = defn->expansion;
2765 xbuf_len = defn->length;
2769 register U_CHAR *exp = defn->expansion;
2770 register int offset; /* offset in expansion,
2771 copied a piece at a time */
2772 register int totlen; /* total amount of exp buffer filled so far */
2774 register struct reflist *ap, *last_ap;
2776 /* Macro really takes args. Compute the expansion of this call. */
2778 /* Compute length in characters of the macro's expansion.
2779 Also count number of times each arg is used. */
2780 xbuf_len = defn->length;
2781 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2785 register struct argdata *arg = &args[ap->argno];
2786 /* Stringify it it hasn't already been */
2787 if (arg->stringified_length < 0)
2789 int arglen = arg->raw_length;
2793 /* Initially need_space is -1. Otherwise, 1 means the
2794 previous character was a space, but we suppressed it;
2795 0 means the previous character was a non-space. */
2796 int need_space = -1;
2798 arg->stringified = CPP_WRITTEN (pfile);
2799 if (!CPP_TRADITIONAL (pfile))
2800 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2801 for (; i < arglen; i++)
2803 c = (ARG_BASE + arg->raw)[i];
2807 /* Internal sequences of whitespace are replaced by
2808 one space except within an string or char token.*/
2811 if (CPP_WRITTEN (pfile) > arg->stringified
2812 && (CPP_PWRITTEN (pfile))[-1] == '@')
2814 /* "@ " escape markers are removed */
2815 CPP_ADJUST_WRITTEN (pfile, -1);
2818 if (need_space == 0)
2822 else if (need_space > 0)
2823 CPP_PUTC (pfile, ' ');
2838 else if (c == '\"' || c == '\'')
2842 /* Escape these chars */
2843 if (c == '\"' || (in_string && c == '\\'))
2844 CPP_PUTC (pfile, '\\');
2846 CPP_PUTC (pfile, c);
2849 CPP_RESERVE (pfile, 4);
2850 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2852 CPP_ADJUST_WRITTEN (pfile, 4);
2855 if (!CPP_TRADITIONAL (pfile))
2856 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2857 arg->stringified_length
2858 = CPP_WRITTEN (pfile) - arg->stringified;
2860 xbuf_len += args[ap->argno].stringified_length;
2862 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2863 /* Add 4 for two newline-space markers to prevent
2864 token concatenation. */
2865 xbuf_len += args[ap->argno].raw_length + 4;
2868 /* We have an ordinary (expanded) occurrence of the arg.
2869 So compute its expansion, if we have not already. */
2870 if (args[ap->argno].expand_length < 0)
2872 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2873 cpp_expand_to_buffer (pfile,
2874 ARG_BASE + args[ap->argno].raw,
2875 args[ap->argno].raw_length);
2877 args[ap->argno].expand_length
2878 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2881 /* Add 4 for two newline-space markers to prevent
2882 token concatenation. */
2883 xbuf_len += args[ap->argno].expand_length + 4;
2885 if (args[ap->argno].use_count < 10)
2886 args[ap->argno].use_count++;
2889 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
2891 /* Generate in XBUF the complete expansion
2892 with arguments substituted in.
2893 TOTLEN is the total size generated so far.
2894 OFFSET is the index in the definition
2895 of where we are copying from. */
2896 offset = totlen = 0;
2897 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2898 last_ap = ap, ap = ap->next)
2900 register struct argdata *arg = &args[ap->argno];
2901 int count_before = totlen;
2903 /* Add chars to XBUF. */
2904 for (i = 0; i < ap->nchars; i++, offset++)
2905 xbuf[totlen++] = exp[offset];
2907 /* If followed by an empty rest arg with concatenation,
2908 delete the last run of nonwhite chars. */
2909 if (rest_zero && totlen > count_before
2910 && ((ap->rest_args && ap->raw_before)
2911 || (last_ap != NULL && last_ap->rest_args
2912 && last_ap->raw_after)))
2914 /* Delete final whitespace. */
2915 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2918 /* Delete the nonwhites before them. */
2919 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2923 if (ap->stringify != 0)
2925 bcopy (ARG_BASE + arg->stringified,
2926 xbuf + totlen, arg->stringified_length);
2927 totlen += arg->stringified_length;
2929 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2931 U_CHAR *p1 = ARG_BASE + arg->raw;
2932 U_CHAR *l1 = p1 + arg->raw_length;
2935 while (p1 != l1 && is_space[*p1]) p1++;
2936 while (p1 != l1 && is_idchar[*p1])
2937 xbuf[totlen++] = *p1++;
2941 /* Arg is concatenated after: delete trailing whitespace,
2942 whitespace markers, and no-reexpansion markers. */
2945 if (is_space[l1[-1]]) l1--;
2946 else if (l1[-1] == '-')
2948 U_CHAR *p2 = l1 - 1;
2949 /* If a `-' is preceded by an odd number of newlines then it
2950 and the last newline are a no-reexpansion marker. */
2951 while (p2 != p1 && p2[-1] == '\n') p2--;
2952 if ((l1 - 1 - p2) & 1) {
2961 bcopy (p1, xbuf + totlen, l1 - p1);
2966 U_CHAR *expanded = ARG_BASE + arg->expanded;
2967 if (!ap->raw_before && totlen > 0 && arg->expand_length
2968 && !CPP_TRADITIONAL(pfile)
2969 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2971 xbuf[totlen++] = '@';
2972 xbuf[totlen++] = ' ';
2975 bcopy (expanded, xbuf + totlen, arg->expand_length);
2976 totlen += arg->expand_length;
2978 if (!ap->raw_after && totlen > 0 && offset < defn->length
2979 && !CPP_TRADITIONAL(pfile)
2980 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2982 xbuf[totlen++] = '@';
2983 xbuf[totlen++] = ' ';
2986 /* If a macro argument with newlines is used multiple times,
2987 then only expand the newlines once. This avoids creating
2988 output lines which don't correspond to any input line,
2989 which confuses gdb and gcov. */
2990 if (arg->use_count > 1 && arg->newlines > 0)
2992 /* Don't bother doing change_newlines for subsequent
2996 = change_newlines (expanded, arg->expand_length);
3000 if (totlen > xbuf_len)
3004 /* if there is anything left of the definition
3005 after handling the arg list, copy that in too. */
3007 for (i = offset; i < defn->length; i++)
3009 /* if we've reached the end of the macro */
3012 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3013 && last_ap->raw_after))
3014 xbuf[totlen++] = exp[i];
3022 pfile->output_escapes--;
3024 /* Now put the expansion on the input stack
3025 so our caller will commence reading from it. */
3026 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3027 CPP_BUFFER (pfile)->has_escapes = 1;
3029 /* Pop the space we've used in the token_buffer for argument expansion. */
3030 CPP_SET_WRITTEN (pfile, old_written);
3032 /* Recursive macro use sometimes works traditionally.
3033 #define foo(x,y) bar (x (y,0), y)
3036 if (!CPP_TRADITIONAL (pfile))
3037 hp->type = T_DISABLED;
3041 push_macro_expansion (
3043 register U_CHAR *xbuf,
3047 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3048 mbuf->cleanup = macro_cleanup;
3051 /* The first chars of the expansion should be a "@ " added by
3052 collect_expansion. This is to prevent accidental token-pasting
3053 between the text preceding the macro invocation, and the macro
3056 We would like to avoid adding unneeded spaces (for the sake of
3057 tools that use cpp, such as imake). In some common cases we can
3058 tell that it is safe to omit the space.
3060 The character before the macro invocation cannot have been an
3061 idchar (or else it would have been pasted with the idchars of
3062 the macro name). Therefore, if the first non-space character
3063 of the expansion is an idchar, we do not need the extra space
3064 to prevent token pasting.
3066 Also, we don't need the extra space if the first char is '(',
3067 or some other (less common) characters. */
3069 if (xbuf[0] == '@' && xbuf[1] == ' '
3070 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3071 || xbuf[2] == '\"'))
3075 /* Like cpp_get_token, except that it does not read past end-of-line.
3076 Also, horizontal space is skipped, and macros are popped. */
3078 static enum cpp_token
3079 get_directive_token (
3084 long old_written = CPP_WRITTEN (pfile);
3085 enum cpp_token token;
3086 cpp_skip_hspace (pfile);
3087 if (PEEKC () == '\n')
3089 token = cpp_get_token (pfile);
3093 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3095 /* ... else fall though ... */
3096 case CPP_HSPACE: case CPP_COMMENT:
3097 CPP_SET_WRITTEN (pfile, old_written);
3105 /* Handle #include and #import.
3106 This function expects to see "fname" or <fname> on the input.
3108 The input is normally in part of the output_buffer following
3109 CPP_WRITTEN, and will get overwritten by output_line_command.
3110 I.e. in input file specification has been popped by handle_directive.
3116 struct directive *keyword,
3117 U_CHAR *unused1, U_CHAR *unused2)
3119 int importing = (keyword->type == T_IMPORT);
3120 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3121 char *fname; /* Dynamically allocated fname buffer */
3124 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3125 enum cpp_token token;
3127 /* Chain of dirs to search */
3128 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3129 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3130 struct file_name_list *searchptr = 0;
3131 long old_written = CPP_WRITTEN (pfile);
3135 int f; /* file number */
3137 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3140 /* char *pcfbuflimit;*/
3142 f= -1; /* JF we iz paranoid! */
3144 if (importing && CPP_OPTIONS (pfile)->warn_import
3145 && !CPP_OPTIONS (pfile)->inhibit_warnings
3146 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3148 pfile->import_warning = 1;
3149 cpp_warning (pfile, "using `#import' is not recommended");
3150 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3151 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3152 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3153 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3154 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3155 fprintf (stderr, " ... <real contents of file> ...\n");
3156 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3157 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3158 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3159 fprintf (stderr, "when it is equipped with such a conditional.\n");
3162 pfile->parsing_include_directive++;
3163 token = get_directive_token (pfile);
3164 pfile->parsing_include_directive--;
3166 if (token == CPP_STRING)
3168 /* FIXME - check no trailing garbage */
3169 fbeg = pfile->token_buffer + old_written + 1;
3170 fend = CPP_PWRITTEN (pfile) - 1;
3171 if (fbeg[-1] == '<')
3174 /* If -I-, start with the first -I dir after the -I-. */
3175 if (CPP_OPTIONS (pfile)->first_bracket_include)
3176 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3178 /* If -I- was specified, don't search current dir, only spec'd ones. */
3179 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3182 /* We have "filename". Figure out directory this source
3183 file is coming from and put it on the front of the list. */
3185 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3190 if ((nam = fp->nominal_fname) != NULL)
3192 /* Found a named file. Figure out dir of the file,
3193 and put it in front of the search list. */
3194 dsp[0].next = search_start;
3197 ep = rindex (nam, '/');
3199 ep = rindex (nam, ']');
3200 if (ep == NULL) ep = rindex (nam, '>');
3201 if (ep == NULL) ep = rindex (nam, ':');
3202 if (ep != NULL) ep++;
3207 dsp[0].fname = (char *) alloca (n + 1);
3208 strncpy (dsp[0].fname, nam, n);
3209 dsp[0].fname[n] = '\0';
3210 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3211 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3215 dsp[0].fname = 0; /* Current directory */
3217 dsp[0].got_name_map = 0;
3224 else if (token == CPP_NAME)
3227 * Support '#include xyz' like VAX-C to allow for easy use of all the
3228 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3229 * code from case '<' is repeated here) and generates a warning.
3232 "VAX-C-style include specification found, use '#include <filename.h>' !");
3234 /* If -I-, start with the first -I dir after the -I-. */
3235 if (CPP_OPTIONS (pfile)->first_bracket_include)
3236 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3237 fbeg = pfile->token_buffer + old_written;
3238 fend = CPP_PWRITTEN (pfile);
3244 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3245 CPP_SET_WRITTEN (pfile, old_written);
3246 skip_rest_of_line (pfile);
3252 token = get_directive_token (pfile);
3253 if (token != CPP_VSPACE)
3255 cpp_error (pfile, "junk at end of `#include'");
3256 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3257 token = get_directive_token (pfile);
3260 /* For #include_next, skip in the search path
3261 past the dir in which the containing file was found. */
3265 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3266 if (fp->fname != NULL)
3268 /* fp->dir is null if the containing file was specified with
3269 an absolute file name. In that case, don't skip anything. */
3270 if (fp->dir == SELF_DIR_DUMMY)
3271 search_start = CPP_OPTIONS (pfile)->include;
3273 search_start = fp->dir->next;
3278 CPP_SET_WRITTEN (pfile, old_written);
3284 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3288 /* Allocate this permanently, because it gets stored in the definitions
3290 fname = (char *) Safe_malloc (pfile->max_include_len + flen + 4);
3291 /* + 2 above for slash and terminating null. */
3292 /* + 2 added for '.h' on VMS (to support '#include filename') */
3294 /* If specified file name is absolute, just open it. */
3297 strncpy (fname, fbeg, flen);
3299 if (redundant_include_p (pfile, fname))
3302 f = lookup_import (pfile, fname, NULL_PTR);
3304 f = open_include_file (pfile, fname, NULL_PTR);
3306 return 0; /* Already included this file */
3308 /* Search directory path, trying to open the file.
3309 Copy each filename tried into FNAME. */
3311 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3312 if (searchptr->fname) {
3313 /* The empty string in a search path is ignored.
3314 This makes it possible to turn off entirely
3315 a standard piece of the list. */
3316 if (searchptr->fname[0] == 0)
3318 strcpy (fname, searchptr->fname);
3319 strcat (fname, "/");
3320 fname[strlen (fname) + flen] = 0;
3324 strncat (fname, fbeg, flen);
3326 /* Change this 1/2 Unix 1/2 VMS file specification into a
3327 full VMS file specification */
3328 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3329 /* Fix up the filename */
3330 hack_vms_include_specification (fname);
3332 /* This is a normal VMS filespec, so use it unchanged. */
3333 strncpy (fname, fbeg, flen);
3335 /* if it's '#include filename', add the missing .h */
3336 if (index(fname,'.')==NULL) {
3337 strcat (fname, ".h");
3341 /* ??? There are currently 3 separate mechanisms for avoiding processing
3342 of redundant include files: #import, #pragma once, and
3343 redundant_include_p. It would be nice if they were unified. */
3344 if (redundant_include_p (pfile, fname))
3347 f = lookup_import (pfile, fname, searchptr);
3349 f = open_include_file (pfile, fname, searchptr);
3351 return 0; /* Already included this file */
3353 else if (f == -1 && errno == EACCES)
3354 cpp_warning (pfile, "Header file %s exists, but is not readable",
3364 /* A file that was not found. */
3365 strncpy (fname, fbeg, flen);
3367 /* If generating dependencies and -MG was specified, we assume missing
3368 files are leaf files, living in the same directory as the source file
3369 or other similar place; these missing files may be generated from
3370 other files and may not exist yet (eg: y.tab.h). */
3372 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3373 && CPP_PRINT_DEPS (pfile)
3374 > (angle_brackets || (pfile->system_include_depth > 0)))
3376 /* If it was requested as a system header file,
3377 then assume it belongs in the first place to look for such. */
3380 for (searchptr = search_start; searchptr;
3381 searchptr = searchptr->next)
3383 if (searchptr->fname)
3387 if (searchptr->fname[0] == 0)
3389 p = (char *) alloca (strlen (searchptr->fname)
3390 + strlen (fname) + 2);
3391 strcpy (p, searchptr->fname);
3394 deps_output (pfile, p, ' ');
3401 /* Otherwise, omit the directory, as if the file existed
3402 in the directory with the source. */
3403 deps_output (pfile, fname, ' ');
3406 /* If -M was specified, and this header file won't be added to the
3407 dependency list, then don't count this as an error, because we can
3408 still produce correct output. Otherwise, we can't produce correct
3409 output, because there may be dependencies we need inside the missing
3410 file, and we don't know what directory this missing file exists in.*/
3411 else if (CPP_PRINT_DEPS (pfile)
3412 && (CPP_PRINT_DEPS (pfile)
3413 <= (angle_brackets || (pfile->system_include_depth > 0))))
3414 cpp_warning (pfile, "No include path in which to find %s", fname);
3415 else if (search_start)
3416 cpp_error_from_errno (pfile, fname);
3418 cpp_error (pfile, "No include path in which to find %s", fname);
3421 /* Check to see if this include file is a once-only include file.
3424 struct file_name_list* ptr;
3426 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3427 if (!strcmp (ptr->fname, fname)) {
3429 return 0; /* This file was once'd. */
3433 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3434 if (!strcmp (ptr->fname, fname))
3435 break; /* This file was included before. */
3439 /* This is the first time for this file. */
3440 /* Add it to list of files included. */
3442 ptr = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3443 ptr->control_macro = 0;
3444 ptr->c_system_include_path = 0;
3445 ptr->next = pfile->all_include_files;
3446 pfile->all_include_files = ptr;
3447 ptr->fname = savestring (fname);
3448 ptr->got_name_map = 0;
3450 /* For -M, add this file to the dependencies. */
3451 if (CPP_PRINT_DEPS (pfile)
3452 > (angle_brackets || (pfile->system_include_depth > 0)))
3453 deps_output (pfile, fname, ' ');
3456 /* Handle -H option. */
3457 if (CPP_OPTIONS(pfile)->print_include_names)
3459 cpp_buffer *buf = CPP_BUFFER (pfile);
3460 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3462 fprintf (stderr, "%s\n", fname);
3466 pfile->system_include_depth++;
3468 /* Actually process the file. */
3470 /* Record file on "seen" list for #import. */
3471 add_import (pfile, f, fname);
3473 pcftry = (char *) alloca (strlen (fname) + 30);
3485 sprintf (pcftry, "%s%d", fname, pcfnum++);
3487 pcf = open (pcftry, O_RDONLY, 0666);
3493 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3495 || stat_f.st_dev != s.st_dev)
3497 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3498 /* Don't need it any more. */
3503 /* Don't need it at all. */
3508 } while (pcf != -1 && !pcfbuf);
3512 /* Actually process the file */
3513 cpp_push_buffer (pfile, NULL, 0);
3514 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3515 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3517 output_line_command (pfile, 0, enter_file);
3518 pfile->only_seen_white = 2;
3522 pfile->system_include_depth--;
3527 /* Return nonzero if there is no need to include file NAME
3528 because it has already been included and it contains a conditional
3529 to make a repeated include do nothing. */
3532 redundant_include_p (
3536 struct file_name_list *l = pfile->all_include_files;
3537 for (; l; l = l->next)
3538 if (! strcmp (name, l->fname)
3540 && cpp_lookup (pfile, l->control_macro, -1, -1))
3545 /* Return nonzero if the given FILENAME is an absolute pathname which
3546 designates a file within one of the known "system" include file
3547 directories. We assume here that if the given FILENAME looks like
3548 it is the name of a file which resides either directly in a "system"
3549 include file directory, or within any subdirectory thereof, then the
3550 given file must be a "system" include file. This function tells us
3551 if we should suppress pedantic errors/warnings for the given FILENAME.
3553 The value is 2 if the file is a C-language system header file
3554 for which C++ should (on most systems) assume `extern "C"'. */
3559 register char *filename)
3561 struct file_name_list *searchptr;
3563 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3564 searchptr = searchptr->next)
3565 if (searchptr->fname) {
3566 register char *sys_dir = searchptr->fname;
3567 register unsigned length = strlen (sys_dir);
3569 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3571 if (searchptr->c_system_include_path)
3582 * Install a name in the assertion hash table.
3584 * If LEN is >= 0, it is the length of the name.
3585 * Otherwise, compute the length by scanning the entire name.
3587 * If HASH is >= 0, it is the precomputed hash code.
3588 * Otherwise, compute the hash code.
3590 static ASSERTION_HASHNODE *
3597 register ASSERTION_HASHNODE *hp;
3598 register int i, bucket;
3599 register U_CHAR *p, *q;
3601 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3602 hp = (ASSERTION_HASHNODE *) Safe_malloc (i);
3604 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3605 hp->next = pfile->assertion_hashtab[bucket];
3606 pfile->assertion_hashtab[bucket] = hp;
3608 if (hp->next != NULL)
3609 hp->next->prev = hp;
3612 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3615 for (i = 0; i < len; i++)
3621 * find the most recent hash node for name name (ending with first
3622 * non-identifier char) installed by install
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.
3631 static ASSERTION_HASHNODE *
3638 register ASSERTION_HASHNODE *bucket;
3640 bucket = pfile->assertion_hashtab[hash];
3642 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3644 bucket = bucket->next;
3651 ASSERTION_HASHNODE *hp)
3653 struct tokenlist_list *tail;
3654 if (hp->prev != NULL)
3655 hp->prev->next = hp->next;
3656 if (hp->next != NULL)
3657 hp->next->prev = hp->prev;
3659 for (tail = hp->value; tail; )
3661 struct tokenlist_list *next = tail->next;
3662 free_token_list (tail->tokens);
3667 /* make sure that the bucket chain header that
3668 the deleted guy was on points to the right thing afterwards. */
3669 if (hp == *hp->bucket_hdr)
3670 *hp->bucket_hdr = hp->next;
3675 /* Convert a character string literal into a nul-terminated string.
3676 The input string is [IN ... LIMIT).
3677 The result is placed in RESULT. RESULT can be the same as IN.
3678 The value returned in the end of the string written to RESULT,
3679 or NULL on error. */
3684 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3704 char *bpc = (char *) in;
3705 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3706 in = (U_CHAR *) bpc;
3708 *result++ = (U_CHAR)c;
3711 /* else fall through */
3721 * interpret #line command. Remembers previously seen fnames
3722 * in its very own hash table.
3724 #define FNAME_HASHSIZE 37
3729 struct directive *keyword)
3731 cpp_buffer *ip = CPP_BUFFER (pfile);
3733 long old_written = CPP_WRITTEN (pfile);
3734 enum file_change_code file_change = same_file;
3735 enum cpp_token token;
3738 token = get_directive_token (pfile);
3740 if (token != CPP_NUMBER
3741 || !isdigit(pfile->token_buffer[old_written]))
3743 cpp_error (pfile, "invalid format `#line' command");
3744 goto bad_line_directive;
3747 /* The Newline at the end of this line remains to be processed.
3748 To put the next line at the specified line number,
3749 we must store a line number now that is one less. */
3750 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3751 CPP_SET_WRITTEN (pfile, old_written);
3753 /* NEW_LINENO is one less than the actual line number here. */
3754 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3755 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3757 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3758 if (PEEKC() && !is_space[PEEKC()]) {
3759 cpp_error (pfile, "invalid format `#line' command");
3760 goto bad_line_directive;
3764 token = get_directive_token (pfile);
3766 if (token == CPP_STRING) {
3767 U_CHAR *fname = pfile->token_buffer + old_written;
3769 static HASHNODE *fname_table[FNAME_HASHSIZE];
3770 HASHNODE *hp, **hash_bucket;
3775 /* Turn the file name, which is a character string literal,
3776 into a null-terminated string. Do this in place. */
3777 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3778 if (end_name == NULL)
3780 cpp_error (pfile, "invalid format `#line' command");
3781 goto bad_line_directive;
3784 fname_length = end_name - fname;
3786 num_start = CPP_WRITTEN (pfile);
3787 token = get_directive_token (pfile);
3788 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3789 p = pfile->token_buffer + num_start;
3790 if (CPP_PEDANTIC (pfile))
3791 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3793 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3795 cpp_error (pfile, "invalid format `#line' command");
3796 goto bad_line_directive;
3799 file_change = enter_file;
3801 file_change = leave_file;
3803 ip->system_header_p = 1;
3804 else /* if (*p == 4) */
3805 ip->system_header_p = 2;
3807 CPP_SET_WRITTEN (pfile, num_start);
3808 token = get_directive_token (pfile);
3809 p = pfile->token_buffer + num_start;
3810 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3811 ip->system_header_p = *p == 3 ? 1 : 2;
3812 token = get_directive_token (pfile);
3814 if (token != CPP_VSPACE) {
3815 cpp_error (pfile, "invalid format `#line' command");
3816 goto bad_line_directive;
3821 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3822 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3823 if (hp->length == fname_length &&
3824 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3825 ip->nominal_fname = hp->value.cpval;
3829 /* Didn't find it; cons up a new one. */
3830 hp = (HASHNODE *) Safe_calloc(1,sizeof (HASHNODE) + fname_length + 1);
3831 hp->next = *hash_bucket;
3834 hp->length = fname_length;
3835 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3836 bcopy (fname, hp->value.cpval, fname_length);
3839 else if (token != CPP_VSPACE && token != CPP_EOF) {
3840 cpp_error (pfile, "invalid format `#line' command");
3841 goto bad_line_directive;
3844 ip->lineno = new_lineno;
3846 skip_rest_of_line (pfile);
3847 CPP_SET_WRITTEN (pfile, old_written);
3848 output_line_command (pfile, 0, file_change);
3853 * remove the definition of a symbol from the symbol table.
3854 * according to un*x /lib/cpp, it is not an error to undef
3855 * something that has no definitions, so it isn't one here either.
3861 struct directive *keyword,
3862 U_CHAR *buf, U_CHAR *limit)
3866 U_CHAR *orig_buf = buf;
3869 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3870 if (pcp_outfile && keyword)
3871 pass_thru_directive (buf, limit, pfile, keyword);
3874 SKIP_WHITE_SPACE (buf);
3875 sym_length = check_macro_name (pfile, buf, "macro");
3877 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3879 /* If we are generating additional info for debugging (with -g) we
3880 need to pass through all effective #undef commands. */
3881 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3882 pass_thru_directive (orig_buf, limit, pfile, keyword);
3883 if (hp->type != T_MACRO)
3884 cpp_warning (pfile, "undefining `%s'", hp->name);
3888 if (CPP_PEDANTIC (pfile)) {
3890 SKIP_WHITE_SPACE (buf);
3892 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3898 * Report an error detected by the program we are processing.
3899 * Use the text of the line in the error message.
3900 * (We use error because it prints the filename & line#.)
3906 struct directive *keyword,
3907 U_CHAR *buf,U_CHAR *limit)
3909 int length = limit - buf;
3910 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3911 bcopy (buf, copy, length);
3913 SKIP_WHITE_SPACE (copy);
3914 cpp_error (pfile, "#error %s", copy);
3919 * Report a warning detected by the program we are processing.
3920 * Use the text of the line in the warning message, then continue.
3921 * (We use error because it prints the filename & line#.)
3927 struct directive *keyword,
3928 U_CHAR *buf,U_CHAR *limit)
3930 int length = limit - buf;
3931 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3932 bcopy (buf, copy, length);
3934 SKIP_WHITE_SPACE (copy);
3935 cpp_warning (pfile, "#warning %s", copy);
3939 /* Remember the name of the current file being read from so that we can
3940 avoid ever including it again. */
3946 cpp_buffer *ip = NULL;
3947 struct file_name_list *new;
3949 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3953 if (ip->fname != NULL)
3958 new = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3959 new->next = pfile->dont_repeat_files;
3960 pfile->dont_repeat_files = new;
3961 new->fname = savestring (ip->fname);
3962 new->control_macro = 0;
3963 new->got_name_map = 0;
3964 new->c_system_include_path = 0;
3969 /* #ident has already been copied to the output file, so just ignore it. */
3974 struct directive *keyword,
3975 U_CHAR *buf, U_CHAR *limit)
3977 /* long old_written = CPP_WRITTEN (pfile);*/
3980 /* Allow #ident in system headers, since that's not user's fault. */
3981 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3982 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3984 /* Leave rest of line to be read by later calls to cpp_get_token. */
3989 /* #pragma and its argument line have already been copied to the output file.
3990 Just check for some recognized pragmas that need validation here. */
3995 struct directive *keyword,
3996 U_CHAR *buf,U_CHAR *limit)
3998 while (*buf == ' ' || *buf == '\t')
4000 if (!strncmp (buf, "once", 4)) {
4001 /* Allow #pragma once in system headers, since that's not the user's
4003 if (!CPP_BUFFER (pfile)->system_header_p)
4004 cpp_warning (pfile, "`#pragma once' is obsolete");
4008 if (!strncmp (buf, "implementation", 14)) {
4009 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4010 been included yet. */
4011 struct file_name_list *ptr;
4012 U_CHAR *p = buf + 14, *fname, *inc_fname;
4014 SKIP_WHITE_SPACE (p);
4015 if (*p == '\n' || *p != '\"')
4019 p = (U_CHAR *) index (fname, '\"');
4020 fname_len = p != NULL ? p - fname : strlen (fname);
4022 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4023 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4024 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4025 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4027 "`#pragma implementation' for `%s' appears after file is included",
4036 /* This was a fun hack, but #pragma seems to start to be useful.
4037 By failing to recognize it, we pass it through unchanged to cc1. */
4040 * the behavior of the #pragma directive is implementation defined.
4041 * this implementation defines it as follows.
4048 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4051 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4053 execl ("/usr/games/hack", "#pragma", 0);
4054 execl ("/usr/games/rogue", "#pragma", 0);
4055 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4056 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4058 fatal ("You are in a maze of twisty compiler features, all different");
4062 /* Just ignore #sccs, on systems where we define it at all. */
4067 struct directive *keyword,
4068 U_CHAR *buf,U_CHAR *limit)
4070 if (CPP_PEDANTIC (pfile))
4071 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4076 * handle #if command by
4077 * 1) inserting special `defined' keyword into the hash table
4078 * that gets turned into 0 or 1 by special_symbol (thus,
4079 * if the luser has a symbol called `defined' already, it won't
4080 * work inside the #if command)
4081 * 2) rescan the input into a temporary output buffer
4082 * 3) pass the output buffer to the yacc parser and collect a value
4083 * 4) clean up the mess left from steps 1 and 2.
4084 * 5) call conditional_skip to skip til the next #endif (etc.),
4085 * or not, depending on the value from step 3.
4091 struct directive *keyword,
4092 U_CHAR *buf,U_CHAR *limit)
4094 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4095 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4100 * handle a #elif directive by not changing if_stack either.
4101 * see the comment above do_else.
4107 struct directive *keyword,
4108 U_CHAR *buf,U_CHAR *limit)
4110 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4111 cpp_error (pfile, "`#elif' not within a conditional");
4114 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4115 cpp_error (pfile, "`#elif' after `#else'");
4117 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4119 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4120 && strcmp (pfile->if_stack->fname,
4121 CPP_BUFFER (pfile)->nominal_fname) != 0)
4122 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4123 fprintf (stderr, ")\n");
4125 pfile->if_stack->type = T_ELIF;
4128 if (pfile->if_stack->if_succeeded)
4129 skip_if_group (pfile, 0);
4131 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4133 skip_if_group (pfile, 0);
4135 ++pfile->if_stack->if_succeeded; /* continue processing input */
4136 output_line_command (pfile, 1, same_file);
4143 * evaluate a #if expression in BUF, of length LENGTH,
4144 * then parse the result as a C expression and return the value as an int.
4146 static HOST_WIDE_INT
4147 eval_if_expression (
4152 HASHNODE *save_defined;
4153 HOST_WIDE_INT value;
4154 long old_written = CPP_WRITTEN (pfile);
4156 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4157 pfile->pcp_inside_if = 1;
4159 value = cpp_parse_expr (pfile);
4160 pfile->pcp_inside_if = 0;
4161 delete_macro (save_defined); /* clean up special symbol */
4163 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4169 * routine to handle ifdef/ifndef. Try to look up the symbol,
4170 * then do or don't skip to the #endif/#else/#elif depending
4171 * on what directive is actually being processed.
4177 struct directive *keyword,
4178 U_CHAR *unused1, U_CHAR *unused2)
4181 cpp_buffer *ip = CPP_BUFFER (pfile);
4184 enum cpp_token token;
4185 int start_of_file = 0;
4186 U_CHAR *control_macro = 0;
4187 int old_written = CPP_WRITTEN (pfile);
4189 /* Detect a #ifndef at start of file (not counting comments). */
4190 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4191 start_of_file = pfile->only_seen_white == 2;
4193 pfile->no_macro_expand++;
4194 token = get_directive_token (pfile);
4195 pfile->no_macro_expand--;
4197 ident = pfile->token_buffer + old_written;
4198 ident_length = CPP_WRITTEN (pfile) - old_written;
4199 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4201 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4203 skip = (keyword->type == T_IFDEF);
4204 if (! CPP_TRADITIONAL (pfile))
4205 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4207 else if (token == CPP_NAME)
4209 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4210 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4211 if (start_of_file && !skip)
4213 control_macro = (U_CHAR *) Safe_malloc (ident_length + 1);
4214 bcopy (ident, control_macro, ident_length + 1);
4219 skip = (keyword->type == T_IFDEF);
4220 if (! CPP_TRADITIONAL (pfile))
4221 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4224 if (!CPP_TRADITIONAL (pfile))
4226 cpp_skip_hspace (pfile);
4228 if (c != EOF && c != '\n')
4229 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4231 skip_rest_of_line (pfile);
4235 /* Output a precondition for this macro. */
4236 if (hp && hp->value.defn->predefined)
4237 fprintf (pcp_outfile, "#define %s\n", hp->name);
4240 fprintf (pcp_outfile, "#undef ");
4241 while (is_idchar[*cp]) /* Ick! */
4242 fputc (*cp++, pcp_outfile);
4243 putc ('\n', pcp_outfile);
4247 conditional_skip (pfile, skip, T_IF, control_macro);
4251 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4252 If this is a #ifndef starting at the beginning of a file,
4253 CONTROL_MACRO is the macro name tested by the #ifndef.
4254 Otherwise, CONTROL_MACRO is 0. */
4260 enum node_type type,
4261 U_CHAR *control_macro)
4263 IF_STACK_FRAME *temp;
4265 temp = (IF_STACK_FRAME *) Safe_calloc(1,sizeof (IF_STACK_FRAME));
4266 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4268 temp->lineno = CPP_BUFFER (pfile)->lineno;
4270 temp->next = pfile->if_stack;
4271 temp->control_macro = control_macro;
4272 pfile->if_stack = temp;
4274 pfile->if_stack->type = type;
4277 skip_if_group (pfile, 0);
4280 ++pfile->if_stack->if_succeeded;
4281 output_line_command (pfile, 1, same_file);
4286 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4287 * leaves input ptr at the sharp sign found.
4288 * If ANY is nonzero, return at next directive of any sort.
4296 struct directive *kt;
4297 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4299 U_CHAR *beg_of_line = bp;
4301 register int ident_length;
4302 U_CHAR *ident /*, *after_ident */ ;
4303 struct parse_marker line_start_mark;
4305 parse_set_mark (&line_start_mark, pfile);
4307 if (CPP_OPTIONS (pfile)->output_conditionals) {
4308 static char failed[] = "#failed\n";
4309 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4311 output_line_command (pfile, 1, same_file);
4315 if (CPP_OPTIONS (pfile)->output_conditionals)
4317 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4318 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4319 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4321 parse_move_mark (&line_start_mark, pfile);
4322 if (!CPP_TRADITIONAL (pfile))
4323 cpp_skip_hspace (pfile);
4327 int old_written = CPP_WRITTEN (pfile);
4328 cpp_skip_hspace (pfile);
4330 parse_name (pfile, GETC());
4331 ident_length = CPP_WRITTEN (pfile) - old_written;
4332 ident = pfile->token_buffer + old_written;
4333 pfile->limit = ident;
4335 if (ident_length == 0)
4336 goto not_a_directive;
4338 /* Handle # followed by a line number. */
4340 /* Avoid error for `###' and similar cases unless -pedantic. */
4343 for (kt = directive_table; kt->length >= 0; kt++)
4345 IF_STACK_FRAME *temp;
4346 if (ident_length == kt->length
4347 && strncmp (ident, kt->name, kt->length) == 0)
4349 /* If we are asked to return on next directive, do so now. */
4359 = (IF_STACK_FRAME *) Safe_calloc(1,sizeof (IF_STACK_FRAME));
4360 temp->next = pfile->if_stack;
4361 pfile->if_stack = temp;
4363 temp->lineno = CPP_BUFFER(pfile)->lineno;
4365 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4366 temp->type = kt->type;
4370 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4371 validate_else (pfile,
4372 kt->type == T_ELSE ? "#else" : "#endif");
4374 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4377 "`#%s' not within a conditional", kt->name);
4380 else if (pfile->if_stack == save_if_stack)
4381 goto done; /* found what we came for */
4383 if (kt->type != T_ENDIF)
4385 if (pfile->if_stack->type == T_ELSE)
4386 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4387 pfile->if_stack->type = kt->type;
4391 temp = pfile->if_stack;
4392 pfile->if_stack = temp->next;
4399 /* Don't let erroneous code go by. */
4400 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4401 && CPP_PEDANTIC (pfile))
4402 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4406 /* We're in the middle of a line. Skip the rest of it. */
4413 case '/': /* possible comment */
4414 c = skip_comment (pfile, NULL);
4421 old = CPP_WRITTEN (pfile);
4422 cpp_get_token (pfile);
4423 CPP_SET_WRITTEN (pfile, old);
4426 /* Char after backslash loses its special meaning. */
4427 if (PEEKC() == '\n')
4437 if (CPP_OPTIONS (pfile)->output_conditionals) {
4438 static char end_failed[] = "#endfailed\n";
4439 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4442 pfile->only_seen_white = 1;
4443 parse_goto_mark (&line_start_mark, pfile);
4444 parse_clear_mark (&line_start_mark);
4448 * handle a #else directive. Do this by just continuing processing
4449 * without changing if_stack ; this is so that the error message
4450 * for missing #endif's etc. will point to the original #if. It
4451 * is possible that something different would be better.
4457 struct directive *keyword,
4458 U_CHAR *buf, U_CHAR *limit)
4460 cpp_buffer *ip = CPP_BUFFER (pfile);
4462 if (CPP_PEDANTIC (pfile))
4463 validate_else (pfile, "#else");
4464 skip_rest_of_line (pfile);
4466 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4467 cpp_error (pfile, "`#else' not within a conditional");
4470 /* #ifndef can't have its special treatment for containing the whole file
4471 if it has a #else clause. */
4472 pfile->if_stack->control_macro = 0;
4474 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4475 cpp_error (pfile, "`#else' after `#else'");
4476 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4477 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4478 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4479 fprintf (stderr, ")\n");
4481 pfile->if_stack->type = T_ELSE;
4484 if (pfile->if_stack->if_succeeded)
4485 skip_if_group (pfile, 0);
4487 ++pfile->if_stack->if_succeeded; /* continue processing input */
4488 output_line_command (pfile, 1, same_file);
4494 * unstack after #endif command
4500 struct directive *keyword,
4501 U_CHAR *buf, U_CHAR *limit)
4503 if (CPP_PEDANTIC (pfile))
4504 validate_else (pfile, "#endif");
4505 skip_rest_of_line (pfile);
4507 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4508 cpp_error (pfile, "unbalanced `#endif'");
4511 IF_STACK_FRAME *temp = pfile->if_stack;
4512 pfile->if_stack = temp->next;
4513 if (temp->control_macro != 0)
4515 /* This #endif matched a #ifndef at the start of the file.
4516 See if it is at the end of the file. */
4517 struct parse_marker start_mark;
4520 parse_set_mark (&start_mark, pfile);
4524 cpp_skip_hspace (pfile);
4529 parse_goto_mark (&start_mark, pfile);
4530 parse_clear_mark (&start_mark);
4534 /* If we get here, this #endif ends a #ifndef
4535 that contains all of the file (aside from whitespace).
4536 Arrange not to include the file again
4537 if the macro that was tested is defined.
4539 Do not do this for the top-level file in a -include or any
4540 file in a -imacros. */
4544 && ! (indepth == 1 && pfile->no_record_file)
4545 && ! (pfile->no_record_file && no_output))
4548 struct file_name_list *ifile = pfile->all_include_files;
4550 for ( ; ifile != NULL; ifile = ifile->next)
4552 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4554 ifile->control_macro = temp->control_macro;
4562 output_line_command (pfile, 1, same_file);
4567 /* When an #else or #endif is found while skipping failed conditional,
4568 if -pedantic was specified, this is called to warn about text after
4569 the command name. P points to the first char after the command name. */
4577 cpp_skip_hspace (pfile);
4579 if (c != EOF && c != '\n')
4581 "text following `%s' violates ANSI standard", directive);
4584 /* Get the next token, and add it to the text in pfile->token_buffer.
4585 Return the kind of token we got. */
4592 register int c, c2, c3;
4593 long old_written = 0;
4594 long start_line, start_column;
4595 enum cpp_token token;
4596 struct cpp_options *opts = CPP_OPTIONS (pfile);
4597 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4603 if (CPP_BUFFER (pfile)->seen_eof)
4605 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4612 cpp_buffer *next_buf
4613 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4614 CPP_BUFFER (pfile)->seen_eof = 1;
4615 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4617 /* We're about to return from an #include file.
4618 Emit #line information now (as part of the CPP_POP) result.
4619 But the #line refers to the file we will pop to. */
4620 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4621 CPP_BUFFER (pfile) = next_buf;
4622 pfile->input_stack_listing_current = 0;
4623 output_line_command (pfile, 0, leave_file);
4624 CPP_BUFFER (pfile) = cur_buffer;
4634 struct parse_marker start_mark;
4636 if (PEEKC () == '=')
4638 if (opts->put_out_comments)
4639 parse_set_mark (&start_mark, pfile);
4641 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4642 &start_line, &start_column);
4643 c = skip_comment (pfile, &newlines);
4644 if (opts->put_out_comments && (c == '/' || c == EOF))
4645 parse_clear_mark (&start_mark);
4650 cpp_error_with_line (pfile, start_line, start_column,
4651 "unterminated comment");
4654 c = '/'; /* Initial letter of comment. */
4656 /* Comments are equivalent to spaces.
4657 For -traditional, a comment is equivalent to nothing. */
4658 if (opts->put_out_comments)
4660 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4662 U_CHAR *start = pbuf->buf + start_mark.position;
4663 int len = pbuf->cur - start;
4664 CPP_RESERVE(pfile, 1 + len);
4665 CPP_PUTC_Q (pfile, c);
4666 CPP_PUTS_Q (pfile, start, len);
4667 pfile->lineno += newlines;
4668 parse_clear_mark (&start_mark);
4671 else if (CPP_TRADITIONAL (pfile))
4678 /* This may not work if cpp_get_token is called recursively,
4679 since many places look for horizontal space. */
4682 /* Copy the newlines into the output buffer, in order to
4683 avoid the pain of a #line every time a multiline comment
4685 CPP_RESERVE(pfile, newlines);
4686 while (--newlines >= 0)
4688 CPP_PUTC_Q (pfile, '\n');
4694 CPP_RESERVE(pfile, 1);
4695 CPP_PUTC_Q (pfile, ' ');
4699 if (opts->for_lint) {
4702 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4704 if (lintcmd != NULL) {
4705 /* I believe it is always safe to emit this newline: */
4707 bcopy ("#pragma lint ", (char *) obp, 13);
4709 bcopy (lintcmd, (char *) obp, cmdlen);
4714 bcopy (argbp, (char *) obp, arglen);
4718 /* OK, now bring us back to the state we were in before we entered
4719 this branch. We need #line b/c the newline for the pragma
4720 could fuck things up. */
4721 output_line_command (pfile, 0, same_file);
4722 *(obp++) = ' '; /* just in case, if comments are copied thru */
4730 /* If this is expanding a macro definition, don't recognize
4731 preprocessor directives. */
4734 /* If this is expand_into_temp_buffer, recognize them
4735 only after an actual newline at this level,
4736 not at the beginning of the input level. */
4737 if (ip->fname == 0 && beg_of_line == ip->buf)
4743 if (!pfile->only_seen_white)
4745 if (handle_directive (pfile))
4746 return CPP_DIRECTIVE;
4747 pfile->only_seen_white = 0;
4752 /* A single quoted string is treated like a double -- some
4753 programs (e.g., troff) are perverse this way */
4754 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4755 &start_line, &start_column);
4756 old_written = CPP_WRITTEN (pfile);
4758 CPP_PUTC (pfile, c);
4764 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4766 /* try harder: this string crosses a macro expansion
4767 boundary. This can happen naturally if -traditional.
4768 Otherwise, only -D can make a macro with an unmatched
4770 cpp_buffer *next_buf
4771 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4772 (*CPP_BUFFER (pfile)->cleanup)
4773 (CPP_BUFFER (pfile), pfile);
4774 CPP_BUFFER (pfile) = next_buf;
4777 if (!CPP_TRADITIONAL (pfile))
4779 cpp_error_with_line (pfile, start_line, start_column,
4780 "unterminated string or character constant");
4781 if (pfile->multiline_string_line != start_line
4782 && pfile->multiline_string_line != 0)
4783 cpp_error_with_line (pfile,
4784 pfile->multiline_string_line, -1,
4785 "possible real start of unterminated constant");
4786 pfile->multiline_string_line = 0;
4790 CPP_PUTC (pfile, cc);
4794 /* Traditionally, end of line ends a string constant with
4795 no error. So exit the loop and record the new line. */
4796 if (CPP_TRADITIONAL (pfile))
4800 cpp_error_with_line (pfile, start_line, start_column,
4801 "unterminated character constant");
4804 if (CPP_PEDANTIC (pfile)
4805 && pfile->multiline_string_line == 0)
4807 cpp_pedwarn_with_line (pfile, start_line, start_column,
4808 "string constant runs past end of line");
4810 if (pfile->multiline_string_line == 0)
4811 pfile->multiline_string_line = start_line;
4818 /* Backslash newline is replaced by nothing at all. */
4819 CPP_ADJUST_WRITTEN (pfile, -1);
4824 /* ANSI stupidly requires that in \\ the second \
4825 is *not* prevented from combining with a newline. */
4828 CPP_PUTC (pfile, cc);
4840 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4841 CPP_PWRITTEN (pfile));
4842 pfile->only_seen_white = 0;
4843 return c == '\'' ? CPP_CHAR : CPP_STRING;
4846 if (!opts->dollars_in_ident)
4851 if (opts->cplusplus && PEEKC () == ':')
4860 if (c2 == c || c2 == '=')
4870 if (PEEKC () == '=')
4877 if (c2 == '-' && opts->chill)
4879 /* Chill style comment */
4880 if (opts->put_out_comments)
4881 parse_set_mark (&start_mark, pfile);
4882 FORWARD(1); /* Skip second '-'. */
4890 /* Don't consider final '\n' to be part of comment. */
4896 goto return_comment;
4898 if (c2 == '-' || c2 == '=' || c2 == '>')
4903 if (pfile->parsing_include_directive)
4907 CPP_PUTC (pfile, c);
4912 if (c == '\n' || c == EOF)
4915 "missing '>' in `#include <FILENAME>'");
4921 /* else fall through */
4930 CPP_RESERVE (pfile, 4);
4931 CPP_PUTC (pfile, c);
4932 CPP_PUTC (pfile, c2);
4936 CPP_PUTC_Q (pfile, GETC ());
4937 CPP_NUL_TERMINATE_Q (pfile);
4938 pfile->only_seen_white = 0;
4942 if (CPP_BUFFER (pfile)->has_escapes)
4947 if (pfile->output_escapes)
4948 CPP_PUTS (pfile, "@-", 2);
4949 parse_name (pfile, GETC ());
4952 else if (is_space [c])
4954 CPP_RESERVE (pfile, 2);
4955 if (pfile->output_escapes)
4956 CPP_PUTC_Q (pfile, '@');
4957 CPP_PUTC_Q (pfile, c);
4961 if (pfile->output_escapes)
4963 CPP_PUTS (pfile, "@@", 2);
4973 CPP_RESERVE(pfile, 2);
4974 CPP_PUTC_Q (pfile, '.');
4978 /* FIXME - misses the case "..\\\n." */
4979 if (c2 == '.' && PEEKN(1) == '.')
4981 CPP_RESERVE(pfile, 4);
4982 CPP_PUTC_Q (pfile, '.');
4983 CPP_PUTC_Q (pfile, '.');
4984 CPP_PUTC_Q (pfile, '.');
4986 CPP_NUL_TERMINATE_Q (pfile);
4987 pfile->only_seen_white = 0;
4994 pfile->only_seen_white = 0;
4996 CPP_RESERVE(pfile, 3);
4997 CPP_PUTC_Q (pfile, c);
4998 CPP_PUTC_Q (pfile, GETC ());
4999 CPP_NUL_TERMINATE_Q (pfile);
5005 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5007 CPP_PUTC (pfile, c);
5013 case '0': case '1': case '2': case '3': case '4':
5014 case '5': case '6': case '7': case '8': case '9':
5019 CPP_RESERVE (pfile, 2);
5020 CPP_PUTC_Q (pfile, c);
5025 if (!is_idchar[c] && c != '.'
5026 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5031 CPP_NUL_TERMINATE_Q (pfile);
5032 pfile->only_seen_white = 0;
5034 case 'b': case 'c': case 'd': case 'h': case 'o':
5035 case 'B': case 'C': case 'D': case 'H': case 'O':
5036 if (opts->chill && PEEKC () == '\'')
5038 pfile->only_seen_white = 0;
5039 CPP_RESERVE (pfile, 2);
5040 CPP_PUTC_Q (pfile, c);
5041 CPP_PUTC_Q (pfile, '\'');
5047 goto chill_number_eof;
5050 if (c == '\\' && PEEKC() == '\n')
5057 CPP_PUTC (pfile, c);
5061 CPP_RESERVE (pfile, 2);
5062 CPP_PUTC_Q (pfile, c);
5063 CPP_NUL_TERMINATE_Q (pfile);
5070 CPP_NUL_TERMINATE (pfile);
5077 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5078 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5079 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5080 case 'x': case 'y': case 'z':
5081 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5082 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5083 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5088 unsigned char *ident;
5089 int before_name_written = CPP_WRITTEN (pfile);
5091 parse_name (pfile, c);
5092 pfile->only_seen_white = 0;
5093 if (pfile->no_macro_expand)
5095 ident = pfile->token_buffer + before_name_written;
5096 ident_len = CPP_PWRITTEN (pfile) - ident;
5097 hp = cpp_lookup (pfile, ident, ident_len, -1);
5100 if (hp->type == T_DISABLED)
5102 if (pfile->output_escapes)
5103 { /* Return "@-IDENT", followed by '\0'. */
5105 CPP_RESERVE (pfile, 3);
5106 ident = pfile->token_buffer + before_name_written;
5107 CPP_ADJUST_WRITTEN (pfile, 2);
5108 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5115 /* If macro wants an arglist, verify that a '(' follows.
5116 first skip all whitespace, copying it to the output
5117 after the macro name. Then, if there is no '(',
5118 decide this is not a macro call and leave things that way. */
5119 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5121 struct parse_marker macro_mark;
5123 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5125 cpp_buffer *next_buf;
5126 cpp_skip_hspace (pfile);
5127 if (PEEKC () != EOF)
5129 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5130 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5131 CPP_BUFFER (pfile) = next_buf;
5133 parse_set_mark (¯o_mark, pfile);
5136 cpp_skip_hspace (pfile);
5138 is_macro_call = c == '(';
5144 parse_goto_mark (¯o_mark, pfile);
5145 parse_clear_mark (¯o_mark);
5149 /* This is now known to be a macro call. */
5151 /* it might not actually be a macro. */
5152 if (hp->type != T_MACRO) {
5153 int xbuf_len; U_CHAR *xbuf;
5154 CPP_SET_WRITTEN (pfile, before_name_written);
5155 special_symbol (hp, pfile);
5156 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5157 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
5158 CPP_SET_WRITTEN (pfile, before_name_written);
5159 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5160 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5164 /* Expand the macro, reading arguments as needed,
5165 and push the expansion on the input stack. */
5166 macroexpand (pfile, hp);
5167 CPP_SET_WRITTEN (pfile, before_name_written);
5170 /* An extra "@ " is added to the end of a macro expansion
5171 to prevent accidental token pasting. We prefer to avoid
5172 unneeded extra spaces (for the sake of cpp-using tools like
5173 imake). Here we remove the space if it is safe to do so. */
5174 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5175 && pfile->buffer->rlimit[-2] == '@'
5176 && pfile->buffer->rlimit[-1] == ' ')
5178 int c1 = pfile->buffer->rlimit[-3];
5179 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5180 if (c2 == EOF || ! unsafe_chars (c1, c2))
5181 pfile->buffer->rlimit -= 2;
5186 case ' ': case '\t': case '\v': case '\r':
5189 CPP_PUTC (pfile, c);
5191 if (c == EOF || !is_hor_space[c])
5205 CPP_PUTC (pfile, c);
5206 if (pfile->only_seen_white == 0)
5207 pfile->only_seen_white = 1;
5209 output_line_command (pfile, 1, same_file);
5212 case '(': token = CPP_LPAREN; goto char1;
5213 case ')': token = CPP_RPAREN; goto char1;
5214 case '{': token = CPP_LBRACE; goto char1;
5215 case '}': token = CPP_RBRACE; goto char1;
5216 case ',': token = CPP_COMMA; goto char1;
5217 case ';': token = CPP_SEMICOLON; goto char1;
5223 pfile->only_seen_white = 0;
5224 CPP_PUTC (pfile, c);
5230 /* Like cpp_get_token, but skip spaces and comments. */
5232 cpp_get_non_space_token (
5235 int old_written = CPP_WRITTEN (pfile);
5238 enum cpp_token token = cpp_get_token (pfile);
5239 if (token != CPP_COMMENT && token != CPP_POP
5240 && token != CPP_HSPACE && token != CPP_VSPACE)
5242 CPP_SET_WRITTEN (pfile, old_written);
5246 /* Parse an identifier starting with C. */
5250 cpp_reader *pfile, int c)
5256 if (c == '\\' && PEEKC() == '\n')
5265 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5266 CPP_PUTC_Q (pfile, c);
5271 CPP_NUL_TERMINATE_Q (pfile);
5276 /* Maintain and search list of included files, for #import. */
5278 /* Hash a file name for import_hash_table. */
5286 while (*f) val += *f++;
5287 return (val%IMPORT_HASH_SIZE);
5290 /* Search for file FILENAME in import_hash_table.
5291 Return -2 if found, either a matching name or a matching inode.
5292 Otherwise, open the file and return a file descriptor if successful
5293 or -1 if unsuccessful. */
5299 struct file_name_list *searchptr)
5301 struct import_file *i;
5307 hashval = import_hash (filename);
5309 /* Attempt to find file in list of already included files */
5310 i = pfile->import_hash_table[hashval];
5313 if (!strcmp (filename, i->name))
5314 return -2; /* return found */
5317 /* Open it and try a match on inode/dev */
5318 fd = open_include_file (pfile, filename, searchptr);
5322 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5323 i = pfile->import_hash_table[h];
5325 /* Compare the inode and the device.
5326 Supposedly on some systems the inode is not a scalar. */
5327 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5328 && i->dev == sb.st_dev) {
5330 return -2; /* return found */
5335 return fd; /* Not found, return open file */
5338 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5346 struct import_file *i;
5350 hashval = import_hash (fname);
5352 i = (struct import_file *)Safe_malloc (sizeof (struct import_file));
5353 i->name = (char *)Safe_malloc (strlen (fname)+1);
5354 strcpy (i->name, fname);
5355 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5357 i->next = pfile->import_hash_table[hashval];
5358 pfile->import_hash_table[hashval] = i;
5361 /* The file_name_map structure holds a mapping of file names for a
5362 particular directory. This mapping is read from the file named
5363 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5364 map filenames on a file system with severe filename restrictions,
5365 such as DOS. The format of the file name map file is just a series
5366 of lines with two tokens on each line. The first token is the name
5367 to map, and the second token is the actual name to use. */
5369 struct file_name_map
5371 struct file_name_map *map_next;
5376 #define FILE_NAME_MAP_FILE "header.gcc"
5378 /* Read a space delimited string of unlimited length from a stdio
5382 read_filename_string (
5390 set = alloc = Safe_malloc (len + 1);
5394 while ((ch = getc (f)) != EOF && ! is_space[ch])
5396 if (set - alloc == len)
5399 alloc = Safe_realloc(alloc, len + 1);
5400 set = alloc + len / 2;
5410 /* This structure holds a linked list of file name maps, one per directory. */
5411 struct file_name_map_list
5413 struct file_name_map_list *map_list_next;
5414 char *map_list_name;
5415 struct file_name_map *map_list_map;
5418 /* Read the file name map file for DIRNAME. */
5420 static struct file_name_map *
5425 register struct file_name_map_list *map_list_ptr;
5429 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5430 map_list_ptr = map_list_ptr->map_list_next)
5431 if (! strcmp (map_list_ptr->map_list_name, dirname))
5432 return map_list_ptr->map_list_map;
5434 map_list_ptr = ((struct file_name_map_list *)
5435 Safe_malloc (sizeof (struct file_name_map_list)));
5436 map_list_ptr->map_list_name = savestring (dirname);
5437 map_list_ptr->map_list_map = NULL;
5439 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5440 strcpy (name, dirname);
5443 strcat (name, FILE_NAME_MAP_FILE);
5444 f = fopen (name, "r");
5446 map_list_ptr->map_list_map = NULL;
5450 int dirlen = strlen (dirname);
5452 while ((ch = getc (f)) != EOF)
5455 struct file_name_map *ptr;
5459 from = read_filename_string (ch, f);
5460 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5462 to = read_filename_string (ch, f);
5464 ptr = ((struct file_name_map *)
5465 Safe_malloc (sizeof (struct file_name_map)));
5466 ptr->map_from = from;
5468 /* Make the real filename absolute. */
5473 ptr->map_to = Safe_malloc (dirlen + strlen (to) + 2);
5474 strcpy (ptr->map_to, dirname);
5475 ptr->map_to[dirlen] = '/';
5476 strcpy (ptr->map_to + dirlen + 1, to);
5480 ptr->map_next = map_list_ptr->map_list_map;
5481 map_list_ptr->map_list_map = ptr;
5483 while ((ch = getc (f)) != '\n')
5490 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5491 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5493 return map_list_ptr->map_list_map;
5496 /* Try to open include file FILENAME. SEARCHPTR is the directory
5497 being tried from the include file search path. This function maps
5498 filenames on file systems based on information read by
5505 struct file_name_list *searchptr)
5507 register struct file_name_map *map;
5508 register char *from;
5511 if (searchptr && ! searchptr->got_name_map)
5513 searchptr->name_map = read_name_map (pfile,
5515 ? searchptr->fname : ".");
5516 searchptr->got_name_map = 1;
5519 /* First check the mapping for the directory we are using. */
5520 if (searchptr && searchptr->name_map)
5523 if (searchptr->fname)
5524 from += strlen (searchptr->fname) + 1;
5525 for (map = searchptr->name_map; map; map = map->map_next)
5527 if (! strcmp (map->map_from, from))
5529 /* Found a match. */
5530 return open (map->map_to, O_RDONLY, 0666);
5535 /* Try to find a mapping file for the particular directory we are
5536 looking in. Thus #include <sys/types.h> will look up sys/types.h
5537 in /usr/include/header.gcc and look up types.h in
5538 /usr/include/sys/header.gcc. */
5539 p = rindex (filename, '/');
5544 && (int) strlen (searchptr->fname) == p - filename
5545 && ! strncmp (searchptr->fname, filename, p - filename))
5547 /* FILENAME is in SEARCHPTR, which we've already checked. */
5548 return open (filename, O_RDONLY, 0666);
5558 dir = (char *) alloca (p - filename + 1);
5559 bcopy (filename, dir, p - filename);
5560 dir[p - filename] = '\0';
5563 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5564 if (! strcmp (map->map_from, from))
5565 return open (map->map_to, O_RDONLY, 0666);
5567 return open (filename, O_RDONLY, 0666);
5570 /* Process the contents of include file FNAME, already open on descriptor F,
5572 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5573 "system" include directories (as decided by the `is_system_include'
5575 DIRPTR is the link in the dir path through which this file was found,
5576 or 0 if the file name was absolute or via the current directory.
5577 Return 1 on success, 0 on failure.
5579 The caller is responsible for the cpp_push_buffer. */
5586 int system_header_p,
5587 struct file_name_list *dirptr)
5593 cpp_buffer *fp; /* For input stack frame */
5594 /* int missing_newline = 0; # KILL */
5596 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5598 cpp_perror_with_name (pfile, fname);
5600 cpp_pop_buffer (pfile);
5604 fp = CPP_BUFFER (pfile);
5605 fp->nominal_fname = fp->fname = fname;
5610 fp->system_header_p = system_header_p;
5613 fp->cleanup = file_cleanup;
5615 if (S_ISREG (st_mode)) {
5616 fp->buf = (U_CHAR *) Safe_malloc (st_size + 2);
5617 fp->alimit = fp->buf + st_size + 2;
5620 /* Read the file contents, knowing that st_size is an upper bound
5621 on the number of bytes we can read. */
5622 length = safe_read (f, fp->buf, st_size);
5623 fp->rlimit = fp->buf + length;
5624 if (length < 0) goto nope;
5626 else if (S_ISDIR (st_mode)) {
5627 cpp_error (pfile, "directory `%s' specified in #include", fname);
5631 /* Cannot count its file size before reading.
5632 First read the entire file into heap and
5633 copy them into buffer on stack. */
5638 fp->buf = (U_CHAR *) Safe_malloc (bsize + 2);
5641 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5643 goto nope; /* error! */
5645 if (st_size != bsize)
5646 break; /* End of file */
5648 fp->buf = (U_CHAR *) Safe_realloc(fp->buf, bsize + 2);
5654 if ((length > 0 && fp->buf[length - 1] != '\n')
5655 /* Backslash-newline at end is not good enough. */
5656 || (length > 1 && fp->buf[length - 2] == '\\')) {
5657 fp->buf[length++] = '\n';
5659 missing_newline = 1;
5662 fp->buf[length] = '\0';
5663 fp->rlimit = fp->buf + length;
5665 /* Close descriptor now, so nesting does not use lots of descriptors. */
5668 /* Must do this before calling trigraph_pcp, so that the correct file name
5669 will be printed in warning messages. */
5671 pfile->input_stack_listing_current = 0;
5681 if (missing_newline)
5684 if (CPP_PEDANTIC (pfile) && missing_newline)
5685 pedwarn ("file does not end in newline");
5688 input_file_stack_tick++;
5695 cpp_perror_with_name (pfile, fname);
5706 struct cpp_options *opts = CPP_OPTIONS (pfile);
5707 struct cpp_pending *pend;
5712 /* The code looks at the defaults through this pointer, rather than through
5713 the constant structure above. This pointer gets changed if an environment
5714 variable specifies other defaults. */
5715 struct default_include *include_defaults = include_defaults_array;
5717 /* Add dirs from CPATH after dirs from -I. */
5718 /* There seems to be confusion about what CPATH should do,
5719 so for the moment it is not documented. */
5720 /* Some people say that CPATH should replace the standard include dirs,
5721 but that seems pointless: it comes before them, so it overrides them
5723 p = (char *) getenv ("CPATH");
5724 if (p != 0 && ! opts->no_standard_includes)
5725 path_include (pfile, p);
5727 /* Now that dollars_in_ident is known, initialize is_idchar. */
5728 initialize_char_syntax (opts);
5730 /* Do partial setup of input buffer for the sake of generating
5731 early #line directives (when -g is in effect). */
5732 fp = cpp_push_buffer (pfile, NULL, 0);
5733 if (opts->in_fname == NULL)
5734 opts->in_fname = "";
5735 fp->nominal_fname = fp->fname = opts->in_fname;
5738 /* Install __LINE__, etc. Must follow initialize_char_syntax
5739 and option processing. */
5740 initialize_builtins (pfile);
5742 /* Do standard #defines and assertions
5743 that identify system and machine type. */
5745 if (!opts->inhibit_predefs) {
5746 char *p = (char *) alloca (strlen (predefs) + 1);
5747 strcpy (p, predefs);
5750 while (*p == ' ' || *p == '\t')
5752 /* Handle -D options. */
5753 if (p[0] == '-' && p[1] == 'D') {
5755 while (*p && *p != ' ' && *p != '\t')
5759 if (opts->debug_output)
5760 output_line_command (pfile, 0, same_file);
5761 cpp_define (pfile, q);
5762 while (*p == ' ' || *p == '\t')
5764 } else if (p[0] == '-' && p[1] == 'A') {
5765 /* Handle -A options (assertions). */
5774 past_name = assertion;
5775 /* Locate end of name. */
5776 while (*past_name && *past_name != ' '
5777 && *past_name != '\t' && *past_name != '(')
5779 /* Locate `(' at start of value. */
5781 while (*value && (*value == ' ' || *value == '\t'))
5783 if (*value++ != '(')
5785 while (*value && (*value == ' ' || *value == '\t'))
5788 /* Locate end of value. */
5789 while (*past_value && *past_value != ' '
5790 && *past_value != '\t' && *past_value != ')')
5792 termination = past_value;
5793 while (*termination && (*termination == ' ' || *termination == '\t'))
5795 if (*termination++ != ')')
5797 if (*termination && *termination != ' ' && *termination != '\t')
5799 /* Temporarily null-terminate the value. */
5800 save_char = *termination;
5801 *termination = '\0';
5802 /* Install the assertion. */
5803 make_assertion (pfile, "-A", assertion);
5804 *termination = (char) save_char;
5806 while (*p == ' ' || *p == '\t')
5814 /* Now handle the command line options. */
5816 /* Do -U's, -D's and -A's in the order they were seen. */
5817 /* First reverse the list. */
5818 opts->pending = nreverse_pending (opts->pending);
5820 for (pend = opts->pending; pend; pend = pend->next)
5822 if (pend->cmd != NULL && pend->cmd[0] == '-')
5824 switch (pend->cmd[1])
5827 if (opts->debug_output)
5828 output_line_command (pfile, 0, same_file);
5829 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5832 if (opts->debug_output)
5833 output_line_command (pfile, 0, same_file);
5834 cpp_define (pfile, pend->arg);
5837 make_assertion (pfile, "-A", pend->arg);
5843 opts->done_initializing = 1;
5845 { /* read the appropriate environment variable and if it exists
5846 replace include_defaults with the listed path. */
5848 switch ((opts->objc << 1) + opts->cplusplus)
5851 epath = getenv ("C_INCLUDE_PATH");
5854 epath = getenv ("CPLUS_INCLUDE_PATH");
5857 epath = getenv ("OBJC_INCLUDE_PATH");
5860 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5863 /* If the environment var for this language is set,
5864 add to the default list of include directories. */
5866 char *nstore = (char *) alloca (strlen (epath) + 2);
5868 char *startp, *endp;
5870 for (num_dirs = 1, startp = epath; *startp; startp++)
5871 if (*startp == PATH_SEPARATOR)
5874 = (struct default_include *) Safe_malloc ((num_dirs
5875 * sizeof (struct default_include))
5876 + sizeof (include_defaults_array));
5877 startp = endp = epath;
5880 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5881 if ((*endp == PATH_SEPARATOR)
5883 strncpy (nstore, startp, endp-startp);
5885 strcpy (nstore, ".");
5887 nstore[endp-startp] = '\0';
5889 include_defaults[num_dirs].fname = savestring (nstore);
5890 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5891 include_defaults[num_dirs].cxx_aware = 1;
5895 endp = startp = endp + 1;
5899 /* Put the usual defaults back in at the end. */
5900 bcopy ((char *) include_defaults_array,
5901 (char *) &include_defaults[num_dirs],
5902 sizeof (include_defaults_array));
5906 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5907 opts->first_system_include = opts->before_system;
5909 /* Unless -fnostdinc,
5910 tack on the standard include file dirs to the specified list */
5911 if (!opts->no_standard_includes) {
5912 struct default_include *p = include_defaults;
5913 char *specd_prefix = opts->include_prefix;
5914 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5915 int default_len = 0;
5916 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5917 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5918 default_len = strlen (default_prefix) - 7;
5919 default_prefix[default_len] = 0;
5921 /* Search "translated" versions of GNU directories.
5922 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5923 if (specd_prefix != 0 && default_len != 0)
5924 for (p = include_defaults; p->fname; p++) {
5925 /* Some standard dirs are only for C++. */
5927 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5928 /* Does this dir start with the prefix? */
5929 if (!strncmp (p->fname, default_prefix, default_len)) {
5930 /* Yes; change prefix and add to search list. */
5931 struct file_name_list *new
5932 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5933 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5934 char *str = (char *) Safe_malloc (this_len + 1);
5935 strcpy (str, specd_prefix);
5936 strcat (str, p->fname + default_len);
5938 new->control_macro = 0;
5939 new->c_system_include_path = !p->cxx_aware;
5940 new->got_name_map = 0;
5941 append_include_chain (pfile, new, new);
5942 if (opts->first_system_include == 0)
5943 opts->first_system_include = new;
5947 /* Search ordinary names for GNU include directories. */
5948 for (p = include_defaults; p->fname; p++) {
5949 /* Some standard dirs are only for C++. */
5951 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5952 struct file_name_list *new
5953 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5954 new->control_macro = 0;
5955 new->c_system_include_path = !p->cxx_aware;
5956 new->fname = p->fname;
5957 new->got_name_map = 0;
5958 append_include_chain (pfile, new, new);
5959 if (opts->first_system_include == 0)
5960 opts->first_system_include = new;
5965 /* Tack the after_include chain at the end of the include chain. */
5966 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5967 if (opts->first_system_include == 0)
5968 opts->first_system_include = opts->after_include;
5970 /* With -v, print the list of dirs to search. */
5971 if (opts->verbose) {
5972 struct file_name_list *p;
5973 fprintf (stderr, "#include \"...\" search starts here:\n");
5974 for (p = opts->include; p; p = p->next) {
5975 if (p == opts->first_bracket_include)
5976 fprintf (stderr, "#include <...> search starts here:\n");
5977 fprintf (stderr, " %s\n", p->fname);
5979 fprintf (stderr, "End of search list.\n");
5982 /* Scan the -imacros files before the main input.
5983 Much like #including them, but with no_output set
5984 so that only their macro definitions matter. */
5986 opts->no_output++; pfile->no_record_file++;
5987 for (pend = opts->pending; pend; pend = pend->next)
5989 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5991 int fd = open (pend->arg, O_RDONLY, 0666);
5994 cpp_perror_with_name (pfile, pend->arg);
5995 return FATAL_EXIT_CODE;
5997 cpp_push_buffer (pfile, NULL, 0);
5998 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
5999 cpp_scan_buffer (pfile);
6002 opts->no_output--; pfile->no_record_file--;
6004 /* Copy the entire contents of the main input file into
6005 the stacked input buffer previously allocated for it. */
6006 if (fname == NULL || *fname == 0) {
6009 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6010 cpp_pfatal_with_name (pfile, fname);
6012 /* -MG doesn't select the form of output and must be specified with one of
6013 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6014 inhibit compilation. */
6015 if (opts->print_deps_missing_files
6016 && (opts->print_deps == 0 || !opts->no_output))
6017 fatal (pfile, "-MG must be specified with one of -M or -MM");
6019 /* Either of two environment variables can specify output of deps.
6020 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6021 where OUTPUT_FILE is the file to write deps info to
6022 and DEPS_TARGET is the target to mention in the deps. */
6024 if (opts->print_deps == 0
6025 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6026 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6027 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6033 spec = getenv ("SUNPRO_DEPENDENCIES");
6034 opts->print_deps = 2;
6037 opts->print_deps = 1;
6040 /* Find the space before the DEPS_TARGET, if there is one. */
6041 /* This should use index. (mrs) */
6042 while (*s != 0 && *s != ' ') s++;
6045 opts->deps_target = s + 1;
6046 output_file = (char *) Safe_malloc (s - spec + 1);
6047 bcopy (spec, output_file, s - spec);
6048 output_file[s - spec] = 0;
6052 opts->deps_target = 0;
6056 opts->deps_file = output_file;
6057 opts->print_deps_append = 1;
6060 /* For -M, print the expected object file name
6061 as the target of this Make-rule. */
6062 if (opts->print_deps)
6064 pfile->deps_allocated_size = 200;
6065 pfile->deps_buffer = (char *) Safe_malloc (pfile->deps_allocated_size);
6066 pfile->deps_buffer[0] = 0;
6067 pfile->deps_size = 0;
6068 pfile->deps_column = 0;
6070 if (opts->deps_target)
6071 deps_output (pfile, opts->deps_target, ':');
6072 else if (*opts->in_fname == 0)
6073 deps_output (pfile, "-", ':');
6079 /* Discard all directory prefixes from filename. */
6080 if ((q = rindex (opts->in_fname, '/')) != NULL
6081 #ifdef DIR_SEPARATOR
6082 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6089 /* Copy remainder to mungable area. */
6090 p = (char *) alloca (strlen(q) + 8);
6093 /* Output P, but remove known suffixes. */
6097 && p[len - 2] == '.'
6098 && index("cCsSm", p[len - 1]))
6101 && p[len - 3] == '.'
6102 && p[len - 2] == 'c'
6103 && p[len - 1] == 'c')
6106 && p[len - 4] == '.'
6107 && p[len - 3] == 'c'
6108 && p[len - 2] == 'x'
6109 && p[len - 1] == 'x')
6112 && p[len - 4] == '.'
6113 && p[len - 3] == 'c'
6114 && p[len - 2] == 'p'
6115 && p[len - 1] == 'p')
6118 /* Supply our own suffix. */
6129 deps_output (pfile, p, ':');
6130 deps_output (pfile, opts->in_fname, ' ');
6135 /* Make sure data ends with a newline. And put a null after it. */
6137 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6138 /* Backslash-newline at end is not good enough. */
6139 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6140 fp->buf[fp->length++] = '\n';
6141 missing_newline = 1;
6143 fp->buf[fp->length] = '\0';
6145 /* Unless inhibited, convert trigraphs in the input. */
6151 /* Scan the -include files before the main input.
6152 We push these in reverse order, so that the first one is handled first. */
6154 pfile->no_record_file++;
6155 opts->pending = nreverse_pending (opts->pending);
6156 for (pend = opts->pending; pend; pend = pend->next)
6158 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6160 int fd = open (pend->arg, O_RDONLY, 0666);
6163 cpp_perror_with_name (pfile, pend->arg);
6164 return FATAL_EXIT_CODE;
6166 cpp_push_buffer (pfile, NULL, 0);
6167 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6170 pfile->no_record_file--;
6172 /* Free the pending list. */
6173 for (pend = opts->pending; pend; )
6175 struct cpp_pending *next = pend->next;
6179 opts->pending = NULL;
6182 /* Scan the input, processing macros and directives. */
6184 rescan (&outbuf, 0);
6186 if (missing_newline)
6189 if (CPP_PEDANTIC (pfile) && missing_newline)
6190 pedwarn ("file does not end in newline");
6193 if (finclude (pfile, f, fname, 0, NULL_PTR))
6194 output_line_command (pfile, 0, same_file);
6195 return SUCCESS_EXIT_CODE;
6202 bzero ((char *) pfile, sizeof (cpp_reader));
6203 pfile->get_token = cpp_get_token;
6205 pfile->token_buffer_size = 200;
6206 pfile->token_buffer = (U_CHAR*)Safe_malloc (pfile->token_buffer_size);
6207 CPP_SET_WRITTEN (pfile, 0);
6209 pfile->system_include_depth = 0;
6210 pfile->dont_repeat_files = 0;
6211 pfile->all_include_files = 0;
6212 pfile->max_include_len = 0;
6213 pfile->timebuf = NULL;
6214 pfile->only_seen_white = 1;
6215 pfile->buffer = CPP_NULL_BUFFER(pfile);
6218 static struct cpp_pending *
6220 struct cpp_pending *list)
6223 register struct cpp_pending *prev = 0, *next, *pend;
6224 for (pend = list; pend; pend = next)
6239 struct cpp_pending *pend
6240 = (struct cpp_pending*)Safe_malloc (sizeof (struct cpp_pending));
6243 pend->next = CPP_OPTIONS (pfile)->pending;
6244 CPP_OPTIONS (pfile)->pending = pend;
6247 /* Handle command-line options in (argc, argv).
6248 Can be called multiple times, to handle multiple sets of options.
6249 Returns if an unrecognized option is seen.
6250 Returns number of handled arguments. */
6253 cpp_handle_options (
6259 struct cpp_options *opts = CPP_OPTIONS (pfile);
6260 for (i = 0; i < argc; i++) {
6261 if (argv[i][0] != '-') {
6262 if (opts->out_fname != NULL)
6263 fatal ("Usage: %s [switches] input output", argv[0]);
6264 else if (opts->in_fname != NULL)
6265 opts->out_fname = argv[i];
6267 opts->in_fname = argv[i];
6269 switch (argv[i][1]) {
6272 if (!strcmp (argv[i], "-include")
6273 || !strcmp (argv[i], "-imacros")) {
6275 fatal ("Filename missing after `%s' option", argv[i]);
6277 push_pending (pfile, argv[i], argv[i+1]), i++;
6279 if (!strcmp (argv[i], "-iprefix")) {
6281 fatal ("Filename missing after `-iprefix' option");
6283 opts->include_prefix = argv[++i];
6285 if (!strcmp (argv[i], "-ifoutput")) {
6286 opts->output_conditionals = 1;
6288 if (!strcmp (argv[i], "-isystem")) {
6289 struct file_name_list *dirtmp;
6292 fatal ("Filename missing after `-isystem' option");
6294 dirtmp = (struct file_name_list *)
6295 Safe_malloc (sizeof (struct file_name_list));
6297 dirtmp->control_macro = 0;
6298 dirtmp->c_system_include_path = 1;
6299 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1]) + 1);
6300 strcpy (dirtmp->fname, argv[++i]);
6301 dirtmp->got_name_map = 0;
6303 if (opts->before_system == 0)
6304 opts->before_system = dirtmp;
6306 opts->last_before_system->next = dirtmp;
6307 opts->last_before_system = dirtmp; /* Tail follows the last one */
6309 /* Add directory to end of path for includes,
6310 with the default prefix at the front of its name. */
6311 if (!strcmp (argv[i], "-iwithprefix")) {
6312 struct file_name_list *dirtmp;
6315 if (opts->include_prefix != 0)
6316 prefix = opts->include_prefix;
6318 prefix = savestring (GCC_INCLUDE_DIR);
6319 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6320 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6321 prefix[strlen (prefix) - 7] = 0;
6324 dirtmp = (struct file_name_list *)
6325 Safe_malloc (sizeof (struct file_name_list));
6326 dirtmp->next = 0; /* New one goes on the end */
6327 dirtmp->control_macro = 0;
6328 dirtmp->c_system_include_path = 0;
6330 fatal ("Directory name missing after `-iwithprefix' option");
6332 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6333 + strlen (prefix) + 1);
6334 strcpy (dirtmp->fname, prefix);
6335 strcat (dirtmp->fname, argv[++i]);
6336 dirtmp->got_name_map = 0;
6338 if (opts->after_include == 0)
6339 opts->after_include = dirtmp;
6341 opts->last_after_include->next = dirtmp;
6342 opts->last_after_include = dirtmp; /* Tail follows the last one */
6344 /* Add directory to main path for includes,
6345 with the default prefix at the front of its name. */
6346 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6347 struct file_name_list *dirtmp;
6350 if (opts->include_prefix != 0)
6351 prefix = opts->include_prefix;
6353 prefix = savestring (GCC_INCLUDE_DIR);
6354 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6355 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6356 prefix[strlen (prefix) - 7] = 0;
6359 dirtmp = (struct file_name_list *)
6360 Safe_malloc (sizeof (struct file_name_list));
6361 dirtmp->next = 0; /* New one goes on the end */
6362 dirtmp->control_macro = 0;
6363 dirtmp->c_system_include_path = 0;
6365 fatal ("Directory name missing after `-iwithprefixbefore' option");
6367 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6368 + strlen (prefix) + 1);
6369 strcpy (dirtmp->fname, prefix);
6370 strcat (dirtmp->fname, argv[++i]);
6371 dirtmp->got_name_map = 0;
6373 append_include_chain (pfile, dirtmp, dirtmp);
6375 /* Add directory to end of path for includes. */
6376 if (!strcmp (argv[i], "-idirafter")) {
6377 struct file_name_list *dirtmp;
6379 dirtmp = (struct file_name_list *)
6380 Safe_malloc (sizeof (struct file_name_list));
6381 dirtmp->next = 0; /* New one goes on the end */
6382 dirtmp->control_macro = 0;
6383 dirtmp->c_system_include_path = 0;
6385 fatal ("Directory name missing after `-idirafter' option");
6387 dirtmp->fname = argv[++i];
6388 dirtmp->got_name_map = 0;
6390 if (opts->after_include == 0)
6391 opts->after_include = dirtmp;
6393 opts->last_after_include->next = dirtmp;
6394 opts->last_after_include = dirtmp; /* Tail follows the last one */
6399 if (opts->out_fname != NULL)
6400 fatal ("Output filename specified twice");
6402 fatal ("Filename missing after -o option");
6403 opts->out_fname = argv[++i];
6404 if (!strcmp (opts->out_fname, "-"))
6405 opts->out_fname = "";
6409 if (!strcmp (argv[i], "-pedantic"))
6410 CPP_PEDANTIC (pfile) = 1;
6411 else if (!strcmp (argv[i], "-pedantic-errors")) {
6412 CPP_PEDANTIC (pfile) = 1;
6413 opts->pedantic_errors = 1;
6416 else if (!strcmp (argv[i], "-pcp")) {
6417 char *pcp_fname = argv[++i];
6419 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6420 ? fopen (pcp_fname, "w")
6421 : fdopen (dup (fileno (stdout)), "w"));
6422 if (pcp_outfile == 0)
6423 cpp_pfatal_with_name (pfile, pcp_fname);
6430 if (!strcmp (argv[i], "-traditional")) {
6431 opts->traditional = 1;
6432 if (opts->dollars_in_ident > 0)
6433 opts->dollars_in_ident = 1;
6434 } else if (!strcmp (argv[i], "-trigraphs")) {
6436 opts->no_trigraphs = 0;
6441 if (! strcmp (argv[i], "-lang-c"))
6442 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6443 if (! strcmp (argv[i], "-lang-c++"))
6444 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6445 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6446 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6447 if (! strcmp (argv[i], "-lang-objc"))
6448 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6449 if (! strcmp (argv[i], "-lang-objc++"))
6450 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6451 if (! strcmp (argv[i], "-lang-asm"))
6453 if (! strcmp (argv[i], "-lint"))
6455 if (! strcmp (argv[i], "-lang-chill"))
6456 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6457 opts->traditional = 1, opts->no_trigraphs = 1;
6461 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6465 opts->inhibit_warnings = 1;
6469 if (!strcmp (argv[i], "-Wtrigraphs"))
6470 opts->warn_trigraphs = 1;
6471 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6472 opts->warn_trigraphs = 0;
6473 else if (!strcmp (argv[i], "-Wcomment"))
6474 opts->warn_comments = 1;
6475 else if (!strcmp (argv[i], "-Wno-comment"))
6476 opts->warn_comments = 0;
6477 else if (!strcmp (argv[i], "-Wcomments"))
6478 opts->warn_comments = 1;
6479 else if (!strcmp (argv[i], "-Wno-comments"))
6480 opts->warn_comments = 0;
6481 else if (!strcmp (argv[i], "-Wtraditional"))
6482 opts->warn_stringify = 1;
6483 else if (!strcmp (argv[i], "-Wno-traditional"))
6484 opts->warn_stringify = 0;
6485 else if (!strcmp (argv[i], "-Wimport"))
6486 opts->warn_import = 1;
6487 else if (!strcmp (argv[i], "-Wno-import"))
6488 opts->warn_import = 0;
6489 else if (!strcmp (argv[i], "-Werror"))
6490 opts->warnings_are_errors = 1;
6491 else if (!strcmp (argv[i], "-Wno-error"))
6492 opts->warnings_are_errors = 0;
6493 else if (!strcmp (argv[i], "-Wall"))
6495 opts->warn_trigraphs = 1;
6496 opts->warn_comments = 1;
6501 /* The style of the choices here is a bit mixed.
6502 The chosen scheme is a hybrid of keeping all options in one string
6503 and specifying each option in a separate argument:
6504 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6505 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6506 -M[M][G][D file]. This is awkward to handle in specs, and is not
6508 /* ??? -MG must be specified in addition to one of -M or -MM.
6509 This can be relaxed in the future without breaking anything.
6510 The converse isn't true. */
6512 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6513 if (!strcmp (argv[i], "-MG"))
6515 opts->print_deps_missing_files = 1;
6518 if (!strcmp (argv[i], "-M"))
6519 opts->print_deps = 2;
6520 else if (!strcmp (argv[i], "-MM"))
6521 opts->print_deps = 1;
6522 else if (!strcmp (argv[i], "-MD"))
6523 opts->print_deps = 2;
6524 else if (!strcmp (argv[i], "-MMD"))
6525 opts->print_deps = 1;
6526 /* For -MD and -MMD options, write deps on file named by next arg. */
6527 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6530 fatal ("Filename missing after %s option", argv[i]);
6531 opts->deps_file = argv[++i];
6535 /* For -M and -MM, write deps on standard output
6536 and suppress the usual output. */
6537 opts->no_output = 1;
6543 char *p = argv[i] + 2;
6545 while ((c = *p++) != 0) {
6546 /* Arg to -d specifies what parts of macros to dump */
6549 opts->dump_macros = dump_only;
6550 opts->no_output = 1;
6553 opts->dump_macros = dump_names;
6556 opts->dump_macros = dump_definitions;
6564 if (argv[i][2] == '3')
6565 opts->debug_output = 1;
6569 fprintf (stderr, "GNU CPP version %s", version_string);
6570 #ifdef TARGET_VERSION
6573 fprintf (stderr, "\n");
6574 // opts->verbose = 1;
6578 opts->print_include_names = 1;
6582 if (argv[i][2] != 0)
6583 push_pending (pfile, "-D", argv[i] + 2);
6584 else if (i + 1 == argc)
6585 fatal ("Macro name missing after -D option");
6587 i++, push_pending (pfile, "-D", argv[i]);
6594 if (argv[i][2] != 0)
6602 fatal ("Assertion missing after -A option");
6607 if (!strcmp (p, "-")) {
6608 struct cpp_pending **ptr;
6609 /* -A- eliminates all predefined macros and assertions.
6610 Let's include also any that were specified earlier
6611 on the command line. That way we can get rid of any
6612 that were passed automatically in from GCC. */
6614 opts->inhibit_predefs = 1;
6615 for (ptr = &opts->pending; *ptr != NULL; )
6617 struct cpp_pending *pend = *ptr;
6618 if (pend->cmd && pend->cmd[0] == '-'
6619 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6628 push_pending (pfile, "-A", p);
6633 case 'U': /* JF #undef something */
6634 if (argv[i][2] != 0)
6635 push_pending (pfile, "-U", argv[i] + 2);
6636 else if (i + 1 == argc)
6637 fatal ("Macro name missing after -U option");
6639 push_pending (pfile, "-U", argv[i+1]), i++;
6643 opts->put_out_comments = 1;
6646 case 'E': /* -E comes from cc -E; ignore it. */
6650 opts->no_line_commands = 1;
6653 case '$': /* Don't include $ in identifiers. */
6654 opts->dollars_in_ident = 0;
6657 case 'I': /* Add directory to path for includes. */
6659 struct file_name_list *dirtmp;
6661 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6662 && !strcmp (argv[i] + 2, "-")) {
6663 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6664 /* Don't use any preceding -I directories for #include <...>. */
6665 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6668 dirtmp = (struct file_name_list *)
6669 Safe_malloc (sizeof (struct file_name_list));
6670 dirtmp->next = 0; /* New one goes on the end */
6671 dirtmp->control_macro = 0;
6672 dirtmp->c_system_include_path = 0;
6673 if (argv[i][2] != 0)
6674 dirtmp->fname = argv[i] + 2;
6675 else if (i + 1 == argc)
6676 fatal ("Directory name missing after -I option");
6678 dirtmp->fname = argv[++i];
6679 dirtmp->got_name_map = 0;
6680 append_include_chain (pfile, dirtmp, dirtmp);
6686 if (!strcmp (argv[i], "-nostdinc"))
6687 /* -nostdinc causes no default include directories.
6688 You must specify all include-file directories with -I. */
6689 opts->no_standard_includes = 1;
6690 else if (!strcmp (argv[i], "-nostdinc++"))
6691 /* -nostdinc++ causes no default C++-specific include directories. */
6692 opts->no_standard_cplusplus_includes = 1;
6694 else if (!strcmp (argv[i], "-noprecomp"))
6700 /* Sun compiler passes undocumented switch "-undef".
6701 Let's assume it means to inhibit the predefined symbols. */
6702 opts->inhibit_predefs = 1;
6705 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6706 if (opts->in_fname == NULL) {
6707 opts->in_fname = "";
6709 } else if (opts->out_fname == NULL) {
6710 opts->out_fname = "";
6712 } /* else fall through into error */
6726 struct cpp_options *opts = CPP_OPTIONS (pfile);
6728 if (opts->print_deps)
6730 /* Stream on which to print the dependency information. */
6733 /* Don't actually write the deps file if compilation has failed. */
6734 if (pfile->errors == 0)
6736 char *deps_mode = opts->print_deps_append ? "a" : "w";
6737 if (opts->deps_file == 0)
6738 deps_stream = stdout;
6739 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6740 cpp_pfatal_with_name (pfile, opts->deps_file);
6741 fputs (pfile->deps_buffer, deps_stream);
6742 putc ('\n', deps_stream);
6743 if (opts->deps_file)
6745 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6746 fatal ("I/O error on output");
6752 /* Free resources used by PFILE. */
6759 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6760 cpp_pop_buffer (pfile);
6762 if (pfile->token_buffer)
6764 free (pfile->token_buffer);
6765 pfile->token_buffer = NULL;
6768 if (pfile->deps_buffer)
6770 free (pfile->deps_buffer);
6771 pfile->deps_buffer = NULL;
6772 pfile->deps_allocated_size = 0;
6775 while (pfile->if_stack)
6777 IF_STACK_FRAME *temp = pfile->if_stack;
6778 pfile->if_stack = temp->next;
6782 while (pfile->dont_repeat_files)
6784 struct file_name_list *temp = pfile->dont_repeat_files;
6785 pfile->dont_repeat_files = temp->next;
6790 while (pfile->all_include_files)
6792 struct file_name_list *temp = pfile->all_include_files;
6793 pfile->all_include_files = temp->next;
6798 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6800 register struct import_file *imp = pfile->import_hash_table[i];
6803 struct import_file *next = imp->next;
6808 pfile->import_hash_table[i] = 0;
6811 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6813 while (pfile->assertion_hashtab[i])
6814 delete_assertion (pfile->assertion_hashtab[i]);
6817 cpp_hash_cleanup (pfile);
6823 struct directive *keyword,
6824 U_CHAR *buf, U_CHAR *limit)
6826 long symstart; /* remember where symbol name starts */
6828 int sym_length; /* and how long it is */
6829 struct arglist *tokens = NULL;
6831 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6832 && !CPP_BUFFER (pfile)->system_header_p)
6833 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6835 cpp_skip_hspace (pfile);
6836 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6837 parse_name (pfile, GETC());
6838 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6841 cpp_skip_hspace (pfile);
6842 if (PEEKC() != '(') {
6843 cpp_error (pfile, "missing token-sequence in `#assert'");
6849 tokens = read_token_list (pfile, &error_flag);
6853 cpp_error (pfile, "empty token-sequence in `#assert'");
6856 cpp_skip_hspace (pfile);
6858 if (c != EOF && c != '\n')
6859 cpp_pedwarn (pfile, "junk at end of `#assert'");
6860 skip_rest_of_line (pfile);
6863 /* If this name isn't already an assertion name, make it one.
6864 Error if it was already in use in some other way. */
6867 ASSERTION_HASHNODE *hp;
6868 U_CHAR *symname = pfile->token_buffer + symstart;
6869 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6870 struct tokenlist_list *value
6871 = (struct tokenlist_list *) Safe_malloc (sizeof (struct tokenlist_list));
6873 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6875 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6876 cpp_error (pfile, "`defined' redefined as assertion");
6877 hp = assertion_install (pfile, symname, sym_length, hashcode);
6880 /* Add the spec'd token-sequence to the list of such. */
6881 value->tokens = tokens;
6882 value->next = hp->value;
6885 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6888 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6889 skip_rest_of_line (pfile);
6896 struct directive *keyword,
6897 U_CHAR *buf, U_CHAR *limit)
6899 long symstart; /* remember where symbol name starts */
6900 int sym_length; /* and how long it is */
6903 struct arglist *tokens = NULL;
6904 int tokens_specified = 0;
6906 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6907 && !CPP_BUFFER (pfile)->system_header_p)
6908 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6910 cpp_skip_hspace (pfile);
6912 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6913 parse_name (pfile, GETC());
6914 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6917 cpp_skip_hspace (pfile);
6918 if (PEEKC() == '(') {
6921 tokens = read_token_list (pfile, &error_flag);
6925 cpp_error (pfile, "empty token list in `#unassert'");
6929 tokens_specified = 1;
6932 cpp_skip_hspace (pfile);
6934 if (c != EOF && c != '\n')
6935 cpp_error (pfile, "junk at end of `#unassert'");
6936 skip_rest_of_line (pfile);
6939 ASSERTION_HASHNODE *hp;
6940 U_CHAR *symname = pfile->token_buffer + symstart;
6941 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6942 struct tokenlist_list *tail, *prev;
6944 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6948 /* If no token list was specified, then eliminate this assertion
6950 if (! tokens_specified)
6951 delete_assertion (hp);
6953 /* If a list of tokens was given, then delete any matching list. */
6958 struct tokenlist_list *next = tail->next;
6959 if (compare_token_lists (tail->tokens, tokens)) {
6963 hp->value = tail->next;
6964 free_token_list (tail->tokens);
6974 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6977 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6978 skip_rest_of_line (pfile);
6982 /* Test whether there is an assertion named NAME
6983 and optionally whether it has an asserted token list TOKENS.
6984 NAME is not null terminated; its length is SYM_LENGTH.
6985 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6992 int tokens_specified,
6993 struct arglist *tokens)
6995 ASSERTION_HASHNODE *hp;
6996 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6998 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6999 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7001 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7003 /* It is not an assertion; just return false. */
7006 /* If no token list was specified, then value is 1. */
7007 if (! tokens_specified)
7011 struct tokenlist_list *tail;
7015 /* If a list of tokens was given,
7016 then succeed if the assertion records a matching list. */
7019 if (compare_token_lists (tail->tokens, tokens))
7024 /* Fail if the assertion has no matching list. */
7029 /* Compare two lists of tokens for equality including order of tokens. */
7032 compare_token_lists (
7033 struct arglist *l1, struct arglist *l2 )
7036 if (l1->length != l2->length)
7038 if (strncmp (l1->name, l2->name, l1->length))
7044 /* Succeed if both lists end at the same time. */
7049 reverse_token_list (
7050 struct arglist *tokens)
7052 register struct arglist *prev = 0, *this, *next;
7053 for (this = tokens; this; this = next)
7062 /* Read a space-separated list of tokens ending in a close parenthesis.
7063 Return a list of strings, in the order they were written.
7064 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7066 static struct arglist *
7071 struct arglist *token_ptrs = 0;
7076 FORWARD (1); /* Skip '(' */
7078 /* Loop over the assertion value tokens. */
7081 struct arglist *temp;
7082 long name_written = CPP_WRITTEN (pfile);
7085 cpp_skip_hspace (pfile);
7089 /* Find the end of the token. */
7092 CPP_PUTC (pfile, c);
7100 CPP_PUTC (pfile, c);
7102 else if (c == '"' || c == '\'')
7105 cpp_get_token (pfile);
7111 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7112 && c != '"' && c != '\'')
7114 CPP_PUTC (pfile, c);
7117 if (c != EOF) FORWARD(-1);
7120 length = CPP_WRITTEN (pfile) - name_written;
7121 temp = (struct arglist *)
7122 Safe_malloc (sizeof (struct arglist) + length + 1);
7123 temp->name = (U_CHAR *) (temp + 1);
7124 bcopy ((char *) (pfile->token_buffer + name_written),
7125 (char *) temp->name, length);
7126 temp->name[length] = 0;
7127 temp->next = token_ptrs;
7129 temp->length = length;
7131 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7133 if (c == EOF || c == '\n')
7136 "unterminated token sequence following `#' operator");
7141 /* We accumulated the names in reverse order.
7142 Now reverse them to get the proper order. */
7143 return reverse_token_list (token_ptrs);
7148 struct arglist *tokens)
7151 struct arglist *next = tokens->next;
7152 free (tokens->name);
7158 /* Get the file-mode and data size of the file open on FD
7159 and store them in *MODE_POINTER and *SIZE_POINTER. */
7162 file_size_and_mode (
7165 long int *size_pointer)
7169 if (fstat (fd, &sbuf) < 0) return (-1);
7170 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7171 if (size_pointer) *size_pointer = sbuf.st_size;
7175 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7176 retrying if necessary. Return a negative value if an error occurs,
7177 otherwise return the actual number of bytes read,
7178 which must be LEN unless end-of-file was reached. */
7188 int nchars = read (desc, ptr, left);
7209 unsigned size = strlen (input);
7210 char *output = Safe_malloc (size + 1);
7211 strcpy (output, input);
7215 /* Initialize PMARK to remember the current position of PFILE. */
7218 struct parse_marker *pmark,
7221 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7222 pmark->next = pbuf->marks;
7223 pbuf->marks = pmark;
7225 pmark->position = pbuf->cur - pbuf->buf;
7228 /* Cleanup PMARK - we no longer need it. */
7231 struct parse_marker *pmark)
7233 struct parse_marker **pp = &pmark->buf->marks;
7234 for (; ; pp = &(*pp)->next) {
7235 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7236 if (*pp == pmark) break;
7241 /* Backup the current position of PFILE to that saved in PMARK. */
7245 struct parse_marker *pmark,
7248 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7249 if (pbuf != pmark->buf)
7250 fatal ("internal error %s", "parse_goto_mark");
7251 pbuf->cur = pbuf->buf + pmark->position;
7254 /* Reset PMARK to point to the current position of PFILE. (Same
7255 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7259 struct parse_marker *pmark,
7262 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7263 if (pbuf != pmark->buf)
7264 fatal ("internal error %s", "parse_move_mark");
7265 pmark->position = pbuf->cur - pbuf->buf;
7269 cpp_read_check_assertion (
7272 int name_start = CPP_WRITTEN (pfile);
7273 int name_length, name_written;
7275 FORWARD (1); /* Skip '#' */
7276 cpp_skip_hspace (pfile);
7277 parse_name (pfile, GETC ());
7278 name_written = CPP_WRITTEN (pfile);
7279 name_length = name_written - name_start;
7280 cpp_skip_hspace (pfile);
7281 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7284 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7285 result = check_assertion (pfile,
7286 pfile->token_buffer + name_start, name_length,
7290 result = check_assertion (pfile,
7291 pfile->token_buffer + name_start, name_length,
7292 0, (struct arglist *)NULL_PTR);
7293 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7298 cpp_print_file_and_line (pfile)
7301 cpp_buffer *ip = cpp_file_buffer (pfile);
7306 cpp_buf_line_and_col (ip, &line, &col);
7307 cpp_file_line_for_message (pfile, ip->nominal_fname,
7308 line, pfile->show_column ? col : -1);
7313 cpp_error (pfile, msg, arg1, arg2, arg3)
7316 char *arg1, *arg2, *arg3;
7318 cpp_print_containing_files (pfile);
7319 cpp_print_file_and_line (pfile);
7320 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7323 /* Print error message but don't count it. */
7326 cpp_warning (pfile, msg, arg1, arg2, arg3)
7329 char *arg1, *arg2, *arg3;
7331 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7334 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7337 cpp_print_containing_files (pfile);
7338 cpp_print_file_and_line (pfile);
7339 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7342 /* Print an error message and maybe count it. */
7345 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7348 char *arg1, *arg2, *arg3;
7350 if (CPP_OPTIONS (pfile)->pedantic_errors)
7351 cpp_error (pfile, msg, arg1, arg2, arg3);
7353 cpp_warning (pfile, msg, arg1, arg2, arg3);
7357 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7361 char *arg1, *arg2, *arg3;
7363 cpp_buffer *ip = cpp_file_buffer (pfile);
7365 cpp_print_containing_files (pfile);
7368 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7370 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7374 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7378 char *arg1, *arg2, *arg3;
7382 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7385 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7388 cpp_print_containing_files (pfile);
7390 ip = cpp_file_buffer (pfile);
7393 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7395 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7399 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7403 char *arg1, *arg2, *arg3;
7405 if (CPP_OPTIONS (pfile)->pedantic_errors)
7406 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7408 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7411 /* Report a warning (or an error if pedantic_errors)
7412 giving specified file name and line number, not current. */
7415 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7420 char *arg1, *arg2, *arg3;
7422 if (!CPP_OPTIONS (pfile)->pedantic_errors
7423 && CPP_OPTIONS (pfile)->inhibit_warnings)
7426 cpp_file_line_for_message (pfile, file, line, -1);
7427 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7428 msg, arg1, arg2, arg3);
7431 /* This defines "errno" properly for VMS, and gives us EACCES. */
7438 #ifndef HAVE_STRERROR
7439 extern int sys_nerr;
7441 extern const char *const sys_errlist[];
7444 extern char *sys_errlist[];
7447 #else /* HAVE_STRERROR */
7451 char *strerror (int,...);
7455 * my_strerror - return the descriptive text associated with an `errno' code.
7459 my_strerror (errnum)
7465 #ifndef HAVE_STRERROR
7466 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7468 result = strerror (errnum);
7471 /* VAXCRTL's strerror() takes an optional second argument, which only
7472 matters when the first argument is EVMSERR. However, it's simplest
7473 just to pass it unconditionally. `vaxc$errno' is declared in
7474 <errno.h>, and maintained by the library in parallel with `errno'.
7475 We assume that caller's `errnum' either matches the last setting of
7476 `errno' by the library or else does not have the value `EVMSERR'. */
7478 result = strerror (errnum, vaxc$errno);
7482 result = "undocumented I/O error";
7487 /* Error including a message from `errno'. */
7490 cpp_error_from_errno (
7494 cpp_buffer *ip = cpp_file_buffer (pfile);
7496 cpp_print_containing_files (pfile);
7499 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7501 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno),NULL);
7505 cpp_perror_with_name (
7509 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7513 * No pre-compiled header file support.
7515 * Possibly different enum token codes for each C/C++ token.
7517 * Should clean up remaining directives to that do_XXX functions
7518 * only take two arguments and all have command_reads_line.
7520 * Find and cleanup remaining uses of static variables,
7522 * Support for trigraphs.
7524 * Support -dM flag (dump_all_macros).
7526 * Support for_lint flag.