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! */
29 #include "../src/config.h"
38 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
39 for the sake of machines with limited C compilers. */
44 #endif /* not EMACS */
45 #define GCC_INCLUDE_DIR "/usr/local/lib/gcc-lib/i386-unknown-freebsd_1.0/2.5.8/include"
46 #ifndef STANDARD_INCLUDE_DIR
47 #define STANDARD_INCLUDE_DIR "/usr/include"
50 #ifndef LOCAL_INCLUDE_DIR
51 #define LOCAL_INCLUDE_DIR "/usr/local/include"
54 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
56 #define PTR_INT_TYPE ptrdiff_t
58 #define PTR_INT_TYPE long
69 /* By default, colon separates directories in a path. */
70 #ifndef PATH_SEPARATOR
71 #define PATH_SEPARATOR ':'
89 #include <sys/time.h> /* for __DATE__ and __TIME__ */
90 #include <sys/resource.h>
92 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
93 // #include <sys/times.h>
101 /* This defines "errno" properly for VMS, and gives us EACCES. */
104 extern char *index ();
105 extern char *rindex ();
113 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
114 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
116 /* Find the largest host integer type and set its size and type. */
118 #ifndef HOST_BITS_PER_WIDE_INT
120 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
121 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
122 #define HOST_WIDE_INT long
124 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
125 #define HOST_WIDE_INT int
131 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
135 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
138 /* Define a generic NULL if one hasn't already been defined. */
145 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
146 #define GENERIC_PTR void *
148 #define GENERIC_PTR char *
153 #define NULL_PTR ((GENERIC_PTR)0)
156 #ifndef INCLUDE_LEN_FUDGE
157 #define INCLUDE_LEN_FUDGE 0
160 /* Symbols to predefine. */
162 #ifdef CPP_PREDEFINES
163 static char *predefs = CPP_PREDEFINES;
165 static char *predefs = "";
168 /* We let tm.h override the types used here, to handle trivial differences
169 such as the choice of unsigned int or long unsigned int for size_t.
170 When machines start needing nontrivial differences in the size type,
171 it would be best to do something here to figure out automatically
172 from other information what type to use. */
174 /* The string value for __SIZE_TYPE__. */
177 #define SIZE_TYPE "long unsigned int"
180 /* The string value for __PTRDIFF_TYPE__. */
183 #define PTRDIFF_TYPE "long int"
186 /* The string value for __WCHAR_TYPE__. */
189 #define WCHAR_TYPE "int"
191 #define CPP_WCHAR_TYPE(PFILE) \
192 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
194 /* The string value for __USER_LABEL_PREFIX__ */
196 #ifndef USER_LABEL_PREFIX
197 #define USER_LABEL_PREFIX ""
200 /* The string value for __REGISTER_PREFIX__ */
202 #ifndef REGISTER_PREFIX
203 #define REGISTER_PREFIX ""
207 /* In the definition of a #assert name, this structure forms
208 a list of the individual values asserted.
209 Each value is itself a list of "tokens".
210 These are strings that are compared by name. */
212 struct tokenlist_list {
213 struct tokenlist_list *next;
214 struct arglist *tokens;
217 struct assertion_hashnode {
218 struct assertion_hashnode *next; /* double links for easy deletion */
219 struct assertion_hashnode *prev;
220 /* also, a back pointer to this node's hash
221 chain is kept, in case the node is the head
222 of the chain and gets deleted. */
223 struct assertion_hashnode **bucket_hdr;
224 int length; /* length of token, for quick comparison */
225 U_CHAR *name; /* the actual name */
226 /* List of token-sequences. */
227 struct tokenlist_list *value;
230 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
231 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
233 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
234 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
235 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
236 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
237 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
238 (Note that it is false while we're expanding marco *arguments*.) */
239 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
241 /* Move all backslash-newline pairs out of embarrassing places.
242 Exchange all such pairs following BP
243 with any potentially-embarrassing characters that follow them.
244 Potentially-embarrassing characters are / and *
245 (because a backslash-newline inside a comment delimiter
246 would cause it not to be recognized). */
248 #define NEWLINE_FIX \
249 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
251 /* Same, but assume we've already read the potential '\\' into C. */
252 #define NEWLINE_FIX1(C) do { \
253 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
257 struct cpp_pending *next;
262 /* Forward declarations. */
264 extern char *xmalloc ();
266 static void add_import ();
267 static void append_include_chain ();
268 static void make_undef ();
269 static void make_assertion ();
270 static void path_include ();
271 static void initialize_builtins ();
272 static void initialize_char_syntax ();
273 static void dump_arg_n ();
274 static void dump_defn_1 ();
275 extern void delete_macro ();
276 static void trigraph_pcp ();
277 static int finclude ();
278 static void validate_else ();
279 static int comp_def_part ();
280 extern void fancy_abort ();
281 static void pipe_closed ();
282 static void print_containing_files ();
283 static int lookup_import ();
284 static int redundant_include_p ();
285 static is_system_include ();
286 static struct file_name_map *read_name_map ();
287 static char *read_filename_string ();
288 static int open_include_file ();
289 static int check_preconditions ();
290 static void pcfinclude ();
291 static void pcstring_used ();
292 static int check_macro_name ();
293 static int compare_defs ();
294 static int compare_token_lists ();
295 static HOST_WIDE_INT eval_if_expression ();
296 static int change_newlines ();
298 static int file_size_and_mode ();
299 static struct arglist *read_token_list ();
300 static void free_token_list ();
301 static int safe_read ();
302 static void push_macro_expansion PARAMS ((cpp_reader *,
303 U_CHAR*, int, HASHNODE*));
304 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
305 extern char *xrealloc ();
306 extern char *xcalloc ();
307 static char *savestring ();
309 static void conditional_skip ();
310 static void skip_if_group ();
312 /* Last arg to output_line_command. */
313 enum file_change_code {same_file, enter_file, leave_file};
315 /* External declarations. */
317 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
319 extern char *getenv ();
320 extern FILE *fdopen ();
321 extern char *version_string;
322 extern struct tm *localtime ();
324 /* These functions are declared to return int instead of void since they
325 are going to be placed in a table and some old compilers have trouble with
326 pointers to functions returning void. */
328 static int do_define ();
329 static int do_line ();
330 static int do_include ();
331 static int do_undef ();
332 static int do_error ();
333 static int do_pragma ();
334 static int do_ident ();
336 static int do_xifdef ();
337 static int do_else ();
338 static int do_elif ();
339 static int do_endif ();
340 static int do_sccs ();
341 static int do_once ();
342 static int do_assert ();
343 static int do_unassert ();
344 static int do_warning ();
346 struct file_name_list
348 struct file_name_list *next;
350 /* If the following is nonzero, it is a macro name.
351 Don't include the file again if that macro is defined. */
352 U_CHAR *control_macro;
353 /* If the following is nonzero, it is a C-language system include
355 int c_system_include_path;
356 /* Mapping of file names for this directory. */
357 struct file_name_map *name_map;
358 /* Non-zero if name_map is valid. */
362 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
363 via the same directory as the file that #included it. */
364 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
366 /* #include "file" looks in source file dir, then stack. */
367 /* #include <file> just looks in the stack. */
368 /* -I directories are added to the end, then the defaults are added. */
370 static struct default_include {
371 char *fname; /* The name of the directory. */
372 int cplusplus; /* Only look here if we're compiling C++. */
373 int cxx_aware; /* Includes in this directory don't need to
374 be wrapped in extern "C" when compiling
376 } include_defaults_array[]
377 #ifdef INCLUDE_DEFAULTS
381 /* Pick up GNU C++ specific include files. */
382 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
384 /* This is the dir for fixincludes. Put it just before
385 the files that we fix. */
386 { GCC_INCLUDE_DIR, 0, 0 },
387 /* For cross-compilation, this dir name is generated
388 automatically in Makefile.in. */
389 { CROSS_INCLUDE_DIR, 0, 0 },
390 /* This is another place that the target system's headers might be. */
391 { TOOL_INCLUDE_DIR, 0, 1 },
392 { LOCAL_INCLUDE_DIR, 0, 1 },
393 #else /* not CROSS_COMPILE */
394 /* This should be /usr/local/include and should come before
395 the fixincludes-fixed header files. */
396 { LOCAL_INCLUDE_DIR, 0, 1 },
397 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
398 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
399 { TOOL_INCLUDE_DIR, 0, 1 },
400 /* This is the dir for fixincludes. Put it just before
401 the files that we fix. */
402 { GCC_INCLUDE_DIR, 0, 0 },
403 /* Some systems have an extra dir of include files. */
404 #ifdef SYSTEM_INCLUDE_DIR
405 { SYSTEM_INCLUDE_DIR, 0, 0 },
407 { STANDARD_INCLUDE_DIR, 0, 0 },
408 #endif /* not CROSS_COMPILE */
411 #endif /* no INCLUDE_DEFAULTS */
413 /* `struct directive' defines one #-directive, including how to handle it. */
416 int length; /* Length of name */
417 int (*func)(); /* Function to handle directive */
418 char *name; /* Name of directive */
419 enum node_type type; /* Code which describes which directive. */
420 char command_reads_line; /* One if rest of line is read by func. */
421 char traditional_comments; /* Nonzero: keep comments if -traditional. */
422 char pass_thru; /* Copy preprocessed directive to output file.*/
425 /* Here is the actual list of #-directives, most-often-used first.
426 The initialize_builtins function assumes #define is the very first. */
428 static struct directive directive_table[] = {
429 { 6, do_define, "define", T_DEFINE, 0, 1},
430 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
431 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
432 { 7, do_include, "include", T_INCLUDE, 1},
433 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
434 { 6, do_include, "import", T_IMPORT, 1},
435 { 5, do_endif, "endif", T_ENDIF, 1},
436 { 4, do_else, "else", T_ELSE, 1},
437 { 2, do_if, "if", T_IF, 1},
438 { 4, do_elif, "elif", T_ELIF, 1},
439 { 5, do_undef, "undef", T_UNDEF},
440 { 5, do_error, "error", T_ERROR},
441 { 7, do_warning, "warning", T_WARNING},
442 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
443 { 4, do_line, "line", T_LINE, 1},
444 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
445 #ifdef SCCS_DIRECTIVE
446 { 4, do_sccs, "sccs", T_SCCS},
448 { 6, do_assert, "assert", T_ASSERT, 1},
449 { 8, do_unassert, "unassert", T_UNASSERT, 1},
450 { -1, 0, "", T_UNUSED},
453 /* table to tell if char can be part of a C identifier. */
454 U_CHAR is_idchar[256];
455 /* table to tell if char can be first char of a c identifier. */
456 U_CHAR is_idstart[256];
457 /* table to tell if c is horizontal space. */
458 U_CHAR is_hor_space[256];
459 /* table to tell if c is horizontal or vertical space. */
460 static U_CHAR is_space[256];
462 /* Initialize syntactic classifications of characters. */
465 initialize_char_syntax (
466 struct cpp_options *opts)
471 * Set up is_idchar and is_idstart tables. These should be
472 * faster than saying (is_alpha (c) || c == '_'), etc.
473 * Set up these things before calling any routines tthat
476 for (i = 'a'; i <= 'z'; i++) {
477 is_idchar[i - 'a' + 'A'] = 1;
479 is_idstart[i - 'a' + 'A'] = 1;
482 for (i = '0'; i <= '9'; i++)
486 is_idchar['$'] = opts->dollars_in_ident;
487 is_idstart['$'] = opts->dollars_in_ident;
489 /* horizontal space table */
490 is_hor_space[' '] = 1;
491 is_hor_space['\t'] = 1;
492 is_hor_space['\v'] = 1;
493 is_hor_space['\f'] = 1;
494 is_hor_space['\r'] = 1;
505 /* Place into PFILE a quoted string representing the string SRC.
506 Caller must reserve enough space in pfile->token_buffer. */
514 CPP_PUTC_Q (pfile, '\"');
516 switch ((c = *src++))
520 CPP_PUTC_Q (pfile, c);
523 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
524 CPP_ADJUST_WRITTEN (pfile, 4);
530 CPP_PUTC_Q (pfile, '\\');
531 CPP_PUTC_Q (pfile, c);
535 CPP_PUTC_Q (pfile, '\"');
536 CPP_NUL_TERMINATE_Q (pfile);
541 /* Make sure PFILE->token_buffer will hold at least N more chars. */
548 long old_written = CPP_WRITTEN (pfile);
549 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
550 pfile->token_buffer = (U_CHAR*)
551 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
552 CPP_SET_WRITTEN (pfile, old_written);
557 * process a given definition string, for initialization
558 * If STR is just an identifier, define it with value 1.
559 * If STR has anything after the identifier, then it should
560 * be identifier=definition.
574 cpp_error (pfile, "malformed option `-D %s'", str);
577 while (is_idchar[*++p])
581 buf = (U_CHAR *) alloca (p - buf + 4);
582 strcpy ((char *)buf, str);
583 strcat ((char *)buf, " 1");
587 cpp_error (pfile, "malformed option `-D %s'", str);
593 /* Copy the entire option so we can modify it. */
594 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
595 strncpy (buf, str, p - str);
596 /* Change the = to a space. */
598 /* Scan for any backslash-newline and remove it. */
603 if (*p == '\\' && p[1] == '\n')
611 do_define (pfile, NULL, buf, buf + strlen (buf));
614 /* Process the string STR as if it appeared as the body of a #assert.
615 OPTION is the option name for which STR was the argument. */
624 struct directive *kt;
627 /* Copy the entire option so we can modify it. */
628 buf = (U_CHAR *) alloca (strlen (str) + 1);
629 strcpy ((char *) buf, str);
630 /* Scan for any backslash-newline and remove it. */
634 if (*p == '\\' && p[1] == '\n')
643 if (!is_idstart[*p]) {
644 cpp_error (pfile, "malformed option `%s %s'", option, str);
647 while (is_idchar[*++p])
649 while (*p == ' ' || *p == '\t') p++;
650 if (! (*p == 0 || *p == '(')) {
651 cpp_error (pfile, "malformed option `%s %s'", option, str);
655 ip = cpp_push_buffer (pfile, buf, strlen (buf));
656 do_assert (pfile, NULL, NULL, NULL);
657 cpp_pop_buffer (pfile);
660 /* Append a chain of `struct file_name_list's
661 to the end of the main include chain.
662 FIRST is the beginning of the chain to append, and LAST is the end. */
665 append_include_chain (
667 struct file_name_list *first,struct file_name_list *last)
669 struct cpp_options *opts = CPP_OPTIONS (pfile);
670 struct file_name_list *dir;
675 if (opts->include == 0)
676 opts->include = first;
678 opts->last_include->next = first;
680 if (opts->first_bracket_include == 0)
681 opts->first_bracket_include = first;
683 for (dir = first; ; dir = dir->next) {
684 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
685 if (len > pfile->max_include_len)
686 pfile->max_include_len = len;
692 opts->last_include = last;
695 /* Add output to `deps_buffer' for the -M switch.
696 STRING points to the text to be output.
697 SPACER is ':' for targets, ' ' for dependencies, zero for text
698 to be inserted literally. */
706 int size = strlen (string);
711 #ifndef MAX_OUTPUT_COLUMNS
712 #define MAX_OUTPUT_COLUMNS 72
715 && pfile->deps_column > 0
716 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
718 deps_output (pfile, " \\\n ", 0);
719 pfile->deps_column = 0;
722 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
724 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
725 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
726 pfile->deps_allocated_size);
728 if (spacer == ' ' && pfile->deps_column > 0)
729 pfile->deps_buffer[pfile->deps_size++] = ' ';
730 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
731 pfile->deps_size += size;
732 pfile->deps_column += size;
734 pfile->deps_buffer[pfile->deps_size++] = ':';
735 pfile->deps_buffer[pfile->deps_size] = 0;
738 /* Given a colon-separated list of file names PATH,
739 add all the names to the search path for include files. */
754 struct file_name_list *dirtmp;
756 /* Find the end of this name. */
757 while (*q != 0 && *q != PATH_SEPARATOR) q++;
759 /* An empty name in the path stands for the current directory. */
760 name = (char *) xmalloc (2);
764 /* Otherwise use the directory that is named. */
765 name = (char *) xmalloc (q - p + 1);
766 bcopy (p, name, q - p);
770 dirtmp = (struct file_name_list *)
771 xmalloc (sizeof (struct file_name_list));
772 dirtmp->next = 0; /* New one goes on the end */
773 dirtmp->control_macro = 0;
774 dirtmp->c_system_include_path = 0;
775 dirtmp->fname = name;
776 dirtmp->got_name_map = 0;
777 append_include_chain (pfile, dirtmp, dirtmp);
779 /* Advance past this name. */
783 /* Skip the colon. */
790 struct cpp_options *opts)
792 bzero ((char *) opts, sizeof *opts);
793 opts->in_fname = NULL;
794 opts->out_fname = NULL;
796 /* Initialize is_idchar to allow $. */
797 opts->dollars_in_ident = 1;
798 initialize_char_syntax (opts);
799 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
801 opts->no_line_commands = 0;
802 opts->no_trigraphs = 1;
803 opts->put_out_comments = 0;
804 opts->print_include_names = 0;
805 opts->dump_macros = dump_none;
808 opts->cplusplus_comments = 0;
815 opts->pedantic_errors = 0;
816 opts->inhibit_warnings = 0;
817 opts->warn_comments = 0;
818 opts->warn_import = 1;
819 opts->warnings_are_errors = 0;
842 HASHNODE *macro = (HASHNODE*)pbuf->data;
843 if (macro->type == T_DISABLED)
844 macro->type = T_MACRO;
845 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
870 register U_CHAR *p = bp;
872 /* First count the backslash-newline pairs here. */
874 while (p[0] == '\\' && p[1] == '\n')
877 /* What follows the backslash-newlines is not embarrassing. */
879 if (*p != '/' && *p != '*')
882 /* Copy all potentially embarrassing characters
883 that follow the backslash-newline pairs
884 down to where the pairs originally started. */
886 while (*p == '*' || *p == '/')
889 /* Now write the same number of pairs after the embarrassing chars. */
897 /* Assuming we have read '/'.
898 If this is the start of a comment (followed by '*' or '/'),
899 skip to the end of the comment, and return ' '.
900 Return EOF if we reached the end of file before the end of the comment.
901 If not the start of a comment, return '/'. */
909 while (PEEKC() == '\\' && PEEKN(1) == '\n')
924 while (c == '\\' && PEEKC() == '\n')
928 FORWARD(1), c = GETC();
930 if (prev_c == '*' && c == '/')
932 if (c == '\n' && linep)
936 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
943 return ' '; /* Allow // to be terminated by EOF. */
944 while (c == '\\' && PEEKC() == '\n')
953 /* Don't consider final '\n' to be part of comment. */
963 /* Skip whitespace \-newline and comments. Does not macro-expand. */
975 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
976 cpp_pedwarn (pfile, "%s in preprocessing directive",
977 c == '\f' ? "formfeed" : "vertical tab");
983 c = skip_comment (pfile, NULL);
986 if (c == EOF || c == '/')
989 else if (c == '\\' && PEEKN(1) == '\n') {
992 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
993 && is_hor_space[PEEKN(1)])
999 /* Read the rest of the current line.
1000 The line is appended to PFILE's output buffer. */
1006 struct cpp_options *opts = CPP_OPTIONS (pfile);
1016 if (PEEKC() == '\n')
1023 goto scan_directive_token;
1027 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
1028 goto scan_directive_token;
1032 if (CPP_PEDANTIC (pfile))
1033 cpp_pedwarn (pfile, "%s in preprocessing directive",
1034 c == '\f' ? "formfeed" : "vertical tab");
1040 scan_directive_token:
1042 cpp_get_token (pfile);
1045 CPP_PUTC (pfile, c);
1048 CPP_NUL_TERMINATE (pfile);
1055 long old = CPP_WRITTEN (pfile);
1056 copy_rest_of_line (pfile);
1057 CPP_SET_WRITTEN (pfile, old);
1060 /* Handle a possible # directive.
1061 '#' has already been read. */
1067 register struct directive *kt;
1070 U_CHAR *ident, *line_end;
1071 long old_written = CPP_WRITTEN (pfile);
1073 cpp_skip_hspace (pfile);
1076 if (c >= '0' && c <= '9')
1078 /* Handle # followed by a line number. */
1079 if (CPP_PEDANTIC (pfile))
1080 cpp_pedwarn (pfile, "`#' followed by integer");
1081 do_line (pfile, NULL);
1082 goto done_a_directive;
1085 /* Now find the directive name. */
1086 CPP_PUTC (pfile, '#');
1087 parse_name (pfile, GETC());
1088 ident = pfile->token_buffer + old_written + 1;
1089 ident_length = CPP_PWRITTEN (pfile) - ident;
1090 if (ident_length == 0 && PEEKC() == '\n')
1092 /* A line of just `#' becomes blank. */
1093 goto done_a_directive;
1097 if (ident_length == 0 || !is_idstart[*ident]) {
1099 while (is_idchar[*p]) {
1100 if (*p < '0' || *p > '9')
1104 /* Avoid error for `###' and similar cases unless -pedantic. */
1106 while (*p == '#' || is_hor_space[*p]) p++;
1108 if (pedantic && !lang_asm)
1109 cpp_warning (pfile, "invalid preprocessor directive");
1115 cpp_error (pfile, "invalid preprocessor directive name");
1121 * Decode the keyword and call the appropriate expansion
1122 * routine, after moving the input pointer up to the next line.
1124 for (kt = directive_table; ; kt++) {
1125 if (kt->length <= 0)
1126 goto not_a_directive;
1127 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1131 if (! kt->command_reads_line)
1133 /* Nonzero means do not delete comments within the directive.
1134 #define needs this when -traditional. */
1135 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1136 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1137 CPP_OPTIONS (pfile)->put_out_comments = comments;
1138 after_ident = CPP_WRITTEN (pfile);
1139 copy_rest_of_line (pfile);
1140 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1143 /* For #pragma and #define, we may want to pass through the directive.
1144 Other directives may create output, but we don't want the directive
1145 itself out, so we pop it now. For example #include may write a
1146 command (see comment in do_include), and conditionals may emit
1147 #failed ... #endfailed stuff. But note that popping the buffer
1148 means the parameters to kt->func may point after pfile->limit
1149 so these parameters are invalid as soon as something gets appended
1150 to the token_buffer. */
1152 line_end = CPP_PWRITTEN (pfile);
1153 if (!kt->pass_thru && kt->type != T_DEFINE)
1154 CPP_SET_WRITTEN (pfile, old_written);
1156 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1158 || (kt->type == T_DEFINE
1159 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1161 /* Just leave the entire #define in the output stack. */
1163 else if (kt->type == T_DEFINE
1164 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1166 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1167 SKIP_WHITE_SPACE (p);
1168 while (is_idchar[*p]) p++;
1170 CPP_PUTC (pfile, '\n');
1172 else if (kt->type == T_DEFINE)
1173 CPP_SET_WRITTEN (pfile, old_written);
1181 /* Pass a directive through to the output file.
1182 BUF points to the contents of the directive, as a contiguous string.
1183 LIMIT points to the first character past the end of the directive.
1184 KEYWORD is the keyword-table entry for the directive. */
1187 pass_thru_directive (
1188 U_CHAR *buf, U_CHAR *limit,
1190 struct directive *keyword)
1192 register unsigned keyword_length = keyword->length;
1194 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1195 CPP_PUTC_Q (pfile, '#');
1196 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1197 if (limit != buf && buf[0] != ' ')
1198 CPP_PUTC_Q (pfile, ' ');
1199 CPP_PUTS_Q (pfile, buf, limit - buf);
1201 CPP_PUTS_Q (pfile, '\n');
1202 /* Count the line we have just made in the output,
1203 to get in sync properly. */
1208 /* The arglist structure is built by do_define to tell
1209 collect_definition where the argument names begin. That
1210 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1211 would contain pointers to the strings x, y, and z.
1212 Collect_definition would then build a DEFINITION node,
1213 with reflist nodes pointing to the places x, y, and z had
1214 appeared. So the arglist is just convenience data passed
1215 between these two routines. It is not kept around after
1216 the current #define has been processed and entered into the
1220 struct arglist *next;
1227 /* Read a replacement list for a macro with parameters.
1228 Build the DEFINITION structure.
1229 Reads characters of text starting at BUF until END.
1230 ARGLIST specifies the formal parameters to look for
1231 in the text of the definition; NARGS is the number of args
1232 in that list, or -1 for a macro name that wants no argument list.
1233 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1234 and NAMELEN is its length in characters.
1236 Note that comments, backslash-newlines, and leading white space
1237 have already been deleted from the argument. */
1242 U_CHAR *buf, U_CHAR *limit,
1244 struct arglist *arglist)
1247 register U_CHAR *p, *lastp, *exp_p;
1248 struct reflist *endpat = NULL;
1249 /* Pointer to first nonspace after last ## seen. */
1251 /* Pointer to first nonspace after last single-# seen. */
1252 U_CHAR *stringify = 0;
1254 int expected_delimiter = '\0';
1256 /* Scan thru the replacement list, ignoring comments and quoted
1257 strings, picking up on the macro calls. It does a linear search
1258 thru the arg list on every potential symbol. Profiling might say
1259 that something smarter should happen. */
1264 /* Find the beginning of the trailing whitespace. */
1266 while (p < limit && is_space[limit[-1]]) limit--;
1268 /* Allocate space for the text in the macro definition.
1269 Leading and trailing whitespace chars need 2 bytes each.
1270 Each other input char may or may not need 1 byte,
1271 so this is an upper bound. The extra 5 are for invented
1272 leading and trailing newline-marker and final null. */
1273 maxsize = (sizeof (DEFINITION)
1275 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1279 defn = (DEFINITION *) xcalloc (1, maxsize);
1281 defn->nargs = nargs;
1282 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1287 /* Add one initial space escape-marker to prevent accidental
1288 token-pasting (often removed by macroexpand). */
1292 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1293 cpp_error (pfile, "`##' at start of macro definition");
1297 /* Process the main body of the definition. */
1299 int skipped_arg = 0;
1300 register U_CHAR c = *p++;
1304 if (!CPP_TRADITIONAL (pfile)) {
1308 if (expected_delimiter != '\0') {
1309 if (c == expected_delimiter)
1310 expected_delimiter = '\0';
1312 expected_delimiter = c;
1316 if (p < limit && expected_delimiter) {
1317 /* In a string, backslash goes through
1318 and makes next char ordinary. */
1324 /* An '@' in a string or character constant stands for itself,
1325 and does not need to be escaped. */
1326 if (!expected_delimiter)
1331 /* # is ordinary inside a string. */
1332 if (expected_delimiter)
1334 if (p < limit && *p == '#') {
1335 /* ##: concatenate preceding and following tokens. */
1336 /* Take out the first #, discard preceding whitespace. */
1338 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1340 /* Skip the second #. */
1342 /* Discard following whitespace. */
1343 SKIP_WHITE_SPACE (p);
1346 cpp_error (pfile, "`##' at end of macro definition");
1347 } else if (nargs >= 0) {
1348 /* Single #: stringify following argument ref.
1349 Don't leave the # in the expansion. */
1351 SKIP_WHITE_SPACE (p);
1352 if (p == limit || ! is_idstart[*p])
1354 "`#' operator is not followed by a macro argument name");
1361 /* In -traditional mode, recognize arguments inside strings and
1362 and character constants, and ignore special properties of #.
1363 Arguments inside strings are considered "stringified", but no
1364 extra quote marks are supplied. */
1368 if (expected_delimiter != '\0') {
1369 if (c == expected_delimiter)
1370 expected_delimiter = '\0';
1372 expected_delimiter = c;
1376 /* Backslash quotes delimiters and itself, but not macro args. */
1377 if (expected_delimiter != 0 && p < limit
1378 && (*p == expected_delimiter || *p == '\\')) {
1385 if (expected_delimiter != '\0') /* No comments inside strings. */
1388 /* If we find a comment that wasn't removed by handle_directive,
1389 this must be -traditional. So replace the comment with
1393 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1396 /* Mark this as a concatenation-point, as if it had been ##. */
1404 /* Handle the start of a symbol. */
1405 if (is_idchar[c] && nargs > 0) {
1406 U_CHAR *id_beg = p - 1;
1410 while (p != limit && is_idchar[*p]) p++;
1411 id_len = p - id_beg;
1413 if (is_idstart[c]) {
1414 register struct arglist *arg;
1416 for (arg = arglist; arg != NULL; arg = arg->next) {
1417 struct reflist *tpat;
1419 if (arg->name[0] == c
1420 && arg->length == id_len
1421 && strncmp (arg->name, id_beg, id_len) == 0) {
1422 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1423 if (CPP_TRADITIONAL (pfile)) {
1424 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1428 "macro arg `%.*s' would be stringified with -traditional.",
1432 /* If ANSI, don't actually substitute inside a string. */
1433 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1435 /* make a pat node for this arg and append it to the end of
1437 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1439 tpat->raw_before = concat == id_beg;
1440 tpat->raw_after = 0;
1441 tpat->rest_args = arg->rest_args;
1442 tpat->stringify = (CPP_TRADITIONAL (pfile)
1443 ? expected_delimiter != '\0'
1444 : stringify == id_beg);
1447 defn->pattern = tpat;
1449 endpat->next = tpat;
1452 tpat->argno = arg->argno;
1453 tpat->nchars = exp_p - lastp;
1455 register U_CHAR *p1 = p;
1456 SKIP_WHITE_SPACE (p1);
1457 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1458 tpat->raw_after = 1;
1460 lastp = exp_p; /* place to start copying from next time */
1467 /* If this was not a macro arg, copy it into the expansion. */
1468 if (! skipped_arg) {
1469 register U_CHAR *lim1 = p;
1473 if (stringify == id_beg)
1475 "`#' operator should be followed by a macro argument name");
1480 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1482 /* If ANSI, put in a "@ " marker to prevent token pasting.
1483 But not if "inside a string" (which in ANSI mode
1484 happens only for -D option). */
1491 defn->length = exp_p - defn->expansion;
1493 /* Crash now if we overrun the allocated size. */
1494 if (defn->length + 1 > maxsize)
1498 /* This isn't worth the time it takes. */
1499 /* give back excess storage */
1500 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1507 * special extension string that can be added to the last macro argument to
1508 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1509 * #define wow(a, b...) process (b, a, b)
1510 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1511 * { wow (one, two); } -> { process (two, one, two); }
1512 * if this "rest_arg" is used with the concat token '##' and if it is not
1513 * supplied then the token attached to with ## will not be outputted. Ex:
1514 * #define wow (a, b...) process (b ## , a, ## b)
1515 * { wow (1, 2); } -> { process (2, 1, 2); }
1516 * { wow (one); } -> { process (one); {
1518 static char rest_extension[] = "...";
1519 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1521 /* Create a DEFINITION node from a #define directive. Arguments are
1522 as for do_define. */
1525 U_CHAR *buf, U_CHAR *limit,
1529 U_CHAR *bp; /* temp ptr into input buffer */
1530 U_CHAR *symname; /* remember where symbol name starts */
1531 int sym_length; /* and how long it is */
1534 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1536 int arglengths = 0; /* Accumulate lengths of arg names
1537 plus number of args. */
1539 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1543 while (is_hor_space[*bp])
1546 symname = bp; /* remember where it starts */
1548 sym_length = check_macro_name (pfile, bp, "macro");
1551 /* Lossage will occur if identifiers or control keywords are broken
1552 across lines using backslash. This is not the right place to take
1556 struct arglist *arg_ptrs = NULL;
1559 bp++; /* skip '(' */
1560 SKIP_WHITE_SPACE (bp);
1562 /* Loop over macro argument names. */
1563 while (*bp != ')') {
1564 struct arglist *temp;
1566 temp = (struct arglist *) alloca (sizeof (struct arglist));
1568 temp->next = arg_ptrs;
1569 temp->argno = argno++;
1570 temp->rest_args = 0;
1574 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1576 if (!is_idstart[*bp])
1577 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1579 /* Find the end of the arg name. */
1580 while (is_idchar[*bp]) {
1582 /* do we have a "special" rest-args extension here? */
1583 if (limit - bp > REST_EXTENSION_LENGTH &&
1584 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1586 temp->rest_args = 1;
1590 temp->length = bp - temp->name;
1592 bp += REST_EXTENSION_LENGTH;
1593 arglengths += temp->length + 2;
1594 SKIP_WHITE_SPACE (bp);
1595 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1596 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1601 SKIP_WHITE_SPACE (bp);
1604 cpp_error (pfile, "unterminated parameter list in `#define'");
1608 struct arglist *otemp;
1610 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1611 if (temp->length == otemp->length &&
1612 strncmp (temp->name, otemp->name, temp->length) == 0) {
1615 name = (U_CHAR *) alloca (temp->length + 1);
1616 (void) strncpy (name, temp->name, temp->length);
1617 name[temp->length] = '\0';
1619 "duplicate argument name `%s' in `#define'", name);
1625 ++bp; /* skip paren */
1626 SKIP_WHITE_SPACE (bp);
1627 /* now everything from bp before limit is the definition. */
1628 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1629 defn->rest_args = rest_args;
1631 /* Now set defn->args.argnames to the result of concatenating
1632 the argument names in reverse order
1633 with comma-space between them. */
1634 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1636 struct arglist *temp;
1638 for (temp = arg_ptrs; temp; temp = temp->next) {
1639 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1641 if (temp->next != 0) {
1642 defn->args.argnames[i++] = ',';
1643 defn->args.argnames[i++] = ' ';
1646 defn->args.argnames[i] = 0;
1649 /* Simple expansion or empty definition. */
1653 if (is_hor_space[*bp]) {
1655 SKIP_WHITE_SPACE (bp);
1658 case '!': case '"': case '#': case '%': case '&': case '\'':
1659 case ')': case '*': case '+': case ',': case '-': case '.':
1660 case '/': case ':': case ';': case '<': case '=': case '>':
1661 case '?': case '[': case '\\': case ']': case '^': case '{':
1662 case '|': case '}': case '~':
1663 cpp_warning (pfile, "missing white space after `#define %.*s'",
1664 sym_length, symname);
1668 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1669 sym_length, symname);
1674 /* now everything from bp before limit is the definition. */
1675 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1676 defn->args.argnames = (U_CHAR *) "";
1682 /* OP is null if this is a predefinition */
1683 defn->predefined = predefinition;
1685 mdef.symnam = symname;
1686 mdef.symlen = sym_length;
1695 /* Check a purported macro name SYMNAME, and yield its length.
1696 USAGE is the kind of name this is intended for. */
1707 for (p = symname; is_idchar[*p]; p++)
1709 sym_length = p - symname;
1710 if (sym_length == 0)
1711 cpp_error (pfile, "invalid %s name", usage);
1712 else if (!is_idstart[*symname]) {
1713 U_CHAR *msg; /* what pain... */
1714 msg = (U_CHAR *) alloca (sym_length + 1);
1715 bcopy (symname, msg, sym_length);
1716 msg[sym_length] = 0;
1717 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1719 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1720 cpp_error (pfile, "invalid %s name `defined'", usage);
1726 * return zero if two DEFINITIONs are isomorphic
1730 DEFINITION *d1, DEFINITION *d2)
1732 register struct reflist *a1, *a2;
1733 register U_CHAR *p1 = d1->expansion;
1734 register U_CHAR *p2 = d2->expansion;
1737 if (d1->nargs != d2->nargs)
1739 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1741 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1742 a1 = a1->next, a2 = a2->next) {
1743 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1744 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1745 || a1->argno != a2->argno
1746 || a1->stringify != a2->stringify
1747 || a1->raw_before != a2->raw_before
1748 || a1->raw_after != a2->raw_after)
1756 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1757 p2, d2->length - (p2 - d2->expansion), 1))
1762 /* Return 1 if two parts of two macro definitions are effectively different.
1763 One of the parts starts at BEG1 and has LEN1 chars;
1764 the other has LEN2 chars at BEG2.
1765 Any sequence of whitespace matches any other sequence of whitespace.
1766 FIRST means these parts are the first of a macro definition;
1767 so ignore leading whitespace entirely.
1768 LAST means these parts are the last of a macro definition;
1769 so ignore trailing whitespace entirely. */
1774 U_CHAR *beg1, int len1,
1775 U_CHAR *beg2, int len2 ,
1778 register U_CHAR *end1 = beg1 + len1;
1779 register U_CHAR *end2 = beg2 + len2;
1781 while (beg1 != end1 && is_space[*beg1]) beg1++;
1782 while (beg2 != end2 && is_space[*beg2]) beg2++;
1785 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1786 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1788 while (beg1 != end1 && beg2 != end2) {
1789 if (is_space[*beg1] && is_space[*beg2]) {
1790 while (beg1 != end1 && is_space[*beg1]) beg1++;
1791 while (beg2 != end2 && is_space[*beg2]) beg2++;
1792 } else if (*beg1 == *beg2) {
1796 return (beg1 != end1) || (beg2 != end2);
1799 /* Process a #define command.
1800 BUF points to the contents of the #define command, as a contiguous string.
1801 LIMIT points to the first character past the end of the definition.
1802 KEYWORD is the keyword-table entry for #define,
1803 or NULL for a "predefined" macro. */
1808 struct directive *keyword,
1809 U_CHAR *buf, U_CHAR *limit)
1816 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1817 if (pcp_outfile && keyword)
1818 pass_thru_directive (buf, limit, pfile, keyword);
1821 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1825 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1827 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1830 /* Redefining a precompiled key is ok. */
1831 if (hp->type == T_PCSTRING)
1833 /* Redefining a macro is ok if the definitions are the same. */
1834 else if (hp->type == T_MACRO)
1835 ok = ! compare_defs (mdef.defn, hp->value.defn);
1836 /* Redefining a constant is ok with -D. */
1837 else if (hp->type == T_CONST)
1838 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1839 /* Print the warning if it's not ok. */
1842 U_CHAR *msg; /* what pain... */
1844 /* If we are passing through #define and #undef directives, do
1845 that for this re-definition now. */
1846 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1847 pass_thru_directive (buf, limit, pfile, keyword);
1849 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1851 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1852 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1853 cpp_pedwarn (pfile, msg);
1854 if (hp->type == T_MACRO)
1855 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1856 "this is the location of the previous definition");
1858 /* Replace the old definition. */
1860 hp->value.defn = mdef.defn;
1864 /* If we are passing through #define and #undef directives, do
1865 that for this new definition now. */
1866 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1867 pass_thru_directive (buf, limit, pfile, keyword);
1868 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1869 (char *) mdef.defn, hashcode);
1879 /* This structure represents one parsed argument in a macro call.
1880 `raw' points to the argument text as written (`raw_length' is its length).
1881 `expanded' points to the argument's macro-expansion
1882 (its length is `expand_length').
1883 `stringified_length' is the length the argument would have
1885 `use_count' is the number of times this macro arg is substituted
1886 into the macro. If the actual use count exceeds 10,
1887 the value stored is 10. */
1889 /* raw and expanded are relative to ARG_BASE */
1890 #define ARG_BASE ((pfile)->token_buffer)
1893 /* Strings relative to pfile->token_buffer */
1894 long raw, expanded, stringified;
1895 int raw_length, expand_length;
1896 int stringified_length;
1908 #ifdef STATIC_BUFFERS
1909 register cpp_buffer *buf = CPP_BUFFER (pfile);
1910 if (buf == pfile->buffer_stack)
1911 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1913 bzero ((char *) buf, sizeof (cpp_buffer));
1914 CPP_BUFFER (pfile) = buf;
1916 register cpp_buffer *buf = (cpp_buffer*) xmalloc (sizeof(cpp_buffer));
1917 bzero ((char *) buf, sizeof (cpp_buffer));
1918 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1919 CPP_BUFFER (pfile) = buf;
1921 buf->if_stack = pfile->if_stack;
1922 buf->cleanup = null_cleanup;
1923 buf->underflow = null_underflow;
1924 buf->buf = buf->cur = buffer;
1925 buf->alimit = buf->rlimit = buffer + length;
1931 cpp_pop_buffer (pfile)
1934 cpp_buffer *buf = CPP_BUFFER (pfile);
1935 #ifdef STATIC_BUFFERS
1936 (*buf->cleanup) (buf, pfile);
1937 return ++CPP_BUFFER (pfile);
1939 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1940 (*buf->cleanup) (buf, pfile);
1941 CPP_BUFFER (pfile) = next_buf;
1947 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1948 Pop the buffer when done. */
1954 cpp_buffer *buffer = CPP_BUFFER (pfile);
1957 enum cpp_token token = cpp_get_token (pfile);
1958 if (token == CPP_EOF) /* Should not happen ... */
1960 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1962 cpp_pop_buffer (pfile);
1969 * Rescan a string (which may have escape marks) into pfile's buffer.
1970 * Place the result in pfile->token_buffer.
1972 * The input is copied before it is scanned, so it is safe to pass
1973 * it something from the token_buffer that will get overwritten
1974 * (because it follows CPP_WRITTEN). This is used by do_include.
1978 cpp_expand_to_buffer (
1983 register cpp_buffer *ip;
1985 U_CHAR *limit = buf + length;
1988 int odepth = indepth;
1994 /* Set up the input on the input stack. */
1996 buf1 = (U_CHAR *) alloca (length + 1);
1998 register U_CHAR *p1 = buf;
1999 register U_CHAR *p2 = buf1;
2006 ip = cpp_push_buffer (pfile, buf1, length);
2007 ip->has_escapes = 1;
2009 ip->lineno = obuf.lineno = 1;
2012 /* Scan the input, create the output. */
2013 cpp_scan_buffer (pfile);
2016 if (indepth != odepth)
2020 CPP_NUL_TERMINATE (pfile);
2035 (*linep)++, (*colp) = 1;
2041 /* Move line_base forward, updating lineno and colno. */
2045 register cpp_buffer *pbuf)
2047 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2048 unsigned char *new_pos = pbuf->cur;
2049 register struct parse_marker *mark;
2050 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2052 if (pbuf->buf + mark->position < new_pos)
2053 new_pos = pbuf->buf + mark->position;
2055 pbuf->line_base += new_pos - old_pos;
2056 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2060 cpp_buf_line_and_col (
2061 register cpp_buffer *pbuf,
2062 long *linep,long *colp)
2069 *linep = pbuf->lineno;
2070 *colp = pbuf->colno;
2071 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2080 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2086 cpp_buffer *ip = CPP_BUFFER (pfile);
2088 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2089 if (ip->fname != NULL)
2096 register U_CHAR *buf,
2097 register U_CHAR *limit)
2099 register long count = 0;
2110 * write out a #line command, for instance, after an #include file.
2111 * If CONDITIONAL is nonzero, we can omit the #line if it would
2112 * appear to be a no-op, and we can output a few newlines instead
2113 * if we want to increase the line number by a small amount.
2114 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2118 output_line_command (
2121 enum file_change_code file_change)
2124 char *line_cmd_buf, *line_end;
2126 cpp_buffer *ip = CPP_BUFFER (pfile);
2128 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2132 update_position (ip);
2133 line = CPP_BUFFER (pfile)->lineno;
2134 col = CPP_BUFFER (pfile)->colno;
2135 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2137 if (CPP_OPTIONS (pfile)->no_line_commands)
2141 if (line == pfile->lineno)
2144 /* If the inherited line number is a little too small,
2145 output some newlines instead of a #line command. */
2146 if (line > pfile->lineno && line < pfile->lineno + 8) {
2147 CPP_RESERVE (pfile, 20);
2148 while (line > pfile->lineno) {
2149 CPP_PUTC_Q (pfile, '\n');
2157 /* Don't output a line number of 0 if we can help it. */
2158 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2159 && *ip->bufp == '\n') {
2165 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2167 static char sharp_line[] = "#line ";
2169 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2172 sprintf (CPP_PWRITTEN (pfile), "%d ", line+2);
2173 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2175 // modification for SDC51
2176 if (*ip->nominal_fname == '\0')
2177 quote_string (pfile,"standard input");
2179 quote_string (pfile, ip->nominal_fname);
2180 if (file_change != same_file) {
2181 CPP_PUTC_Q (pfile, ' ');
2182 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2184 /* Tell cc1 if following text comes from a system header file. */
2185 if (ip->system_header_p) {
2186 CPP_PUTC_Q (pfile, ' ');
2187 CPP_PUTC_Q (pfile, '3');
2189 #ifndef NO_IMPLICIT_EXTERN_C
2190 /* Tell cc1plus if following text should be treated as C. */
2191 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2192 CPP_PUTC_Q (pfile, ' ');
2193 CPP_PUTC_Q (pfile, '4');
2196 CPP_PUTC_Q (pfile, '\n');
2197 pfile->lineno = line;
2201 * Parse a macro argument and append the info on PFILE's token_buffer.
2202 * REST_ARGS means to absorb the rest of the args.
2203 * Return nonzero to indicate a syntax error.
2206 static enum cpp_token
2212 enum cpp_token token;
2213 long arg_start = CPP_WRITTEN (pfile);
2214 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2215 CPP_OPTIONS (pfile)->put_out_comments = 0;
2217 /* Try to parse as much of the argument as exists at this
2218 input stack level. */
2219 pfile->no_macro_expand++;
2222 token = cpp_get_token (pfile);
2228 /* If we've hit end of file, it's an error (reported by caller).
2229 Ditto if it's the end of cpp_expand_to_buffer text.
2230 If we've hit end of macro, just continue. */
2231 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2242 /* if we've returned to lowest level and
2243 we aren't absorbing all args */
2244 if (paren == 0 && rest_args == 0)
2248 /* Remove ',' or ')' from argument buffer. */
2249 CPP_ADJUST_WRITTEN (pfile, -1);
2256 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2257 pfile->no_macro_expand--;
2262 /* Turn newlines to spaces in the string of length LENGTH at START,
2263 except inside of string constants.
2264 The string is copied into itself with its beginning staying fixed. */
2271 register U_CHAR *ibp;
2272 register U_CHAR *obp;
2273 register U_CHAR *limit;
2277 limit = start + length;
2280 while (ibp < limit) {
2281 *obp++ = c = *ibp++;
2286 /* Notice and skip strings, so that we don't delete newlines in them. */
2289 while (ibp < limit) {
2290 *obp++ = c = *ibp++;
2293 if (c == '\n' && quotec == '\'')
2309 if (!pfile->timebuf) {
2310 time_t t = time ((time_t *)0);
2311 pfile->timebuf = localtime (&t);
2313 return pfile->timebuf;
2316 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2317 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2321 * expand things like __FILE__. Place the expansion into the output
2322 * buffer *without* rescanning.
2333 cpp_buffer *ip = NULL;
2336 int paren = 0; /* For special `defined' keyword */
2339 if (pcp_outfile && pcp_inside_if
2340 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2342 "Predefined macro `%s' used inside `#if' during precompilation",
2346 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2350 cpp_error (pfile, "cccp error: not in any file?!");
2351 return; /* the show must go on */
2353 if (ip->fname != NULL)
2363 if (hp->type == T_BASE_FILE)
2365 while (CPP_PREV_BUFFER (ip))
2366 ip = CPP_PREV_BUFFER (ip);
2368 string = ip->nominal_fname;
2372 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2373 quote_string (pfile, string);
2377 case T_INCLUDE_LEVEL:
2379 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2380 if (ip->fname != NULL)
2383 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2384 sprintf (buf, "%d", true_indepth - 1);
2388 buf = (char *) alloca (3 + strlen (version_string));
2389 sprintf (buf, "\"%s\"", version_string);
2392 #ifndef NO_BUILTIN_SIZE_TYPE
2398 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2399 case T_PTRDIFF_TYPE:
2405 buf = CPP_WCHAR_TYPE (pfile);
2408 case T_USER_LABEL_PREFIX_TYPE:
2409 buf = USER_LABEL_PREFIX;
2412 case T_REGISTER_PREFIX_TYPE:
2413 buf = REGISTER_PREFIX;
2417 buf = (char *) alloca (4 * sizeof (int));
2418 sprintf (buf, "%d", hp->value.ival);
2420 if (pcp_inside_if && pcp_outfile)
2421 /* Output a precondition for this macro use */
2422 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2428 long line = ip->lineno;
2429 long col = ip->colno;
2430 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2432 buf = (char *) alloca (10);
2433 sprintf (buf, "%d", line);
2439 buf = (char *) alloca (20);
2440 timebuf = timestamp (pfile);
2441 if (hp->type == T_DATE)
2442 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2443 timebuf->tm_mday, timebuf->tm_year + 1900);
2445 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2449 case T_SPEC_DEFINED:
2450 buf = " 0 "; /* Assume symbol is not defined */
2451 ip = CPP_BUFFER (pfile);
2452 SKIP_WHITE_SPACE (ip->cur);
2453 if (*ip->cur == '(')
2456 ip->cur++; /* Skip over the paren */
2457 SKIP_WHITE_SPACE (ip->cur);
2460 if (!is_idstart[*ip->cur])
2462 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2465 if (pcp_outfile && pcp_inside_if
2466 && (hp->type == T_CONST
2467 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2468 /* Output a precondition for this macro use. */
2469 fprintf (pcp_outfile, "#define %s\n", hp->name);
2475 if (pcp_outfile && pcp_inside_if)
2477 /* Output a precondition for this macro use */
2478 U_CHAR *cp = ip->bufp;
2479 fprintf (pcp_outfile, "#undef ");
2480 while (is_idchar[*cp]) /* Ick! */
2481 fputc (*cp++, pcp_outfile);
2482 putc ('\n', pcp_outfile);
2485 while (is_idchar[*ip->cur])
2487 SKIP_WHITE_SPACE (ip->cur);
2490 if (*ip->cur != ')')
2498 cpp_error (pfile, "`defined' without an identifier");
2502 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2506 CPP_RESERVE (pfile, len + 1);
2507 CPP_PUTS_Q (pfile, buf, len);
2508 CPP_NUL_TERMINATE_Q (pfile);
2513 /* Initialize the built-in macros. */
2516 initialize_builtins (
2519 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2520 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2521 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2522 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2523 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2524 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2525 #ifndef NO_BUILTIN_SIZE_TYPE
2526 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2528 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2529 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2531 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2532 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2533 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2534 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2535 if (!CPP_TRADITIONAL (pfile))
2536 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2537 if (CPP_OPTIONS (pfile)->objc)
2538 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2539 /* This is supplied using a -D by the compiler driver
2540 so that it is present only when truly compiling with GNU C. */
2541 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2543 if (CPP_OPTIONS (pfile)->debug_output)
2545 char directive[2048];
2546 register struct directive *dp = &directive_table[0];
2547 struct tm *timebuf = timestamp (pfile);
2548 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2550 while (CPP_PREV_BUFFER (pbuffer))
2551 pbuffer = CPP_PREV_BUFFER (pbuffer);
2552 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2553 pbuffer->nominal_fname);
2554 output_line_command (pfile, 0, same_file);
2555 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2557 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2558 output_line_command (pfile, 0, same_file);
2559 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2561 #ifndef NO_BUILTIN_SIZE_TYPE
2562 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2563 output_line_command (pfile, 0, same_file);
2564 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2567 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2568 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2569 output_line_command (pfile, 0, same_file);
2570 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2573 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2574 output_line_command (pfile, 0, same_file);
2575 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2577 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2578 monthnames[timebuf->tm_mon],
2579 timebuf->tm_mday, timebuf->tm_year + 1900);
2580 output_line_command (pfile, 0, same_file);
2581 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2583 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2584 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2585 output_line_command (pfile, 0, same_file);
2586 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2588 if (!CPP_TRADITIONAL (pfile))
2590 sprintf (directive, " __STDC__ 1");
2591 output_line_command (pfile, 0, same_file);
2592 pass_thru_directive (directive, &directive[strlen (directive)],
2595 if (CPP_OPTIONS (pfile)->objc)
2597 sprintf (directive, " __OBJC__ 1");
2598 output_line_command (pfile, 0, same_file);
2599 pass_thru_directive (directive, &directive[strlen (directive)],
2605 /* Return 1 iff a token ending in C1 followed directly by a token C2
2606 could cause mis-tokenization. */
2615 if (c2 == c1 || c2 == '=')
2619 case '0': case '1': case '2': case '3': case '4':
2620 case '5': case '6': case '7': case '8': case '9':
2622 if (c2 == '-' || c2 == '+')
2623 return 1; /* could extend a pre-processing number */
2626 if (c2 == '\'' || c2 == '\"')
2627 return 1; /* Could turn into L"xxx" or L'xxx'. */
2631 case 'a': case 'b': case 'c': case 'd': case 'f':
2632 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2633 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2634 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2636 case 'A': case 'B': case 'C': case 'D': case 'F':
2637 case 'G': case 'H': case 'I': case 'J': case 'K':
2638 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2639 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2641 /* We're in the middle of either a name or a pre-processing number. */
2642 return (is_idchar[c2] || c2 == '.');
2643 case '<': case '>': case '!': case '%': case '#': case ':':
2644 case '^': case '&': case '|': case '*': case '/': case '=':
2645 return (c2 == c1 || c2 == '=');
2650 /* Expand a macro call.
2651 HP points to the symbol that is the macro being called.
2652 Put the result of expansion onto the input stack
2653 so that subsequent input by our caller will use it.
2655 If macro wants arguments, caller has already verified that
2656 an argument list follows; arguments come from the input stack. */
2664 DEFINITION *defn = hp->value.defn;
2665 register U_CHAR *xbuf;
2666 long start_line, start_column;
2668 struct argdata *args;
2669 long old_written = CPP_WRITTEN (pfile);
2671 int start_line = instack[indepth].lineno;
2673 int rest_args, rest_zero;
2677 CHECK_DEPTH (return;);
2681 /* This macro is being used inside a #if, which means it must be */
2682 /* recorded as a precondition. */
2683 if (pcp_inside_if && pcp_outfile && defn->predefined)
2684 dump_single_macro (hp, pcp_outfile);
2687 pfile->output_escapes++;
2688 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2690 nargs = defn->nargs;
2694 enum cpp_token token;
2696 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2698 for (i = 0; i < nargs; i++)
2700 args[i].raw = args[i].expanded = 0;
2701 args[i].raw_length = 0;
2702 args[i].expand_length = args[i].stringified_length = -1;
2703 args[i].use_count = 0;
2706 /* Parse all the macro args that are supplied. I counts them.
2707 The first NARGS args are stored in ARGS.
2708 The rest are discarded. If rest_args is set then we assume
2709 macarg absorbed the rest of the args. */
2713 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2718 if (i < nargs || (nargs == 0 && i == 0))
2720 /* if we are working on last arg which absorbs rest of args... */
2721 if (i == nargs - 1 && defn->rest_args)
2723 args[i].raw = CPP_WRITTEN (pfile);
2724 token = macarg (pfile, rest_args);
2725 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2726 args[i].newlines = 0; /* FIXME */
2729 token = macarg (pfile, 0);
2730 if (token == CPP_EOF || token == CPP_POP)
2732 cpp_error_with_line (pfile, start_line, start_column,
2733 "unterminated macro call");
2737 } while (token == CPP_COMMA);
2739 /* If we got one arg but it was just whitespace, call that 0 args. */
2742 register U_CHAR *bp = ARG_BASE + args[0].raw;
2743 register U_CHAR *lim = bp + args[0].raw_length;
2744 /* cpp.texi says for foo ( ) we provide one argument.
2745 However, if foo wants just 0 arguments, treat this as 0. */
2747 while (bp != lim && is_space[*bp]) bp++;
2752 /* Don't output an error message if we have already output one for
2753 a parse error above. */
2755 if (nargs == 0 && i > 0)
2757 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2761 /* traditional C allows foo() if foo wants one argument. */
2762 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2764 /* the rest args token is allowed to absorb 0 tokens */
2765 else if (i == nargs - 1 && defn->rest_args)
2768 cpp_error (pfile, "macro `%s' used without args", hp->name);
2770 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2772 cpp_error (pfile, "macro `%s' used with only %d args",
2778 "macro `%s' used with too many (%d) args", hp->name, i);
2782 /* If macro wants zero args, we parsed the arglist for checking only.
2783 Read directly from the macro definition. */
2786 xbuf = defn->expansion;
2787 xbuf_len = defn->length;
2791 register U_CHAR *exp = defn->expansion;
2792 register int offset; /* offset in expansion,
2793 copied a piece at a time */
2794 register int totlen; /* total amount of exp buffer filled so far */
2796 register struct reflist *ap, *last_ap;
2798 /* Macro really takes args. Compute the expansion of this call. */
2800 /* Compute length in characters of the macro's expansion.
2801 Also count number of times each arg is used. */
2802 xbuf_len = defn->length;
2803 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2807 register struct argdata *arg = &args[ap->argno];
2808 /* Stringify it it hasn't already been */
2809 if (arg->stringified_length < 0)
2811 int arglen = arg->raw_length;
2815 /* Initially need_space is -1. Otherwise, 1 means the
2816 previous character was a space, but we suppressed it;
2817 0 means the previous character was a non-space. */
2818 int need_space = -1;
2820 arg->stringified = CPP_WRITTEN (pfile);
2821 if (!CPP_TRADITIONAL (pfile))
2822 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2823 for (; i < arglen; i++)
2825 c = (ARG_BASE + arg->raw)[i];
2829 /* Internal sequences of whitespace are replaced by
2830 one space except within an string or char token.*/
2833 if (CPP_WRITTEN (pfile) > arg->stringified
2834 && (CPP_PWRITTEN (pfile))[-1] == '@')
2836 /* "@ " escape markers are removed */
2837 CPP_ADJUST_WRITTEN (pfile, -1);
2840 if (need_space == 0)
2844 else if (need_space > 0)
2845 CPP_PUTC (pfile, ' ');
2860 else if (c == '\"' || c == '\'')
2864 /* Escape these chars */
2865 if (c == '\"' || (in_string && c == '\\'))
2866 CPP_PUTC (pfile, '\\');
2868 CPP_PUTC (pfile, c);
2871 CPP_RESERVE (pfile, 4);
2872 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2874 CPP_ADJUST_WRITTEN (pfile, 4);
2877 if (!CPP_TRADITIONAL (pfile))
2878 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2879 arg->stringified_length
2880 = CPP_WRITTEN (pfile) - arg->stringified;
2882 xbuf_len += args[ap->argno].stringified_length;
2884 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2885 /* Add 4 for two newline-space markers to prevent
2886 token concatenation. */
2887 xbuf_len += args[ap->argno].raw_length + 4;
2890 /* We have an ordinary (expanded) occurrence of the arg.
2891 So compute its expansion, if we have not already. */
2892 if (args[ap->argno].expand_length < 0)
2894 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2895 cpp_expand_to_buffer (pfile,
2896 ARG_BASE + args[ap->argno].raw,
2897 args[ap->argno].raw_length);
2899 args[ap->argno].expand_length
2900 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2903 /* Add 4 for two newline-space markers to prevent
2904 token concatenation. */
2905 xbuf_len += args[ap->argno].expand_length + 4;
2907 if (args[ap->argno].use_count < 10)
2908 args[ap->argno].use_count++;
2911 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2913 /* Generate in XBUF the complete expansion
2914 with arguments substituted in.
2915 TOTLEN is the total size generated so far.
2916 OFFSET is the index in the definition
2917 of where we are copying from. */
2918 offset = totlen = 0;
2919 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2920 last_ap = ap, ap = ap->next)
2922 register struct argdata *arg = &args[ap->argno];
2923 int count_before = totlen;
2925 /* Add chars to XBUF. */
2926 for (i = 0; i < ap->nchars; i++, offset++)
2927 xbuf[totlen++] = exp[offset];
2929 /* If followed by an empty rest arg with concatenation,
2930 delete the last run of nonwhite chars. */
2931 if (rest_zero && totlen > count_before
2932 && ((ap->rest_args && ap->raw_before)
2933 || (last_ap != NULL && last_ap->rest_args
2934 && last_ap->raw_after)))
2936 /* Delete final whitespace. */
2937 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2940 /* Delete the nonwhites before them. */
2941 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2945 if (ap->stringify != 0)
2947 bcopy (ARG_BASE + arg->stringified,
2948 xbuf + totlen, arg->stringified_length);
2949 totlen += arg->stringified_length;
2951 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2953 U_CHAR *p1 = ARG_BASE + arg->raw;
2954 U_CHAR *l1 = p1 + arg->raw_length;
2957 while (p1 != l1 && is_space[*p1]) p1++;
2958 while (p1 != l1 && is_idchar[*p1])
2959 xbuf[totlen++] = *p1++;
2963 /* Arg is concatenated after: delete trailing whitespace,
2964 whitespace markers, and no-reexpansion markers. */
2967 if (is_space[l1[-1]]) l1--;
2968 else if (l1[-1] == '-')
2970 U_CHAR *p2 = l1 - 1;
2971 /* If a `-' is preceded by an odd number of newlines then it
2972 and the last newline are a no-reexpansion marker. */
2973 while (p2 != p1 && p2[-1] == '\n') p2--;
2974 if ((l1 - 1 - p2) & 1) {
2983 bcopy (p1, xbuf + totlen, l1 - p1);
2988 U_CHAR *expanded = ARG_BASE + arg->expanded;
2989 if (!ap->raw_before && totlen > 0 && arg->expand_length
2990 && !CPP_TRADITIONAL(pfile)
2991 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2993 xbuf[totlen++] = '@';
2994 xbuf[totlen++] = ' ';
2997 bcopy (expanded, xbuf + totlen, arg->expand_length);
2998 totlen += arg->expand_length;
3000 if (!ap->raw_after && totlen > 0 && offset < defn->length
3001 && !CPP_TRADITIONAL(pfile)
3002 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3004 xbuf[totlen++] = '@';
3005 xbuf[totlen++] = ' ';
3008 /* If a macro argument with newlines is used multiple times,
3009 then only expand the newlines once. This avoids creating
3010 output lines which don't correspond to any input line,
3011 which confuses gdb and gcov. */
3012 if (arg->use_count > 1 && arg->newlines > 0)
3014 /* Don't bother doing change_newlines for subsequent
3018 = change_newlines (expanded, arg->expand_length);
3022 if (totlen > xbuf_len)
3026 /* if there is anything left of the definition
3027 after handling the arg list, copy that in too. */
3029 for (i = offset; i < defn->length; i++)
3031 /* if we've reached the end of the macro */
3034 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3035 && last_ap->raw_after))
3036 xbuf[totlen++] = exp[i];
3044 pfile->output_escapes--;
3046 /* Now put the expansion on the input stack
3047 so our caller will commence reading from it. */
3048 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3049 CPP_BUFFER (pfile)->has_escapes = 1;
3051 /* Pop the space we've used in the token_buffer for argument expansion. */
3052 CPP_SET_WRITTEN (pfile, old_written);
3054 /* Recursive macro use sometimes works traditionally.
3055 #define foo(x,y) bar (x (y,0), y)
3058 if (!CPP_TRADITIONAL (pfile))
3059 hp->type = T_DISABLED;
3063 push_macro_expansion (
3065 register U_CHAR *xbuf,
3069 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3070 mbuf->cleanup = macro_cleanup;
3073 /* The first chars of the expansion should be a "@ " added by
3074 collect_expansion. This is to prevent accidental token-pasting
3075 between the text preceding the macro invocation, and the macro
3078 We would like to avoid adding unneeded spaces (for the sake of
3079 tools that use cpp, such as imake). In some common cases we can
3080 tell that it is safe to omit the space.
3082 The character before the macro invocation cannot have been an
3083 idchar (or else it would have been pasted with the idchars of
3084 the macro name). Therefore, if the first non-space character
3085 of the expansion is an idchar, we do not need the extra space
3086 to prevent token pasting.
3088 Also, we don't need the extra space if the first char is '(',
3089 or some other (less common) characters. */
3091 if (xbuf[0] == '@' && xbuf[1] == ' '
3092 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3093 || xbuf[2] == '\"'))
3097 /* Like cpp_get_token, except that it does not read past end-of-line.
3098 Also, horizontal space is skipped, and macros are popped. */
3100 static enum cpp_token
3101 get_directive_token (
3106 long old_written = CPP_WRITTEN (pfile);
3107 enum cpp_token token;
3108 cpp_skip_hspace (pfile);
3109 if (PEEKC () == '\n')
3111 token = cpp_get_token (pfile);
3115 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3117 /* ... else fall though ... */
3118 case CPP_HSPACE: case CPP_COMMENT:
3119 CPP_SET_WRITTEN (pfile, old_written);
3127 /* Handle #include and #import.
3128 This function expects to see "fname" or <fname> on the input.
3130 The input is normally in part of the output_buffer following
3131 CPP_WRITTEN, and will get overwritten by output_line_command.
3132 I.e. in input file specification has been popped by handle_directive.
3138 struct directive *keyword,
3139 U_CHAR *unused1, U_CHAR *unused2)
3141 int importing = (keyword->type == T_IMPORT);
3142 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3143 char *fname; /* Dynamically allocated fname buffer */
3146 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3147 enum cpp_token token;
3149 /* Chain of dirs to search */
3150 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3151 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3152 struct file_name_list *searchptr = 0;
3153 long old_written = CPP_WRITTEN (pfile);
3157 int f; /* file number */
3159 int retried = 0; /* Have already tried macro
3160 expanding the include line*/
3161 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3166 f= -1; /* JF we iz paranoid! */
3168 if (importing && CPP_OPTIONS (pfile)->warn_import
3169 && !CPP_OPTIONS (pfile)->inhibit_warnings
3170 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3172 pfile->import_warning = 1;
3173 cpp_warning (pfile, "using `#import' is not recommended");
3174 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3175 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3176 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3177 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3178 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3179 fprintf (stderr, " ... <real contents of file> ...\n");
3180 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3181 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3182 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3183 fprintf (stderr, "when it is equipped with such a conditional.\n");
3186 pfile->parsing_include_directive++;
3187 token = get_directive_token (pfile);
3188 pfile->parsing_include_directive--;
3190 if (token == CPP_STRING)
3192 /* FIXME - check no trailing garbage */
3193 fbeg = pfile->token_buffer + old_written + 1;
3194 fend = CPP_PWRITTEN (pfile) - 1;
3195 if (fbeg[-1] == '<')
3198 /* If -I-, start with the first -I dir after the -I-. */
3199 if (CPP_OPTIONS (pfile)->first_bracket_include)
3200 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3202 /* If -I- was specified, don't search current dir, only spec'd ones. */
3203 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3206 /* We have "filename". Figure out directory this source
3207 file is coming from and put it on the front of the list. */
3209 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3214 if ((nam = fp->nominal_fname) != NULL)
3216 /* Found a named file. Figure out dir of the file,
3217 and put it in front of the search list. */
3218 dsp[0].next = search_start;
3221 ep = rindex (nam, '/');
3223 ep = rindex (nam, ']');
3224 if (ep == NULL) ep = rindex (nam, '>');
3225 if (ep == NULL) ep = rindex (nam, ':');
3226 if (ep != NULL) ep++;
3231 dsp[0].fname = (char *) alloca (n + 1);
3232 strncpy (dsp[0].fname, nam, n);
3233 dsp[0].fname[n] = '\0';
3234 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3235 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3239 dsp[0].fname = 0; /* Current directory */
3241 dsp[0].got_name_map = 0;
3248 else if (token == CPP_NAME)
3251 * Support '#include xyz' like VAX-C to allow for easy use of all the
3252 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3253 * code from case '<' is repeated here) and generates a warning.
3256 "VAX-C-style include specification found, use '#include <filename.h>' !");
3258 /* If -I-, start with the first -I dir after the -I-. */
3259 if (CPP_OPTIONS (pfile)->first_bracket_include)
3260 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3261 fbeg = pfile->token_buffer + old_written;
3262 fend = CPP_PWRITTEN (pfile);
3268 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3269 CPP_SET_WRITTEN (pfile, old_written);
3270 skip_rest_of_line (pfile);
3276 token = get_directive_token (pfile);
3277 if (token != CPP_VSPACE)
3279 cpp_error (pfile, "junk at end of `#include'");
3280 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3281 token = get_directive_token (pfile);
3284 /* For #include_next, skip in the search path
3285 past the dir in which the containing file was found. */
3289 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3290 if (fp->fname != NULL)
3292 /* fp->dir is null if the containing file was specified with
3293 an absolute file name. In that case, don't skip anything. */
3294 if (fp->dir == SELF_DIR_DUMMY)
3295 search_start = CPP_OPTIONS (pfile)->include;
3297 search_start = fp->dir->next;
3302 CPP_SET_WRITTEN (pfile, old_written);
3308 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3312 /* Allocate this permanently, because it gets stored in the definitions
3314 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3315 /* + 2 above for slash and terminating null. */
3316 /* + 2 added for '.h' on VMS (to support '#include filename') */
3318 /* If specified file name is absolute, just open it. */
3321 strncpy (fname, fbeg, flen);
3323 if (redundant_include_p (pfile, fname))
3326 f = lookup_import (pfile, fname, NULL_PTR);
3328 f = open_include_file (pfile, fname, NULL_PTR);
3330 return 0; /* Already included this file */
3332 /* Search directory path, trying to open the file.
3333 Copy each filename tried into FNAME. */
3335 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3336 if (searchptr->fname) {
3337 /* The empty string in a search path is ignored.
3338 This makes it possible to turn off entirely
3339 a standard piece of the list. */
3340 if (searchptr->fname[0] == 0)
3342 strcpy (fname, searchptr->fname);
3343 strcat (fname, "/");
3344 fname[strlen (fname) + flen] = 0;
3348 strncat (fname, fbeg, flen);
3350 /* Change this 1/2 Unix 1/2 VMS file specification into a
3351 full VMS file specification */
3352 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3353 /* Fix up the filename */
3354 hack_vms_include_specification (fname);
3356 /* This is a normal VMS filespec, so use it unchanged. */
3357 strncpy (fname, fbeg, flen);
3359 /* if it's '#include filename', add the missing .h */
3360 if (index(fname,'.')==NULL) {
3361 strcat (fname, ".h");
3365 /* ??? There are currently 3 separate mechanisms for avoiding processing
3366 of redundant include files: #import, #pragma once, and
3367 redundant_include_p. It would be nice if they were unified. */
3368 if (redundant_include_p (pfile, fname))
3371 f = lookup_import (pfile, fname, searchptr);
3373 f = open_include_file (pfile, fname, searchptr);
3375 return 0; /* Already included this file */
3377 else if (f == -1 && errno == EACCES)
3378 cpp_warning (pfile, "Header file %s exists, but is not readable",
3388 /* A file that was not found. */
3389 strncpy (fname, fbeg, flen);
3391 /* If generating dependencies and -MG was specified, we assume missing
3392 files are leaf files, living in the same directory as the source file
3393 or other similar place; these missing files may be generated from
3394 other files and may not exist yet (eg: y.tab.h). */
3396 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3397 && CPP_PRINT_DEPS (pfile)
3398 > (angle_brackets || (pfile->system_include_depth > 0)))
3400 /* If it was requested as a system header file,
3401 then assume it belongs in the first place to look for such. */
3404 for (searchptr = search_start; searchptr;
3405 searchptr = searchptr->next)
3407 if (searchptr->fname)
3411 if (searchptr->fname[0] == 0)
3413 p = (char *) alloca (strlen (searchptr->fname)
3414 + strlen (fname) + 2);
3415 strcpy (p, searchptr->fname);
3418 deps_output (pfile, p, ' ');
3425 /* Otherwise, omit the directory, as if the file existed
3426 in the directory with the source. */
3427 deps_output (pfile, fname, ' ');
3430 /* If -M was specified, and this header file won't be added to the
3431 dependency list, then don't count this as an error, because we can
3432 still produce correct output. Otherwise, we can't produce correct
3433 output, because there may be dependencies we need inside the missing
3434 file, and we don't know what directory this missing file exists in.*/
3435 else if (CPP_PRINT_DEPS (pfile)
3436 && (CPP_PRINT_DEPS (pfile)
3437 <= (angle_brackets || (pfile->system_include_depth > 0))))
3438 cpp_warning (pfile, "No include path in which to find %s", fname);
3439 else if (search_start)
3440 cpp_error_from_errno (pfile, fname);
3442 cpp_error (pfile, "No include path in which to find %s", fname);
3445 /* Check to see if this include file is a once-only include file.
3448 struct file_name_list* ptr;
3450 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3451 if (!strcmp (ptr->fname, fname)) {
3453 return 0; /* This file was once'd. */
3457 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3458 if (!strcmp (ptr->fname, fname))
3459 break; /* This file was included before. */
3463 /* This is the first time for this file. */
3464 /* Add it to list of files included. */
3466 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3467 ptr->control_macro = 0;
3468 ptr->c_system_include_path = 0;
3469 ptr->next = pfile->all_include_files;
3470 pfile->all_include_files = ptr;
3471 ptr->fname = savestring (fname);
3472 ptr->got_name_map = 0;
3474 /* For -M, add this file to the dependencies. */
3475 if (CPP_PRINT_DEPS (pfile)
3476 > (angle_brackets || (pfile->system_include_depth > 0)))
3477 deps_output (pfile, fname, ' ');
3480 /* Handle -H option. */
3481 if (CPP_OPTIONS(pfile)->print_include_names)
3483 cpp_buffer *buf = CPP_BUFFER (pfile);
3484 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3486 fprintf (stderr, "%s\n", fname);
3490 pfile->system_include_depth++;
3492 /* Actually process the file. */
3494 /* Record file on "seen" list for #import. */
3495 add_import (pfile, f, fname);
3497 pcftry = (char *) alloca (strlen (fname) + 30);
3509 sprintf (pcftry, "%s%d", fname, pcfnum++);
3511 pcf = open (pcftry, O_RDONLY, 0666);
3517 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3519 || stat_f.st_dev != s.st_dev)
3521 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3522 /* Don't need it any more. */
3527 /* Don't need it at all. */
3532 } while (pcf != -1 && !pcfbuf);
3536 /* Actually process the file */
3537 cpp_push_buffer (pfile, NULL, 0);
3538 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3539 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3541 output_line_command (pfile, 0, enter_file);
3542 pfile->only_seen_white = 2;
3546 pfile->system_include_depth--;
3551 /* Return nonzero if there is no need to include file NAME
3552 because it has already been included and it contains a conditional
3553 to make a repeated include do nothing. */
3556 redundant_include_p (
3560 struct file_name_list *l = pfile->all_include_files;
3561 for (; l; l = l->next)
3562 if (! strcmp (name, l->fname)
3564 && cpp_lookup (pfile, l->control_macro, -1, -1))
3569 /* Return nonzero if the given FILENAME is an absolute pathname which
3570 designates a file within one of the known "system" include file
3571 directories. We assume here that if the given FILENAME looks like
3572 it is the name of a file which resides either directly in a "system"
3573 include file directory, or within any subdirectory thereof, then the
3574 given file must be a "system" include file. This function tells us
3575 if we should suppress pedantic errors/warnings for the given FILENAME.
3577 The value is 2 if the file is a C-language system header file
3578 for which C++ should (on most systems) assume `extern "C"'. */
3583 register char *filename)
3585 struct file_name_list *searchptr;
3587 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3588 searchptr = searchptr->next)
3589 if (searchptr->fname) {
3590 register char *sys_dir = searchptr->fname;
3591 register unsigned length = strlen (sys_dir);
3593 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3595 if (searchptr->c_system_include_path)
3606 * Install a name in the assertion hash table.
3608 * If LEN is >= 0, it is the length of the name.
3609 * Otherwise, compute the length by scanning the entire name.
3611 * If HASH is >= 0, it is the precomputed hash code.
3612 * Otherwise, compute the hash code.
3614 static ASSERTION_HASHNODE *
3621 register ASSERTION_HASHNODE *hp;
3622 register int i, bucket;
3623 register U_CHAR *p, *q;
3625 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3626 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3628 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3629 hp->next = pfile->assertion_hashtab[bucket];
3630 pfile->assertion_hashtab[bucket] = hp;
3632 if (hp->next != NULL)
3633 hp->next->prev = hp;
3636 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3639 for (i = 0; i < len; i++)
3645 * find the most recent hash node for name name (ending with first
3646 * non-identifier char) installed by install
3648 * If LEN is >= 0, it is the length of the name.
3649 * Otherwise, compute the length by scanning the entire name.
3651 * If HASH is >= 0, it is the precomputed hash code.
3652 * Otherwise, compute the hash code.
3655 static ASSERTION_HASHNODE *
3662 register ASSERTION_HASHNODE *bucket;
3664 bucket = pfile->assertion_hashtab[hash];
3666 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3668 bucket = bucket->next;
3675 ASSERTION_HASHNODE *hp)
3677 struct tokenlist_list *tail;
3678 if (hp->prev != NULL)
3679 hp->prev->next = hp->next;
3680 if (hp->next != NULL)
3681 hp->next->prev = hp->prev;
3683 for (tail = hp->value; tail; )
3685 struct tokenlist_list *next = tail->next;
3686 free_token_list (tail->tokens);
3691 /* make sure that the bucket chain header that
3692 the deleted guy was on points to the right thing afterwards. */
3693 if (hp == *hp->bucket_hdr)
3694 *hp->bucket_hdr = hp->next;
3699 /* Convert a character string literal into a nul-terminated string.
3700 The input string is [IN ... LIMIT).
3701 The result is placed in RESULT. RESULT can be the same as IN.
3702 The value returned in the end of the string written to RESULT,
3703 or NULL on error. */
3708 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3728 char *bpc = (char *) in;
3729 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3730 in = (U_CHAR *) bpc;
3732 *result++ = (U_CHAR)c;
3735 /* else fall through */
3745 * interpret #line command. Remembers previously seen fnames
3746 * in its very own hash table.
3748 #define FNAME_HASHSIZE 37
3753 struct directive *keyword)
3755 cpp_buffer *ip = CPP_BUFFER (pfile);
3757 long old_written = CPP_WRITTEN (pfile);
3758 enum file_change_code file_change = same_file;
3759 enum cpp_token token;
3762 token = get_directive_token (pfile);
3764 if (token != CPP_NUMBER
3765 || !isdigit(pfile->token_buffer[old_written]))
3767 cpp_error (pfile, "invalid format `#line' command");
3768 goto bad_line_directive;
3771 /* The Newline at the end of this line remains to be processed.
3772 To put the next line at the specified line number,
3773 we must store a line number now that is one less. */
3774 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3775 CPP_SET_WRITTEN (pfile, old_written);
3777 /* NEW_LINENO is one less than the actual line number here. */
3778 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3779 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3781 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3782 if (PEEKC() && !is_space[PEEKC()]) {
3783 cpp_error (pfile, "invalid format `#line' command");
3784 goto bad_line_directive;
3788 token = get_directive_token (pfile);
3790 if (token == CPP_STRING) {
3791 U_CHAR *fname = pfile->token_buffer + old_written;
3793 static HASHNODE *fname_table[FNAME_HASHSIZE];
3794 HASHNODE *hp, **hash_bucket;
3799 /* Turn the file name, which is a character string literal,
3800 into a null-terminated string. Do this in place. */
3801 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3802 if (end_name == NULL)
3804 cpp_error (pfile, "invalid format `#line' command");
3805 goto bad_line_directive;
3808 fname_length = end_name - fname;
3810 num_start = CPP_WRITTEN (pfile);
3811 token = get_directive_token (pfile);
3812 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3813 p = pfile->token_buffer + num_start;
3814 if (CPP_PEDANTIC (pfile))
3815 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3817 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3819 cpp_error (pfile, "invalid format `#line' command");
3820 goto bad_line_directive;
3823 file_change = enter_file;
3825 file_change = leave_file;
3827 ip->system_header_p = 1;
3828 else /* if (*p == 4) */
3829 ip->system_header_p = 2;
3831 CPP_SET_WRITTEN (pfile, num_start);
3832 token = get_directive_token (pfile);
3833 p = pfile->token_buffer + num_start;
3834 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3835 ip->system_header_p = *p == 3 ? 1 : 2;
3836 token = get_directive_token (pfile);
3838 if (token != CPP_VSPACE) {
3839 cpp_error (pfile, "invalid format `#line' command");
3840 goto bad_line_directive;
3845 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3846 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3847 if (hp->length == fname_length &&
3848 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3849 ip->nominal_fname = hp->value.cpval;
3853 /* Didn't find it; cons up a new one. */
3854 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3855 hp->next = *hash_bucket;
3858 hp->length = fname_length;
3859 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3860 bcopy (fname, hp->value.cpval, fname_length);
3863 else if (token != CPP_VSPACE && token != CPP_EOF) {
3864 cpp_error (pfile, "invalid format `#line' command");
3865 goto bad_line_directive;
3868 ip->lineno = new_lineno;
3870 skip_rest_of_line (pfile);
3871 CPP_SET_WRITTEN (pfile, old_written);
3872 output_line_command (pfile, 0, file_change);
3877 * remove the definition of a symbol from the symbol table.
3878 * according to un*x /lib/cpp, it is not an error to undef
3879 * something that has no definitions, so it isn't one here either.
3885 struct directive *keyword,
3886 U_CHAR *buf, U_CHAR *limit)
3890 U_CHAR *orig_buf = buf;
3893 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3894 if (pcp_outfile && keyword)
3895 pass_thru_directive (buf, limit, pfile, keyword);
3898 SKIP_WHITE_SPACE (buf);
3899 sym_length = check_macro_name (pfile, buf, "macro");
3901 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3903 /* If we are generating additional info for debugging (with -g) we
3904 need to pass through all effective #undef commands. */
3905 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3906 pass_thru_directive (orig_buf, limit, pfile, keyword);
3907 if (hp->type != T_MACRO)
3908 cpp_warning (pfile, "undefining `%s'", hp->name);
3912 if (CPP_PEDANTIC (pfile)) {
3914 SKIP_WHITE_SPACE (buf);
3916 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3922 * Report an error detected by the program we are processing.
3923 * Use the text of the line in the error message.
3924 * (We use error because it prints the filename & line#.)
3930 struct directive *keyword,
3931 U_CHAR *buf,U_CHAR *limit)
3933 int length = limit - buf;
3934 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3935 bcopy (buf, copy, length);
3937 SKIP_WHITE_SPACE (copy);
3938 cpp_error (pfile, "#error %s", copy);
3943 * Report a warning detected by the program we are processing.
3944 * Use the text of the line in the warning message, then continue.
3945 * (We use error because it prints the filename & line#.)
3951 struct directive *keyword,
3952 U_CHAR *buf,U_CHAR *limit)
3954 int length = limit - buf;
3955 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3956 bcopy (buf, copy, length);
3958 SKIP_WHITE_SPACE (copy);
3959 cpp_warning (pfile, "#warning %s", copy);
3963 /* Remember the name of the current file being read from so that we can
3964 avoid ever including it again. */
3970 cpp_buffer *ip = NULL;
3971 struct file_name_list *new;
3973 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3977 if (ip->fname != NULL)
3982 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3983 new->next = pfile->dont_repeat_files;
3984 pfile->dont_repeat_files = new;
3985 new->fname = savestring (ip->fname);
3986 new->control_macro = 0;
3987 new->got_name_map = 0;
3988 new->c_system_include_path = 0;
3993 /* #ident has already been copied to the output file, so just ignore it. */
3998 struct directive *keyword,
3999 U_CHAR *buf, U_CHAR *limit)
4001 /* long old_written = CPP_WRITTEN (pfile);*/
4004 /* Allow #ident in system headers, since that's not user's fault. */
4005 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
4006 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
4008 /* Leave rest of line to be read by later calls to cpp_get_token. */
4013 /* #pragma and its argument line have already been copied to the output file.
4014 Just check for some recognized pragmas that need validation here. */
4019 struct directive *keyword,
4020 U_CHAR *buf,U_CHAR *limit)
4022 while (*buf == ' ' || *buf == '\t')
4024 if (!strncmp (buf, "once", 4)) {
4025 /* Allow #pragma once in system headers, since that's not the user's
4027 if (!CPP_BUFFER (pfile)->system_header_p)
4028 cpp_warning (pfile, "`#pragma once' is obsolete");
4032 if (!strncmp (buf, "implementation", 14)) {
4033 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4034 been included yet. */
4035 struct file_name_list *ptr;
4036 U_CHAR *p = buf + 14, *fname, *inc_fname;
4038 SKIP_WHITE_SPACE (p);
4039 if (*p == '\n' || *p != '\"')
4043 p = (U_CHAR *) index (fname, '\"');
4044 fname_len = p != NULL ? p - fname : strlen (fname);
4046 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4047 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4048 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4049 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4051 "`#pragma implementation' for `%s' appears after file is included",
4060 /* This was a fun hack, but #pragma seems to start to be useful.
4061 By failing to recognize it, we pass it through unchanged to cc1. */
4064 * the behavior of the #pragma directive is implementation defined.
4065 * this implementation defines it as follows.
4072 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4075 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4077 execl ("/usr/games/hack", "#pragma", 0);
4078 execl ("/usr/games/rogue", "#pragma", 0);
4079 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4080 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4082 fatal ("You are in a maze of twisty compiler features, all different");
4086 /* Just ignore #sccs, on systems where we define it at all. */
4091 struct directive *keyword,
4092 U_CHAR *buf,U_CHAR *limit)
4094 if (CPP_PEDANTIC (pfile))
4095 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4100 * handle #if command by
4101 * 1) inserting special `defined' keyword into the hash table
4102 * that gets turned into 0 or 1 by special_symbol (thus,
4103 * if the luser has a symbol called `defined' already, it won't
4104 * work inside the #if command)
4105 * 2) rescan the input into a temporary output buffer
4106 * 3) pass the output buffer to the yacc parser and collect a value
4107 * 4) clean up the mess left from steps 1 and 2.
4108 * 5) call conditional_skip to skip til the next #endif (etc.),
4109 * or not, depending on the value from step 3.
4115 struct directive *keyword,
4116 U_CHAR *buf,U_CHAR *limit)
4118 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4119 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4124 * handle a #elif directive by not changing if_stack either.
4125 * see the comment above do_else.
4131 struct directive *keyword,
4132 U_CHAR *buf,U_CHAR *limit)
4134 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4135 cpp_error (pfile, "`#elif' not within a conditional");
4138 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4139 cpp_error (pfile, "`#elif' after `#else'");
4141 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4143 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4144 && strcmp (pfile->if_stack->fname,
4145 CPP_BUFFER (pfile)->nominal_fname) != 0)
4146 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4147 fprintf (stderr, ")\n");
4149 pfile->if_stack->type = T_ELIF;
4152 if (pfile->if_stack->if_succeeded)
4153 skip_if_group (pfile, 0);
4155 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4157 skip_if_group (pfile, 0);
4159 ++pfile->if_stack->if_succeeded; /* continue processing input */
4160 output_line_command (pfile, 1, same_file);
4167 * evaluate a #if expression in BUF, of length LENGTH,
4168 * then parse the result as a C expression and return the value as an int.
4170 static HOST_WIDE_INT
4171 eval_if_expression (
4176 HASHNODE *save_defined;
4177 HOST_WIDE_INT value;
4178 long old_written = CPP_WRITTEN (pfile);
4180 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4181 pfile->pcp_inside_if = 1;
4183 value = cpp_parse_expr (pfile);
4184 pfile->pcp_inside_if = 0;
4185 delete_macro (save_defined); /* clean up special symbol */
4187 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4193 * routine to handle ifdef/ifndef. Try to look up the symbol,
4194 * then do or don't skip to the #endif/#else/#elif depending
4195 * on what directive is actually being processed.
4201 struct directive *keyword,
4202 U_CHAR *unused1, U_CHAR *unused2)
4205 cpp_buffer *ip = CPP_BUFFER (pfile);
4208 enum cpp_token token;
4209 int start_of_file = 0;
4210 U_CHAR *control_macro = 0;
4211 int old_written = CPP_WRITTEN (pfile);
4213 /* Detect a #ifndef at start of file (not counting comments). */
4214 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4215 start_of_file = pfile->only_seen_white == 2;
4217 pfile->no_macro_expand++;
4218 token = get_directive_token (pfile);
4219 pfile->no_macro_expand--;
4221 ident = pfile->token_buffer + old_written;
4222 ident_length = CPP_WRITTEN (pfile) - old_written;
4223 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4225 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4227 skip = (keyword->type == T_IFDEF);
4228 if (! CPP_TRADITIONAL (pfile))
4229 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4231 else if (token == CPP_NAME)
4233 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4234 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4235 if (start_of_file && !skip)
4237 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4238 bcopy (ident, control_macro, ident_length + 1);
4243 skip = (keyword->type == T_IFDEF);
4244 if (! CPP_TRADITIONAL (pfile))
4245 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4248 if (!CPP_TRADITIONAL (pfile))
4250 cpp_skip_hspace (pfile);
4252 if (c != EOF && c != '\n')
4253 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4255 skip_rest_of_line (pfile);
4259 /* Output a precondition for this macro. */
4260 if (hp && hp->value.defn->predefined)
4261 fprintf (pcp_outfile, "#define %s\n", hp->name);
4264 fprintf (pcp_outfile, "#undef ");
4265 while (is_idchar[*cp]) /* Ick! */
4266 fputc (*cp++, pcp_outfile);
4267 putc ('\n', pcp_outfile);
4271 conditional_skip (pfile, skip, T_IF, control_macro);
4275 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4276 If this is a #ifndef starting at the beginning of a file,
4277 CONTROL_MACRO is the macro name tested by the #ifndef.
4278 Otherwise, CONTROL_MACRO is 0. */
4284 enum node_type type,
4285 U_CHAR *control_macro)
4287 IF_STACK_FRAME *temp;
4289 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4290 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4292 temp->lineno = CPP_BUFFER (pfile)->lineno;
4294 temp->next = pfile->if_stack;
4295 temp->control_macro = control_macro;
4296 pfile->if_stack = temp;
4298 pfile->if_stack->type = type;
4301 skip_if_group (pfile, 0);
4304 ++pfile->if_stack->if_succeeded;
4305 output_line_command (pfile, 1, same_file);
4310 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4311 * leaves input ptr at the sharp sign found.
4312 * If ANY is nonzero, return at next directive of any sort.
4320 int at_beg_of_line = 1;
4321 struct directive *kt;
4322 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4324 U_CHAR *beg_of_line = bp;
4326 register int ident_length;
4327 U_CHAR *ident, *after_ident;
4328 struct parse_marker line_start_mark;
4330 parse_set_mark (&line_start_mark, pfile);
4332 if (CPP_OPTIONS (pfile)->output_conditionals) {
4333 static char failed[] = "#failed\n";
4334 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4336 output_line_command (pfile, 1, same_file);
4340 if (CPP_OPTIONS (pfile)->output_conditionals)
4342 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4343 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4344 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4346 parse_move_mark (&line_start_mark, pfile);
4347 if (!CPP_TRADITIONAL (pfile))
4348 cpp_skip_hspace (pfile);
4352 int old_written = CPP_WRITTEN (pfile);
4353 cpp_skip_hspace (pfile);
4355 parse_name (pfile, GETC());
4356 ident_length = CPP_WRITTEN (pfile) - old_written;
4357 ident = pfile->token_buffer + old_written;
4358 pfile->limit = ident;
4360 if (ident_length == 0)
4361 goto not_a_directive;
4363 /* Handle # followed by a line number. */
4365 /* Avoid error for `###' and similar cases unless -pedantic. */
4368 for (kt = directive_table; kt->length >= 0; kt++)
4370 IF_STACK_FRAME *temp;
4371 if (ident_length == kt->length
4372 && strncmp (ident, kt->name, kt->length) == 0)
4374 /* If we are asked to return on next directive, do so now. */
4384 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4385 temp->next = pfile->if_stack;
4386 pfile->if_stack = temp;
4388 temp->lineno = CPP_BUFFER(pfile)->lineno;
4390 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4391 temp->type = kt->type;
4395 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4396 validate_else (pfile,
4397 kt->type == T_ELSE ? "#else" : "#endif");
4399 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4402 "`#%s' not within a conditional", kt->name);
4405 else if (pfile->if_stack == save_if_stack)
4406 goto done; /* found what we came for */
4408 if (kt->type != T_ENDIF)
4410 if (pfile->if_stack->type == T_ELSE)
4411 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4412 pfile->if_stack->type = kt->type;
4416 temp = pfile->if_stack;
4417 pfile->if_stack = temp->next;
4424 /* Don't let erroneous code go by. */
4425 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4426 && CPP_PEDANTIC (pfile))
4427 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4431 /* We're in the middle of a line. Skip the rest of it. */
4438 case '/': /* possible comment */
4439 c = skip_comment (pfile, NULL);
4446 old = CPP_WRITTEN (pfile);
4447 cpp_get_token (pfile);
4448 CPP_SET_WRITTEN (pfile, old);
4451 /* Char after backslash loses its special meaning. */
4452 if (PEEKC() == '\n')
4462 if (CPP_OPTIONS (pfile)->output_conditionals) {
4463 static char end_failed[] = "#endfailed\n";
4464 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4467 pfile->only_seen_white = 1;
4468 parse_goto_mark (&line_start_mark, pfile);
4469 parse_clear_mark (&line_start_mark);
4473 * handle a #else directive. Do this by just continuing processing
4474 * without changing if_stack ; this is so that the error message
4475 * for missing #endif's etc. will point to the original #if. It
4476 * is possible that something different would be better.
4482 struct directive *keyword,
4483 U_CHAR *buf, U_CHAR *limit)
4485 cpp_buffer *ip = CPP_BUFFER (pfile);
4487 if (CPP_PEDANTIC (pfile))
4488 validate_else (pfile, "#else");
4489 skip_rest_of_line (pfile);
4491 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4492 cpp_error (pfile, "`#else' not within a conditional");
4495 /* #ifndef can't have its special treatment for containing the whole file
4496 if it has a #else clause. */
4497 pfile->if_stack->control_macro = 0;
4499 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4500 cpp_error (pfile, "`#else' after `#else'");
4501 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4502 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4503 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4504 fprintf (stderr, ")\n");
4506 pfile->if_stack->type = T_ELSE;
4509 if (pfile->if_stack->if_succeeded)
4510 skip_if_group (pfile, 0);
4512 ++pfile->if_stack->if_succeeded; /* continue processing input */
4513 output_line_command (pfile, 1, same_file);
4519 * unstack after #endif command
4525 struct directive *keyword,
4526 U_CHAR *buf, U_CHAR *limit)
4528 if (CPP_PEDANTIC (pfile))
4529 validate_else (pfile, "#endif");
4530 skip_rest_of_line (pfile);
4532 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4533 cpp_error (pfile, "unbalanced `#endif'");
4536 IF_STACK_FRAME *temp = pfile->if_stack;
4537 pfile->if_stack = temp->next;
4538 if (temp->control_macro != 0)
4540 /* This #endif matched a #ifndef at the start of the file.
4541 See if it is at the end of the file. */
4542 struct parse_marker start_mark;
4545 parse_set_mark (&start_mark, pfile);
4549 cpp_skip_hspace (pfile);
4554 parse_goto_mark (&start_mark, pfile);
4555 parse_clear_mark (&start_mark);
4559 /* If we get here, this #endif ends a #ifndef
4560 that contains all of the file (aside from whitespace).
4561 Arrange not to include the file again
4562 if the macro that was tested is defined.
4564 Do not do this for the top-level file in a -include or any
4565 file in a -imacros. */
4569 && ! (indepth == 1 && pfile->no_record_file)
4570 && ! (pfile->no_record_file && no_output))
4573 struct file_name_list *ifile = pfile->all_include_files;
4575 for ( ; ifile != NULL; ifile = ifile->next)
4577 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4579 ifile->control_macro = temp->control_macro;
4587 output_line_command (pfile, 1, same_file);
4592 /* When an #else or #endif is found while skipping failed conditional,
4593 if -pedantic was specified, this is called to warn about text after
4594 the command name. P points to the first char after the command name. */
4602 cpp_skip_hspace (pfile);
4604 if (c != EOF && c != '\n')
4606 "text following `%s' violates ANSI standard", directive);
4609 /* Get the next token, and add it to the text in pfile->token_buffer.
4610 Return the kind of token we got. */
4617 register int c, c2, c3;
4619 long start_line, start_column;
4620 enum cpp_token token;
4621 struct cpp_options *opts = CPP_OPTIONS (pfile);
4622 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4628 if (CPP_BUFFER (pfile)->seen_eof)
4630 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4637 cpp_buffer *next_buf
4638 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4639 CPP_BUFFER (pfile)->seen_eof = 1;
4640 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4642 /* We're about to return from an #include file.
4643 Emit #line information now (as part of the CPP_POP) result.
4644 But the #line refers to the file we will pop to. */
4645 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4646 CPP_BUFFER (pfile) = next_buf;
4647 pfile->input_stack_listing_current = 0;
4648 output_line_command (pfile, 0, leave_file);
4649 CPP_BUFFER (pfile) = cur_buffer;
4659 struct parse_marker start_mark;
4661 if (PEEKC () == '=')
4663 if (opts->put_out_comments)
4664 parse_set_mark (&start_mark, pfile);
4666 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4667 &start_line, &start_column);
4668 c = skip_comment (pfile, &newlines);
4669 if (opts->put_out_comments && (c == '/' || c == EOF))
4670 parse_clear_mark (&start_mark);
4675 cpp_error_with_line (pfile, start_line, start_column,
4676 "unterminated comment");
4679 c = '/'; /* Initial letter of comment. */
4681 /* Comments are equivalent to spaces.
4682 For -traditional, a comment is equivalent to nothing. */
4683 if (opts->put_out_comments)
4685 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4687 U_CHAR *start = pbuf->buf + start_mark.position;
4688 int len = pbuf->cur - start;
4689 CPP_RESERVE(pfile, 1 + len);
4690 CPP_PUTC_Q (pfile, c);
4691 CPP_PUTS_Q (pfile, start, len);
4692 pfile->lineno += newlines;
4693 parse_clear_mark (&start_mark);
4696 else if (CPP_TRADITIONAL (pfile))
4703 /* This may not work if cpp_get_token is called recursively,
4704 since many places look for horizontal space. */
4707 /* Copy the newlines into the output buffer, in order to
4708 avoid the pain of a #line every time a multiline comment
4710 CPP_RESERVE(pfile, newlines);
4711 while (--newlines >= 0)
4713 CPP_PUTC_Q (pfile, '\n');
4719 CPP_RESERVE(pfile, 1);
4720 CPP_PUTC_Q (pfile, ' ');
4724 if (opts->for_lint) {
4727 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4729 if (lintcmd != NULL) {
4730 /* I believe it is always safe to emit this newline: */
4732 bcopy ("#pragma lint ", (char *) obp, 13);
4734 bcopy (lintcmd, (char *) obp, cmdlen);
4739 bcopy (argbp, (char *) obp, arglen);
4743 /* OK, now bring us back to the state we were in before we entered
4744 this branch. We need #line b/c the newline for the pragma
4745 could fuck things up. */
4746 output_line_command (pfile, 0, same_file);
4747 *(obp++) = ' '; /* just in case, if comments are copied thru */
4755 /* If this is expanding a macro definition, don't recognize
4756 preprocessor directives. */
4759 /* If this is expand_into_temp_buffer, recognize them
4760 only after an actual newline at this level,
4761 not at the beginning of the input level. */
4762 if (ip->fname == 0 && beg_of_line == ip->buf)
4768 if (!pfile->only_seen_white)
4770 if (handle_directive (pfile))
4771 return CPP_DIRECTIVE;
4772 pfile->only_seen_white = 0;
4777 /* A single quoted string is treated like a double -- some
4778 programs (e.g., troff) are perverse this way */
4779 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4780 &start_line, &start_column);
4781 old_written = CPP_WRITTEN (pfile);
4783 CPP_PUTC (pfile, c);
4789 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4791 /* try harder: this string crosses a macro expansion
4792 boundary. This can happen naturally if -traditional.
4793 Otherwise, only -D can make a macro with an unmatched
4795 cpp_buffer *next_buf
4796 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4797 (*CPP_BUFFER (pfile)->cleanup)
4798 (CPP_BUFFER (pfile), pfile);
4799 CPP_BUFFER (pfile) = next_buf;
4802 if (!CPP_TRADITIONAL (pfile))
4804 cpp_error_with_line (pfile, start_line, start_column,
4805 "unterminated string or character constant");
4806 if (pfile->multiline_string_line != start_line
4807 && pfile->multiline_string_line != 0)
4808 cpp_error_with_line (pfile,
4809 pfile->multiline_string_line, -1,
4810 "possible real start of unterminated constant");
4811 pfile->multiline_string_line = 0;
4815 CPP_PUTC (pfile, cc);
4819 /* Traditionally, end of line ends a string constant with
4820 no error. So exit the loop and record the new line. */
4821 if (CPP_TRADITIONAL (pfile))
4825 cpp_error_with_line (pfile, start_line, start_column,
4826 "unterminated character constant");
4829 if (CPP_PEDANTIC (pfile)
4830 && pfile->multiline_string_line == 0)
4832 cpp_pedwarn_with_line (pfile, start_line, start_column,
4833 "string constant runs past end of line");
4835 if (pfile->multiline_string_line == 0)
4836 pfile->multiline_string_line = start_line;
4843 /* Backslash newline is replaced by nothing at all. */
4844 CPP_ADJUST_WRITTEN (pfile, -1);
4849 /* ANSI stupidly requires that in \\ the second \
4850 is *not* prevented from combining with a newline. */
4853 CPP_PUTC (pfile, cc);
4865 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4866 CPP_PWRITTEN (pfile));
4867 pfile->only_seen_white = 0;
4868 return c == '\'' ? CPP_CHAR : CPP_STRING;
4871 if (!opts->dollars_in_ident)
4876 if (opts->cplusplus && PEEKC () == ':')
4885 if (c2 == c || c2 == '=')
4895 if (PEEKC () == '=')
4902 if (c2 == '-' && opts->chill)
4904 /* Chill style comment */
4905 if (opts->put_out_comments)
4906 parse_set_mark (&start_mark, pfile);
4907 FORWARD(1); /* Skip second '-'. */
4915 /* Don't consider final '\n' to be part of comment. */
4921 goto return_comment;
4923 if (c2 == '-' || c2 == '=' || c2 == '>')
4928 if (pfile->parsing_include_directive)
4932 CPP_PUTC (pfile, c);
4937 if (c == '\n' || c == EOF)
4940 "missing '>' in `#include <FILENAME>'");
4946 /* else fall through */
4955 CPP_RESERVE (pfile, 4);
4956 CPP_PUTC (pfile, c);
4957 CPP_PUTC (pfile, c2);
4961 CPP_PUTC_Q (pfile, GETC ());
4962 CPP_NUL_TERMINATE_Q (pfile);
4963 pfile->only_seen_white = 0;
4967 if (CPP_BUFFER (pfile)->has_escapes)
4972 if (pfile->output_escapes)
4973 CPP_PUTS (pfile, "@-", 2);
4974 parse_name (pfile, GETC ());
4977 else if (is_space [c])
4979 CPP_RESERVE (pfile, 2);
4980 if (pfile->output_escapes)
4981 CPP_PUTC_Q (pfile, '@');
4982 CPP_PUTC_Q (pfile, c);
4986 if (pfile->output_escapes)
4988 CPP_PUTS (pfile, "@@", 2);
4998 CPP_RESERVE(pfile, 2);
4999 CPP_PUTC_Q (pfile, '.');
5003 /* FIXME - misses the case "..\\\n." */
5004 if (c2 == '.' && PEEKN(1) == '.')
5006 CPP_RESERVE(pfile, 4);
5007 CPP_PUTC_Q (pfile, '.');
5008 CPP_PUTC_Q (pfile, '.');
5009 CPP_PUTC_Q (pfile, '.');
5011 CPP_NUL_TERMINATE_Q (pfile);
5012 pfile->only_seen_white = 0;
5019 pfile->only_seen_white = 0;
5021 CPP_RESERVE(pfile, 3);
5022 CPP_PUTC_Q (pfile, c);
5023 CPP_PUTC_Q (pfile, GETC ());
5024 CPP_NUL_TERMINATE_Q (pfile);
5030 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5032 CPP_PUTC (pfile, c);
5038 case '0': case '1': case '2': case '3': case '4':
5039 case '5': case '6': case '7': case '8': case '9':
5044 CPP_RESERVE (pfile, 2);
5045 CPP_PUTC_Q (pfile, c);
5050 if (!is_idchar[c] && c != '.'
5051 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5056 CPP_NUL_TERMINATE_Q (pfile);
5057 pfile->only_seen_white = 0;
5059 case 'b': case 'c': case 'd': case 'h': case 'o':
5060 case 'B': case 'C': case 'D': case 'H': case 'O':
5061 if (opts->chill && PEEKC () == '\'')
5063 pfile->only_seen_white = 0;
5064 CPP_RESERVE (pfile, 2);
5065 CPP_PUTC_Q (pfile, c);
5066 CPP_PUTC_Q (pfile, '\'');
5072 goto chill_number_eof;
5075 if (c == '\\' && PEEKC() == '\n')
5082 CPP_PUTC (pfile, c);
5086 CPP_RESERVE (pfile, 2);
5087 CPP_PUTC_Q (pfile, c);
5088 CPP_NUL_TERMINATE_Q (pfile);
5095 CPP_NUL_TERMINATE (pfile);
5102 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5103 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5104 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5105 case 'x': case 'y': case 'z':
5106 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5107 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5108 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5113 unsigned char *ident;
5114 int before_name_written = CPP_WRITTEN (pfile);
5116 parse_name (pfile, c);
5117 pfile->only_seen_white = 0;
5118 if (pfile->no_macro_expand)
5120 ident = pfile->token_buffer + before_name_written;
5121 ident_len = CPP_PWRITTEN (pfile) - ident;
5122 hp = cpp_lookup (pfile, ident, ident_len, -1);
5125 if (hp->type == T_DISABLED)
5127 if (pfile->output_escapes)
5128 { /* Return "@-IDENT", followed by '\0'. */
5130 CPP_RESERVE (pfile, 3);
5131 ident = pfile->token_buffer + before_name_written;
5132 CPP_ADJUST_WRITTEN (pfile, 2);
5133 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5140 /* If macro wants an arglist, verify that a '(' follows.
5141 first skip all whitespace, copying it to the output
5142 after the macro name. Then, if there is no '(',
5143 decide this is not a macro call and leave things that way. */
5144 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5146 struct parse_marker macro_mark;
5148 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5150 cpp_buffer *next_buf;
5151 cpp_skip_hspace (pfile);
5152 if (PEEKC () != EOF)
5154 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5155 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5156 CPP_BUFFER (pfile) = next_buf;
5158 parse_set_mark (¯o_mark, pfile);
5161 cpp_skip_hspace (pfile);
5163 is_macro_call = c == '(';
5169 parse_goto_mark (¯o_mark, pfile);
5170 parse_clear_mark (¯o_mark);
5174 /* This is now known to be a macro call. */
5176 /* it might not actually be a macro. */
5177 if (hp->type != T_MACRO) {
5178 int xbuf_len; U_CHAR *xbuf;
5179 CPP_SET_WRITTEN (pfile, before_name_written);
5180 special_symbol (hp, pfile);
5181 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5182 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5183 CPP_SET_WRITTEN (pfile, before_name_written);
5184 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5185 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5189 /* Expand the macro, reading arguments as needed,
5190 and push the expansion on the input stack. */
5191 macroexpand (pfile, hp);
5192 CPP_SET_WRITTEN (pfile, before_name_written);
5195 /* An extra "@ " is added to the end of a macro expansion
5196 to prevent accidental token pasting. We prefer to avoid
5197 unneeded extra spaces (for the sake of cpp-using tools like
5198 imake). Here we remove the space if it is safe to do so. */
5199 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5200 && pfile->buffer->rlimit[-2] == '@'
5201 && pfile->buffer->rlimit[-1] == ' ')
5203 int c1 = pfile->buffer->rlimit[-3];
5204 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5205 if (c2 == EOF || ! unsafe_chars (c1, c2))
5206 pfile->buffer->rlimit -= 2;
5211 case ' ': case '\t': case '\v': case '\r':
5214 CPP_PUTC (pfile, c);
5216 if (c == EOF || !is_hor_space[c])
5230 CPP_PUTC (pfile, c);
5231 if (pfile->only_seen_white == 0)
5232 pfile->only_seen_white = 1;
5234 output_line_command (pfile, 1, same_file);
5237 case '(': token = CPP_LPAREN; goto char1;
5238 case ')': token = CPP_RPAREN; goto char1;
5239 case '{': token = CPP_LBRACE; goto char1;
5240 case '}': token = CPP_RBRACE; goto char1;
5241 case ',': token = CPP_COMMA; goto char1;
5242 case ';': token = CPP_SEMICOLON; goto char1;
5248 pfile->only_seen_white = 0;
5249 CPP_PUTC (pfile, c);
5255 /* Like cpp_get_token, but skip spaces and comments. */
5257 cpp_get_non_space_token (
5260 int old_written = CPP_WRITTEN (pfile);
5263 enum cpp_token token = cpp_get_token (pfile);
5264 if (token != CPP_COMMENT && token != CPP_POP
5265 && token != CPP_HSPACE && token != CPP_VSPACE)
5267 CPP_SET_WRITTEN (pfile, old_written);
5271 /* Parse an identifier starting with C. */
5275 cpp_reader *pfile, int c)
5281 if (c == '\\' && PEEKC() == '\n')
5290 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5291 CPP_PUTC_Q (pfile, c);
5296 CPP_NUL_TERMINATE_Q (pfile);
5301 /* Maintain and search list of included files, for #import. */
5303 /* Hash a file name for import_hash_table. */
5311 while (*f) val += *f++;
5312 return (val%IMPORT_HASH_SIZE);
5315 /* Search for file FILENAME in import_hash_table.
5316 Return -2 if found, either a matching name or a matching inode.
5317 Otherwise, open the file and return a file descriptor if successful
5318 or -1 if unsuccessful. */
5324 struct file_name_list *searchptr)
5326 struct import_file *i;
5332 hashval = import_hash (filename);
5334 /* Attempt to find file in list of already included files */
5335 i = pfile->import_hash_table[hashval];
5338 if (!strcmp (filename, i->name))
5339 return -2; /* return found */
5342 /* Open it and try a match on inode/dev */
5343 fd = open_include_file (pfile, filename, searchptr);
5347 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5348 i = pfile->import_hash_table[h];
5350 /* Compare the inode and the device.
5351 Supposedly on some systems the inode is not a scalar. */
5352 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5353 && i->dev == sb.st_dev) {
5355 return -2; /* return found */
5360 return fd; /* Not found, return open file */
5363 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5371 struct import_file *i;
5375 hashval = import_hash (fname);
5377 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5378 i->name = (char *)xmalloc (strlen (fname)+1);
5379 strcpy (i->name, fname);
5380 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5382 i->next = pfile->import_hash_table[hashval];
5383 pfile->import_hash_table[hashval] = i;
5386 /* The file_name_map structure holds a mapping of file names for a
5387 particular directory. This mapping is read from the file named
5388 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5389 map filenames on a file system with severe filename restrictions,
5390 such as DOS. The format of the file name map file is just a series
5391 of lines with two tokens on each line. The first token is the name
5392 to map, and the second token is the actual name to use. */
5394 struct file_name_map
5396 struct file_name_map *map_next;
5401 #define FILE_NAME_MAP_FILE "header.gcc"
5403 /* Read a space delimited string of unlimited length from a stdio
5407 read_filename_string (
5415 set = alloc = xmalloc (len + 1);
5419 while ((ch = getc (f)) != EOF && ! is_space[ch])
5421 if (set - alloc == len)
5424 alloc = xrealloc (alloc, len + 1);
5425 set = alloc + len / 2;
5435 /* This structure holds a linked list of file name maps, one per directory. */
5436 struct file_name_map_list
5438 struct file_name_map_list *map_list_next;
5439 char *map_list_name;
5440 struct file_name_map *map_list_map;
5443 /* Read the file name map file for DIRNAME. */
5445 static struct file_name_map *
5450 register struct file_name_map_list *map_list_ptr;
5454 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5455 map_list_ptr = map_list_ptr->map_list_next)
5456 if (! strcmp (map_list_ptr->map_list_name, dirname))
5457 return map_list_ptr->map_list_map;
5459 map_list_ptr = ((struct file_name_map_list *)
5460 xmalloc (sizeof (struct file_name_map_list)));
5461 map_list_ptr->map_list_name = savestring (dirname);
5462 map_list_ptr->map_list_map = NULL;
5464 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5465 strcpy (name, dirname);
5468 strcat (name, FILE_NAME_MAP_FILE);
5469 f = fopen (name, "r");
5471 map_list_ptr->map_list_map = NULL;
5475 int dirlen = strlen (dirname);
5477 while ((ch = getc (f)) != EOF)
5480 struct file_name_map *ptr;
5484 from = read_filename_string (ch, f);
5485 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5487 to = read_filename_string (ch, f);
5489 ptr = ((struct file_name_map *)
5490 xmalloc (sizeof (struct file_name_map)));
5491 ptr->map_from = from;
5493 /* Make the real filename absolute. */
5498 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5499 strcpy (ptr->map_to, dirname);
5500 ptr->map_to[dirlen] = '/';
5501 strcpy (ptr->map_to + dirlen + 1, to);
5505 ptr->map_next = map_list_ptr->map_list_map;
5506 map_list_ptr->map_list_map = ptr;
5508 while ((ch = getc (f)) != '\n')
5515 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5516 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5518 return map_list_ptr->map_list_map;
5521 /* Try to open include file FILENAME. SEARCHPTR is the directory
5522 being tried from the include file search path. This function maps
5523 filenames on file systems based on information read by
5530 struct file_name_list *searchptr)
5532 register struct file_name_map *map;
5533 register char *from;
5536 if (searchptr && ! searchptr->got_name_map)
5538 searchptr->name_map = read_name_map (pfile,
5540 ? searchptr->fname : ".");
5541 searchptr->got_name_map = 1;
5544 /* First check the mapping for the directory we are using. */
5545 if (searchptr && searchptr->name_map)
5548 if (searchptr->fname)
5549 from += strlen (searchptr->fname) + 1;
5550 for (map = searchptr->name_map; map; map = map->map_next)
5552 if (! strcmp (map->map_from, from))
5554 /* Found a match. */
5555 return open (map->map_to, O_RDONLY, 0666);
5560 /* Try to find a mapping file for the particular directory we are
5561 looking in. Thus #include <sys/types.h> will look up sys/types.h
5562 in /usr/include/header.gcc and look up types.h in
5563 /usr/include/sys/header.gcc. */
5564 p = rindex (filename, '/');
5569 && strlen (searchptr->fname) == p - filename
5570 && ! strncmp (searchptr->fname, filename, p - filename))
5572 /* FILENAME is in SEARCHPTR, which we've already checked. */
5573 return open (filename, O_RDONLY, 0666);
5583 dir = (char *) alloca (p - filename + 1);
5584 bcopy (filename, dir, p - filename);
5585 dir[p - filename] = '\0';
5588 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5589 if (! strcmp (map->map_from, from))
5590 return open (map->map_to, O_RDONLY, 0666);
5592 return open (filename, O_RDONLY, 0666);
5595 /* Process the contents of include file FNAME, already open on descriptor F,
5597 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5598 "system" include directories (as decided by the `is_system_include'
5600 DIRPTR is the link in the dir path through which this file was found,
5601 or 0 if the file name was absolute or via the current directory.
5602 Return 1 on success, 0 on failure.
5604 The caller is responsible for the cpp_push_buffer. */
5611 int system_header_p,
5612 struct file_name_list *dirptr)
5618 cpp_buffer *fp; /* For input stack frame */
5619 int missing_newline = 0;
5621 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5623 cpp_perror_with_name (pfile, fname);
5625 cpp_pop_buffer (pfile);
5629 fp = CPP_BUFFER (pfile);
5630 fp->nominal_fname = fp->fname = fname;
5635 fp->system_header_p = system_header_p;
5638 fp->cleanup = file_cleanup;
5640 if (S_ISREG (st_mode)) {
5641 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5642 fp->alimit = fp->buf + st_size + 2;
5645 /* Read the file contents, knowing that st_size is an upper bound
5646 on the number of bytes we can read. */
5647 length = safe_read (f, fp->buf, st_size);
5648 fp->rlimit = fp->buf + length;
5649 if (length < 0) goto nope;
5651 else if (S_ISDIR (st_mode)) {
5652 cpp_error (pfile, "directory `%s' specified in #include", fname);
5656 /* Cannot count its file size before reading.
5657 First read the entire file into heap and
5658 copy them into buffer on stack. */
5663 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5666 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5668 goto nope; /* error! */
5670 if (st_size != bsize)
5671 break; /* End of file */
5673 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5679 if ((length > 0 && fp->buf[length - 1] != '\n')
5680 /* Backslash-newline at end is not good enough. */
5681 || (length > 1 && fp->buf[length - 2] == '\\')) {
5682 fp->buf[length++] = '\n';
5684 missing_newline = 1;
5687 fp->buf[length] = '\0';
5688 fp->rlimit = fp->buf + length;
5690 /* Close descriptor now, so nesting does not use lots of descriptors. */
5693 /* Must do this before calling trigraph_pcp, so that the correct file name
5694 will be printed in warning messages. */
5696 pfile->input_stack_listing_current = 0;
5706 if (missing_newline)
5709 if (CPP_PEDANTIC (pfile) && missing_newline)
5710 pedwarn ("file does not end in newline");
5713 input_file_stack_tick++;
5720 cpp_perror_with_name (pfile, fname);
5731 struct cpp_options *opts = CPP_OPTIONS (pfile);
5732 struct cpp_pending *pend;
5737 /* The code looks at the defaults through this pointer, rather than through
5738 the constant structure above. This pointer gets changed if an environment
5739 variable specifies other defaults. */
5740 struct default_include *include_defaults = include_defaults_array;
5742 /* Add dirs from CPATH after dirs from -I. */
5743 /* There seems to be confusion about what CPATH should do,
5744 so for the moment it is not documented. */
5745 /* Some people say that CPATH should replace the standard include dirs,
5746 but that seems pointless: it comes before them, so it overrides them
5748 p = (char *) getenv ("CPATH");
5749 if (p != 0 && ! opts->no_standard_includes)
5750 path_include (pfile, p);
5752 /* Now that dollars_in_ident is known, initialize is_idchar. */
5753 initialize_char_syntax (opts);
5755 /* Do partial setup of input buffer for the sake of generating
5756 early #line directives (when -g is in effect). */
5757 fp = cpp_push_buffer (pfile, NULL, 0);
5758 if (opts->in_fname == NULL)
5759 opts->in_fname = "";
5760 fp->nominal_fname = fp->fname = opts->in_fname;
5763 /* Install __LINE__, etc. Must follow initialize_char_syntax
5764 and option processing. */
5765 initialize_builtins (pfile);
5767 /* Do standard #defines and assertions
5768 that identify system and machine type. */
5770 if (!opts->inhibit_predefs) {
5771 char *p = (char *) alloca (strlen (predefs) + 1);
5772 strcpy (p, predefs);
5775 while (*p == ' ' || *p == '\t')
5777 /* Handle -D options. */
5778 if (p[0] == '-' && p[1] == 'D') {
5780 while (*p && *p != ' ' && *p != '\t')
5784 if (opts->debug_output)
5785 output_line_command (pfile, 0, same_file);
5786 cpp_define (pfile, q);
5787 while (*p == ' ' || *p == '\t')
5789 } else if (p[0] == '-' && p[1] == 'A') {
5790 /* Handle -A options (assertions). */
5799 past_name = assertion;
5800 /* Locate end of name. */
5801 while (*past_name && *past_name != ' '
5802 && *past_name != '\t' && *past_name != '(')
5804 /* Locate `(' at start of value. */
5806 while (*value && (*value == ' ' || *value == '\t'))
5808 if (*value++ != '(')
5810 while (*value && (*value == ' ' || *value == '\t'))
5813 /* Locate end of value. */
5814 while (*past_value && *past_value != ' '
5815 && *past_value != '\t' && *past_value != ')')
5817 termination = past_value;
5818 while (*termination && (*termination == ' ' || *termination == '\t'))
5820 if (*termination++ != ')')
5822 if (*termination && *termination != ' ' && *termination != '\t')
5824 /* Temporarily null-terminate the value. */
5825 save_char = *termination;
5826 *termination = '\0';
5827 /* Install the assertion. */
5828 make_assertion (pfile, "-A", assertion);
5829 *termination = (char) save_char;
5831 while (*p == ' ' || *p == '\t')
5839 /* Now handle the command line options. */
5841 /* Do -U's, -D's and -A's in the order they were seen. */
5842 /* First reverse the list. */
5843 opts->pending = nreverse_pending (opts->pending);
5845 for (pend = opts->pending; pend; pend = pend->next)
5847 if (pend->cmd != NULL && pend->cmd[0] == '-')
5849 switch (pend->cmd[1])
5852 if (opts->debug_output)
5853 output_line_command (pfile, 0, same_file);
5854 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5857 if (opts->debug_output)
5858 output_line_command (pfile, 0, same_file);
5859 cpp_define (pfile, pend->arg);
5862 make_assertion (pfile, "-A", pend->arg);
5868 opts->done_initializing = 1;
5870 { /* read the appropriate environment variable and if it exists
5871 replace include_defaults with the listed path. */
5873 switch ((opts->objc << 1) + opts->cplusplus)
5876 epath = getenv ("C_INCLUDE_PATH");
5879 epath = getenv ("CPLUS_INCLUDE_PATH");
5882 epath = getenv ("OBJC_INCLUDE_PATH");
5885 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5888 /* If the environment var for this language is set,
5889 add to the default list of include directories. */
5891 char *nstore = (char *) alloca (strlen (epath) + 2);
5893 char *startp, *endp;
5895 for (num_dirs = 1, startp = epath; *startp; startp++)
5896 if (*startp == PATH_SEPARATOR)
5899 = (struct default_include *) xmalloc ((num_dirs
5900 * sizeof (struct default_include))
5901 + sizeof (include_defaults_array));
5902 startp = endp = epath;
5905 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5906 if ((*endp == PATH_SEPARATOR)
5908 strncpy (nstore, startp, endp-startp);
5910 strcpy (nstore, ".");
5912 nstore[endp-startp] = '\0';
5914 include_defaults[num_dirs].fname = savestring (nstore);
5915 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5916 include_defaults[num_dirs].cxx_aware = 1;
5920 endp = startp = endp + 1;
5924 /* Put the usual defaults back in at the end. */
5925 bcopy ((char *) include_defaults_array,
5926 (char *) &include_defaults[num_dirs],
5927 sizeof (include_defaults_array));
5931 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5932 opts->first_system_include = opts->before_system;
5934 /* Unless -fnostdinc,
5935 tack on the standard include file dirs to the specified list */
5936 if (!opts->no_standard_includes) {
5937 struct default_include *p = include_defaults;
5938 char *specd_prefix = opts->include_prefix;
5939 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5940 int default_len = 0;
5941 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5942 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5943 default_len = strlen (default_prefix) - 7;
5944 default_prefix[default_len] = 0;
5946 /* Search "translated" versions of GNU directories.
5947 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5948 if (specd_prefix != 0 && default_len != 0)
5949 for (p = include_defaults; p->fname; p++) {
5950 /* Some standard dirs are only for C++. */
5952 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5953 /* Does this dir start with the prefix? */
5954 if (!strncmp (p->fname, default_prefix, default_len)) {
5955 /* Yes; change prefix and add to search list. */
5956 struct file_name_list *new
5957 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5958 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5959 char *str = (char *) xmalloc (this_len + 1);
5960 strcpy (str, specd_prefix);
5961 strcat (str, p->fname + default_len);
5963 new->control_macro = 0;
5964 new->c_system_include_path = !p->cxx_aware;
5965 new->got_name_map = 0;
5966 append_include_chain (pfile, new, new);
5967 if (opts->first_system_include == 0)
5968 opts->first_system_include = new;
5972 /* Search ordinary names for GNU include directories. */
5973 for (p = include_defaults; p->fname; p++) {
5974 /* Some standard dirs are only for C++. */
5976 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5977 struct file_name_list *new
5978 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5979 new->control_macro = 0;
5980 new->c_system_include_path = !p->cxx_aware;
5981 new->fname = p->fname;
5982 new->got_name_map = 0;
5983 append_include_chain (pfile, new, new);
5984 if (opts->first_system_include == 0)
5985 opts->first_system_include = new;
5990 /* Tack the after_include chain at the end of the include chain. */
5991 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5992 if (opts->first_system_include == 0)
5993 opts->first_system_include = opts->after_include;
5995 /* With -v, print the list of dirs to search. */
5996 if (opts->verbose) {
5997 struct file_name_list *p;
5998 fprintf (stderr, "#include \"...\" search starts here:\n");
5999 for (p = opts->include; p; p = p->next) {
6000 if (p == opts->first_bracket_include)
6001 fprintf (stderr, "#include <...> search starts here:\n");
6002 fprintf (stderr, " %s\n", p->fname);
6004 fprintf (stderr, "End of search list.\n");
6007 /* Scan the -imacros files before the main input.
6008 Much like #including them, but with no_output set
6009 so that only their macro definitions matter. */
6011 opts->no_output++; pfile->no_record_file++;
6012 for (pend = opts->pending; pend; pend = pend->next)
6014 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6016 int fd = open (pend->arg, O_RDONLY, 0666);
6019 cpp_perror_with_name (pfile, pend->arg);
6020 return FATAL_EXIT_CODE;
6022 cpp_push_buffer (pfile, NULL, 0);
6023 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6024 cpp_scan_buffer (pfile);
6027 opts->no_output--; pfile->no_record_file--;
6029 /* Copy the entire contents of the main input file into
6030 the stacked input buffer previously allocated for it. */
6031 if (fname == NULL || *fname == 0) {
6034 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6035 cpp_pfatal_with_name (pfile, fname);
6037 /* -MG doesn't select the form of output and must be specified with one of
6038 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6039 inhibit compilation. */
6040 if (opts->print_deps_missing_files
6041 && (opts->print_deps == 0 || !opts->no_output))
6042 fatal (pfile, "-MG must be specified with one of -M or -MM");
6044 /* Either of two environment variables can specify output of deps.
6045 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6046 where OUTPUT_FILE is the file to write deps info to
6047 and DEPS_TARGET is the target to mention in the deps. */
6049 if (opts->print_deps == 0
6050 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6051 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6052 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6058 spec = getenv ("SUNPRO_DEPENDENCIES");
6059 opts->print_deps = 2;
6062 opts->print_deps = 1;
6065 /* Find the space before the DEPS_TARGET, if there is one. */
6066 /* This should use index. (mrs) */
6067 while (*s != 0 && *s != ' ') s++;
6070 opts->deps_target = s + 1;
6071 output_file = (char *) xmalloc (s - spec + 1);
6072 bcopy (spec, output_file, s - spec);
6073 output_file[s - spec] = 0;
6077 opts->deps_target = 0;
6081 opts->deps_file = output_file;
6082 opts->print_deps_append = 1;
6085 /* For -M, print the expected object file name
6086 as the target of this Make-rule. */
6087 if (opts->print_deps)
6089 pfile->deps_allocated_size = 200;
6090 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6091 pfile->deps_buffer[0] = 0;
6092 pfile->deps_size = 0;
6093 pfile->deps_column = 0;
6095 if (opts->deps_target)
6096 deps_output (pfile, opts->deps_target, ':');
6097 else if (*opts->in_fname == 0)
6098 deps_output (pfile, "-", ':');
6104 /* Discard all directory prefixes from filename. */
6105 if ((q = rindex (opts->in_fname, '/')) != NULL
6106 #ifdef DIR_SEPARATOR
6107 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6114 /* Copy remainder to mungable area. */
6115 p = (char *) alloca (strlen(q) + 8);
6118 /* Output P, but remove known suffixes. */
6122 && p[len - 2] == '.'
6123 && index("cCsSm", p[len - 1]))
6126 && p[len - 3] == '.'
6127 && p[len - 2] == 'c'
6128 && p[len - 1] == 'c')
6131 && p[len - 4] == '.'
6132 && p[len - 3] == 'c'
6133 && p[len - 2] == 'x'
6134 && p[len - 1] == 'x')
6137 && p[len - 4] == '.'
6138 && p[len - 3] == 'c'
6139 && p[len - 2] == 'p'
6140 && p[len - 1] == 'p')
6143 /* Supply our own suffix. */
6154 deps_output (pfile, p, ':');
6155 deps_output (pfile, opts->in_fname, ' ');
6160 /* Make sure data ends with a newline. And put a null after it. */
6162 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6163 /* Backslash-newline at end is not good enough. */
6164 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6165 fp->buf[fp->length++] = '\n';
6166 missing_newline = 1;
6168 fp->buf[fp->length] = '\0';
6170 /* Unless inhibited, convert trigraphs in the input. */
6176 /* Scan the -include files before the main input.
6177 We push these in reverse order, so that the first one is handled first. */
6179 pfile->no_record_file++;
6180 opts->pending = nreverse_pending (opts->pending);
6181 for (pend = opts->pending; pend; pend = pend->next)
6183 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6185 int fd = open (pend->arg, O_RDONLY, 0666);
6188 cpp_perror_with_name (pfile, pend->arg);
6189 return FATAL_EXIT_CODE;
6191 cpp_push_buffer (pfile, NULL, 0);
6192 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6195 pfile->no_record_file--;
6197 /* Free the pending list. */
6198 for (pend = opts->pending; pend; )
6200 struct cpp_pending *next = pend->next;
6204 opts->pending = NULL;
6207 /* Scan the input, processing macros and directives. */
6209 rescan (&outbuf, 0);
6211 if (missing_newline)
6214 if (CPP_PEDANTIC (pfile) && missing_newline)
6215 pedwarn ("file does not end in newline");
6218 if (finclude (pfile, f, fname, 0, NULL_PTR))
6219 output_line_command (pfile, 0, same_file);
6220 return SUCCESS_EXIT_CODE;
6227 bzero ((char *) pfile, sizeof (cpp_reader));
6228 pfile->get_token = cpp_get_token;
6230 pfile->token_buffer_size = 200;
6231 pfile->token_buffer = (U_CHAR*)xmalloc (pfile->token_buffer_size);
6232 CPP_SET_WRITTEN (pfile, 0);
6234 pfile->system_include_depth = 0;
6235 pfile->dont_repeat_files = 0;
6236 pfile->all_include_files = 0;
6237 pfile->max_include_len = 0;
6238 pfile->timebuf = NULL;
6239 pfile->only_seen_white = 1;
6240 pfile->buffer = CPP_NULL_BUFFER(pfile);
6243 static struct cpp_pending *
6245 struct cpp_pending *list)
6248 register struct cpp_pending *prev = 0, *next, *pend;
6249 for (pend = list; pend; pend = next)
6264 struct cpp_pending *pend
6265 = (struct cpp_pending*)xmalloc (sizeof (struct cpp_pending));
6268 pend->next = CPP_OPTIONS (pfile)->pending;
6269 CPP_OPTIONS (pfile)->pending = pend;
6272 /* Handle command-line options in (argc, argv).
6273 Can be called multiple times, to handle multiple sets of options.
6274 Returns if an unrecognized option is seen.
6275 Returns number of handled arguments. */
6278 cpp_handle_options (
6284 struct cpp_options *opts = CPP_OPTIONS (pfile);
6285 for (i = 0; i < argc; i++) {
6286 if (argv[i][0] != '-') {
6287 if (opts->out_fname != NULL)
6288 fatal ("Usage: %s [switches] input output", argv[0]);
6289 else if (opts->in_fname != NULL)
6290 opts->out_fname = argv[i];
6292 opts->in_fname = argv[i];
6294 switch (argv[i][1]) {
6297 if (!strcmp (argv[i], "-include")
6298 || !strcmp (argv[i], "-imacros")) {
6300 fatal ("Filename missing after `%s' option", argv[i]);
6302 push_pending (pfile, argv[i], argv[i+1]), i++;
6304 if (!strcmp (argv[i], "-iprefix")) {
6306 fatal ("Filename missing after `-iprefix' option");
6308 opts->include_prefix = argv[++i];
6310 if (!strcmp (argv[i], "-ifoutput")) {
6311 opts->output_conditionals = 1;
6313 if (!strcmp (argv[i], "-isystem")) {
6314 struct file_name_list *dirtmp;
6317 fatal ("Filename missing after `-isystem' option");
6319 dirtmp = (struct file_name_list *)
6320 xmalloc (sizeof (struct file_name_list));
6322 dirtmp->control_macro = 0;
6323 dirtmp->c_system_include_path = 1;
6324 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6325 strcpy (dirtmp->fname, argv[++i]);
6326 dirtmp->got_name_map = 0;
6328 if (opts->before_system == 0)
6329 opts->before_system = dirtmp;
6331 opts->last_before_system->next = dirtmp;
6332 opts->last_before_system = dirtmp; /* Tail follows the last one */
6334 /* Add directory to end of path for includes,
6335 with the default prefix at the front of its name. */
6336 if (!strcmp (argv[i], "-iwithprefix")) {
6337 struct file_name_list *dirtmp;
6340 if (opts->include_prefix != 0)
6341 prefix = opts->include_prefix;
6343 prefix = savestring (GCC_INCLUDE_DIR);
6344 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6345 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6346 prefix[strlen (prefix) - 7] = 0;
6349 dirtmp = (struct file_name_list *)
6350 xmalloc (sizeof (struct file_name_list));
6351 dirtmp->next = 0; /* New one goes on the end */
6352 dirtmp->control_macro = 0;
6353 dirtmp->c_system_include_path = 0;
6355 fatal ("Directory name missing after `-iwithprefix' option");
6357 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6358 + strlen (prefix) + 1);
6359 strcpy (dirtmp->fname, prefix);
6360 strcat (dirtmp->fname, argv[++i]);
6361 dirtmp->got_name_map = 0;
6363 if (opts->after_include == 0)
6364 opts->after_include = dirtmp;
6366 opts->last_after_include->next = dirtmp;
6367 opts->last_after_include = dirtmp; /* Tail follows the last one */
6369 /* Add directory to main path for includes,
6370 with the default prefix at the front of its name. */
6371 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6372 struct file_name_list *dirtmp;
6375 if (opts->include_prefix != 0)
6376 prefix = opts->include_prefix;
6378 prefix = savestring (GCC_INCLUDE_DIR);
6379 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6380 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6381 prefix[strlen (prefix) - 7] = 0;
6384 dirtmp = (struct file_name_list *)
6385 xmalloc (sizeof (struct file_name_list));
6386 dirtmp->next = 0; /* New one goes on the end */
6387 dirtmp->control_macro = 0;
6388 dirtmp->c_system_include_path = 0;
6390 fatal ("Directory name missing after `-iwithprefixbefore' option");
6392 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6393 + strlen (prefix) + 1);
6394 strcpy (dirtmp->fname, prefix);
6395 strcat (dirtmp->fname, argv[++i]);
6396 dirtmp->got_name_map = 0;
6398 append_include_chain (pfile, dirtmp, dirtmp);
6400 /* Add directory to end of path for includes. */
6401 if (!strcmp (argv[i], "-idirafter")) {
6402 struct file_name_list *dirtmp;
6404 dirtmp = (struct file_name_list *)
6405 xmalloc (sizeof (struct file_name_list));
6406 dirtmp->next = 0; /* New one goes on the end */
6407 dirtmp->control_macro = 0;
6408 dirtmp->c_system_include_path = 0;
6410 fatal ("Directory name missing after `-idirafter' option");
6412 dirtmp->fname = argv[++i];
6413 dirtmp->got_name_map = 0;
6415 if (opts->after_include == 0)
6416 opts->after_include = dirtmp;
6418 opts->last_after_include->next = dirtmp;
6419 opts->last_after_include = dirtmp; /* Tail follows the last one */
6424 if (opts->out_fname != NULL)
6425 fatal ("Output filename specified twice");
6427 fatal ("Filename missing after -o option");
6428 opts->out_fname = argv[++i];
6429 if (!strcmp (opts->out_fname, "-"))
6430 opts->out_fname = "";
6434 if (!strcmp (argv[i], "-pedantic"))
6435 CPP_PEDANTIC (pfile) = 1;
6436 else if (!strcmp (argv[i], "-pedantic-errors")) {
6437 CPP_PEDANTIC (pfile) = 1;
6438 opts->pedantic_errors = 1;
6441 else if (!strcmp (argv[i], "-pcp")) {
6442 char *pcp_fname = argv[++i];
6444 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6445 ? fopen (pcp_fname, "w")
6446 : fdopen (dup (fileno (stdout)), "w"));
6447 if (pcp_outfile == 0)
6448 cpp_pfatal_with_name (pfile, pcp_fname);
6455 if (!strcmp (argv[i], "-traditional")) {
6456 opts->traditional = 1;
6457 if (opts->dollars_in_ident > 0)
6458 opts->dollars_in_ident = 1;
6459 } else if (!strcmp (argv[i], "-trigraphs")) {
6461 opts->no_trigraphs = 0;
6466 if (! strcmp (argv[i], "-lang-c"))
6467 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6468 if (! strcmp (argv[i], "-lang-c++"))
6469 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6470 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6471 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6472 if (! strcmp (argv[i], "-lang-objc"))
6473 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6474 if (! strcmp (argv[i], "-lang-objc++"))
6475 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6476 if (! strcmp (argv[i], "-lang-asm"))
6478 if (! strcmp (argv[i], "-lint"))
6480 if (! strcmp (argv[i], "-lang-chill"))
6481 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6482 opts->traditional = 1, opts->no_trigraphs = 1;
6486 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6490 opts->inhibit_warnings = 1;
6494 if (!strcmp (argv[i], "-Wtrigraphs"))
6495 opts->warn_trigraphs = 1;
6496 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6497 opts->warn_trigraphs = 0;
6498 else if (!strcmp (argv[i], "-Wcomment"))
6499 opts->warn_comments = 1;
6500 else if (!strcmp (argv[i], "-Wno-comment"))
6501 opts->warn_comments = 0;
6502 else if (!strcmp (argv[i], "-Wcomments"))
6503 opts->warn_comments = 1;
6504 else if (!strcmp (argv[i], "-Wno-comments"))
6505 opts->warn_comments = 0;
6506 else if (!strcmp (argv[i], "-Wtraditional"))
6507 opts->warn_stringify = 1;
6508 else if (!strcmp (argv[i], "-Wno-traditional"))
6509 opts->warn_stringify = 0;
6510 else if (!strcmp (argv[i], "-Wimport"))
6511 opts->warn_import = 1;
6512 else if (!strcmp (argv[i], "-Wno-import"))
6513 opts->warn_import = 0;
6514 else if (!strcmp (argv[i], "-Werror"))
6515 opts->warnings_are_errors = 1;
6516 else if (!strcmp (argv[i], "-Wno-error"))
6517 opts->warnings_are_errors = 0;
6518 else if (!strcmp (argv[i], "-Wall"))
6520 opts->warn_trigraphs = 1;
6521 opts->warn_comments = 1;
6526 /* The style of the choices here is a bit mixed.
6527 The chosen scheme is a hybrid of keeping all options in one string
6528 and specifying each option in a separate argument:
6529 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6530 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6531 -M[M][G][D file]. This is awkward to handle in specs, and is not
6533 /* ??? -MG must be specified in addition to one of -M or -MM.
6534 This can be relaxed in the future without breaking anything.
6535 The converse isn't true. */
6537 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6538 if (!strcmp (argv[i], "-MG"))
6540 opts->print_deps_missing_files = 1;
6543 if (!strcmp (argv[i], "-M"))
6544 opts->print_deps = 2;
6545 else if (!strcmp (argv[i], "-MM"))
6546 opts->print_deps = 1;
6547 else if (!strcmp (argv[i], "-MD"))
6548 opts->print_deps = 2;
6549 else if (!strcmp (argv[i], "-MMD"))
6550 opts->print_deps = 1;
6551 /* For -MD and -MMD options, write deps on file named by next arg. */
6552 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6555 fatal ("Filename missing after %s option", argv[i]);
6556 opts->deps_file = argv[++i];
6560 /* For -M and -MM, write deps on standard output
6561 and suppress the usual output. */
6562 opts->no_output = 1;
6568 char *p = argv[i] + 2;
6570 while ((c = *p++) != 0) {
6571 /* Arg to -d specifies what parts of macros to dump */
6574 opts->dump_macros = dump_only;
6575 opts->no_output = 1;
6578 opts->dump_macros = dump_names;
6581 opts->dump_macros = dump_definitions;
6589 if (argv[i][2] == '3')
6590 opts->debug_output = 1;
6594 fprintf (stderr, "GNU CPP version %s", version_string);
6595 #ifdef TARGET_VERSION
6598 fprintf (stderr, "\n");
6599 // opts->verbose = 1;
6603 opts->print_include_names = 1;
6607 if (argv[i][2] != 0)
6608 push_pending (pfile, "-D", argv[i] + 2);
6609 else if (i + 1 == argc)
6610 fatal ("Macro name missing after -D option");
6612 i++, push_pending (pfile, "-D", argv[i]);
6619 if (argv[i][2] != 0)
6621 else if (i + 1 == argc)
6622 fatal ("Assertion missing after -A option");
6626 if (!strcmp (p, "-")) {
6627 struct cpp_pending **ptr;
6628 /* -A- eliminates all predefined macros and assertions.
6629 Let's include also any that were specified earlier
6630 on the command line. That way we can get rid of any
6631 that were passed automatically in from GCC. */
6633 opts->inhibit_predefs = 1;
6634 for (ptr = &opts->pending; *ptr != NULL; )
6636 struct cpp_pending *pend = *ptr;
6637 if (pend->cmd && pend->cmd[0] == '-'
6638 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6647 push_pending (pfile, "-A", p);
6652 case 'U': /* JF #undef something */
6653 if (argv[i][2] != 0)
6654 push_pending (pfile, "-U", argv[i] + 2);
6655 else if (i + 1 == argc)
6656 fatal ("Macro name missing after -U option");
6658 push_pending (pfile, "-U", argv[i+1]), i++;
6662 opts->put_out_comments = 1;
6665 case 'E': /* -E comes from cc -E; ignore it. */
6669 opts->no_line_commands = 1;
6672 case '$': /* Don't include $ in identifiers. */
6673 opts->dollars_in_ident = 0;
6676 case 'I': /* Add directory to path for includes. */
6678 struct file_name_list *dirtmp;
6680 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6681 && !strcmp (argv[i] + 2, "-")) {
6682 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6683 /* Don't use any preceding -I directories for #include <...>. */
6684 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6687 dirtmp = (struct file_name_list *)
6688 xmalloc (sizeof (struct file_name_list));
6689 dirtmp->next = 0; /* New one goes on the end */
6690 dirtmp->control_macro = 0;
6691 dirtmp->c_system_include_path = 0;
6692 if (argv[i][2] != 0)
6693 dirtmp->fname = argv[i] + 2;
6694 else if (i + 1 == argc)
6695 fatal ("Directory name missing after -I option");
6697 dirtmp->fname = argv[++i];
6698 dirtmp->got_name_map = 0;
6699 append_include_chain (pfile, dirtmp, dirtmp);
6705 if (!strcmp (argv[i], "-nostdinc"))
6706 /* -nostdinc causes no default include directories.
6707 You must specify all include-file directories with -I. */
6708 opts->no_standard_includes = 1;
6709 else if (!strcmp (argv[i], "-nostdinc++"))
6710 /* -nostdinc++ causes no default C++-specific include directories. */
6711 opts->no_standard_cplusplus_includes = 1;
6713 else if (!strcmp (argv[i], "-noprecomp"))
6719 /* Sun compiler passes undocumented switch "-undef".
6720 Let's assume it means to inhibit the predefined symbols. */
6721 opts->inhibit_predefs = 1;
6724 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6725 if (opts->in_fname == NULL) {
6726 opts->in_fname = "";
6728 } else if (opts->out_fname == NULL) {
6729 opts->out_fname = "";
6731 } /* else fall through into error */
6745 struct cpp_options *opts = CPP_OPTIONS (pfile);
6747 if (opts->print_deps)
6749 /* Stream on which to print the dependency information. */
6752 /* Don't actually write the deps file if compilation has failed. */
6753 if (pfile->errors == 0)
6755 char *deps_mode = opts->print_deps_append ? "a" : "w";
6756 if (opts->deps_file == 0)
6757 deps_stream = stdout;
6758 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6759 cpp_pfatal_with_name (pfile, opts->deps_file);
6760 fputs (pfile->deps_buffer, deps_stream);
6761 putc ('\n', deps_stream);
6762 if (opts->deps_file)
6764 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6765 fatal ("I/O error on output");
6771 /* Free resources used by PFILE. */
6778 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6779 cpp_pop_buffer (pfile);
6781 if (pfile->token_buffer)
6783 free (pfile->token_buffer);
6784 pfile->token_buffer = NULL;
6787 if (pfile->deps_buffer)
6789 free (pfile->deps_buffer);
6790 pfile->deps_buffer = NULL;
6791 pfile->deps_allocated_size = 0;
6794 while (pfile->if_stack)
6796 IF_STACK_FRAME *temp = pfile->if_stack;
6797 pfile->if_stack = temp->next;
6801 while (pfile->dont_repeat_files)
6803 struct file_name_list *temp = pfile->dont_repeat_files;
6804 pfile->dont_repeat_files = temp->next;
6809 while (pfile->all_include_files)
6811 struct file_name_list *temp = pfile->all_include_files;
6812 pfile->all_include_files = temp->next;
6817 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6819 register struct import_file *imp = pfile->import_hash_table[i];
6822 struct import_file *next = imp->next;
6827 pfile->import_hash_table[i] = 0;
6830 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6832 while (pfile->assertion_hashtab[i])
6833 delete_assertion (pfile->assertion_hashtab[i]);
6836 cpp_hash_cleanup (pfile);
6842 struct directive *keyword,
6843 U_CHAR *buf, U_CHAR *limit)
6845 long symstart; /* remember where symbol name starts */
6847 int sym_length; /* and how long it is */
6848 struct arglist *tokens = NULL;
6850 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6851 && !CPP_BUFFER (pfile)->system_header_p)
6852 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6854 cpp_skip_hspace (pfile);
6855 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6856 parse_name (pfile, GETC());
6857 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6860 cpp_skip_hspace (pfile);
6861 if (PEEKC() != '(') {
6862 cpp_error (pfile, "missing token-sequence in `#assert'");
6868 tokens = read_token_list (pfile, &error_flag);
6872 cpp_error (pfile, "empty token-sequence in `#assert'");
6875 cpp_skip_hspace (pfile);
6877 if (c != EOF && c != '\n')
6878 cpp_pedwarn (pfile, "junk at end of `#assert'");
6879 skip_rest_of_line (pfile);
6882 /* If this name isn't already an assertion name, make it one.
6883 Error if it was already in use in some other way. */
6886 ASSERTION_HASHNODE *hp;
6887 U_CHAR *symname = pfile->token_buffer + symstart;
6888 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6889 struct tokenlist_list *value
6890 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6892 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6894 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6895 cpp_error (pfile, "`defined' redefined as assertion");
6896 hp = assertion_install (pfile, symname, sym_length, hashcode);
6899 /* Add the spec'd token-sequence to the list of such. */
6900 value->tokens = tokens;
6901 value->next = hp->value;
6904 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6907 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6908 skip_rest_of_line (pfile);
6915 struct directive *keyword,
6916 U_CHAR *buf, U_CHAR *limit)
6918 long symstart; /* remember where symbol name starts */
6919 int sym_length; /* and how long it is */
6922 struct arglist *tokens = NULL;
6923 int tokens_specified = 0;
6925 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6926 && !CPP_BUFFER (pfile)->system_header_p)
6927 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6929 cpp_skip_hspace (pfile);
6931 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6932 parse_name (pfile, GETC());
6933 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6936 cpp_skip_hspace (pfile);
6937 if (PEEKC() == '(') {
6940 tokens = read_token_list (pfile, &error_flag);
6944 cpp_error (pfile, "empty token list in `#unassert'");
6948 tokens_specified = 1;
6951 cpp_skip_hspace (pfile);
6953 if (c != EOF && c != '\n')
6954 cpp_error (pfile, "junk at end of `#unassert'");
6955 skip_rest_of_line (pfile);
6958 ASSERTION_HASHNODE *hp;
6959 U_CHAR *symname = pfile->token_buffer + symstart;
6960 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6961 struct tokenlist_list *tail, *prev;
6963 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6967 /* If no token list was specified, then eliminate this assertion
6969 if (! tokens_specified)
6970 delete_assertion (hp);
6972 /* If a list of tokens was given, then delete any matching list. */
6977 struct tokenlist_list *next = tail->next;
6978 if (compare_token_lists (tail->tokens, tokens)) {
6982 hp->value = tail->next;
6983 free_token_list (tail->tokens);
6993 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6996 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6997 skip_rest_of_line (pfile);
7001 /* Test whether there is an assertion named NAME
7002 and optionally whether it has an asserted token list TOKENS.
7003 NAME is not null terminated; its length is SYM_LENGTH.
7004 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7011 int tokens_specified,
7012 struct arglist *tokens)
7014 ASSERTION_HASHNODE *hp;
7015 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7017 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7018 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7020 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7022 /* It is not an assertion; just return false. */
7025 /* If no token list was specified, then value is 1. */
7026 if (! tokens_specified)
7030 struct tokenlist_list *tail;
7034 /* If a list of tokens was given,
7035 then succeed if the assertion records a matching list. */
7038 if (compare_token_lists (tail->tokens, tokens))
7043 /* Fail if the assertion has no matching list. */
7048 /* Compare two lists of tokens for equality including order of tokens. */
7051 compare_token_lists (
7052 struct arglist *l1, struct arglist *l2 )
7055 if (l1->length != l2->length)
7057 if (strncmp (l1->name, l2->name, l1->length))
7063 /* Succeed if both lists end at the same time. */
7068 reverse_token_list (
7069 struct arglist *tokens)
7071 register struct arglist *prev = 0, *this, *next;
7072 for (this = tokens; this; this = next)
7081 /* Read a space-separated list of tokens ending in a close parenthesis.
7082 Return a list of strings, in the order they were written.
7083 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7085 static struct arglist *
7090 struct arglist *token_ptrs = 0;
7095 FORWARD (1); /* Skip '(' */
7097 /* Loop over the assertion value tokens. */
7100 struct arglist *temp;
7101 long name_written = CPP_WRITTEN (pfile);
7102 int eofp = 0; int c;
7104 cpp_skip_hspace (pfile);
7108 /* Find the end of the token. */
7111 CPP_PUTC (pfile, c);
7119 CPP_PUTC (pfile, c);
7121 else if (c == '"' || c == '\'')
7124 cpp_get_token (pfile);
7130 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7131 && c != '"' && c != '\'')
7133 CPP_PUTC (pfile, c);
7136 if (c != EOF) FORWARD(-1);
7139 length = CPP_WRITTEN (pfile) - name_written;
7140 temp = (struct arglist *)
7141 xmalloc (sizeof (struct arglist) + length + 1);
7142 temp->name = (U_CHAR *) (temp + 1);
7143 bcopy ((char *) (pfile->token_buffer + name_written),
7144 (char *) temp->name, length);
7145 temp->name[length] = 0;
7146 temp->next = token_ptrs;
7148 temp->length = length;
7150 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7152 if (c == EOF || c == '\n')
7155 "unterminated token sequence following `#' operator");
7160 /* We accumulated the names in reverse order.
7161 Now reverse them to get the proper order. */
7162 return reverse_token_list (token_ptrs);
7167 struct arglist *tokens)
7170 struct arglist *next = tokens->next;
7171 free (tokens->name);
7177 /* Get the file-mode and data size of the file open on FD
7178 and store them in *MODE_POINTER and *SIZE_POINTER. */
7181 file_size_and_mode (
7184 long int *size_pointer)
7188 if (fstat (fd, &sbuf) < 0) return (-1);
7189 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7190 if (size_pointer) *size_pointer = sbuf.st_size;
7194 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7195 retrying if necessary. Return a negative value if an error occurs,
7196 otherwise return the actual number of bytes read,
7197 which must be LEN unless end-of-file was reached. */
7207 int nchars = read (desc, ptr, left);
7228 unsigned size = strlen (input);
7229 char *output = xmalloc (size + 1);
7230 strcpy (output, input);
7234 /* Initialize PMARK to remember the current position of PFILE. */
7237 struct parse_marker *pmark,
7240 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7241 pmark->next = pbuf->marks;
7242 pbuf->marks = pmark;
7244 pmark->position = pbuf->cur - pbuf->buf;
7247 /* Cleanup PMARK - we no longer need it. */
7250 struct parse_marker *pmark)
7252 struct parse_marker **pp = &pmark->buf->marks;
7253 for (; ; pp = &(*pp)->next) {
7254 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7255 if (*pp == pmark) break;
7260 /* Backup the current position of PFILE to that saved in PMARK. */
7264 struct parse_marker *pmark,
7267 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7268 if (pbuf != pmark->buf)
7269 fatal ("internal error %s", "parse_goto_mark");
7270 pbuf->cur = pbuf->buf + pmark->position;
7273 /* Reset PMARK to point to the current position of PFILE. (Same
7274 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7278 struct parse_marker *pmark,
7281 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7282 if (pbuf != pmark->buf)
7283 fatal ("internal error %s", "parse_move_mark");
7284 pmark->position = pbuf->cur - pbuf->buf;
7288 cpp_read_check_assertion (
7291 int name_start = CPP_WRITTEN (pfile);
7292 int name_length, name_written;
7294 FORWARD (1); /* Skip '#' */
7295 cpp_skip_hspace (pfile);
7296 parse_name (pfile, GETC ());
7297 name_written = CPP_WRITTEN (pfile);
7298 name_length = name_written - name_start;
7299 cpp_skip_hspace (pfile);
7300 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7303 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7304 result = check_assertion (pfile,
7305 pfile->token_buffer + name_start, name_length,
7309 result = check_assertion (pfile,
7310 pfile->token_buffer + name_start, name_length,
7312 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7317 cpp_print_file_and_line (pfile)
7320 cpp_buffer *ip = cpp_file_buffer (pfile);
7325 cpp_buf_line_and_col (ip, &line, &col);
7326 cpp_file_line_for_message (pfile, ip->nominal_fname,
7327 line, pfile->show_column ? col : -1);
7332 cpp_error (pfile, msg, arg1, arg2, arg3)
7335 char *arg1, *arg2, *arg3;
7337 cpp_print_containing_files (pfile);
7338 cpp_print_file_and_line (pfile);
7339 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7342 /* Print error message but don't count it. */
7345 cpp_warning (pfile, msg, arg1, arg2, arg3)
7348 char *arg1, *arg2, *arg3;
7350 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7353 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7356 cpp_print_containing_files (pfile);
7357 cpp_print_file_and_line (pfile);
7358 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7361 /* Print an error message and maybe count it. */
7364 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7367 char *arg1, *arg2, *arg3;
7369 if (CPP_OPTIONS (pfile)->pedantic_errors)
7370 cpp_error (pfile, msg, arg1, arg2, arg3);
7372 cpp_warning (pfile, msg, arg1, arg2, arg3);
7376 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7380 char *arg1, *arg2, *arg3;
7383 cpp_buffer *ip = cpp_file_buffer (pfile);
7385 cpp_print_containing_files (pfile);
7388 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7390 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7394 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7398 char *arg1, *arg2, *arg3;
7403 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7406 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7409 cpp_print_containing_files (pfile);
7411 ip = cpp_file_buffer (pfile);
7414 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7416 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7420 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7424 char *arg1, *arg2, *arg3;
7426 if (CPP_OPTIONS (pfile)->pedantic_errors)
7427 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7429 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7432 /* Report a warning (or an error if pedantic_errors)
7433 giving specified file name and line number, not current. */
7436 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7441 char *arg1, *arg2, *arg3;
7443 if (!CPP_OPTIONS (pfile)->pedantic_errors
7444 && CPP_OPTIONS (pfile)->inhibit_warnings)
7447 cpp_file_line_for_message (pfile, file, line, -1);
7448 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7449 msg, arg1, arg2, arg3);
7452 /* This defines "errno" properly for VMS, and gives us EACCES. */
7459 #ifndef HAVE_STRERROR
7460 extern int sys_nerr;
7462 extern const char *const sys_errlist[];
7465 extern char *sys_errlist[];
7468 #else /* HAVE_STRERROR */
7472 char *strerror (int,...);
7476 * my_strerror - return the descriptive text associated with an `errno' code.
7480 my_strerror (errnum)
7486 #ifndef HAVE_STRERROR
7487 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7489 result = strerror (errnum);
7492 /* VAXCRTL's strerror() takes an optional second argument, which only
7493 matters when the first argument is EVMSERR. However, it's simplest
7494 just to pass it unconditionally. `vaxc$errno' is declared in
7495 <errno.h>, and maintained by the library in parallel with `errno'.
7496 We assume that caller's `errnum' either matches the last setting of
7497 `errno' by the library or else does not have the value `EVMSERR'. */
7499 result = strerror (errnum, vaxc$errno);
7503 result = "undocumented I/O error";
7508 /* Error including a message from `errno'. */
7511 cpp_error_from_errno (
7518 cpp_buffer *ip = cpp_file_buffer (pfile);
7520 cpp_print_containing_files (pfile);
7523 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7525 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7529 cpp_perror_with_name (
7533 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7537 * No pre-compiled header file support.
7539 * Possibly different enum token codes for each C/C++ token.
7541 * Should clean up remaining directives to that do_XXX functions
7542 * only take two arguments and all have command_reads_line.
7544 * Find and cleanup remaining uses of static variables,
7546 * Support for trigraphs.
7548 * Support -dM flag (dump_all_macros).
7550 * Support for_lint flag.