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 ':'
86 #include <sys/time.h> /* for __DATE__ and __TIME__ */
87 #include <sys/resource.h>
89 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
90 // #include <sys/times.h>
97 /* This defines "errno" properly for VMS, and gives us EACCES. */
100 extern char *index ();
101 extern char *rindex ();
109 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
110 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
112 /* Find the largest host integer type and set its size and type. */
114 #ifndef HOST_BITS_PER_WIDE_INT
116 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
117 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
118 #define HOST_WIDE_INT long
120 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
121 #define HOST_WIDE_INT int
127 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
131 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
134 /* Define a generic NULL if one hasn't already been defined. */
141 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
142 #define GENERIC_PTR void *
144 #define GENERIC_PTR char *
149 #define NULL_PTR ((GENERIC_PTR)0)
152 #ifndef INCLUDE_LEN_FUDGE
153 #define INCLUDE_LEN_FUDGE 0
156 /* Symbols to predefine. */
158 #ifdef CPP_PREDEFINES
159 static char *predefs = CPP_PREDEFINES;
161 static char *predefs = "";
164 /* We let tm.h override the types used here, to handle trivial differences
165 such as the choice of unsigned int or long unsigned int for size_t.
166 When machines start needing nontrivial differences in the size type,
167 it would be best to do something here to figure out automatically
168 from other information what type to use. */
170 /* The string value for __SIZE_TYPE__. */
173 #define SIZE_TYPE "long unsigned int"
176 /* The string value for __PTRDIFF_TYPE__. */
179 #define PTRDIFF_TYPE "long int"
182 /* The string value for __WCHAR_TYPE__. */
185 #define WCHAR_TYPE "int"
187 #define CPP_WCHAR_TYPE(PFILE) \
188 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
190 /* The string value for __USER_LABEL_PREFIX__ */
192 #ifndef USER_LABEL_PREFIX
193 #define USER_LABEL_PREFIX ""
196 /* The string value for __REGISTER_PREFIX__ */
198 #ifndef REGISTER_PREFIX
199 #define REGISTER_PREFIX ""
203 /* In the definition of a #assert name, this structure forms
204 a list of the individual values asserted.
205 Each value is itself a list of "tokens".
206 These are strings that are compared by name. */
208 struct tokenlist_list {
209 struct tokenlist_list *next;
210 struct arglist *tokens;
213 struct assertion_hashnode {
214 struct assertion_hashnode *next; /* double links for easy deletion */
215 struct assertion_hashnode *prev;
216 /* also, a back pointer to this node's hash
217 chain is kept, in case the node is the head
218 of the chain and gets deleted. */
219 struct assertion_hashnode **bucket_hdr;
220 int length; /* length of token, for quick comparison */
221 U_CHAR *name; /* the actual name */
222 /* List of token-sequences. */
223 struct tokenlist_list *value;
226 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
227 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
229 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
230 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
231 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
232 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
233 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
234 (Note that it is false while we're expanding marco *arguments*.) */
235 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
237 /* Move all backslash-newline pairs out of embarrassing places.
238 Exchange all such pairs following BP
239 with any potentially-embarrassing characters that follow them.
240 Potentially-embarrassing characters are / and *
241 (because a backslash-newline inside a comment delimiter
242 would cause it not to be recognized). */
244 #define NEWLINE_FIX \
245 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
247 /* Same, but assume we've already read the potential '\\' into C. */
248 #define NEWLINE_FIX1(C) do { \
249 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
253 struct cpp_pending *next;
258 /* Forward declarations. */
260 extern char *xmalloc ();
262 static void add_import ();
263 static void append_include_chain ();
264 static void make_undef ();
265 static void make_assertion ();
266 static void path_include ();
267 static void initialize_builtins ();
268 static void initialize_char_syntax ();
269 static void dump_arg_n ();
270 static void dump_defn_1 ();
271 extern void delete_macro ();
272 static void trigraph_pcp ();
273 static int finclude ();
274 static void validate_else ();
275 static int comp_def_part ();
276 extern void fancy_abort ();
277 static void pipe_closed ();
278 static void print_containing_files ();
279 static int lookup_import ();
280 static int redundant_include_p ();
281 static is_system_include ();
282 static struct file_name_map *read_name_map ();
283 static char *read_filename_string ();
284 static int open_include_file ();
285 static int check_preconditions ();
286 static void pcfinclude ();
287 static void pcstring_used ();
288 static int check_macro_name ();
289 static int compare_defs ();
290 static int compare_token_lists ();
291 static HOST_WIDE_INT eval_if_expression ();
292 static int change_newlines ();
294 static int file_size_and_mode ();
295 static struct arglist *read_token_list ();
296 static void free_token_list ();
297 static int safe_read ();
298 static void push_macro_expansion PARAMS ((cpp_reader *,
299 U_CHAR*, int, HASHNODE*));
300 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
301 extern char *xrealloc ();
302 extern char *xcalloc ();
303 static char *savestring ();
305 static void conditional_skip ();
306 static void skip_if_group ();
308 /* Last arg to output_line_command. */
309 enum file_change_code {same_file, enter_file, leave_file};
311 /* External declarations. */
313 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
315 extern char *getenv ();
316 extern FILE *fdopen ();
317 extern char *version_string;
318 extern struct tm *localtime ();
320 /* These functions are declared to return int instead of void since they
321 are going to be placed in a table and some old compilers have trouble with
322 pointers to functions returning void. */
324 static int do_define ();
325 static int do_line ();
326 static int do_include ();
327 static int do_undef ();
328 static int do_error ();
329 static int do_pragma ();
330 static int do_ident ();
332 static int do_xifdef ();
333 static int do_else ();
334 static int do_elif ();
335 static int do_endif ();
336 static int do_sccs ();
337 static int do_once ();
338 static int do_assert ();
339 static int do_unassert ();
340 static int do_warning ();
342 struct file_name_list
344 struct file_name_list *next;
346 /* If the following is nonzero, it is a macro name.
347 Don't include the file again if that macro is defined. */
348 U_CHAR *control_macro;
349 /* If the following is nonzero, it is a C-language system include
351 int c_system_include_path;
352 /* Mapping of file names for this directory. */
353 struct file_name_map *name_map;
354 /* Non-zero if name_map is valid. */
358 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
359 via the same directory as the file that #included it. */
360 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
362 /* #include "file" looks in source file dir, then stack. */
363 /* #include <file> just looks in the stack. */
364 /* -I directories are added to the end, then the defaults are added. */
366 static struct default_include {
367 char *fname; /* The name of the directory. */
368 int cplusplus; /* Only look here if we're compiling C++. */
369 int cxx_aware; /* Includes in this directory don't need to
370 be wrapped in extern "C" when compiling
372 } include_defaults_array[]
373 #ifdef INCLUDE_DEFAULTS
377 /* Pick up GNU C++ specific include files. */
378 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
380 /* This is the dir for fixincludes. Put it just before
381 the files that we fix. */
382 { GCC_INCLUDE_DIR, 0, 0 },
383 /* For cross-compilation, this dir name is generated
384 automatically in Makefile.in. */
385 { CROSS_INCLUDE_DIR, 0, 0 },
386 /* This is another place that the target system's headers might be. */
387 { TOOL_INCLUDE_DIR, 0, 1 },
388 { LOCAL_INCLUDE_DIR, 0, 1 },
389 #else /* not CROSS_COMPILE */
390 /* This should be /usr/local/include and should come before
391 the fixincludes-fixed header files. */
392 { LOCAL_INCLUDE_DIR, 0, 1 },
393 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
394 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
395 { TOOL_INCLUDE_DIR, 0, 1 },
396 /* This is the dir for fixincludes. Put it just before
397 the files that we fix. */
398 { GCC_INCLUDE_DIR, 0, 0 },
399 /* Some systems have an extra dir of include files. */
400 #ifdef SYSTEM_INCLUDE_DIR
401 { SYSTEM_INCLUDE_DIR, 0, 0 },
403 { STANDARD_INCLUDE_DIR, 0, 0 },
404 #endif /* not CROSS_COMPILE */
407 #endif /* no INCLUDE_DEFAULTS */
409 /* `struct directive' defines one #-directive, including how to handle it. */
412 int length; /* Length of name */
413 int (*func)(); /* Function to handle directive */
414 char *name; /* Name of directive */
415 enum node_type type; /* Code which describes which directive. */
416 char command_reads_line; /* One if rest of line is read by func. */
417 char traditional_comments; /* Nonzero: keep comments if -traditional. */
418 char pass_thru; /* Copy preprocessed directive to output file.*/
421 /* Here is the actual list of #-directives, most-often-used first.
422 The initialize_builtins function assumes #define is the very first. */
424 static struct directive directive_table[] = {
425 { 6, do_define, "define", T_DEFINE, 0, 1},
426 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
427 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
428 { 7, do_include, "include", T_INCLUDE, 1},
429 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
430 { 6, do_include, "import", T_IMPORT, 1},
431 { 5, do_endif, "endif", T_ENDIF, 1},
432 { 4, do_else, "else", T_ELSE, 1},
433 { 2, do_if, "if", T_IF, 1},
434 { 4, do_elif, "elif", T_ELIF, 1},
435 { 5, do_undef, "undef", T_UNDEF},
436 { 5, do_error, "error", T_ERROR},
437 { 7, do_warning, "warning", T_WARNING},
438 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
439 { 4, do_line, "line", T_LINE, 1},
440 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
441 #ifdef SCCS_DIRECTIVE
442 { 4, do_sccs, "sccs", T_SCCS},
444 { 6, do_assert, "assert", T_ASSERT, 1},
445 { 8, do_unassert, "unassert", T_UNASSERT, 1},
446 { -1, 0, "", T_UNUSED},
449 /* table to tell if char can be part of a C identifier. */
450 U_CHAR is_idchar[256];
451 /* table to tell if char can be first char of a c identifier. */
452 U_CHAR is_idstart[256];
453 /* table to tell if c is horizontal space. */
454 U_CHAR is_hor_space[256];
455 /* table to tell if c is horizontal or vertical space. */
456 static U_CHAR is_space[256];
458 /* Initialize syntactic classifications of characters. */
461 initialize_char_syntax (
462 struct cpp_options *opts)
467 * Set up is_idchar and is_idstart tables. These should be
468 * faster than saying (is_alpha (c) || c == '_'), etc.
469 * Set up these things before calling any routines tthat
472 for (i = 'a'; i <= 'z'; i++) {
473 is_idchar[i - 'a' + 'A'] = 1;
475 is_idstart[i - 'a' + 'A'] = 1;
478 for (i = '0'; i <= '9'; i++)
482 is_idchar['$'] = opts->dollars_in_ident;
483 is_idstart['$'] = opts->dollars_in_ident;
485 /* horizontal space table */
486 is_hor_space[' '] = 1;
487 is_hor_space['\t'] = 1;
488 is_hor_space['\v'] = 1;
489 is_hor_space['\f'] = 1;
490 is_hor_space['\r'] = 1;
501 /* Place into PFILE a quoted string representing the string SRC.
502 Caller must reserve enough space in pfile->token_buffer. */
510 CPP_PUTC_Q (pfile, '\"');
512 switch ((c = *src++))
516 CPP_PUTC_Q (pfile, c);
519 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
520 CPP_ADJUST_WRITTEN (pfile, 4);
526 CPP_PUTC_Q (pfile, '\\');
527 CPP_PUTC_Q (pfile, c);
531 CPP_PUTC_Q (pfile, '\"');
532 CPP_NUL_TERMINATE_Q (pfile);
537 /* Make sure PFILE->token_buffer will hold at least N more chars. */
544 long old_written = CPP_WRITTEN (pfile);
545 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
546 pfile->token_buffer = (U_CHAR*)
547 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
548 CPP_SET_WRITTEN (pfile, old_written);
553 * process a given definition string, for initialization
554 * If STR is just an identifier, define it with value 1.
555 * If STR has anything after the identifier, then it should
556 * be identifier=definition.
570 cpp_error (pfile, "malformed option `-D %s'", str);
573 while (is_idchar[*++p])
577 buf = (U_CHAR *) alloca (p - buf + 4);
578 strcpy ((char *)buf, str);
579 strcat ((char *)buf, " 1");
583 cpp_error (pfile, "malformed option `-D %s'", str);
589 /* Copy the entire option so we can modify it. */
590 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
591 strncpy (buf, str, p - str);
592 /* Change the = to a space. */
594 /* Scan for any backslash-newline and remove it. */
599 if (*p == '\\' && p[1] == '\n')
607 do_define (pfile, NULL, buf, buf + strlen (buf));
610 /* Process the string STR as if it appeared as the body of a #assert.
611 OPTION is the option name for which STR was the argument. */
620 struct directive *kt;
623 /* Copy the entire option so we can modify it. */
624 buf = (U_CHAR *) alloca (strlen (str) + 1);
625 strcpy ((char *) buf, str);
626 /* Scan for any backslash-newline and remove it. */
630 if (*p == '\\' && p[1] == '\n')
639 if (!is_idstart[*p]) {
640 cpp_error (pfile, "malformed option `%s %s'", option, str);
643 while (is_idchar[*++p])
645 while (*p == ' ' || *p == '\t') p++;
646 if (! (*p == 0 || *p == '(')) {
647 cpp_error (pfile, "malformed option `%s %s'", option, str);
651 ip = cpp_push_buffer (pfile, buf, strlen (buf));
652 do_assert (pfile, NULL, NULL, NULL);
653 cpp_pop_buffer (pfile);
656 /* Append a chain of `struct file_name_list's
657 to the end of the main include chain.
658 FIRST is the beginning of the chain to append, and LAST is the end. */
661 append_include_chain (
663 struct file_name_list *first,struct file_name_list *last)
665 struct cpp_options *opts = CPP_OPTIONS (pfile);
666 struct file_name_list *dir;
671 if (opts->include == 0)
672 opts->include = first;
674 opts->last_include->next = first;
676 if (opts->first_bracket_include == 0)
677 opts->first_bracket_include = first;
679 for (dir = first; ; dir = dir->next) {
680 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
681 if (len > pfile->max_include_len)
682 pfile->max_include_len = len;
688 opts->last_include = last;
691 /* Add output to `deps_buffer' for the -M switch.
692 STRING points to the text to be output.
693 SPACER is ':' for targets, ' ' for dependencies, zero for text
694 to be inserted literally. */
702 int size = strlen (string);
707 #ifndef MAX_OUTPUT_COLUMNS
708 #define MAX_OUTPUT_COLUMNS 72
711 && pfile->deps_column > 0
712 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
714 deps_output (pfile, " \\\n ", 0);
715 pfile->deps_column = 0;
718 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
720 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
721 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
722 pfile->deps_allocated_size);
724 if (spacer == ' ' && pfile->deps_column > 0)
725 pfile->deps_buffer[pfile->deps_size++] = ' ';
726 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
727 pfile->deps_size += size;
728 pfile->deps_column += size;
730 pfile->deps_buffer[pfile->deps_size++] = ':';
731 pfile->deps_buffer[pfile->deps_size] = 0;
734 /* Given a colon-separated list of file names PATH,
735 add all the names to the search path for include files. */
750 struct file_name_list *dirtmp;
752 /* Find the end of this name. */
753 while (*q != 0 && *q != PATH_SEPARATOR) q++;
755 /* An empty name in the path stands for the current directory. */
756 name = (char *) xmalloc (2);
760 /* Otherwise use the directory that is named. */
761 name = (char *) xmalloc (q - p + 1);
762 bcopy (p, name, q - p);
766 dirtmp = (struct file_name_list *)
767 xmalloc (sizeof (struct file_name_list));
768 dirtmp->next = 0; /* New one goes on the end */
769 dirtmp->control_macro = 0;
770 dirtmp->c_system_include_path = 0;
771 dirtmp->fname = name;
772 dirtmp->got_name_map = 0;
773 append_include_chain (pfile, dirtmp, dirtmp);
775 /* Advance past this name. */
779 /* Skip the colon. */
786 struct cpp_options *opts)
788 bzero ((char *) opts, sizeof *opts);
789 opts->in_fname = NULL;
790 opts->out_fname = NULL;
792 /* Initialize is_idchar to allow $. */
793 opts->dollars_in_ident = 1;
794 initialize_char_syntax (opts);
795 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
797 opts->no_line_commands = 0;
798 opts->no_trigraphs = 1;
799 opts->put_out_comments = 0;
800 opts->print_include_names = 0;
801 opts->dump_macros = dump_none;
804 opts->cplusplus_comments = 0;
811 opts->pedantic_errors = 0;
812 opts->inhibit_warnings = 0;
813 opts->warn_comments = 0;
814 opts->warn_import = 1;
815 opts->warnings_are_errors = 0;
838 HASHNODE *macro = (HASHNODE*)pbuf->data;
839 if (macro->type == T_DISABLED)
840 macro->type = T_MACRO;
841 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
866 register U_CHAR *p = bp;
868 /* First count the backslash-newline pairs here. */
870 while (p[0] == '\\' && p[1] == '\n')
873 /* What follows the backslash-newlines is not embarrassing. */
875 if (*p != '/' && *p != '*')
878 /* Copy all potentially embarrassing characters
879 that follow the backslash-newline pairs
880 down to where the pairs originally started. */
882 while (*p == '*' || *p == '/')
885 /* Now write the same number of pairs after the embarrassing chars. */
893 /* Assuming we have read '/'.
894 If this is the start of a comment (followed by '*' or '/'),
895 skip to the end of the comment, and return ' '.
896 Return EOF if we reached the end of file before the end of the comment.
897 If not the start of a comment, return '/'. */
905 while (PEEKC() == '\\' && PEEKN(1) == '\n')
920 while (c == '\\' && PEEKC() == '\n')
924 FORWARD(1), c = GETC();
926 if (prev_c == '*' && c == '/')
928 if (c == '\n' && linep)
932 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
939 return ' '; /* Allow // to be terminated by EOF. */
940 while (c == '\\' && PEEKC() == '\n')
949 /* Don't consider final '\n' to be part of comment. */
959 /* Skip whitespace \-newline and comments. Does not macro-expand. */
971 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
972 cpp_pedwarn (pfile, "%s in preprocessing directive",
973 c == '\f' ? "formfeed" : "vertical tab");
979 c = skip_comment (pfile, NULL);
982 if (c == EOF || c == '/')
985 else if (c == '\\' && PEEKN(1) == '\n') {
988 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
989 && is_hor_space[PEEKN(1)])
995 /* Read the rest of the current line.
996 The line is appended to PFILE's output buffer. */
1002 struct cpp_options *opts = CPP_OPTIONS (pfile);
1012 if (PEEKC() == '\n')
1019 goto scan_directive_token;
1023 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
1024 goto scan_directive_token;
1028 if (CPP_PEDANTIC (pfile))
1029 cpp_pedwarn (pfile, "%s in preprocessing directive",
1030 c == '\f' ? "formfeed" : "vertical tab");
1036 scan_directive_token:
1038 cpp_get_token (pfile);
1041 CPP_PUTC (pfile, c);
1044 CPP_NUL_TERMINATE (pfile);
1051 long old = CPP_WRITTEN (pfile);
1052 copy_rest_of_line (pfile);
1053 CPP_SET_WRITTEN (pfile, old);
1056 /* Handle a possible # directive.
1057 '#' has already been read. */
1063 register struct directive *kt;
1066 U_CHAR *ident, *line_end;
1067 long old_written = CPP_WRITTEN (pfile);
1069 cpp_skip_hspace (pfile);
1072 if (c >= '0' && c <= '9')
1074 /* Handle # followed by a line number. */
1075 if (CPP_PEDANTIC (pfile))
1076 cpp_pedwarn (pfile, "`#' followed by integer");
1077 do_line (pfile, NULL);
1078 goto done_a_directive;
1081 /* Now find the directive name. */
1082 CPP_PUTC (pfile, '#');
1083 parse_name (pfile, GETC());
1084 ident = pfile->token_buffer + old_written + 1;
1085 ident_length = CPP_PWRITTEN (pfile) - ident;
1086 if (ident_length == 0 && PEEKC() == '\n')
1088 /* A line of just `#' becomes blank. */
1089 goto done_a_directive;
1093 if (ident_length == 0 || !is_idstart[*ident]) {
1095 while (is_idchar[*p]) {
1096 if (*p < '0' || *p > '9')
1100 /* Avoid error for `###' and similar cases unless -pedantic. */
1102 while (*p == '#' || is_hor_space[*p]) p++;
1104 if (pedantic && !lang_asm)
1105 cpp_warning (pfile, "invalid preprocessor directive");
1111 cpp_error (pfile, "invalid preprocessor directive name");
1117 * Decode the keyword and call the appropriate expansion
1118 * routine, after moving the input pointer up to the next line.
1120 for (kt = directive_table; ; kt++) {
1121 if (kt->length <= 0)
1122 goto not_a_directive;
1123 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1127 if (! kt->command_reads_line)
1129 /* Nonzero means do not delete comments within the directive.
1130 #define needs this when -traditional. */
1131 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1132 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1133 CPP_OPTIONS (pfile)->put_out_comments = comments;
1134 after_ident = CPP_WRITTEN (pfile);
1135 copy_rest_of_line (pfile);
1136 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1139 /* For #pragma and #define, we may want to pass through the directive.
1140 Other directives may create output, but we don't want the directive
1141 itself out, so we pop it now. For example #include may write a
1142 command (see comment in do_include), and conditionals may emit
1143 #failed ... #endfailed stuff. But note that popping the buffer
1144 means the parameters to kt->func may point after pfile->limit
1145 so these parameters are invalid as soon as something gets appended
1146 to the token_buffer. */
1148 line_end = CPP_PWRITTEN (pfile);
1149 if (!kt->pass_thru && kt->type != T_DEFINE)
1150 CPP_SET_WRITTEN (pfile, old_written);
1152 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1154 || (kt->type == T_DEFINE
1155 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1157 /* Just leave the entire #define in the output stack. */
1159 else if (kt->type == T_DEFINE
1160 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1162 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1163 SKIP_WHITE_SPACE (p);
1164 while (is_idchar[*p]) p++;
1166 CPP_PUTC (pfile, '\n');
1168 else if (kt->type == T_DEFINE)
1169 CPP_SET_WRITTEN (pfile, old_written);
1177 /* Pass a directive through to the output file.
1178 BUF points to the contents of the directive, as a contiguous string.
1179 LIMIT points to the first character past the end of the directive.
1180 KEYWORD is the keyword-table entry for the directive. */
1183 pass_thru_directive (
1184 U_CHAR *buf, U_CHAR *limit,
1186 struct directive *keyword)
1188 register unsigned keyword_length = keyword->length;
1190 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1191 CPP_PUTC_Q (pfile, '#');
1192 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1193 if (limit != buf && buf[0] != ' ')
1194 CPP_PUTC_Q (pfile, ' ');
1195 CPP_PUTS_Q (pfile, buf, limit - buf);
1197 CPP_PUTS_Q (pfile, '\n');
1198 /* Count the line we have just made in the output,
1199 to get in sync properly. */
1204 /* The arglist structure is built by do_define to tell
1205 collect_definition where the argument names begin. That
1206 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1207 would contain pointers to the strings x, y, and z.
1208 Collect_definition would then build a DEFINITION node,
1209 with reflist nodes pointing to the places x, y, and z had
1210 appeared. So the arglist is just convenience data passed
1211 between these two routines. It is not kept around after
1212 the current #define has been processed and entered into the
1216 struct arglist *next;
1223 /* Read a replacement list for a macro with parameters.
1224 Build the DEFINITION structure.
1225 Reads characters of text starting at BUF until END.
1226 ARGLIST specifies the formal parameters to look for
1227 in the text of the definition; NARGS is the number of args
1228 in that list, or -1 for a macro name that wants no argument list.
1229 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1230 and NAMELEN is its length in characters.
1232 Note that comments, backslash-newlines, and leading white space
1233 have already been deleted from the argument. */
1238 U_CHAR *buf, U_CHAR *limit,
1240 struct arglist *arglist)
1243 register U_CHAR *p, *lastp, *exp_p;
1244 struct reflist *endpat = NULL;
1245 /* Pointer to first nonspace after last ## seen. */
1247 /* Pointer to first nonspace after last single-# seen. */
1248 U_CHAR *stringify = 0;
1250 int expected_delimiter = '\0';
1252 /* Scan thru the replacement list, ignoring comments and quoted
1253 strings, picking up on the macro calls. It does a linear search
1254 thru the arg list on every potential symbol. Profiling might say
1255 that something smarter should happen. */
1260 /* Find the beginning of the trailing whitespace. */
1262 while (p < limit && is_space[limit[-1]]) limit--;
1264 /* Allocate space for the text in the macro definition.
1265 Leading and trailing whitespace chars need 2 bytes each.
1266 Each other input char may or may not need 1 byte,
1267 so this is an upper bound. The extra 5 are for invented
1268 leading and trailing newline-marker and final null. */
1269 maxsize = (sizeof (DEFINITION)
1271 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1275 defn = (DEFINITION *) xcalloc (1, maxsize);
1277 defn->nargs = nargs;
1278 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1283 /* Add one initial space escape-marker to prevent accidental
1284 token-pasting (often removed by macroexpand). */
1288 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1289 cpp_error (pfile, "`##' at start of macro definition");
1293 /* Process the main body of the definition. */
1295 int skipped_arg = 0;
1296 register U_CHAR c = *p++;
1300 if (!CPP_TRADITIONAL (pfile)) {
1304 if (expected_delimiter != '\0') {
1305 if (c == expected_delimiter)
1306 expected_delimiter = '\0';
1308 expected_delimiter = c;
1312 if (p < limit && expected_delimiter) {
1313 /* In a string, backslash goes through
1314 and makes next char ordinary. */
1320 /* An '@' in a string or character constant stands for itself,
1321 and does not need to be escaped. */
1322 if (!expected_delimiter)
1327 /* # is ordinary inside a string. */
1328 if (expected_delimiter)
1330 if (p < limit && *p == '#') {
1331 /* ##: concatenate preceding and following tokens. */
1332 /* Take out the first #, discard preceding whitespace. */
1334 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1336 /* Skip the second #. */
1338 /* Discard following whitespace. */
1339 SKIP_WHITE_SPACE (p);
1342 cpp_error (pfile, "`##' at end of macro definition");
1343 } else if (nargs >= 0) {
1344 /* Single #: stringify following argument ref.
1345 Don't leave the # in the expansion. */
1347 SKIP_WHITE_SPACE (p);
1348 if (p == limit || ! is_idstart[*p])
1350 "`#' operator is not followed by a macro argument name");
1357 /* In -traditional mode, recognize arguments inside strings and
1358 and character constants, and ignore special properties of #.
1359 Arguments inside strings are considered "stringified", but no
1360 extra quote marks are supplied. */
1364 if (expected_delimiter != '\0') {
1365 if (c == expected_delimiter)
1366 expected_delimiter = '\0';
1368 expected_delimiter = c;
1372 /* Backslash quotes delimiters and itself, but not macro args. */
1373 if (expected_delimiter != 0 && p < limit
1374 && (*p == expected_delimiter || *p == '\\')) {
1381 if (expected_delimiter != '\0') /* No comments inside strings. */
1384 /* If we find a comment that wasn't removed by handle_directive,
1385 this must be -traditional. So replace the comment with
1389 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1392 /* Mark this as a concatenation-point, as if it had been ##. */
1400 /* Handle the start of a symbol. */
1401 if (is_idchar[c] && nargs > 0) {
1402 U_CHAR *id_beg = p - 1;
1406 while (p != limit && is_idchar[*p]) p++;
1407 id_len = p - id_beg;
1409 if (is_idstart[c]) {
1410 register struct arglist *arg;
1412 for (arg = arglist; arg != NULL; arg = arg->next) {
1413 struct reflist *tpat;
1415 if (arg->name[0] == c
1416 && arg->length == id_len
1417 && strncmp (arg->name, id_beg, id_len) == 0) {
1418 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1419 if (CPP_TRADITIONAL (pfile)) {
1420 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1424 "macro arg `%.*s' would be stringified with -traditional.",
1428 /* If ANSI, don't actually substitute inside a string. */
1429 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1431 /* make a pat node for this arg and append it to the end of
1433 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1435 tpat->raw_before = concat == id_beg;
1436 tpat->raw_after = 0;
1437 tpat->rest_args = arg->rest_args;
1438 tpat->stringify = (CPP_TRADITIONAL (pfile)
1439 ? expected_delimiter != '\0'
1440 : stringify == id_beg);
1443 defn->pattern = tpat;
1445 endpat->next = tpat;
1448 tpat->argno = arg->argno;
1449 tpat->nchars = exp_p - lastp;
1451 register U_CHAR *p1 = p;
1452 SKIP_WHITE_SPACE (p1);
1453 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1454 tpat->raw_after = 1;
1456 lastp = exp_p; /* place to start copying from next time */
1463 /* If this was not a macro arg, copy it into the expansion. */
1464 if (! skipped_arg) {
1465 register U_CHAR *lim1 = p;
1469 if (stringify == id_beg)
1471 "`#' operator should be followed by a macro argument name");
1476 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1478 /* If ANSI, put in a "@ " marker to prevent token pasting.
1479 But not if "inside a string" (which in ANSI mode
1480 happens only for -D option). */
1487 defn->length = exp_p - defn->expansion;
1489 /* Crash now if we overrun the allocated size. */
1490 if (defn->length + 1 > maxsize)
1494 /* This isn't worth the time it takes. */
1495 /* give back excess storage */
1496 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1503 * special extension string that can be added to the last macro argument to
1504 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1505 * #define wow(a, b...) process (b, a, b)
1506 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1507 * { wow (one, two); } -> { process (two, one, two); }
1508 * if this "rest_arg" is used with the concat token '##' and if it is not
1509 * supplied then the token attached to with ## will not be outputted. Ex:
1510 * #define wow (a, b...) process (b ## , a, ## b)
1511 * { wow (1, 2); } -> { process (2, 1, 2); }
1512 * { wow (one); } -> { process (one); {
1514 static char rest_extension[] = "...";
1515 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1517 /* Create a DEFINITION node from a #define directive. Arguments are
1518 as for do_define. */
1521 U_CHAR *buf, U_CHAR *limit,
1525 U_CHAR *bp; /* temp ptr into input buffer */
1526 U_CHAR *symname; /* remember where symbol name starts */
1527 int sym_length; /* and how long it is */
1530 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1532 int arglengths = 0; /* Accumulate lengths of arg names
1533 plus number of args. */
1535 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1539 while (is_hor_space[*bp])
1542 symname = bp; /* remember where it starts */
1544 sym_length = check_macro_name (pfile, bp, "macro");
1547 /* Lossage will occur if identifiers or control keywords are broken
1548 across lines using backslash. This is not the right place to take
1552 struct arglist *arg_ptrs = NULL;
1555 bp++; /* skip '(' */
1556 SKIP_WHITE_SPACE (bp);
1558 /* Loop over macro argument names. */
1559 while (*bp != ')') {
1560 struct arglist *temp;
1562 temp = (struct arglist *) alloca (sizeof (struct arglist));
1564 temp->next = arg_ptrs;
1565 temp->argno = argno++;
1566 temp->rest_args = 0;
1570 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1572 if (!is_idstart[*bp])
1573 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1575 /* Find the end of the arg name. */
1576 while (is_idchar[*bp]) {
1578 /* do we have a "special" rest-args extension here? */
1579 if (limit - bp > REST_EXTENSION_LENGTH &&
1580 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1582 temp->rest_args = 1;
1586 temp->length = bp - temp->name;
1588 bp += REST_EXTENSION_LENGTH;
1589 arglengths += temp->length + 2;
1590 SKIP_WHITE_SPACE (bp);
1591 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1592 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1597 SKIP_WHITE_SPACE (bp);
1600 cpp_error (pfile, "unterminated parameter list in `#define'");
1604 struct arglist *otemp;
1606 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1607 if (temp->length == otemp->length &&
1608 strncmp (temp->name, otemp->name, temp->length) == 0) {
1611 name = (U_CHAR *) alloca (temp->length + 1);
1612 (void) strncpy (name, temp->name, temp->length);
1613 name[temp->length] = '\0';
1615 "duplicate argument name `%s' in `#define'", name);
1621 ++bp; /* skip paren */
1622 SKIP_WHITE_SPACE (bp);
1623 /* now everything from bp before limit is the definition. */
1624 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1625 defn->rest_args = rest_args;
1627 /* Now set defn->args.argnames to the result of concatenating
1628 the argument names in reverse order
1629 with comma-space between them. */
1630 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1632 struct arglist *temp;
1634 for (temp = arg_ptrs; temp; temp = temp->next) {
1635 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1637 if (temp->next != 0) {
1638 defn->args.argnames[i++] = ',';
1639 defn->args.argnames[i++] = ' ';
1642 defn->args.argnames[i] = 0;
1645 /* Simple expansion or empty definition. */
1649 if (is_hor_space[*bp]) {
1651 SKIP_WHITE_SPACE (bp);
1654 case '!': case '"': case '#': case '%': case '&': case '\'':
1655 case ')': case '*': case '+': case ',': case '-': case '.':
1656 case '/': case ':': case ';': case '<': case '=': case '>':
1657 case '?': case '[': case '\\': case ']': case '^': case '{':
1658 case '|': case '}': case '~':
1659 cpp_warning (pfile, "missing white space after `#define %.*s'",
1660 sym_length, symname);
1664 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1665 sym_length, symname);
1670 /* now everything from bp before limit is the definition. */
1671 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1672 defn->args.argnames = (U_CHAR *) "";
1678 /* OP is null if this is a predefinition */
1679 defn->predefined = predefinition;
1681 mdef.symnam = symname;
1682 mdef.symlen = sym_length;
1691 /* Check a purported macro name SYMNAME, and yield its length.
1692 USAGE is the kind of name this is intended for. */
1703 for (p = symname; is_idchar[*p]; p++)
1705 sym_length = p - symname;
1706 if (sym_length == 0)
1707 cpp_error (pfile, "invalid %s name", usage);
1708 else if (!is_idstart[*symname]) {
1709 U_CHAR *msg; /* what pain... */
1710 msg = (U_CHAR *) alloca (sym_length + 1);
1711 bcopy (symname, msg, sym_length);
1712 msg[sym_length] = 0;
1713 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1715 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1716 cpp_error (pfile, "invalid %s name `defined'", usage);
1722 * return zero if two DEFINITIONs are isomorphic
1726 DEFINITION *d1, DEFINITION *d2)
1728 register struct reflist *a1, *a2;
1729 register U_CHAR *p1 = d1->expansion;
1730 register U_CHAR *p2 = d2->expansion;
1733 if (d1->nargs != d2->nargs)
1735 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1737 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1738 a1 = a1->next, a2 = a2->next) {
1739 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1740 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1741 || a1->argno != a2->argno
1742 || a1->stringify != a2->stringify
1743 || a1->raw_before != a2->raw_before
1744 || a1->raw_after != a2->raw_after)
1752 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1753 p2, d2->length - (p2 - d2->expansion), 1))
1758 /* Return 1 if two parts of two macro definitions are effectively different.
1759 One of the parts starts at BEG1 and has LEN1 chars;
1760 the other has LEN2 chars at BEG2.
1761 Any sequence of whitespace matches any other sequence of whitespace.
1762 FIRST means these parts are the first of a macro definition;
1763 so ignore leading whitespace entirely.
1764 LAST means these parts are the last of a macro definition;
1765 so ignore trailing whitespace entirely. */
1770 U_CHAR *beg1, int len1,
1771 U_CHAR *beg2, int len2 ,
1774 register U_CHAR *end1 = beg1 + len1;
1775 register U_CHAR *end2 = beg2 + len2;
1777 while (beg1 != end1 && is_space[*beg1]) beg1++;
1778 while (beg2 != end2 && is_space[*beg2]) beg2++;
1781 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1782 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1784 while (beg1 != end1 && beg2 != end2) {
1785 if (is_space[*beg1] && is_space[*beg2]) {
1786 while (beg1 != end1 && is_space[*beg1]) beg1++;
1787 while (beg2 != end2 && is_space[*beg2]) beg2++;
1788 } else if (*beg1 == *beg2) {
1792 return (beg1 != end1) || (beg2 != end2);
1795 /* Process a #define command.
1796 BUF points to the contents of the #define command, as a contiguous string.
1797 LIMIT points to the first character past the end of the definition.
1798 KEYWORD is the keyword-table entry for #define,
1799 or NULL for a "predefined" macro. */
1804 struct directive *keyword,
1805 U_CHAR *buf, U_CHAR *limit)
1812 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1813 if (pcp_outfile && keyword)
1814 pass_thru_directive (buf, limit, pfile, keyword);
1817 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1821 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1823 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1826 /* Redefining a precompiled key is ok. */
1827 if (hp->type == T_PCSTRING)
1829 /* Redefining a macro is ok if the definitions are the same. */
1830 else if (hp->type == T_MACRO)
1831 ok = ! compare_defs (mdef.defn, hp->value.defn);
1832 /* Redefining a constant is ok with -D. */
1833 else if (hp->type == T_CONST)
1834 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1835 /* Print the warning if it's not ok. */
1838 U_CHAR *msg; /* what pain... */
1840 /* If we are passing through #define and #undef directives, do
1841 that for this re-definition now. */
1842 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1843 pass_thru_directive (buf, limit, pfile, keyword);
1845 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1847 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1848 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1849 cpp_pedwarn (pfile, msg);
1850 if (hp->type == T_MACRO)
1851 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1852 "this is the location of the previous definition");
1854 /* Replace the old definition. */
1856 hp->value.defn = mdef.defn;
1860 /* If we are passing through #define and #undef directives, do
1861 that for this new definition now. */
1862 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1863 pass_thru_directive (buf, limit, pfile, keyword);
1864 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1865 (char *) mdef.defn, hashcode);
1875 /* This structure represents one parsed argument in a macro call.
1876 `raw' points to the argument text as written (`raw_length' is its length).
1877 `expanded' points to the argument's macro-expansion
1878 (its length is `expand_length').
1879 `stringified_length' is the length the argument would have
1881 `use_count' is the number of times this macro arg is substituted
1882 into the macro. If the actual use count exceeds 10,
1883 the value stored is 10. */
1885 /* raw and expanded are relative to ARG_BASE */
1886 #define ARG_BASE ((pfile)->token_buffer)
1889 /* Strings relative to pfile->token_buffer */
1890 long raw, expanded, stringified;
1891 int raw_length, expand_length;
1892 int stringified_length;
1904 #ifdef STATIC_BUFFERS
1905 register cpp_buffer *buf = CPP_BUFFER (pfile);
1906 if (buf == pfile->buffer_stack)
1907 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1909 bzero ((char *) buf, sizeof (cpp_buffer));
1910 CPP_BUFFER (pfile) = buf;
1912 register cpp_buffer *buf = (cpp_buffer*) xmalloc (sizeof(cpp_buffer));
1913 bzero ((char *) buf, sizeof (cpp_buffer));
1914 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1915 CPP_BUFFER (pfile) = buf;
1917 buf->if_stack = pfile->if_stack;
1918 buf->cleanup = null_cleanup;
1919 buf->underflow = null_underflow;
1920 buf->buf = buf->cur = buffer;
1921 buf->alimit = buf->rlimit = buffer + length;
1927 cpp_pop_buffer (pfile)
1930 cpp_buffer *buf = CPP_BUFFER (pfile);
1931 #ifdef STATIC_BUFFERS
1932 (*buf->cleanup) (buf, pfile);
1933 return ++CPP_BUFFER (pfile);
1935 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1936 (*buf->cleanup) (buf, pfile);
1937 CPP_BUFFER (pfile) = next_buf;
1943 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1944 Pop the buffer when done. */
1950 cpp_buffer *buffer = CPP_BUFFER (pfile);
1953 enum cpp_token token = cpp_get_token (pfile);
1954 if (token == CPP_EOF) /* Should not happen ... */
1956 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1958 cpp_pop_buffer (pfile);
1965 * Rescan a string (which may have escape marks) into pfile's buffer.
1966 * Place the result in pfile->token_buffer.
1968 * The input is copied before it is scanned, so it is safe to pass
1969 * it something from the token_buffer that will get overwritten
1970 * (because it follows CPP_WRITTEN). This is used by do_include.
1974 cpp_expand_to_buffer (
1979 register cpp_buffer *ip;
1981 U_CHAR *limit = buf + length;
1984 int odepth = indepth;
1990 /* Set up the input on the input stack. */
1992 buf1 = (U_CHAR *) alloca (length + 1);
1994 register U_CHAR *p1 = buf;
1995 register U_CHAR *p2 = buf1;
2002 ip = cpp_push_buffer (pfile, buf1, length);
2003 ip->has_escapes = 1;
2005 ip->lineno = obuf.lineno = 1;
2008 /* Scan the input, create the output. */
2009 cpp_scan_buffer (pfile);
2012 if (indepth != odepth)
2016 CPP_NUL_TERMINATE (pfile);
2031 (*linep)++, (*colp) = 1;
2037 /* Move line_base forward, updating lineno and colno. */
2041 register cpp_buffer *pbuf)
2043 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2044 unsigned char *new_pos = pbuf->cur;
2045 register struct parse_marker *mark;
2046 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2048 if (pbuf->buf + mark->position < new_pos)
2049 new_pos = pbuf->buf + mark->position;
2051 pbuf->line_base += new_pos - old_pos;
2052 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2056 cpp_buf_line_and_col (
2057 register cpp_buffer *pbuf,
2058 long *linep,long *colp)
2065 *linep = pbuf->lineno;
2066 *colp = pbuf->colno;
2067 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2076 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2082 cpp_buffer *ip = CPP_BUFFER (pfile);
2084 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2085 if (ip->fname != NULL)
2092 register U_CHAR *buf,
2093 register U_CHAR *limit)
2095 register long count = 0;
2106 * write out a #line command, for instance, after an #include file.
2107 * If CONDITIONAL is nonzero, we can omit the #line if it would
2108 * appear to be a no-op, and we can output a few newlines instead
2109 * if we want to increase the line number by a small amount.
2110 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2114 output_line_command (
2117 enum file_change_code file_change)
2120 char *line_cmd_buf, *line_end;
2122 cpp_buffer *ip = CPP_BUFFER (pfile);
2124 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2128 update_position (ip);
2129 line = CPP_BUFFER (pfile)->lineno;
2130 col = CPP_BUFFER (pfile)->colno;
2131 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2133 if (CPP_OPTIONS (pfile)->no_line_commands)
2137 if (line == pfile->lineno)
2140 /* If the inherited line number is a little too small,
2141 output some newlines instead of a #line command. */
2142 if (line > pfile->lineno && line < pfile->lineno + 8) {
2143 CPP_RESERVE (pfile, 20);
2144 while (line > pfile->lineno) {
2145 CPP_PUTC_Q (pfile, '\n');
2153 /* Don't output a line number of 0 if we can help it. */
2154 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2155 && *ip->bufp == '\n') {
2161 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2163 static char sharp_line[] = "#line ";
2165 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2168 sprintf (CPP_PWRITTEN (pfile), "%d ", line+2);
2169 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2171 // modification for SDC51
2172 if (*ip->nominal_fname == '\0')
2173 quote_string (pfile,"standard input");
2175 quote_string (pfile, ip->nominal_fname);
2176 if (file_change != same_file) {
2177 CPP_PUTC_Q (pfile, ' ');
2178 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2180 /* Tell cc1 if following text comes from a system header file. */
2181 if (ip->system_header_p) {
2182 CPP_PUTC_Q (pfile, ' ');
2183 CPP_PUTC_Q (pfile, '3');
2185 #ifndef NO_IMPLICIT_EXTERN_C
2186 /* Tell cc1plus if following text should be treated as C. */
2187 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2188 CPP_PUTC_Q (pfile, ' ');
2189 CPP_PUTC_Q (pfile, '4');
2192 CPP_PUTC_Q (pfile, '\n');
2193 pfile->lineno = line;
2197 * Parse a macro argument and append the info on PFILE's token_buffer.
2198 * REST_ARGS means to absorb the rest of the args.
2199 * Return nonzero to indicate a syntax error.
2202 static enum cpp_token
2208 enum cpp_token token;
2209 long arg_start = CPP_WRITTEN (pfile);
2210 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2211 CPP_OPTIONS (pfile)->put_out_comments = 0;
2213 /* Try to parse as much of the argument as exists at this
2214 input stack level. */
2215 pfile->no_macro_expand++;
2218 token = cpp_get_token (pfile);
2224 /* If we've hit end of file, it's an error (reported by caller).
2225 Ditto if it's the end of cpp_expand_to_buffer text.
2226 If we've hit end of macro, just continue. */
2227 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2238 /* if we've returned to lowest level and
2239 we aren't absorbing all args */
2240 if (paren == 0 && rest_args == 0)
2244 /* Remove ',' or ')' from argument buffer. */
2245 CPP_ADJUST_WRITTEN (pfile, -1);
2252 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2253 pfile->no_macro_expand--;
2258 /* Turn newlines to spaces in the string of length LENGTH at START,
2259 except inside of string constants.
2260 The string is copied into itself with its beginning staying fixed. */
2267 register U_CHAR *ibp;
2268 register U_CHAR *obp;
2269 register U_CHAR *limit;
2273 limit = start + length;
2276 while (ibp < limit) {
2277 *obp++ = c = *ibp++;
2282 /* Notice and skip strings, so that we don't delete newlines in them. */
2285 while (ibp < limit) {
2286 *obp++ = c = *ibp++;
2289 if (c == '\n' && quotec == '\'')
2305 if (!pfile->timebuf) {
2306 time_t t = time ((time_t *)0);
2307 pfile->timebuf = localtime (&t);
2309 return pfile->timebuf;
2312 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2313 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2317 * expand things like __FILE__. Place the expansion into the output
2318 * buffer *without* rescanning.
2329 cpp_buffer *ip = NULL;
2332 int paren = 0; /* For special `defined' keyword */
2335 if (pcp_outfile && pcp_inside_if
2336 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2338 "Predefined macro `%s' used inside `#if' during precompilation",
2342 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2346 cpp_error (pfile, "cccp error: not in any file?!");
2347 return; /* the show must go on */
2349 if (ip->fname != NULL)
2359 if (hp->type == T_BASE_FILE)
2361 while (CPP_PREV_BUFFER (ip))
2362 ip = CPP_PREV_BUFFER (ip);
2364 string = ip->nominal_fname;
2368 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2369 quote_string (pfile, string);
2373 case T_INCLUDE_LEVEL:
2375 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2376 if (ip->fname != NULL)
2379 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2380 sprintf (buf, "%d", true_indepth - 1);
2384 buf = (char *) alloca (3 + strlen (version_string));
2385 sprintf (buf, "\"%s\"", version_string);
2388 #ifndef NO_BUILTIN_SIZE_TYPE
2394 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2395 case T_PTRDIFF_TYPE:
2401 buf = CPP_WCHAR_TYPE (pfile);
2404 case T_USER_LABEL_PREFIX_TYPE:
2405 buf = USER_LABEL_PREFIX;
2408 case T_REGISTER_PREFIX_TYPE:
2409 buf = REGISTER_PREFIX;
2413 buf = (char *) alloca (4 * sizeof (int));
2414 sprintf (buf, "%d", hp->value.ival);
2416 if (pcp_inside_if && pcp_outfile)
2417 /* Output a precondition for this macro use */
2418 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2424 long line = ip->lineno;
2425 long col = ip->colno;
2426 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2428 buf = (char *) alloca (10);
2429 sprintf (buf, "%d", line);
2435 buf = (char *) alloca (20);
2436 timebuf = timestamp (pfile);
2437 if (hp->type == T_DATE)
2438 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2439 timebuf->tm_mday, timebuf->tm_year + 1900);
2441 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2445 case T_SPEC_DEFINED:
2446 buf = " 0 "; /* Assume symbol is not defined */
2447 ip = CPP_BUFFER (pfile);
2448 SKIP_WHITE_SPACE (ip->cur);
2449 if (*ip->cur == '(')
2452 ip->cur++; /* Skip over the paren */
2453 SKIP_WHITE_SPACE (ip->cur);
2456 if (!is_idstart[*ip->cur])
2458 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2461 if (pcp_outfile && pcp_inside_if
2462 && (hp->type == T_CONST
2463 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2464 /* Output a precondition for this macro use. */
2465 fprintf (pcp_outfile, "#define %s\n", hp->name);
2471 if (pcp_outfile && pcp_inside_if)
2473 /* Output a precondition for this macro use */
2474 U_CHAR *cp = ip->bufp;
2475 fprintf (pcp_outfile, "#undef ");
2476 while (is_idchar[*cp]) /* Ick! */
2477 fputc (*cp++, pcp_outfile);
2478 putc ('\n', pcp_outfile);
2481 while (is_idchar[*ip->cur])
2483 SKIP_WHITE_SPACE (ip->cur);
2486 if (*ip->cur != ')')
2494 cpp_error (pfile, "`defined' without an identifier");
2498 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2502 CPP_RESERVE (pfile, len + 1);
2503 CPP_PUTS_Q (pfile, buf, len);
2504 CPP_NUL_TERMINATE_Q (pfile);
2509 /* Initialize the built-in macros. */
2512 initialize_builtins (
2515 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2516 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2517 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2518 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2519 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2520 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2521 #ifndef NO_BUILTIN_SIZE_TYPE
2522 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2524 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2525 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2527 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2528 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2529 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2530 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2531 if (!CPP_TRADITIONAL (pfile))
2532 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2533 if (CPP_OPTIONS (pfile)->objc)
2534 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2535 /* This is supplied using a -D by the compiler driver
2536 so that it is present only when truly compiling with GNU C. */
2537 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2539 if (CPP_OPTIONS (pfile)->debug_output)
2541 char directive[2048];
2542 register struct directive *dp = &directive_table[0];
2543 struct tm *timebuf = timestamp (pfile);
2544 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2546 while (CPP_PREV_BUFFER (pbuffer))
2547 pbuffer = CPP_PREV_BUFFER (pbuffer);
2548 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2549 pbuffer->nominal_fname);
2550 output_line_command (pfile, 0, same_file);
2551 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2553 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2554 output_line_command (pfile, 0, same_file);
2555 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2557 #ifndef NO_BUILTIN_SIZE_TYPE
2558 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2559 output_line_command (pfile, 0, same_file);
2560 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2563 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2564 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2565 output_line_command (pfile, 0, same_file);
2566 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2569 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2570 output_line_command (pfile, 0, same_file);
2571 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2573 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2574 monthnames[timebuf->tm_mon],
2575 timebuf->tm_mday, timebuf->tm_year + 1900);
2576 output_line_command (pfile, 0, same_file);
2577 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2579 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2580 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2581 output_line_command (pfile, 0, same_file);
2582 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2584 if (!CPP_TRADITIONAL (pfile))
2586 sprintf (directive, " __STDC__ 1");
2587 output_line_command (pfile, 0, same_file);
2588 pass_thru_directive (directive, &directive[strlen (directive)],
2591 if (CPP_OPTIONS (pfile)->objc)
2593 sprintf (directive, " __OBJC__ 1");
2594 output_line_command (pfile, 0, same_file);
2595 pass_thru_directive (directive, &directive[strlen (directive)],
2601 /* Return 1 iff a token ending in C1 followed directly by a token C2
2602 could cause mis-tokenization. */
2611 if (c2 == c1 || c2 == '=')
2615 case '0': case '1': case '2': case '3': case '4':
2616 case '5': case '6': case '7': case '8': case '9':
2618 if (c2 == '-' || c2 == '+')
2619 return 1; /* could extend a pre-processing number */
2622 if (c2 == '\'' || c2 == '\"')
2623 return 1; /* Could turn into L"xxx" or L'xxx'. */
2627 case 'a': case 'b': case 'c': case 'd': case 'f':
2628 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2629 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2630 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2632 case 'A': case 'B': case 'C': case 'D': case 'F':
2633 case 'G': case 'H': case 'I': case 'J': case 'K':
2634 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2635 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2637 /* We're in the middle of either a name or a pre-processing number. */
2638 return (is_idchar[c2] || c2 == '.');
2639 case '<': case '>': case '!': case '%': case '#': case ':':
2640 case '^': case '&': case '|': case '*': case '/': case '=':
2641 return (c2 == c1 || c2 == '=');
2646 /* Expand a macro call.
2647 HP points to the symbol that is the macro being called.
2648 Put the result of expansion onto the input stack
2649 so that subsequent input by our caller will use it.
2651 If macro wants arguments, caller has already verified that
2652 an argument list follows; arguments come from the input stack. */
2660 DEFINITION *defn = hp->value.defn;
2661 register U_CHAR *xbuf;
2662 long start_line, start_column;
2664 struct argdata *args;
2665 long old_written = CPP_WRITTEN (pfile);
2667 int start_line = instack[indepth].lineno;
2669 int rest_args, rest_zero;
2673 CHECK_DEPTH (return;);
2677 /* This macro is being used inside a #if, which means it must be */
2678 /* recorded as a precondition. */
2679 if (pcp_inside_if && pcp_outfile && defn->predefined)
2680 dump_single_macro (hp, pcp_outfile);
2683 pfile->output_escapes++;
2684 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2686 nargs = defn->nargs;
2690 enum cpp_token token;
2692 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2694 for (i = 0; i < nargs; i++)
2696 args[i].raw = args[i].expanded = 0;
2697 args[i].raw_length = 0;
2698 args[i].expand_length = args[i].stringified_length = -1;
2699 args[i].use_count = 0;
2702 /* Parse all the macro args that are supplied. I counts them.
2703 The first NARGS args are stored in ARGS.
2704 The rest are discarded. If rest_args is set then we assume
2705 macarg absorbed the rest of the args. */
2709 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2714 if (i < nargs || (nargs == 0 && i == 0))
2716 /* if we are working on last arg which absorbs rest of args... */
2717 if (i == nargs - 1 && defn->rest_args)
2719 args[i].raw = CPP_WRITTEN (pfile);
2720 token = macarg (pfile, rest_args);
2721 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2722 args[i].newlines = 0; /* FIXME */
2725 token = macarg (pfile, 0);
2726 if (token == CPP_EOF || token == CPP_POP)
2728 cpp_error_with_line (pfile, start_line, start_column,
2729 "unterminated macro call");
2733 } while (token == CPP_COMMA);
2735 /* If we got one arg but it was just whitespace, call that 0 args. */
2738 register U_CHAR *bp = ARG_BASE + args[0].raw;
2739 register U_CHAR *lim = bp + args[0].raw_length;
2740 /* cpp.texi says for foo ( ) we provide one argument.
2741 However, if foo wants just 0 arguments, treat this as 0. */
2743 while (bp != lim && is_space[*bp]) bp++;
2748 /* Don't output an error message if we have already output one for
2749 a parse error above. */
2751 if (nargs == 0 && i > 0)
2753 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2757 /* traditional C allows foo() if foo wants one argument. */
2758 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2760 /* the rest args token is allowed to absorb 0 tokens */
2761 else if (i == nargs - 1 && defn->rest_args)
2764 cpp_error (pfile, "macro `%s' used without args", hp->name);
2766 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2768 cpp_error (pfile, "macro `%s' used with only %d args",
2774 "macro `%s' used with too many (%d) args", hp->name, i);
2778 /* If macro wants zero args, we parsed the arglist for checking only.
2779 Read directly from the macro definition. */
2782 xbuf = defn->expansion;
2783 xbuf_len = defn->length;
2787 register U_CHAR *exp = defn->expansion;
2788 register int offset; /* offset in expansion,
2789 copied a piece at a time */
2790 register int totlen; /* total amount of exp buffer filled so far */
2792 register struct reflist *ap, *last_ap;
2794 /* Macro really takes args. Compute the expansion of this call. */
2796 /* Compute length in characters of the macro's expansion.
2797 Also count number of times each arg is used. */
2798 xbuf_len = defn->length;
2799 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2803 register struct argdata *arg = &args[ap->argno];
2804 /* Stringify it it hasn't already been */
2805 if (arg->stringified_length < 0)
2807 int arglen = arg->raw_length;
2811 /* Initially need_space is -1. Otherwise, 1 means the
2812 previous character was a space, but we suppressed it;
2813 0 means the previous character was a non-space. */
2814 int need_space = -1;
2816 arg->stringified = CPP_WRITTEN (pfile);
2817 if (!CPP_TRADITIONAL (pfile))
2818 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2819 for (; i < arglen; i++)
2821 c = (ARG_BASE + arg->raw)[i];
2825 /* Internal sequences of whitespace are replaced by
2826 one space except within an string or char token.*/
2829 if (CPP_WRITTEN (pfile) > arg->stringified
2830 && (CPP_PWRITTEN (pfile))[-1] == '@')
2832 /* "@ " escape markers are removed */
2833 CPP_ADJUST_WRITTEN (pfile, -1);
2836 if (need_space == 0)
2840 else if (need_space > 0)
2841 CPP_PUTC (pfile, ' ');
2856 else if (c == '\"' || c == '\'')
2860 /* Escape these chars */
2861 if (c == '\"' || (in_string && c == '\\'))
2862 CPP_PUTC (pfile, '\\');
2864 CPP_PUTC (pfile, c);
2867 CPP_RESERVE (pfile, 4);
2868 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2870 CPP_ADJUST_WRITTEN (pfile, 4);
2873 if (!CPP_TRADITIONAL (pfile))
2874 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2875 arg->stringified_length
2876 = CPP_WRITTEN (pfile) - arg->stringified;
2878 xbuf_len += args[ap->argno].stringified_length;
2880 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2881 /* Add 4 for two newline-space markers to prevent
2882 token concatenation. */
2883 xbuf_len += args[ap->argno].raw_length + 4;
2886 /* We have an ordinary (expanded) occurrence of the arg.
2887 So compute its expansion, if we have not already. */
2888 if (args[ap->argno].expand_length < 0)
2890 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2891 cpp_expand_to_buffer (pfile,
2892 ARG_BASE + args[ap->argno].raw,
2893 args[ap->argno].raw_length);
2895 args[ap->argno].expand_length
2896 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2899 /* Add 4 for two newline-space markers to prevent
2900 token concatenation. */
2901 xbuf_len += args[ap->argno].expand_length + 4;
2903 if (args[ap->argno].use_count < 10)
2904 args[ap->argno].use_count++;
2907 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2909 /* Generate in XBUF the complete expansion
2910 with arguments substituted in.
2911 TOTLEN is the total size generated so far.
2912 OFFSET is the index in the definition
2913 of where we are copying from. */
2914 offset = totlen = 0;
2915 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2916 last_ap = ap, ap = ap->next)
2918 register struct argdata *arg = &args[ap->argno];
2919 int count_before = totlen;
2921 /* Add chars to XBUF. */
2922 for (i = 0; i < ap->nchars; i++, offset++)
2923 xbuf[totlen++] = exp[offset];
2925 /* If followed by an empty rest arg with concatenation,
2926 delete the last run of nonwhite chars. */
2927 if (rest_zero && totlen > count_before
2928 && ((ap->rest_args && ap->raw_before)
2929 || (last_ap != NULL && last_ap->rest_args
2930 && last_ap->raw_after)))
2932 /* Delete final whitespace. */
2933 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2936 /* Delete the nonwhites before them. */
2937 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2941 if (ap->stringify != 0)
2943 bcopy (ARG_BASE + arg->stringified,
2944 xbuf + totlen, arg->stringified_length);
2945 totlen += arg->stringified_length;
2947 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2949 U_CHAR *p1 = ARG_BASE + arg->raw;
2950 U_CHAR *l1 = p1 + arg->raw_length;
2953 while (p1 != l1 && is_space[*p1]) p1++;
2954 while (p1 != l1 && is_idchar[*p1])
2955 xbuf[totlen++] = *p1++;
2959 /* Arg is concatenated after: delete trailing whitespace,
2960 whitespace markers, and no-reexpansion markers. */
2963 if (is_space[l1[-1]]) l1--;
2964 else if (l1[-1] == '-')
2966 U_CHAR *p2 = l1 - 1;
2967 /* If a `-' is preceded by an odd number of newlines then it
2968 and the last newline are a no-reexpansion marker. */
2969 while (p2 != p1 && p2[-1] == '\n') p2--;
2970 if ((l1 - 1 - p2) & 1) {
2979 bcopy (p1, xbuf + totlen, l1 - p1);
2984 U_CHAR *expanded = ARG_BASE + arg->expanded;
2985 if (!ap->raw_before && totlen > 0 && arg->expand_length
2986 && !CPP_TRADITIONAL(pfile)
2987 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2989 xbuf[totlen++] = '@';
2990 xbuf[totlen++] = ' ';
2993 bcopy (expanded, xbuf + totlen, arg->expand_length);
2994 totlen += arg->expand_length;
2996 if (!ap->raw_after && totlen > 0 && offset < defn->length
2997 && !CPP_TRADITIONAL(pfile)
2998 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3000 xbuf[totlen++] = '@';
3001 xbuf[totlen++] = ' ';
3004 /* If a macro argument with newlines is used multiple times,
3005 then only expand the newlines once. This avoids creating
3006 output lines which don't correspond to any input line,
3007 which confuses gdb and gcov. */
3008 if (arg->use_count > 1 && arg->newlines > 0)
3010 /* Don't bother doing change_newlines for subsequent
3014 = change_newlines (expanded, arg->expand_length);
3018 if (totlen > xbuf_len)
3022 /* if there is anything left of the definition
3023 after handling the arg list, copy that in too. */
3025 for (i = offset; i < defn->length; i++)
3027 /* if we've reached the end of the macro */
3030 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3031 && last_ap->raw_after))
3032 xbuf[totlen++] = exp[i];
3040 pfile->output_escapes--;
3042 /* Now put the expansion on the input stack
3043 so our caller will commence reading from it. */
3044 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3045 CPP_BUFFER (pfile)->has_escapes = 1;
3047 /* Pop the space we've used in the token_buffer for argument expansion. */
3048 CPP_SET_WRITTEN (pfile, old_written);
3050 /* Recursive macro use sometimes works traditionally.
3051 #define foo(x,y) bar (x (y,0), y)
3054 if (!CPP_TRADITIONAL (pfile))
3055 hp->type = T_DISABLED;
3059 push_macro_expansion (
3061 register U_CHAR *xbuf,
3065 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3066 mbuf->cleanup = macro_cleanup;
3069 /* The first chars of the expansion should be a "@ " added by
3070 collect_expansion. This is to prevent accidental token-pasting
3071 between the text preceding the macro invocation, and the macro
3074 We would like to avoid adding unneeded spaces (for the sake of
3075 tools that use cpp, such as imake). In some common cases we can
3076 tell that it is safe to omit the space.
3078 The character before the macro invocation cannot have been an
3079 idchar (or else it would have been pasted with the idchars of
3080 the macro name). Therefore, if the first non-space character
3081 of the expansion is an idchar, we do not need the extra space
3082 to prevent token pasting.
3084 Also, we don't need the extra space if the first char is '(',
3085 or some other (less common) characters. */
3087 if (xbuf[0] == '@' && xbuf[1] == ' '
3088 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3089 || xbuf[2] == '\"'))
3093 /* Like cpp_get_token, except that it does not read past end-of-line.
3094 Also, horizontal space is skipped, and macros are popped. */
3096 static enum cpp_token
3097 get_directive_token (
3102 long old_written = CPP_WRITTEN (pfile);
3103 enum cpp_token token;
3104 cpp_skip_hspace (pfile);
3105 if (PEEKC () == '\n')
3107 token = cpp_get_token (pfile);
3111 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3113 /* ... else fall though ... */
3114 case CPP_HSPACE: case CPP_COMMENT:
3115 CPP_SET_WRITTEN (pfile, old_written);
3123 /* Handle #include and #import.
3124 This function expects to see "fname" or <fname> on the input.
3126 The input is normally in part of the output_buffer following
3127 CPP_WRITTEN, and will get overwritten by output_line_command.
3128 I.e. in input file specification has been popped by handle_directive.
3134 struct directive *keyword,
3135 U_CHAR *unused1, U_CHAR *unused2)
3137 int importing = (keyword->type == T_IMPORT);
3138 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3139 char *fname; /* Dynamically allocated fname buffer */
3142 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3143 enum cpp_token token;
3145 /* Chain of dirs to search */
3146 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3147 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3148 struct file_name_list *searchptr = 0;
3149 long old_written = CPP_WRITTEN (pfile);
3153 int f; /* file number */
3155 int retried = 0; /* Have already tried macro
3156 expanding the include line*/
3157 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3162 f= -1; /* JF we iz paranoid! */
3164 if (importing && CPP_OPTIONS (pfile)->warn_import
3165 && !CPP_OPTIONS (pfile)->inhibit_warnings
3166 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3168 pfile->import_warning = 1;
3169 cpp_warning (pfile, "using `#import' is not recommended");
3170 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3171 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3172 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3173 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3174 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3175 fprintf (stderr, " ... <real contents of file> ...\n");
3176 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3177 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3178 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3179 fprintf (stderr, "when it is equipped with such a conditional.\n");
3182 pfile->parsing_include_directive++;
3183 token = get_directive_token (pfile);
3184 pfile->parsing_include_directive--;
3186 if (token == CPP_STRING)
3188 /* FIXME - check no trailing garbage */
3189 fbeg = pfile->token_buffer + old_written + 1;
3190 fend = CPP_PWRITTEN (pfile) - 1;
3191 if (fbeg[-1] == '<')
3194 /* If -I-, start with the first -I dir after the -I-. */
3195 if (CPP_OPTIONS (pfile)->first_bracket_include)
3196 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3198 /* If -I- was specified, don't search current dir, only spec'd ones. */
3199 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3202 /* We have "filename". Figure out directory this source
3203 file is coming from and put it on the front of the list. */
3205 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3210 if ((nam = fp->nominal_fname) != NULL)
3212 /* Found a named file. Figure out dir of the file,
3213 and put it in front of the search list. */
3214 dsp[0].next = search_start;
3217 ep = rindex (nam, '/');
3219 ep = rindex (nam, ']');
3220 if (ep == NULL) ep = rindex (nam, '>');
3221 if (ep == NULL) ep = rindex (nam, ':');
3222 if (ep != NULL) ep++;
3227 dsp[0].fname = (char *) alloca (n + 1);
3228 strncpy (dsp[0].fname, nam, n);
3229 dsp[0].fname[n] = '\0';
3230 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3231 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3235 dsp[0].fname = 0; /* Current directory */
3237 dsp[0].got_name_map = 0;
3244 else if (token == CPP_NAME)
3247 * Support '#include xyz' like VAX-C to allow for easy use of all the
3248 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3249 * code from case '<' is repeated here) and generates a warning.
3252 "VAX-C-style include specification found, use '#include <filename.h>' !");
3254 /* If -I-, start with the first -I dir after the -I-. */
3255 if (CPP_OPTIONS (pfile)->first_bracket_include)
3256 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3257 fbeg = pfile->token_buffer + old_written;
3258 fend = CPP_PWRITTEN (pfile);
3264 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3265 CPP_SET_WRITTEN (pfile, old_written);
3266 skip_rest_of_line (pfile);
3272 token = get_directive_token (pfile);
3273 if (token != CPP_VSPACE)
3275 cpp_error (pfile, "junk at end of `#include'");
3276 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3277 token = get_directive_token (pfile);
3280 /* For #include_next, skip in the search path
3281 past the dir in which the containing file was found. */
3285 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3286 if (fp->fname != NULL)
3288 /* fp->dir is null if the containing file was specified with
3289 an absolute file name. In that case, don't skip anything. */
3290 if (fp->dir == SELF_DIR_DUMMY)
3291 search_start = CPP_OPTIONS (pfile)->include;
3293 search_start = fp->dir->next;
3298 CPP_SET_WRITTEN (pfile, old_written);
3304 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3308 /* Allocate this permanently, because it gets stored in the definitions
3310 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3311 /* + 2 above for slash and terminating null. */
3312 /* + 2 added for '.h' on VMS (to support '#include filename') */
3314 /* If specified file name is absolute, just open it. */
3317 strncpy (fname, fbeg, flen);
3319 if (redundant_include_p (pfile, fname))
3322 f = lookup_import (pfile, fname, NULL_PTR);
3324 f = open_include_file (pfile, fname, NULL_PTR);
3326 return 0; /* Already included this file */
3328 /* Search directory path, trying to open the file.
3329 Copy each filename tried into FNAME. */
3331 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3332 if (searchptr->fname) {
3333 /* The empty string in a search path is ignored.
3334 This makes it possible to turn off entirely
3335 a standard piece of the list. */
3336 if (searchptr->fname[0] == 0)
3338 strcpy (fname, searchptr->fname);
3339 strcat (fname, "/");
3340 fname[strlen (fname) + flen] = 0;
3344 strncat (fname, fbeg, flen);
3346 /* Change this 1/2 Unix 1/2 VMS file specification into a
3347 full VMS file specification */
3348 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3349 /* Fix up the filename */
3350 hack_vms_include_specification (fname);
3352 /* This is a normal VMS filespec, so use it unchanged. */
3353 strncpy (fname, fbeg, flen);
3355 /* if it's '#include filename', add the missing .h */
3356 if (index(fname,'.')==NULL) {
3357 strcat (fname, ".h");
3361 /* ??? There are currently 3 separate mechanisms for avoiding processing
3362 of redundant include files: #import, #pragma once, and
3363 redundant_include_p. It would be nice if they were unified. */
3364 if (redundant_include_p (pfile, fname))
3367 f = lookup_import (pfile, fname, searchptr);
3369 f = open_include_file (pfile, fname, searchptr);
3371 return 0; /* Already included this file */
3373 else if (f == -1 && errno == EACCES)
3374 cpp_warning (pfile, "Header file %s exists, but is not readable",
3384 /* A file that was not found. */
3385 strncpy (fname, fbeg, flen);
3387 /* If generating dependencies and -MG was specified, we assume missing
3388 files are leaf files, living in the same directory as the source file
3389 or other similar place; these missing files may be generated from
3390 other files and may not exist yet (eg: y.tab.h). */
3392 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3393 && CPP_PRINT_DEPS (pfile)
3394 > (angle_brackets || (pfile->system_include_depth > 0)))
3396 /* If it was requested as a system header file,
3397 then assume it belongs in the first place to look for such. */
3400 for (searchptr = search_start; searchptr;
3401 searchptr = searchptr->next)
3403 if (searchptr->fname)
3407 if (searchptr->fname[0] == 0)
3409 p = (char *) alloca (strlen (searchptr->fname)
3410 + strlen (fname) + 2);
3411 strcpy (p, searchptr->fname);
3414 deps_output (pfile, p, ' ');
3421 /* Otherwise, omit the directory, as if the file existed
3422 in the directory with the source. */
3423 deps_output (pfile, fname, ' ');
3426 /* If -M was specified, and this header file won't be added to the
3427 dependency list, then don't count this as an error, because we can
3428 still produce correct output. Otherwise, we can't produce correct
3429 output, because there may be dependencies we need inside the missing
3430 file, and we don't know what directory this missing file exists in.*/
3431 else if (CPP_PRINT_DEPS (pfile)
3432 && (CPP_PRINT_DEPS (pfile)
3433 <= (angle_brackets || (pfile->system_include_depth > 0))))
3434 cpp_warning (pfile, "No include path in which to find %s", fname);
3435 else if (search_start)
3436 cpp_error_from_errno (pfile, fname);
3438 cpp_error (pfile, "No include path in which to find %s", fname);
3441 /* Check to see if this include file is a once-only include file.
3444 struct file_name_list* ptr;
3446 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3447 if (!strcmp (ptr->fname, fname)) {
3449 return 0; /* This file was once'd. */
3453 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3454 if (!strcmp (ptr->fname, fname))
3455 break; /* This file was included before. */
3459 /* This is the first time for this file. */
3460 /* Add it to list of files included. */
3462 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3463 ptr->control_macro = 0;
3464 ptr->c_system_include_path = 0;
3465 ptr->next = pfile->all_include_files;
3466 pfile->all_include_files = ptr;
3467 ptr->fname = savestring (fname);
3468 ptr->got_name_map = 0;
3470 /* For -M, add this file to the dependencies. */
3471 if (CPP_PRINT_DEPS (pfile)
3472 > (angle_brackets || (pfile->system_include_depth > 0)))
3473 deps_output (pfile, fname, ' ');
3476 /* Handle -H option. */
3477 if (CPP_OPTIONS(pfile)->print_include_names)
3479 cpp_buffer *buf = CPP_BUFFER (pfile);
3480 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3482 fprintf (stderr, "%s\n", fname);
3486 pfile->system_include_depth++;
3488 /* Actually process the file. */
3490 /* Record file on "seen" list for #import. */
3491 add_import (pfile, f, fname);
3493 pcftry = (char *) alloca (strlen (fname) + 30);
3505 sprintf (pcftry, "%s%d", fname, pcfnum++);
3507 pcf = open (pcftry, O_RDONLY, 0666);
3513 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3515 || stat_f.st_dev != s.st_dev)
3517 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3518 /* Don't need it any more. */
3523 /* Don't need it at all. */
3528 } while (pcf != -1 && !pcfbuf);
3532 /* Actually process the file */
3533 cpp_push_buffer (pfile, NULL, 0);
3534 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3535 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3537 output_line_command (pfile, 0, enter_file);
3538 pfile->only_seen_white = 2;
3542 pfile->system_include_depth--;
3547 /* Return nonzero if there is no need to include file NAME
3548 because it has already been included and it contains a conditional
3549 to make a repeated include do nothing. */
3552 redundant_include_p (
3556 struct file_name_list *l = pfile->all_include_files;
3557 for (; l; l = l->next)
3558 if (! strcmp (name, l->fname)
3560 && cpp_lookup (pfile, l->control_macro, -1, -1))
3565 /* Return nonzero if the given FILENAME is an absolute pathname which
3566 designates a file within one of the known "system" include file
3567 directories. We assume here that if the given FILENAME looks like
3568 it is the name of a file which resides either directly in a "system"
3569 include file directory, or within any subdirectory thereof, then the
3570 given file must be a "system" include file. This function tells us
3571 if we should suppress pedantic errors/warnings for the given FILENAME.
3573 The value is 2 if the file is a C-language system header file
3574 for which C++ should (on most systems) assume `extern "C"'. */
3579 register char *filename)
3581 struct file_name_list *searchptr;
3583 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3584 searchptr = searchptr->next)
3585 if (searchptr->fname) {
3586 register char *sys_dir = searchptr->fname;
3587 register unsigned length = strlen (sys_dir);
3589 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3591 if (searchptr->c_system_include_path)
3602 * Install a name in the assertion hash table.
3604 * If LEN is >= 0, it is the length of the name.
3605 * Otherwise, compute the length by scanning the entire name.
3607 * If HASH is >= 0, it is the precomputed hash code.
3608 * Otherwise, compute the hash code.
3610 static ASSERTION_HASHNODE *
3617 register ASSERTION_HASHNODE *hp;
3618 register int i, bucket;
3619 register U_CHAR *p, *q;
3621 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3622 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3624 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3625 hp->next = pfile->assertion_hashtab[bucket];
3626 pfile->assertion_hashtab[bucket] = hp;
3628 if (hp->next != NULL)
3629 hp->next->prev = hp;
3632 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3635 for (i = 0; i < len; i++)
3641 * find the most recent hash node for name name (ending with first
3642 * non-identifier char) installed by install
3644 * If LEN is >= 0, it is the length of the name.
3645 * Otherwise, compute the length by scanning the entire name.
3647 * If HASH is >= 0, it is the precomputed hash code.
3648 * Otherwise, compute the hash code.
3651 static ASSERTION_HASHNODE *
3658 register ASSERTION_HASHNODE *bucket;
3660 bucket = pfile->assertion_hashtab[hash];
3662 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3664 bucket = bucket->next;
3671 ASSERTION_HASHNODE *hp)
3673 struct tokenlist_list *tail;
3674 if (hp->prev != NULL)
3675 hp->prev->next = hp->next;
3676 if (hp->next != NULL)
3677 hp->next->prev = hp->prev;
3679 for (tail = hp->value; tail; )
3681 struct tokenlist_list *next = tail->next;
3682 free_token_list (tail->tokens);
3687 /* make sure that the bucket chain header that
3688 the deleted guy was on points to the right thing afterwards. */
3689 if (hp == *hp->bucket_hdr)
3690 *hp->bucket_hdr = hp->next;
3695 /* Convert a character string literal into a nul-terminated string.
3696 The input string is [IN ... LIMIT).
3697 The result is placed in RESULT. RESULT can be the same as IN.
3698 The value returned in the end of the string written to RESULT,
3699 or NULL on error. */
3704 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3724 char *bpc = (char *) in;
3725 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3726 in = (U_CHAR *) bpc;
3728 *result++ = (U_CHAR)c;
3731 /* else fall through */
3741 * interpret #line command. Remembers previously seen fnames
3742 * in its very own hash table.
3744 #define FNAME_HASHSIZE 37
3749 struct directive *keyword)
3751 cpp_buffer *ip = CPP_BUFFER (pfile);
3753 long old_written = CPP_WRITTEN (pfile);
3754 enum file_change_code file_change = same_file;
3755 enum cpp_token token;
3758 token = get_directive_token (pfile);
3760 if (token != CPP_NUMBER
3761 || !isdigit(pfile->token_buffer[old_written]))
3763 cpp_error (pfile, "invalid format `#line' command");
3764 goto bad_line_directive;
3767 /* The Newline at the end of this line remains to be processed.
3768 To put the next line at the specified line number,
3769 we must store a line number now that is one less. */
3770 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3771 CPP_SET_WRITTEN (pfile, old_written);
3773 /* NEW_LINENO is one less than the actual line number here. */
3774 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3775 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3777 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3778 if (PEEKC() && !is_space[PEEKC()]) {
3779 cpp_error (pfile, "invalid format `#line' command");
3780 goto bad_line_directive;
3784 token = get_directive_token (pfile);
3786 if (token == CPP_STRING) {
3787 U_CHAR *fname = pfile->token_buffer + old_written;
3789 static HASHNODE *fname_table[FNAME_HASHSIZE];
3790 HASHNODE *hp, **hash_bucket;
3795 /* Turn the file name, which is a character string literal,
3796 into a null-terminated string. Do this in place. */
3797 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3798 if (end_name == NULL)
3800 cpp_error (pfile, "invalid format `#line' command");
3801 goto bad_line_directive;
3804 fname_length = end_name - fname;
3806 num_start = CPP_WRITTEN (pfile);
3807 token = get_directive_token (pfile);
3808 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3809 p = pfile->token_buffer + num_start;
3810 if (CPP_PEDANTIC (pfile))
3811 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3813 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3815 cpp_error (pfile, "invalid format `#line' command");
3816 goto bad_line_directive;
3819 file_change = enter_file;
3821 file_change = leave_file;
3823 ip->system_header_p = 1;
3824 else /* if (*p == 4) */
3825 ip->system_header_p = 2;
3827 CPP_SET_WRITTEN (pfile, num_start);
3828 token = get_directive_token (pfile);
3829 p = pfile->token_buffer + num_start;
3830 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3831 ip->system_header_p = *p == 3 ? 1 : 2;
3832 token = get_directive_token (pfile);
3834 if (token != CPP_VSPACE) {
3835 cpp_error (pfile, "invalid format `#line' command");
3836 goto bad_line_directive;
3841 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3842 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3843 if (hp->length == fname_length &&
3844 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3845 ip->nominal_fname = hp->value.cpval;
3849 /* Didn't find it; cons up a new one. */
3850 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3851 hp->next = *hash_bucket;
3854 hp->length = fname_length;
3855 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3856 bcopy (fname, hp->value.cpval, fname_length);
3859 else if (token != CPP_VSPACE && token != CPP_EOF) {
3860 cpp_error (pfile, "invalid format `#line' command");
3861 goto bad_line_directive;
3864 ip->lineno = new_lineno;
3866 skip_rest_of_line (pfile);
3867 CPP_SET_WRITTEN (pfile, old_written);
3868 output_line_command (pfile, 0, file_change);
3873 * remove the definition of a symbol from the symbol table.
3874 * according to un*x /lib/cpp, it is not an error to undef
3875 * something that has no definitions, so it isn't one here either.
3881 struct directive *keyword,
3882 U_CHAR *buf, U_CHAR *limit)
3886 U_CHAR *orig_buf = buf;
3889 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3890 if (pcp_outfile && keyword)
3891 pass_thru_directive (buf, limit, pfile, keyword);
3894 SKIP_WHITE_SPACE (buf);
3895 sym_length = check_macro_name (pfile, buf, "macro");
3897 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3899 /* If we are generating additional info for debugging (with -g) we
3900 need to pass through all effective #undef commands. */
3901 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3902 pass_thru_directive (orig_buf, limit, pfile, keyword);
3903 if (hp->type != T_MACRO)
3904 cpp_warning (pfile, "undefining `%s'", hp->name);
3908 if (CPP_PEDANTIC (pfile)) {
3910 SKIP_WHITE_SPACE (buf);
3912 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3918 * Report an error detected by the program we are processing.
3919 * Use the text of the line in the error message.
3920 * (We use error because it prints the filename & line#.)
3926 struct directive *keyword,
3927 U_CHAR *buf,U_CHAR *limit)
3929 int length = limit - buf;
3930 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3931 bcopy (buf, copy, length);
3933 SKIP_WHITE_SPACE (copy);
3934 cpp_error (pfile, "#error %s", copy);
3939 * Report a warning detected by the program we are processing.
3940 * Use the text of the line in the warning message, then continue.
3941 * (We use error because it prints the filename & line#.)
3947 struct directive *keyword,
3948 U_CHAR *buf,U_CHAR *limit)
3950 int length = limit - buf;
3951 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3952 bcopy (buf, copy, length);
3954 SKIP_WHITE_SPACE (copy);
3955 cpp_warning (pfile, "#warning %s", copy);
3959 /* Remember the name of the current file being read from so that we can
3960 avoid ever including it again. */
3966 cpp_buffer *ip = NULL;
3967 struct file_name_list *new;
3969 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3973 if (ip->fname != NULL)
3978 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3979 new->next = pfile->dont_repeat_files;
3980 pfile->dont_repeat_files = new;
3981 new->fname = savestring (ip->fname);
3982 new->control_macro = 0;
3983 new->got_name_map = 0;
3984 new->c_system_include_path = 0;
3989 /* #ident has already been copied to the output file, so just ignore it. */
3994 struct directive *keyword,
3995 U_CHAR *buf, U_CHAR *limit)
3997 /* long old_written = CPP_WRITTEN (pfile);*/
4000 /* Allow #ident in system headers, since that's not user's fault. */
4001 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
4002 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
4004 /* Leave rest of line to be read by later calls to cpp_get_token. */
4009 /* #pragma and its argument line have already been copied to the output file.
4010 Just check for some recognized pragmas that need validation here. */
4015 struct directive *keyword,
4016 U_CHAR *buf,U_CHAR *limit)
4018 while (*buf == ' ' || *buf == '\t')
4020 if (!strncmp (buf, "once", 4)) {
4021 /* Allow #pragma once in system headers, since that's not the user's
4023 if (!CPP_BUFFER (pfile)->system_header_p)
4024 cpp_warning (pfile, "`#pragma once' is obsolete");
4028 if (!strncmp (buf, "implementation", 14)) {
4029 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4030 been included yet. */
4031 struct file_name_list *ptr;
4032 U_CHAR *p = buf + 14, *fname, *inc_fname;
4034 SKIP_WHITE_SPACE (p);
4035 if (*p == '\n' || *p != '\"')
4039 p = (U_CHAR *) index (fname, '\"');
4040 fname_len = p != NULL ? p - fname : strlen (fname);
4042 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4043 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4044 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4045 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4047 "`#pragma implementation' for `%s' appears after file is included",
4056 /* This was a fun hack, but #pragma seems to start to be useful.
4057 By failing to recognize it, we pass it through unchanged to cc1. */
4060 * the behavior of the #pragma directive is implementation defined.
4061 * this implementation defines it as follows.
4068 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4071 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4073 execl ("/usr/games/hack", "#pragma", 0);
4074 execl ("/usr/games/rogue", "#pragma", 0);
4075 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4076 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4078 fatal ("You are in a maze of twisty compiler features, all different");
4082 /* Just ignore #sccs, on systems where we define it at all. */
4087 struct directive *keyword,
4088 U_CHAR *buf,U_CHAR *limit)
4090 if (CPP_PEDANTIC (pfile))
4091 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4096 * handle #if command by
4097 * 1) inserting special `defined' keyword into the hash table
4098 * that gets turned into 0 or 1 by special_symbol (thus,
4099 * if the luser has a symbol called `defined' already, it won't
4100 * work inside the #if command)
4101 * 2) rescan the input into a temporary output buffer
4102 * 3) pass the output buffer to the yacc parser and collect a value
4103 * 4) clean up the mess left from steps 1 and 2.
4104 * 5) call conditional_skip to skip til the next #endif (etc.),
4105 * or not, depending on the value from step 3.
4111 struct directive *keyword,
4112 U_CHAR *buf,U_CHAR *limit)
4114 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4115 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4120 * handle a #elif directive by not changing if_stack either.
4121 * see the comment above do_else.
4127 struct directive *keyword,
4128 U_CHAR *buf,U_CHAR *limit)
4130 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4131 cpp_error (pfile, "`#elif' not within a conditional");
4134 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4135 cpp_error (pfile, "`#elif' after `#else'");
4137 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4139 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4140 && strcmp (pfile->if_stack->fname,
4141 CPP_BUFFER (pfile)->nominal_fname) != 0)
4142 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4143 fprintf (stderr, ")\n");
4145 pfile->if_stack->type = T_ELIF;
4148 if (pfile->if_stack->if_succeeded)
4149 skip_if_group (pfile, 0);
4151 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4153 skip_if_group (pfile, 0);
4155 ++pfile->if_stack->if_succeeded; /* continue processing input */
4156 output_line_command (pfile, 1, same_file);
4163 * evaluate a #if expression in BUF, of length LENGTH,
4164 * then parse the result as a C expression and return the value as an int.
4166 static HOST_WIDE_INT
4167 eval_if_expression (
4172 HASHNODE *save_defined;
4173 HOST_WIDE_INT value;
4174 long old_written = CPP_WRITTEN (pfile);
4176 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4177 pfile->pcp_inside_if = 1;
4179 value = cpp_parse_expr (pfile);
4180 pfile->pcp_inside_if = 0;
4181 delete_macro (save_defined); /* clean up special symbol */
4183 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4189 * routine to handle ifdef/ifndef. Try to look up the symbol,
4190 * then do or don't skip to the #endif/#else/#elif depending
4191 * on what directive is actually being processed.
4197 struct directive *keyword,
4198 U_CHAR *unused1, U_CHAR *unused2)
4201 cpp_buffer *ip = CPP_BUFFER (pfile);
4204 enum cpp_token token;
4205 int start_of_file = 0;
4206 U_CHAR *control_macro = 0;
4207 int old_written = CPP_WRITTEN (pfile);
4209 /* Detect a #ifndef at start of file (not counting comments). */
4210 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4211 start_of_file = pfile->only_seen_white == 2;
4213 pfile->no_macro_expand++;
4214 token = get_directive_token (pfile);
4215 pfile->no_macro_expand--;
4217 ident = pfile->token_buffer + old_written;
4218 ident_length = CPP_WRITTEN (pfile) - old_written;
4219 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4221 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4223 skip = (keyword->type == T_IFDEF);
4224 if (! CPP_TRADITIONAL (pfile))
4225 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4227 else if (token == CPP_NAME)
4229 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4230 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4231 if (start_of_file && !skip)
4233 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4234 bcopy (ident, control_macro, ident_length + 1);
4239 skip = (keyword->type == T_IFDEF);
4240 if (! CPP_TRADITIONAL (pfile))
4241 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4244 if (!CPP_TRADITIONAL (pfile))
4246 cpp_skip_hspace (pfile);
4248 if (c != EOF && c != '\n')
4249 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4251 skip_rest_of_line (pfile);
4255 /* Output a precondition for this macro. */
4256 if (hp && hp->value.defn->predefined)
4257 fprintf (pcp_outfile, "#define %s\n", hp->name);
4260 fprintf (pcp_outfile, "#undef ");
4261 while (is_idchar[*cp]) /* Ick! */
4262 fputc (*cp++, pcp_outfile);
4263 putc ('\n', pcp_outfile);
4267 conditional_skip (pfile, skip, T_IF, control_macro);
4271 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4272 If this is a #ifndef starting at the beginning of a file,
4273 CONTROL_MACRO is the macro name tested by the #ifndef.
4274 Otherwise, CONTROL_MACRO is 0. */
4280 enum node_type type,
4281 U_CHAR *control_macro)
4283 IF_STACK_FRAME *temp;
4285 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4286 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4288 temp->lineno = CPP_BUFFER (pfile)->lineno;
4290 temp->next = pfile->if_stack;
4291 temp->control_macro = control_macro;
4292 pfile->if_stack = temp;
4294 pfile->if_stack->type = type;
4297 skip_if_group (pfile, 0);
4300 ++pfile->if_stack->if_succeeded;
4301 output_line_command (pfile, 1, same_file);
4306 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4307 * leaves input ptr at the sharp sign found.
4308 * If ANY is nonzero, return at next directive of any sort.
4316 int at_beg_of_line = 1;
4317 struct directive *kt;
4318 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4320 U_CHAR *beg_of_line = bp;
4322 register int ident_length;
4323 U_CHAR *ident, *after_ident;
4324 struct parse_marker line_start_mark;
4326 parse_set_mark (&line_start_mark, pfile);
4328 if (CPP_OPTIONS (pfile)->output_conditionals) {
4329 static char failed[] = "#failed\n";
4330 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4332 output_line_command (pfile, 1, same_file);
4336 if (CPP_OPTIONS (pfile)->output_conditionals)
4338 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4339 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4340 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4342 parse_move_mark (&line_start_mark, pfile);
4343 if (!CPP_TRADITIONAL (pfile))
4344 cpp_skip_hspace (pfile);
4348 int old_written = CPP_WRITTEN (pfile);
4349 cpp_skip_hspace (pfile);
4351 parse_name (pfile, GETC());
4352 ident_length = CPP_WRITTEN (pfile) - old_written;
4353 ident = pfile->token_buffer + old_written;
4354 pfile->limit = ident;
4356 if (ident_length == 0)
4357 goto not_a_directive;
4359 /* Handle # followed by a line number. */
4361 /* Avoid error for `###' and similar cases unless -pedantic. */
4364 for (kt = directive_table; kt->length >= 0; kt++)
4366 IF_STACK_FRAME *temp;
4367 if (ident_length == kt->length
4368 && strncmp (ident, kt->name, kt->length) == 0)
4370 /* If we are asked to return on next directive, do so now. */
4380 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4381 temp->next = pfile->if_stack;
4382 pfile->if_stack = temp;
4384 temp->lineno = CPP_BUFFER(pfile)->lineno;
4386 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4387 temp->type = kt->type;
4391 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4392 validate_else (pfile,
4393 kt->type == T_ELSE ? "#else" : "#endif");
4395 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4398 "`#%s' not within a conditional", kt->name);
4401 else if (pfile->if_stack == save_if_stack)
4402 goto done; /* found what we came for */
4404 if (kt->type != T_ENDIF)
4406 if (pfile->if_stack->type == T_ELSE)
4407 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4408 pfile->if_stack->type = kt->type;
4412 temp = pfile->if_stack;
4413 pfile->if_stack = temp->next;
4420 /* Don't let erroneous code go by. */
4421 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4422 && CPP_PEDANTIC (pfile))
4423 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4427 /* We're in the middle of a line. Skip the rest of it. */
4434 case '/': /* possible comment */
4435 c = skip_comment (pfile, NULL);
4442 old = CPP_WRITTEN (pfile);
4443 cpp_get_token (pfile);
4444 CPP_SET_WRITTEN (pfile, old);
4447 /* Char after backslash loses its special meaning. */
4448 if (PEEKC() == '\n')
4458 if (CPP_OPTIONS (pfile)->output_conditionals) {
4459 static char end_failed[] = "#endfailed\n";
4460 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4463 pfile->only_seen_white = 1;
4464 parse_goto_mark (&line_start_mark, pfile);
4465 parse_clear_mark (&line_start_mark);
4469 * handle a #else directive. Do this by just continuing processing
4470 * without changing if_stack ; this is so that the error message
4471 * for missing #endif's etc. will point to the original #if. It
4472 * is possible that something different would be better.
4478 struct directive *keyword,
4479 U_CHAR *buf, U_CHAR *limit)
4481 cpp_buffer *ip = CPP_BUFFER (pfile);
4483 if (CPP_PEDANTIC (pfile))
4484 validate_else (pfile, "#else");
4485 skip_rest_of_line (pfile);
4487 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4488 cpp_error (pfile, "`#else' not within a conditional");
4491 /* #ifndef can't have its special treatment for containing the whole file
4492 if it has a #else clause. */
4493 pfile->if_stack->control_macro = 0;
4495 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4496 cpp_error (pfile, "`#else' after `#else'");
4497 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4498 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4499 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4500 fprintf (stderr, ")\n");
4502 pfile->if_stack->type = T_ELSE;
4505 if (pfile->if_stack->if_succeeded)
4506 skip_if_group (pfile, 0);
4508 ++pfile->if_stack->if_succeeded; /* continue processing input */
4509 output_line_command (pfile, 1, same_file);
4515 * unstack after #endif command
4521 struct directive *keyword,
4522 U_CHAR *buf, U_CHAR *limit)
4524 if (CPP_PEDANTIC (pfile))
4525 validate_else (pfile, "#endif");
4526 skip_rest_of_line (pfile);
4528 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4529 cpp_error (pfile, "unbalanced `#endif'");
4532 IF_STACK_FRAME *temp = pfile->if_stack;
4533 pfile->if_stack = temp->next;
4534 if (temp->control_macro != 0)
4536 /* This #endif matched a #ifndef at the start of the file.
4537 See if it is at the end of the file. */
4538 struct parse_marker start_mark;
4541 parse_set_mark (&start_mark, pfile);
4545 cpp_skip_hspace (pfile);
4550 parse_goto_mark (&start_mark, pfile);
4551 parse_clear_mark (&start_mark);
4555 /* If we get here, this #endif ends a #ifndef
4556 that contains all of the file (aside from whitespace).
4557 Arrange not to include the file again
4558 if the macro that was tested is defined.
4560 Do not do this for the top-level file in a -include or any
4561 file in a -imacros. */
4565 && ! (indepth == 1 && pfile->no_record_file)
4566 && ! (pfile->no_record_file && no_output))
4569 struct file_name_list *ifile = pfile->all_include_files;
4571 for ( ; ifile != NULL; ifile = ifile->next)
4573 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4575 ifile->control_macro = temp->control_macro;
4583 output_line_command (pfile, 1, same_file);
4588 /* When an #else or #endif is found while skipping failed conditional,
4589 if -pedantic was specified, this is called to warn about text after
4590 the command name. P points to the first char after the command name. */
4598 cpp_skip_hspace (pfile);
4600 if (c != EOF && c != '\n')
4602 "text following `%s' violates ANSI standard", directive);
4605 /* Get the next token, and add it to the text in pfile->token_buffer.
4606 Return the kind of token we got. */
4613 register int c, c2, c3;
4615 long start_line, start_column;
4616 enum cpp_token token;
4617 struct cpp_options *opts = CPP_OPTIONS (pfile);
4618 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4624 if (CPP_BUFFER (pfile)->seen_eof)
4626 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4633 cpp_buffer *next_buf
4634 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4635 CPP_BUFFER (pfile)->seen_eof = 1;
4636 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4638 /* We're about to return from an #include file.
4639 Emit #line information now (as part of the CPP_POP) result.
4640 But the #line refers to the file we will pop to. */
4641 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4642 CPP_BUFFER (pfile) = next_buf;
4643 pfile->input_stack_listing_current = 0;
4644 output_line_command (pfile, 0, leave_file);
4645 CPP_BUFFER (pfile) = cur_buffer;
4655 struct parse_marker start_mark;
4657 if (PEEKC () == '=')
4659 if (opts->put_out_comments)
4660 parse_set_mark (&start_mark, pfile);
4662 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4663 &start_line, &start_column);
4664 c = skip_comment (pfile, &newlines);
4665 if (opts->put_out_comments && (c == '/' || c == EOF))
4666 parse_clear_mark (&start_mark);
4671 cpp_error_with_line (pfile, start_line, start_column,
4672 "unterminated comment");
4675 c = '/'; /* Initial letter of comment. */
4677 /* Comments are equivalent to spaces.
4678 For -traditional, a comment is equivalent to nothing. */
4679 if (opts->put_out_comments)
4681 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4683 U_CHAR *start = pbuf->buf + start_mark.position;
4684 int len = pbuf->cur - start;
4685 CPP_RESERVE(pfile, 1 + len);
4686 CPP_PUTC_Q (pfile, c);
4687 CPP_PUTS_Q (pfile, start, len);
4688 pfile->lineno += newlines;
4689 parse_clear_mark (&start_mark);
4692 else if (CPP_TRADITIONAL (pfile))
4699 /* This may not work if cpp_get_token is called recursively,
4700 since many places look for horizontal space. */
4703 /* Copy the newlines into the output buffer, in order to
4704 avoid the pain of a #line every time a multiline comment
4706 CPP_RESERVE(pfile, newlines);
4707 while (--newlines >= 0)
4709 CPP_PUTC_Q (pfile, '\n');
4715 CPP_RESERVE(pfile, 1);
4716 CPP_PUTC_Q (pfile, ' ');
4720 if (opts->for_lint) {
4723 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4725 if (lintcmd != NULL) {
4726 /* I believe it is always safe to emit this newline: */
4728 bcopy ("#pragma lint ", (char *) obp, 13);
4730 bcopy (lintcmd, (char *) obp, cmdlen);
4735 bcopy (argbp, (char *) obp, arglen);
4739 /* OK, now bring us back to the state we were in before we entered
4740 this branch. We need #line b/c the newline for the pragma
4741 could fuck things up. */
4742 output_line_command (pfile, 0, same_file);
4743 *(obp++) = ' '; /* just in case, if comments are copied thru */
4751 /* If this is expanding a macro definition, don't recognize
4752 preprocessor directives. */
4755 /* If this is expand_into_temp_buffer, recognize them
4756 only after an actual newline at this level,
4757 not at the beginning of the input level. */
4758 if (ip->fname == 0 && beg_of_line == ip->buf)
4764 if (!pfile->only_seen_white)
4766 if (handle_directive (pfile))
4767 return CPP_DIRECTIVE;
4768 pfile->only_seen_white = 0;
4773 /* A single quoted string is treated like a double -- some
4774 programs (e.g., troff) are perverse this way */
4775 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4776 &start_line, &start_column);
4777 old_written = CPP_WRITTEN (pfile);
4779 CPP_PUTC (pfile, c);
4785 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4787 /* try harder: this string crosses a macro expansion
4788 boundary. This can happen naturally if -traditional.
4789 Otherwise, only -D can make a macro with an unmatched
4791 cpp_buffer *next_buf
4792 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4793 (*CPP_BUFFER (pfile)->cleanup)
4794 (CPP_BUFFER (pfile), pfile);
4795 CPP_BUFFER (pfile) = next_buf;
4798 if (!CPP_TRADITIONAL (pfile))
4800 cpp_error_with_line (pfile, start_line, start_column,
4801 "unterminated string or character constant");
4802 if (pfile->multiline_string_line != start_line
4803 && pfile->multiline_string_line != 0)
4804 cpp_error_with_line (pfile,
4805 pfile->multiline_string_line, -1,
4806 "possible real start of unterminated constant");
4807 pfile->multiline_string_line = 0;
4811 CPP_PUTC (pfile, cc);
4815 /* Traditionally, end of line ends a string constant with
4816 no error. So exit the loop and record the new line. */
4817 if (CPP_TRADITIONAL (pfile))
4821 cpp_error_with_line (pfile, start_line, start_column,
4822 "unterminated character constant");
4825 if (CPP_PEDANTIC (pfile)
4826 && pfile->multiline_string_line == 0)
4828 cpp_pedwarn_with_line (pfile, start_line, start_column,
4829 "string constant runs past end of line");
4831 if (pfile->multiline_string_line == 0)
4832 pfile->multiline_string_line = start_line;
4839 /* Backslash newline is replaced by nothing at all. */
4840 CPP_ADJUST_WRITTEN (pfile, -1);
4845 /* ANSI stupidly requires that in \\ the second \
4846 is *not* prevented from combining with a newline. */
4849 CPP_PUTC (pfile, cc);
4861 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4862 CPP_PWRITTEN (pfile));
4863 pfile->only_seen_white = 0;
4864 return c == '\'' ? CPP_CHAR : CPP_STRING;
4867 if (!opts->dollars_in_ident)
4872 if (opts->cplusplus && PEEKC () == ':')
4881 if (c2 == c || c2 == '=')
4891 if (PEEKC () == '=')
4898 if (c2 == '-' && opts->chill)
4900 /* Chill style comment */
4901 if (opts->put_out_comments)
4902 parse_set_mark (&start_mark, pfile);
4903 FORWARD(1); /* Skip second '-'. */
4911 /* Don't consider final '\n' to be part of comment. */
4917 goto return_comment;
4919 if (c2 == '-' || c2 == '=' || c2 == '>')
4924 if (pfile->parsing_include_directive)
4928 CPP_PUTC (pfile, c);
4933 if (c == '\n' || c == EOF)
4936 "missing '>' in `#include <FILENAME>'");
4942 /* else fall through */
4951 CPP_RESERVE (pfile, 4);
4952 CPP_PUTC (pfile, c);
4953 CPP_PUTC (pfile, c2);
4957 CPP_PUTC_Q (pfile, GETC ());
4958 CPP_NUL_TERMINATE_Q (pfile);
4959 pfile->only_seen_white = 0;
4963 if (CPP_BUFFER (pfile)->has_escapes)
4968 if (pfile->output_escapes)
4969 CPP_PUTS (pfile, "@-", 2);
4970 parse_name (pfile, GETC ());
4973 else if (is_space [c])
4975 CPP_RESERVE (pfile, 2);
4976 if (pfile->output_escapes)
4977 CPP_PUTC_Q (pfile, '@');
4978 CPP_PUTC_Q (pfile, c);
4982 if (pfile->output_escapes)
4984 CPP_PUTS (pfile, "@@", 2);
4994 CPP_RESERVE(pfile, 2);
4995 CPP_PUTC_Q (pfile, '.');
4999 /* FIXME - misses the case "..\\\n." */
5000 if (c2 == '.' && PEEKN(1) == '.')
5002 CPP_RESERVE(pfile, 4);
5003 CPP_PUTC_Q (pfile, '.');
5004 CPP_PUTC_Q (pfile, '.');
5005 CPP_PUTC_Q (pfile, '.');
5007 CPP_NUL_TERMINATE_Q (pfile);
5008 pfile->only_seen_white = 0;
5015 pfile->only_seen_white = 0;
5017 CPP_RESERVE(pfile, 3);
5018 CPP_PUTC_Q (pfile, c);
5019 CPP_PUTC_Q (pfile, GETC ());
5020 CPP_NUL_TERMINATE_Q (pfile);
5026 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5028 CPP_PUTC (pfile, c);
5034 case '0': case '1': case '2': case '3': case '4':
5035 case '5': case '6': case '7': case '8': case '9':
5040 CPP_RESERVE (pfile, 2);
5041 CPP_PUTC_Q (pfile, c);
5046 if (!is_idchar[c] && c != '.'
5047 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5052 CPP_NUL_TERMINATE_Q (pfile);
5053 pfile->only_seen_white = 0;
5055 case 'b': case 'c': case 'd': case 'h': case 'o':
5056 case 'B': case 'C': case 'D': case 'H': case 'O':
5057 if (opts->chill && PEEKC () == '\'')
5059 pfile->only_seen_white = 0;
5060 CPP_RESERVE (pfile, 2);
5061 CPP_PUTC_Q (pfile, c);
5062 CPP_PUTC_Q (pfile, '\'');
5068 goto chill_number_eof;
5071 if (c == '\\' && PEEKC() == '\n')
5078 CPP_PUTC (pfile, c);
5082 CPP_RESERVE (pfile, 2);
5083 CPP_PUTC_Q (pfile, c);
5084 CPP_NUL_TERMINATE_Q (pfile);
5091 CPP_NUL_TERMINATE (pfile);
5098 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5099 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5100 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5101 case 'x': case 'y': case 'z':
5102 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5103 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5104 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5109 unsigned char *ident;
5110 int before_name_written = CPP_WRITTEN (pfile);
5112 parse_name (pfile, c);
5113 pfile->only_seen_white = 0;
5114 if (pfile->no_macro_expand)
5116 ident = pfile->token_buffer + before_name_written;
5117 ident_len = CPP_PWRITTEN (pfile) - ident;
5118 hp = cpp_lookup (pfile, ident, ident_len, -1);
5121 if (hp->type == T_DISABLED)
5123 if (pfile->output_escapes)
5124 { /* Return "@-IDENT", followed by '\0'. */
5126 CPP_RESERVE (pfile, 3);
5127 ident = pfile->token_buffer + before_name_written;
5128 CPP_ADJUST_WRITTEN (pfile, 2);
5129 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5136 /* If macro wants an arglist, verify that a '(' follows.
5137 first skip all whitespace, copying it to the output
5138 after the macro name. Then, if there is no '(',
5139 decide this is not a macro call and leave things that way. */
5140 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5142 struct parse_marker macro_mark;
5144 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5146 cpp_buffer *next_buf;
5147 cpp_skip_hspace (pfile);
5148 if (PEEKC () != EOF)
5150 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5151 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5152 CPP_BUFFER (pfile) = next_buf;
5154 parse_set_mark (¯o_mark, pfile);
5157 cpp_skip_hspace (pfile);
5159 is_macro_call = c == '(';
5165 parse_goto_mark (¯o_mark, pfile);
5166 parse_clear_mark (¯o_mark);
5170 /* This is now known to be a macro call. */
5172 /* it might not actually be a macro. */
5173 if (hp->type != T_MACRO) {
5174 int xbuf_len; U_CHAR *xbuf;
5175 CPP_SET_WRITTEN (pfile, before_name_written);
5176 special_symbol (hp, pfile);
5177 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5178 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5179 CPP_SET_WRITTEN (pfile, before_name_written);
5180 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5181 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5185 /* Expand the macro, reading arguments as needed,
5186 and push the expansion on the input stack. */
5187 macroexpand (pfile, hp);
5188 CPP_SET_WRITTEN (pfile, before_name_written);
5191 /* An extra "@ " is added to the end of a macro expansion
5192 to prevent accidental token pasting. We prefer to avoid
5193 unneeded extra spaces (for the sake of cpp-using tools like
5194 imake). Here we remove the space if it is safe to do so. */
5195 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5196 && pfile->buffer->rlimit[-2] == '@'
5197 && pfile->buffer->rlimit[-1] == ' ')
5199 int c1 = pfile->buffer->rlimit[-3];
5200 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5201 if (c2 == EOF || ! unsafe_chars (c1, c2))
5202 pfile->buffer->rlimit -= 2;
5207 case ' ': case '\t': case '\v': case '\r':
5210 CPP_PUTC (pfile, c);
5212 if (c == EOF || !is_hor_space[c])
5226 CPP_PUTC (pfile, c);
5227 if (pfile->only_seen_white == 0)
5228 pfile->only_seen_white = 1;
5230 output_line_command (pfile, 1, same_file);
5233 case '(': token = CPP_LPAREN; goto char1;
5234 case ')': token = CPP_RPAREN; goto char1;
5235 case '{': token = CPP_LBRACE; goto char1;
5236 case '}': token = CPP_RBRACE; goto char1;
5237 case ',': token = CPP_COMMA; goto char1;
5238 case ';': token = CPP_SEMICOLON; goto char1;
5244 pfile->only_seen_white = 0;
5245 CPP_PUTC (pfile, c);
5251 /* Like cpp_get_token, but skip spaces and comments. */
5253 cpp_get_non_space_token (
5256 int old_written = CPP_WRITTEN (pfile);
5259 enum cpp_token token = cpp_get_token (pfile);
5260 if (token != CPP_COMMENT && token != CPP_POP
5261 && token != CPP_HSPACE && token != CPP_VSPACE)
5263 CPP_SET_WRITTEN (pfile, old_written);
5267 /* Parse an identifier starting with C. */
5271 cpp_reader *pfile, int c)
5277 if (c == '\\' && PEEKC() == '\n')
5286 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5287 CPP_PUTC_Q (pfile, c);
5292 CPP_NUL_TERMINATE_Q (pfile);
5297 /* Maintain and search list of included files, for #import. */
5299 /* Hash a file name for import_hash_table. */
5307 while (*f) val += *f++;
5308 return (val%IMPORT_HASH_SIZE);
5311 /* Search for file FILENAME in import_hash_table.
5312 Return -2 if found, either a matching name or a matching inode.
5313 Otherwise, open the file and return a file descriptor if successful
5314 or -1 if unsuccessful. */
5320 struct file_name_list *searchptr)
5322 struct import_file *i;
5328 hashval = import_hash (filename);
5330 /* Attempt to find file in list of already included files */
5331 i = pfile->import_hash_table[hashval];
5334 if (!strcmp (filename, i->name))
5335 return -2; /* return found */
5338 /* Open it and try a match on inode/dev */
5339 fd = open_include_file (pfile, filename, searchptr);
5343 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5344 i = pfile->import_hash_table[h];
5346 /* Compare the inode and the device.
5347 Supposedly on some systems the inode is not a scalar. */
5348 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5349 && i->dev == sb.st_dev) {
5351 return -2; /* return found */
5356 return fd; /* Not found, return open file */
5359 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5367 struct import_file *i;
5371 hashval = import_hash (fname);
5373 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5374 i->name = (char *)xmalloc (strlen (fname)+1);
5375 strcpy (i->name, fname);
5376 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5378 i->next = pfile->import_hash_table[hashval];
5379 pfile->import_hash_table[hashval] = i;
5382 /* The file_name_map structure holds a mapping of file names for a
5383 particular directory. This mapping is read from the file named
5384 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5385 map filenames on a file system with severe filename restrictions,
5386 such as DOS. The format of the file name map file is just a series
5387 of lines with two tokens on each line. The first token is the name
5388 to map, and the second token is the actual name to use. */
5390 struct file_name_map
5392 struct file_name_map *map_next;
5397 #define FILE_NAME_MAP_FILE "header.gcc"
5399 /* Read a space delimited string of unlimited length from a stdio
5403 read_filename_string (
5411 set = alloc = xmalloc (len + 1);
5415 while ((ch = getc (f)) != EOF && ! is_space[ch])
5417 if (set - alloc == len)
5420 alloc = xrealloc (alloc, len + 1);
5421 set = alloc + len / 2;
5431 /* This structure holds a linked list of file name maps, one per directory. */
5432 struct file_name_map_list
5434 struct file_name_map_list *map_list_next;
5435 char *map_list_name;
5436 struct file_name_map *map_list_map;
5439 /* Read the file name map file for DIRNAME. */
5441 static struct file_name_map *
5446 register struct file_name_map_list *map_list_ptr;
5450 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5451 map_list_ptr = map_list_ptr->map_list_next)
5452 if (! strcmp (map_list_ptr->map_list_name, dirname))
5453 return map_list_ptr->map_list_map;
5455 map_list_ptr = ((struct file_name_map_list *)
5456 xmalloc (sizeof (struct file_name_map_list)));
5457 map_list_ptr->map_list_name = savestring (dirname);
5458 map_list_ptr->map_list_map = NULL;
5460 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5461 strcpy (name, dirname);
5464 strcat (name, FILE_NAME_MAP_FILE);
5465 f = fopen (name, "r");
5467 map_list_ptr->map_list_map = NULL;
5471 int dirlen = strlen (dirname);
5473 while ((ch = getc (f)) != EOF)
5476 struct file_name_map *ptr;
5480 from = read_filename_string (ch, f);
5481 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5483 to = read_filename_string (ch, f);
5485 ptr = ((struct file_name_map *)
5486 xmalloc (sizeof (struct file_name_map)));
5487 ptr->map_from = from;
5489 /* Make the real filename absolute. */
5494 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5495 strcpy (ptr->map_to, dirname);
5496 ptr->map_to[dirlen] = '/';
5497 strcpy (ptr->map_to + dirlen + 1, to);
5501 ptr->map_next = map_list_ptr->map_list_map;
5502 map_list_ptr->map_list_map = ptr;
5504 while ((ch = getc (f)) != '\n')
5511 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5512 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5514 return map_list_ptr->map_list_map;
5517 /* Try to open include file FILENAME. SEARCHPTR is the directory
5518 being tried from the include file search path. This function maps
5519 filenames on file systems based on information read by
5526 struct file_name_list *searchptr)
5528 register struct file_name_map *map;
5529 register char *from;
5532 if (searchptr && ! searchptr->got_name_map)
5534 searchptr->name_map = read_name_map (pfile,
5536 ? searchptr->fname : ".");
5537 searchptr->got_name_map = 1;
5540 /* First check the mapping for the directory we are using. */
5541 if (searchptr && searchptr->name_map)
5544 if (searchptr->fname)
5545 from += strlen (searchptr->fname) + 1;
5546 for (map = searchptr->name_map; map; map = map->map_next)
5548 if (! strcmp (map->map_from, from))
5550 /* Found a match. */
5551 return open (map->map_to, O_RDONLY, 0666);
5556 /* Try to find a mapping file for the particular directory we are
5557 looking in. Thus #include <sys/types.h> will look up sys/types.h
5558 in /usr/include/header.gcc and look up types.h in
5559 /usr/include/sys/header.gcc. */
5560 p = rindex (filename, '/');
5565 && strlen (searchptr->fname) == p - filename
5566 && ! strncmp (searchptr->fname, filename, p - filename))
5568 /* FILENAME is in SEARCHPTR, which we've already checked. */
5569 return open (filename, O_RDONLY, 0666);
5579 dir = (char *) alloca (p - filename + 1);
5580 bcopy (filename, dir, p - filename);
5581 dir[p - filename] = '\0';
5584 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5585 if (! strcmp (map->map_from, from))
5586 return open (map->map_to, O_RDONLY, 0666);
5588 return open (filename, O_RDONLY, 0666);
5591 /* Process the contents of include file FNAME, already open on descriptor F,
5593 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5594 "system" include directories (as decided by the `is_system_include'
5596 DIRPTR is the link in the dir path through which this file was found,
5597 or 0 if the file name was absolute or via the current directory.
5598 Return 1 on success, 0 on failure.
5600 The caller is responsible for the cpp_push_buffer. */
5607 int system_header_p,
5608 struct file_name_list *dirptr)
5614 cpp_buffer *fp; /* For input stack frame */
5615 int missing_newline = 0;
5617 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5619 cpp_perror_with_name (pfile, fname);
5621 cpp_pop_buffer (pfile);
5625 fp = CPP_BUFFER (pfile);
5626 fp->nominal_fname = fp->fname = fname;
5631 fp->system_header_p = system_header_p;
5634 fp->cleanup = file_cleanup;
5636 if (S_ISREG (st_mode)) {
5637 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5638 fp->alimit = fp->buf + st_size + 2;
5641 /* Read the file contents, knowing that st_size is an upper bound
5642 on the number of bytes we can read. */
5643 length = safe_read (f, fp->buf, st_size);
5644 fp->rlimit = fp->buf + length;
5645 if (length < 0) goto nope;
5647 else if (S_ISDIR (st_mode)) {
5648 cpp_error (pfile, "directory `%s' specified in #include", fname);
5652 /* Cannot count its file size before reading.
5653 First read the entire file into heap and
5654 copy them into buffer on stack. */
5659 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5662 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5664 goto nope; /* error! */
5666 if (st_size != bsize)
5667 break; /* End of file */
5669 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5675 if ((length > 0 && fp->buf[length - 1] != '\n')
5676 /* Backslash-newline at end is not good enough. */
5677 || (length > 1 && fp->buf[length - 2] == '\\')) {
5678 fp->buf[length++] = '\n';
5680 missing_newline = 1;
5683 fp->buf[length] = '\0';
5684 fp->rlimit = fp->buf + length;
5686 /* Close descriptor now, so nesting does not use lots of descriptors. */
5689 /* Must do this before calling trigraph_pcp, so that the correct file name
5690 will be printed in warning messages. */
5692 pfile->input_stack_listing_current = 0;
5702 if (missing_newline)
5705 if (CPP_PEDANTIC (pfile) && missing_newline)
5706 pedwarn ("file does not end in newline");
5709 input_file_stack_tick++;
5716 cpp_perror_with_name (pfile, fname);
5727 struct cpp_options *opts = CPP_OPTIONS (pfile);
5728 struct cpp_pending *pend;
5733 /* The code looks at the defaults through this pointer, rather than through
5734 the constant structure above. This pointer gets changed if an environment
5735 variable specifies other defaults. */
5736 struct default_include *include_defaults = include_defaults_array;
5738 /* Add dirs from CPATH after dirs from -I. */
5739 /* There seems to be confusion about what CPATH should do,
5740 so for the moment it is not documented. */
5741 /* Some people say that CPATH should replace the standard include dirs,
5742 but that seems pointless: it comes before them, so it overrides them
5744 p = (char *) getenv ("CPATH");
5745 if (p != 0 && ! opts->no_standard_includes)
5746 path_include (pfile, p);
5748 /* Now that dollars_in_ident is known, initialize is_idchar. */
5749 initialize_char_syntax (opts);
5751 /* Do partial setup of input buffer for the sake of generating
5752 early #line directives (when -g is in effect). */
5753 fp = cpp_push_buffer (pfile, NULL, 0);
5754 if (opts->in_fname == NULL)
5755 opts->in_fname = "";
5756 fp->nominal_fname = fp->fname = opts->in_fname;
5759 /* Install __LINE__, etc. Must follow initialize_char_syntax
5760 and option processing. */
5761 initialize_builtins (pfile);
5763 /* Do standard #defines and assertions
5764 that identify system and machine type. */
5766 if (!opts->inhibit_predefs) {
5767 char *p = (char *) alloca (strlen (predefs) + 1);
5768 strcpy (p, predefs);
5771 while (*p == ' ' || *p == '\t')
5773 /* Handle -D options. */
5774 if (p[0] == '-' && p[1] == 'D') {
5776 while (*p && *p != ' ' && *p != '\t')
5780 if (opts->debug_output)
5781 output_line_command (pfile, 0, same_file);
5782 cpp_define (pfile, q);
5783 while (*p == ' ' || *p == '\t')
5785 } else if (p[0] == '-' && p[1] == 'A') {
5786 /* Handle -A options (assertions). */
5795 past_name = assertion;
5796 /* Locate end of name. */
5797 while (*past_name && *past_name != ' '
5798 && *past_name != '\t' && *past_name != '(')
5800 /* Locate `(' at start of value. */
5802 while (*value && (*value == ' ' || *value == '\t'))
5804 if (*value++ != '(')
5806 while (*value && (*value == ' ' || *value == '\t'))
5809 /* Locate end of value. */
5810 while (*past_value && *past_value != ' '
5811 && *past_value != '\t' && *past_value != ')')
5813 termination = past_value;
5814 while (*termination && (*termination == ' ' || *termination == '\t'))
5816 if (*termination++ != ')')
5818 if (*termination && *termination != ' ' && *termination != '\t')
5820 /* Temporarily null-terminate the value. */
5821 save_char = *termination;
5822 *termination = '\0';
5823 /* Install the assertion. */
5824 make_assertion (pfile, "-A", assertion);
5825 *termination = (char) save_char;
5827 while (*p == ' ' || *p == '\t')
5835 /* Now handle the command line options. */
5837 /* Do -U's, -D's and -A's in the order they were seen. */
5838 /* First reverse the list. */
5839 opts->pending = nreverse_pending (opts->pending);
5841 for (pend = opts->pending; pend; pend = pend->next)
5843 if (pend->cmd != NULL && pend->cmd[0] == '-')
5845 switch (pend->cmd[1])
5848 if (opts->debug_output)
5849 output_line_command (pfile, 0, same_file);
5850 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5853 if (opts->debug_output)
5854 output_line_command (pfile, 0, same_file);
5855 cpp_define (pfile, pend->arg);
5858 make_assertion (pfile, "-A", pend->arg);
5864 opts->done_initializing = 1;
5866 { /* read the appropriate environment variable and if it exists
5867 replace include_defaults with the listed path. */
5869 switch ((opts->objc << 1) + opts->cplusplus)
5872 epath = getenv ("C_INCLUDE_PATH");
5875 epath = getenv ("CPLUS_INCLUDE_PATH");
5878 epath = getenv ("OBJC_INCLUDE_PATH");
5881 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5884 /* If the environment var for this language is set,
5885 add to the default list of include directories. */
5887 char *nstore = (char *) alloca (strlen (epath) + 2);
5889 char *startp, *endp;
5891 for (num_dirs = 1, startp = epath; *startp; startp++)
5892 if (*startp == PATH_SEPARATOR)
5895 = (struct default_include *) xmalloc ((num_dirs
5896 * sizeof (struct default_include))
5897 + sizeof (include_defaults_array));
5898 startp = endp = epath;
5901 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5902 if ((*endp == PATH_SEPARATOR)
5904 strncpy (nstore, startp, endp-startp);
5906 strcpy (nstore, ".");
5908 nstore[endp-startp] = '\0';
5910 include_defaults[num_dirs].fname = savestring (nstore);
5911 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5912 include_defaults[num_dirs].cxx_aware = 1;
5916 endp = startp = endp + 1;
5920 /* Put the usual defaults back in at the end. */
5921 bcopy ((char *) include_defaults_array,
5922 (char *) &include_defaults[num_dirs],
5923 sizeof (include_defaults_array));
5927 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5928 opts->first_system_include = opts->before_system;
5930 /* Unless -fnostdinc,
5931 tack on the standard include file dirs to the specified list */
5932 if (!opts->no_standard_includes) {
5933 struct default_include *p = include_defaults;
5934 char *specd_prefix = opts->include_prefix;
5935 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5936 int default_len = 0;
5937 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5938 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5939 default_len = strlen (default_prefix) - 7;
5940 default_prefix[default_len] = 0;
5942 /* Search "translated" versions of GNU directories.
5943 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5944 if (specd_prefix != 0 && default_len != 0)
5945 for (p = include_defaults; p->fname; p++) {
5946 /* Some standard dirs are only for C++. */
5948 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5949 /* Does this dir start with the prefix? */
5950 if (!strncmp (p->fname, default_prefix, default_len)) {
5951 /* Yes; change prefix and add to search list. */
5952 struct file_name_list *new
5953 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5954 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5955 char *str = (char *) xmalloc (this_len + 1);
5956 strcpy (str, specd_prefix);
5957 strcat (str, p->fname + default_len);
5959 new->control_macro = 0;
5960 new->c_system_include_path = !p->cxx_aware;
5961 new->got_name_map = 0;
5962 append_include_chain (pfile, new, new);
5963 if (opts->first_system_include == 0)
5964 opts->first_system_include = new;
5968 /* Search ordinary names for GNU include directories. */
5969 for (p = include_defaults; p->fname; p++) {
5970 /* Some standard dirs are only for C++. */
5972 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5973 struct file_name_list *new
5974 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5975 new->control_macro = 0;
5976 new->c_system_include_path = !p->cxx_aware;
5977 new->fname = p->fname;
5978 new->got_name_map = 0;
5979 append_include_chain (pfile, new, new);
5980 if (opts->first_system_include == 0)
5981 opts->first_system_include = new;
5986 /* Tack the after_include chain at the end of the include chain. */
5987 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5988 if (opts->first_system_include == 0)
5989 opts->first_system_include = opts->after_include;
5991 /* With -v, print the list of dirs to search. */
5992 if (opts->verbose) {
5993 struct file_name_list *p;
5994 fprintf (stderr, "#include \"...\" search starts here:\n");
5995 for (p = opts->include; p; p = p->next) {
5996 if (p == opts->first_bracket_include)
5997 fprintf (stderr, "#include <...> search starts here:\n");
5998 fprintf (stderr, " %s\n", p->fname);
6000 fprintf (stderr, "End of search list.\n");
6003 /* Scan the -imacros files before the main input.
6004 Much like #including them, but with no_output set
6005 so that only their macro definitions matter. */
6007 opts->no_output++; pfile->no_record_file++;
6008 for (pend = opts->pending; pend; pend = pend->next)
6010 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6012 int fd = open (pend->arg, O_RDONLY, 0666);
6015 cpp_perror_with_name (pfile, pend->arg);
6016 return FATAL_EXIT_CODE;
6018 cpp_push_buffer (pfile, NULL, 0);
6019 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6020 cpp_scan_buffer (pfile);
6023 opts->no_output--; pfile->no_record_file--;
6025 /* Copy the entire contents of the main input file into
6026 the stacked input buffer previously allocated for it. */
6027 if (fname == NULL || *fname == 0) {
6030 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6031 cpp_pfatal_with_name (pfile, fname);
6033 /* -MG doesn't select the form of output and must be specified with one of
6034 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6035 inhibit compilation. */
6036 if (opts->print_deps_missing_files
6037 && (opts->print_deps == 0 || !opts->no_output))
6038 fatal (pfile, "-MG must be specified with one of -M or -MM");
6040 /* Either of two environment variables can specify output of deps.
6041 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6042 where OUTPUT_FILE is the file to write deps info to
6043 and DEPS_TARGET is the target to mention in the deps. */
6045 if (opts->print_deps == 0
6046 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6047 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6048 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6054 spec = getenv ("SUNPRO_DEPENDENCIES");
6055 opts->print_deps = 2;
6058 opts->print_deps = 1;
6061 /* Find the space before the DEPS_TARGET, if there is one. */
6062 /* This should use index. (mrs) */
6063 while (*s != 0 && *s != ' ') s++;
6066 opts->deps_target = s + 1;
6067 output_file = (char *) xmalloc (s - spec + 1);
6068 bcopy (spec, output_file, s - spec);
6069 output_file[s - spec] = 0;
6073 opts->deps_target = 0;
6077 opts->deps_file = output_file;
6078 opts->print_deps_append = 1;
6081 /* For -M, print the expected object file name
6082 as the target of this Make-rule. */
6083 if (opts->print_deps)
6085 pfile->deps_allocated_size = 200;
6086 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6087 pfile->deps_buffer[0] = 0;
6088 pfile->deps_size = 0;
6089 pfile->deps_column = 0;
6091 if (opts->deps_target)
6092 deps_output (pfile, opts->deps_target, ':');
6093 else if (*opts->in_fname == 0)
6094 deps_output (pfile, "-", ':');
6100 /* Discard all directory prefixes from filename. */
6101 if ((q = rindex (opts->in_fname, '/')) != NULL
6102 #ifdef DIR_SEPARATOR
6103 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6110 /* Copy remainder to mungable area. */
6111 p = (char *) alloca (strlen(q) + 8);
6114 /* Output P, but remove known suffixes. */
6118 && p[len - 2] == '.'
6119 && index("cCsSm", p[len - 1]))
6122 && p[len - 3] == '.'
6123 && p[len - 2] == 'c'
6124 && p[len - 1] == 'c')
6127 && p[len - 4] == '.'
6128 && p[len - 3] == 'c'
6129 && p[len - 2] == 'x'
6130 && p[len - 1] == 'x')
6133 && p[len - 4] == '.'
6134 && p[len - 3] == 'c'
6135 && p[len - 2] == 'p'
6136 && p[len - 1] == 'p')
6139 /* Supply our own suffix. */
6150 deps_output (pfile, p, ':');
6151 deps_output (pfile, opts->in_fname, ' ');
6156 /* Make sure data ends with a newline. And put a null after it. */
6158 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6159 /* Backslash-newline at end is not good enough. */
6160 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6161 fp->buf[fp->length++] = '\n';
6162 missing_newline = 1;
6164 fp->buf[fp->length] = '\0';
6166 /* Unless inhibited, convert trigraphs in the input. */
6172 /* Scan the -include files before the main input.
6173 We push these in reverse order, so that the first one is handled first. */
6175 pfile->no_record_file++;
6176 opts->pending = nreverse_pending (opts->pending);
6177 for (pend = opts->pending; pend; pend = pend->next)
6179 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6181 int fd = open (pend->arg, O_RDONLY, 0666);
6184 cpp_perror_with_name (pfile, pend->arg);
6185 return FATAL_EXIT_CODE;
6187 cpp_push_buffer (pfile, NULL, 0);
6188 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6191 pfile->no_record_file--;
6193 /* Free the pending list. */
6194 for (pend = opts->pending; pend; )
6196 struct cpp_pending *next = pend->next;
6200 opts->pending = NULL;
6203 /* Scan the input, processing macros and directives. */
6205 rescan (&outbuf, 0);
6207 if (missing_newline)
6210 if (CPP_PEDANTIC (pfile) && missing_newline)
6211 pedwarn ("file does not end in newline");
6214 if (finclude (pfile, f, fname, 0, NULL_PTR))
6215 output_line_command (pfile, 0, same_file);
6216 return SUCCESS_EXIT_CODE;
6223 bzero ((char *) pfile, sizeof (cpp_reader));
6224 pfile->get_token = cpp_get_token;
6226 pfile->token_buffer_size = 200;
6227 pfile->token_buffer = (U_CHAR*)xmalloc (pfile->token_buffer_size);
6228 CPP_SET_WRITTEN (pfile, 0);
6230 pfile->system_include_depth = 0;
6231 pfile->dont_repeat_files = 0;
6232 pfile->all_include_files = 0;
6233 pfile->max_include_len = 0;
6234 pfile->timebuf = NULL;
6235 pfile->only_seen_white = 1;
6236 pfile->buffer = CPP_NULL_BUFFER(pfile);
6239 static struct cpp_pending *
6241 struct cpp_pending *list)
6244 register struct cpp_pending *prev = 0, *next, *pend;
6245 for (pend = list; pend; pend = next)
6260 struct cpp_pending *pend
6261 = (struct cpp_pending*)xmalloc (sizeof (struct cpp_pending));
6264 pend->next = CPP_OPTIONS (pfile)->pending;
6265 CPP_OPTIONS (pfile)->pending = pend;
6268 /* Handle command-line options in (argc, argv).
6269 Can be called multiple times, to handle multiple sets of options.
6270 Returns if an unrecognized option is seen.
6271 Returns number of handled arguments. */
6274 cpp_handle_options (
6280 struct cpp_options *opts = CPP_OPTIONS (pfile);
6281 for (i = 0; i < argc; i++) {
6282 if (argv[i][0] != '-') {
6283 if (opts->out_fname != NULL)
6284 fatal ("Usage: %s [switches] input output", argv[0]);
6285 else if (opts->in_fname != NULL)
6286 opts->out_fname = argv[i];
6288 opts->in_fname = argv[i];
6290 switch (argv[i][1]) {
6293 if (!strcmp (argv[i], "-include")
6294 || !strcmp (argv[i], "-imacros")) {
6296 fatal ("Filename missing after `%s' option", argv[i]);
6298 push_pending (pfile, argv[i], argv[i+1]), i++;
6300 if (!strcmp (argv[i], "-iprefix")) {
6302 fatal ("Filename missing after `-iprefix' option");
6304 opts->include_prefix = argv[++i];
6306 if (!strcmp (argv[i], "-ifoutput")) {
6307 opts->output_conditionals = 1;
6309 if (!strcmp (argv[i], "-isystem")) {
6310 struct file_name_list *dirtmp;
6313 fatal ("Filename missing after `-isystem' option");
6315 dirtmp = (struct file_name_list *)
6316 xmalloc (sizeof (struct file_name_list));
6318 dirtmp->control_macro = 0;
6319 dirtmp->c_system_include_path = 1;
6320 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6321 strcpy (dirtmp->fname, argv[++i]);
6322 dirtmp->got_name_map = 0;
6324 if (opts->before_system == 0)
6325 opts->before_system = dirtmp;
6327 opts->last_before_system->next = dirtmp;
6328 opts->last_before_system = dirtmp; /* Tail follows the last one */
6330 /* Add directory to end of path for includes,
6331 with the default prefix at the front of its name. */
6332 if (!strcmp (argv[i], "-iwithprefix")) {
6333 struct file_name_list *dirtmp;
6336 if (opts->include_prefix != 0)
6337 prefix = opts->include_prefix;
6339 prefix = savestring (GCC_INCLUDE_DIR);
6340 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6341 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6342 prefix[strlen (prefix) - 7] = 0;
6345 dirtmp = (struct file_name_list *)
6346 xmalloc (sizeof (struct file_name_list));
6347 dirtmp->next = 0; /* New one goes on the end */
6348 dirtmp->control_macro = 0;
6349 dirtmp->c_system_include_path = 0;
6351 fatal ("Directory name missing after `-iwithprefix' option");
6353 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6354 + strlen (prefix) + 1);
6355 strcpy (dirtmp->fname, prefix);
6356 strcat (dirtmp->fname, argv[++i]);
6357 dirtmp->got_name_map = 0;
6359 if (opts->after_include == 0)
6360 opts->after_include = dirtmp;
6362 opts->last_after_include->next = dirtmp;
6363 opts->last_after_include = dirtmp; /* Tail follows the last one */
6365 /* Add directory to main path for includes,
6366 with the default prefix at the front of its name. */
6367 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6368 struct file_name_list *dirtmp;
6371 if (opts->include_prefix != 0)
6372 prefix = opts->include_prefix;
6374 prefix = savestring (GCC_INCLUDE_DIR);
6375 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6376 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6377 prefix[strlen (prefix) - 7] = 0;
6380 dirtmp = (struct file_name_list *)
6381 xmalloc (sizeof (struct file_name_list));
6382 dirtmp->next = 0; /* New one goes on the end */
6383 dirtmp->control_macro = 0;
6384 dirtmp->c_system_include_path = 0;
6386 fatal ("Directory name missing after `-iwithprefixbefore' option");
6388 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6389 + strlen (prefix) + 1);
6390 strcpy (dirtmp->fname, prefix);
6391 strcat (dirtmp->fname, argv[++i]);
6392 dirtmp->got_name_map = 0;
6394 append_include_chain (pfile, dirtmp, dirtmp);
6396 /* Add directory to end of path for includes. */
6397 if (!strcmp (argv[i], "-idirafter")) {
6398 struct file_name_list *dirtmp;
6400 dirtmp = (struct file_name_list *)
6401 xmalloc (sizeof (struct file_name_list));
6402 dirtmp->next = 0; /* New one goes on the end */
6403 dirtmp->control_macro = 0;
6404 dirtmp->c_system_include_path = 0;
6406 fatal ("Directory name missing after `-idirafter' option");
6408 dirtmp->fname = argv[++i];
6409 dirtmp->got_name_map = 0;
6411 if (opts->after_include == 0)
6412 opts->after_include = dirtmp;
6414 opts->last_after_include->next = dirtmp;
6415 opts->last_after_include = dirtmp; /* Tail follows the last one */
6420 if (opts->out_fname != NULL)
6421 fatal ("Output filename specified twice");
6423 fatal ("Filename missing after -o option");
6424 opts->out_fname = argv[++i];
6425 if (!strcmp (opts->out_fname, "-"))
6426 opts->out_fname = "";
6430 if (!strcmp (argv[i], "-pedantic"))
6431 CPP_PEDANTIC (pfile) = 1;
6432 else if (!strcmp (argv[i], "-pedantic-errors")) {
6433 CPP_PEDANTIC (pfile) = 1;
6434 opts->pedantic_errors = 1;
6437 else if (!strcmp (argv[i], "-pcp")) {
6438 char *pcp_fname = argv[++i];
6440 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6441 ? fopen (pcp_fname, "w")
6442 : fdopen (dup (fileno (stdout)), "w"));
6443 if (pcp_outfile == 0)
6444 cpp_pfatal_with_name (pfile, pcp_fname);
6451 if (!strcmp (argv[i], "-traditional")) {
6452 opts->traditional = 1;
6453 if (opts->dollars_in_ident > 0)
6454 opts->dollars_in_ident = 1;
6455 } else if (!strcmp (argv[i], "-trigraphs")) {
6457 opts->no_trigraphs = 0;
6462 if (! strcmp (argv[i], "-lang-c"))
6463 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6464 if (! strcmp (argv[i], "-lang-c++"))
6465 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6466 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6467 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6468 if (! strcmp (argv[i], "-lang-objc"))
6469 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6470 if (! strcmp (argv[i], "-lang-objc++"))
6471 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6472 if (! strcmp (argv[i], "-lang-asm"))
6474 if (! strcmp (argv[i], "-lint"))
6476 if (! strcmp (argv[i], "-lang-chill"))
6477 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6478 opts->traditional = 1, opts->no_trigraphs = 1;
6482 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6486 opts->inhibit_warnings = 1;
6490 if (!strcmp (argv[i], "-Wtrigraphs"))
6491 opts->warn_trigraphs = 1;
6492 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6493 opts->warn_trigraphs = 0;
6494 else if (!strcmp (argv[i], "-Wcomment"))
6495 opts->warn_comments = 1;
6496 else if (!strcmp (argv[i], "-Wno-comment"))
6497 opts->warn_comments = 0;
6498 else if (!strcmp (argv[i], "-Wcomments"))
6499 opts->warn_comments = 1;
6500 else if (!strcmp (argv[i], "-Wno-comments"))
6501 opts->warn_comments = 0;
6502 else if (!strcmp (argv[i], "-Wtraditional"))
6503 opts->warn_stringify = 1;
6504 else if (!strcmp (argv[i], "-Wno-traditional"))
6505 opts->warn_stringify = 0;
6506 else if (!strcmp (argv[i], "-Wimport"))
6507 opts->warn_import = 1;
6508 else if (!strcmp (argv[i], "-Wno-import"))
6509 opts->warn_import = 0;
6510 else if (!strcmp (argv[i], "-Werror"))
6511 opts->warnings_are_errors = 1;
6512 else if (!strcmp (argv[i], "-Wno-error"))
6513 opts->warnings_are_errors = 0;
6514 else if (!strcmp (argv[i], "-Wall"))
6516 opts->warn_trigraphs = 1;
6517 opts->warn_comments = 1;
6522 /* The style of the choices here is a bit mixed.
6523 The chosen scheme is a hybrid of keeping all options in one string
6524 and specifying each option in a separate argument:
6525 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6526 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6527 -M[M][G][D file]. This is awkward to handle in specs, and is not
6529 /* ??? -MG must be specified in addition to one of -M or -MM.
6530 This can be relaxed in the future without breaking anything.
6531 The converse isn't true. */
6533 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6534 if (!strcmp (argv[i], "-MG"))
6536 opts->print_deps_missing_files = 1;
6539 if (!strcmp (argv[i], "-M"))
6540 opts->print_deps = 2;
6541 else if (!strcmp (argv[i], "-MM"))
6542 opts->print_deps = 1;
6543 else if (!strcmp (argv[i], "-MD"))
6544 opts->print_deps = 2;
6545 else if (!strcmp (argv[i], "-MMD"))
6546 opts->print_deps = 1;
6547 /* For -MD and -MMD options, write deps on file named by next arg. */
6548 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6551 fatal ("Filename missing after %s option", argv[i]);
6552 opts->deps_file = argv[++i];
6556 /* For -M and -MM, write deps on standard output
6557 and suppress the usual output. */
6558 opts->no_output = 1;
6564 char *p = argv[i] + 2;
6566 while ((c = *p++) != 0) {
6567 /* Arg to -d specifies what parts of macros to dump */
6570 opts->dump_macros = dump_only;
6571 opts->no_output = 1;
6574 opts->dump_macros = dump_names;
6577 opts->dump_macros = dump_definitions;
6585 if (argv[i][2] == '3')
6586 opts->debug_output = 1;
6590 fprintf (stderr, "GNU CPP version %s", version_string);
6591 #ifdef TARGET_VERSION
6594 fprintf (stderr, "\n");
6595 // opts->verbose = 1;
6599 opts->print_include_names = 1;
6603 if (argv[i][2] != 0)
6604 push_pending (pfile, "-D", argv[i] + 2);
6605 else if (i + 1 == argc)
6606 fatal ("Macro name missing after -D option");
6608 i++, push_pending (pfile, "-D", argv[i]);
6615 if (argv[i][2] != 0)
6617 else if (i + 1 == argc)
6618 fatal ("Assertion missing after -A option");
6622 if (!strcmp (p, "-")) {
6623 struct cpp_pending **ptr;
6624 /* -A- eliminates all predefined macros and assertions.
6625 Let's include also any that were specified earlier
6626 on the command line. That way we can get rid of any
6627 that were passed automatically in from GCC. */
6629 opts->inhibit_predefs = 1;
6630 for (ptr = &opts->pending; *ptr != NULL; )
6632 struct cpp_pending *pend = *ptr;
6633 if (pend->cmd && pend->cmd[0] == '-'
6634 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6643 push_pending (pfile, "-A", p);
6648 case 'U': /* JF #undef something */
6649 if (argv[i][2] != 0)
6650 push_pending (pfile, "-U", argv[i] + 2);
6651 else if (i + 1 == argc)
6652 fatal ("Macro name missing after -U option");
6654 push_pending (pfile, "-U", argv[i+1]), i++;
6658 opts->put_out_comments = 1;
6661 case 'E': /* -E comes from cc -E; ignore it. */
6665 opts->no_line_commands = 1;
6668 case '$': /* Don't include $ in identifiers. */
6669 opts->dollars_in_ident = 0;
6672 case 'I': /* Add directory to path for includes. */
6674 struct file_name_list *dirtmp;
6676 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6677 && !strcmp (argv[i] + 2, "-")) {
6678 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6679 /* Don't use any preceding -I directories for #include <...>. */
6680 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6683 dirtmp = (struct file_name_list *)
6684 xmalloc (sizeof (struct file_name_list));
6685 dirtmp->next = 0; /* New one goes on the end */
6686 dirtmp->control_macro = 0;
6687 dirtmp->c_system_include_path = 0;
6688 if (argv[i][2] != 0)
6689 dirtmp->fname = argv[i] + 2;
6690 else if (i + 1 == argc)
6691 fatal ("Directory name missing after -I option");
6693 dirtmp->fname = argv[++i];
6694 dirtmp->got_name_map = 0;
6695 append_include_chain (pfile, dirtmp, dirtmp);
6701 if (!strcmp (argv[i], "-nostdinc"))
6702 /* -nostdinc causes no default include directories.
6703 You must specify all include-file directories with -I. */
6704 opts->no_standard_includes = 1;
6705 else if (!strcmp (argv[i], "-nostdinc++"))
6706 /* -nostdinc++ causes no default C++-specific include directories. */
6707 opts->no_standard_cplusplus_includes = 1;
6709 else if (!strcmp (argv[i], "-noprecomp"))
6715 /* Sun compiler passes undocumented switch "-undef".
6716 Let's assume it means to inhibit the predefined symbols. */
6717 opts->inhibit_predefs = 1;
6720 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6721 if (opts->in_fname == NULL) {
6722 opts->in_fname = "";
6724 } else if (opts->out_fname == NULL) {
6725 opts->out_fname = "";
6727 } /* else fall through into error */
6741 struct cpp_options *opts = CPP_OPTIONS (pfile);
6743 if (opts->print_deps)
6745 /* Stream on which to print the dependency information. */
6748 /* Don't actually write the deps file if compilation has failed. */
6749 if (pfile->errors == 0)
6751 char *deps_mode = opts->print_deps_append ? "a" : "w";
6752 if (opts->deps_file == 0)
6753 deps_stream = stdout;
6754 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6755 cpp_pfatal_with_name (pfile, opts->deps_file);
6756 fputs (pfile->deps_buffer, deps_stream);
6757 putc ('\n', deps_stream);
6758 if (opts->deps_file)
6760 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6761 fatal ("I/O error on output");
6767 /* Free resources used by PFILE. */
6774 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6775 cpp_pop_buffer (pfile);
6777 if (pfile->token_buffer)
6779 free (pfile->token_buffer);
6780 pfile->token_buffer = NULL;
6783 if (pfile->deps_buffer)
6785 free (pfile->deps_buffer);
6786 pfile->deps_buffer = NULL;
6787 pfile->deps_allocated_size = 0;
6790 while (pfile->if_stack)
6792 IF_STACK_FRAME *temp = pfile->if_stack;
6793 pfile->if_stack = temp->next;
6797 while (pfile->dont_repeat_files)
6799 struct file_name_list *temp = pfile->dont_repeat_files;
6800 pfile->dont_repeat_files = temp->next;
6805 while (pfile->all_include_files)
6807 struct file_name_list *temp = pfile->all_include_files;
6808 pfile->all_include_files = temp->next;
6813 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6815 register struct import_file *imp = pfile->import_hash_table[i];
6818 struct import_file *next = imp->next;
6823 pfile->import_hash_table[i] = 0;
6826 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6828 while (pfile->assertion_hashtab[i])
6829 delete_assertion (pfile->assertion_hashtab[i]);
6832 cpp_hash_cleanup (pfile);
6838 struct directive *keyword,
6839 U_CHAR *buf, U_CHAR *limit)
6841 long symstart; /* remember where symbol name starts */
6843 int sym_length; /* and how long it is */
6844 struct arglist *tokens = NULL;
6846 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6847 && !CPP_BUFFER (pfile)->system_header_p)
6848 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6850 cpp_skip_hspace (pfile);
6851 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6852 parse_name (pfile, GETC());
6853 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6856 cpp_skip_hspace (pfile);
6857 if (PEEKC() != '(') {
6858 cpp_error (pfile, "missing token-sequence in `#assert'");
6864 tokens = read_token_list (pfile, &error_flag);
6868 cpp_error (pfile, "empty token-sequence in `#assert'");
6871 cpp_skip_hspace (pfile);
6873 if (c != EOF && c != '\n')
6874 cpp_pedwarn (pfile, "junk at end of `#assert'");
6875 skip_rest_of_line (pfile);
6878 /* If this name isn't already an assertion name, make it one.
6879 Error if it was already in use in some other way. */
6882 ASSERTION_HASHNODE *hp;
6883 U_CHAR *symname = pfile->token_buffer + symstart;
6884 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6885 struct tokenlist_list *value
6886 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6888 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6890 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6891 cpp_error (pfile, "`defined' redefined as assertion");
6892 hp = assertion_install (pfile, symname, sym_length, hashcode);
6895 /* Add the spec'd token-sequence to the list of such. */
6896 value->tokens = tokens;
6897 value->next = hp->value;
6900 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6903 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6904 skip_rest_of_line (pfile);
6911 struct directive *keyword,
6912 U_CHAR *buf, U_CHAR *limit)
6914 long symstart; /* remember where symbol name starts */
6915 int sym_length; /* and how long it is */
6918 struct arglist *tokens = NULL;
6919 int tokens_specified = 0;
6921 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6922 && !CPP_BUFFER (pfile)->system_header_p)
6923 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6925 cpp_skip_hspace (pfile);
6927 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6928 parse_name (pfile, GETC());
6929 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6932 cpp_skip_hspace (pfile);
6933 if (PEEKC() == '(') {
6936 tokens = read_token_list (pfile, &error_flag);
6940 cpp_error (pfile, "empty token list in `#unassert'");
6944 tokens_specified = 1;
6947 cpp_skip_hspace (pfile);
6949 if (c != EOF && c != '\n')
6950 cpp_error (pfile, "junk at end of `#unassert'");
6951 skip_rest_of_line (pfile);
6954 ASSERTION_HASHNODE *hp;
6955 U_CHAR *symname = pfile->token_buffer + symstart;
6956 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6957 struct tokenlist_list *tail, *prev;
6959 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6963 /* If no token list was specified, then eliminate this assertion
6965 if (! tokens_specified)
6966 delete_assertion (hp);
6968 /* If a list of tokens was given, then delete any matching list. */
6973 struct tokenlist_list *next = tail->next;
6974 if (compare_token_lists (tail->tokens, tokens)) {
6978 hp->value = tail->next;
6979 free_token_list (tail->tokens);
6989 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6992 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6993 skip_rest_of_line (pfile);
6997 /* Test whether there is an assertion named NAME
6998 and optionally whether it has an asserted token list TOKENS.
6999 NAME is not null terminated; its length is SYM_LENGTH.
7000 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7007 int tokens_specified,
7008 struct arglist *tokens)
7010 ASSERTION_HASHNODE *hp;
7011 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7013 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7014 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7016 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7018 /* It is not an assertion; just return false. */
7021 /* If no token list was specified, then value is 1. */
7022 if (! tokens_specified)
7026 struct tokenlist_list *tail;
7030 /* If a list of tokens was given,
7031 then succeed if the assertion records a matching list. */
7034 if (compare_token_lists (tail->tokens, tokens))
7039 /* Fail if the assertion has no matching list. */
7044 /* Compare two lists of tokens for equality including order of tokens. */
7047 compare_token_lists (
7048 struct arglist *l1, struct arglist *l2 )
7051 if (l1->length != l2->length)
7053 if (strncmp (l1->name, l2->name, l1->length))
7059 /* Succeed if both lists end at the same time. */
7064 reverse_token_list (
7065 struct arglist *tokens)
7067 register struct arglist *prev = 0, *this, *next;
7068 for (this = tokens; this; this = next)
7077 /* Read a space-separated list of tokens ending in a close parenthesis.
7078 Return a list of strings, in the order they were written.
7079 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7081 static struct arglist *
7086 struct arglist *token_ptrs = 0;
7091 FORWARD (1); /* Skip '(' */
7093 /* Loop over the assertion value tokens. */
7096 struct arglist *temp;
7097 long name_written = CPP_WRITTEN (pfile);
7098 int eofp = 0; int c;
7100 cpp_skip_hspace (pfile);
7104 /* Find the end of the token. */
7107 CPP_PUTC (pfile, c);
7115 CPP_PUTC (pfile, c);
7117 else if (c == '"' || c == '\'')
7120 cpp_get_token (pfile);
7126 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7127 && c != '"' && c != '\'')
7129 CPP_PUTC (pfile, c);
7132 if (c != EOF) FORWARD(-1);
7135 length = CPP_WRITTEN (pfile) - name_written;
7136 temp = (struct arglist *)
7137 xmalloc (sizeof (struct arglist) + length + 1);
7138 temp->name = (U_CHAR *) (temp + 1);
7139 bcopy ((char *) (pfile->token_buffer + name_written),
7140 (char *) temp->name, length);
7141 temp->name[length] = 0;
7142 temp->next = token_ptrs;
7144 temp->length = length;
7146 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7148 if (c == EOF || c == '\n')
7151 "unterminated token sequence following `#' operator");
7156 /* We accumulated the names in reverse order.
7157 Now reverse them to get the proper order. */
7158 return reverse_token_list (token_ptrs);
7163 struct arglist *tokens)
7166 struct arglist *next = tokens->next;
7167 free (tokens->name);
7173 /* Get the file-mode and data size of the file open on FD
7174 and store them in *MODE_POINTER and *SIZE_POINTER. */
7177 file_size_and_mode (
7180 long int *size_pointer)
7184 if (fstat (fd, &sbuf) < 0) return (-1);
7185 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7186 if (size_pointer) *size_pointer = sbuf.st_size;
7190 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7191 retrying if necessary. Return a negative value if an error occurs,
7192 otherwise return the actual number of bytes read,
7193 which must be LEN unless end-of-file was reached. */
7203 int nchars = read (desc, ptr, left);
7224 unsigned size = strlen (input);
7225 char *output = xmalloc (size + 1);
7226 strcpy (output, input);
7230 /* Initialize PMARK to remember the current position of PFILE. */
7233 struct parse_marker *pmark,
7236 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7237 pmark->next = pbuf->marks;
7238 pbuf->marks = pmark;
7240 pmark->position = pbuf->cur - pbuf->buf;
7243 /* Cleanup PMARK - we no longer need it. */
7246 struct parse_marker *pmark)
7248 struct parse_marker **pp = &pmark->buf->marks;
7249 for (; ; pp = &(*pp)->next) {
7250 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7251 if (*pp == pmark) break;
7256 /* Backup the current position of PFILE to that saved in PMARK. */
7260 struct parse_marker *pmark,
7263 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7264 if (pbuf != pmark->buf)
7265 fatal ("internal error %s", "parse_goto_mark");
7266 pbuf->cur = pbuf->buf + pmark->position;
7269 /* Reset PMARK to point to the current position of PFILE. (Same
7270 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7274 struct parse_marker *pmark,
7277 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7278 if (pbuf != pmark->buf)
7279 fatal ("internal error %s", "parse_move_mark");
7280 pmark->position = pbuf->cur - pbuf->buf;
7284 cpp_read_check_assertion (
7287 int name_start = CPP_WRITTEN (pfile);
7288 int name_length, name_written;
7290 FORWARD (1); /* Skip '#' */
7291 cpp_skip_hspace (pfile);
7292 parse_name (pfile, GETC ());
7293 name_written = CPP_WRITTEN (pfile);
7294 name_length = name_written - name_start;
7295 cpp_skip_hspace (pfile);
7296 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7299 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7300 result = check_assertion (pfile,
7301 pfile->token_buffer + name_start, name_length,
7305 result = check_assertion (pfile,
7306 pfile->token_buffer + name_start, name_length,
7308 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7313 cpp_print_file_and_line (pfile)
7316 cpp_buffer *ip = cpp_file_buffer (pfile);
7321 cpp_buf_line_and_col (ip, &line, &col);
7322 cpp_file_line_for_message (pfile, ip->nominal_fname,
7323 line, pfile->show_column ? col : -1);
7328 cpp_error (pfile, msg, arg1, arg2, arg3)
7331 char *arg1, *arg2, *arg3;
7333 cpp_print_containing_files (pfile);
7334 cpp_print_file_and_line (pfile);
7335 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7338 /* Print error message but don't count it. */
7341 cpp_warning (pfile, msg, arg1, arg2, arg3)
7344 char *arg1, *arg2, *arg3;
7346 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7349 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7352 cpp_print_containing_files (pfile);
7353 cpp_print_file_and_line (pfile);
7354 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7357 /* Print an error message and maybe count it. */
7360 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7363 char *arg1, *arg2, *arg3;
7365 if (CPP_OPTIONS (pfile)->pedantic_errors)
7366 cpp_error (pfile, msg, arg1, arg2, arg3);
7368 cpp_warning (pfile, msg, arg1, arg2, arg3);
7372 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7376 char *arg1, *arg2, *arg3;
7379 cpp_buffer *ip = cpp_file_buffer (pfile);
7381 cpp_print_containing_files (pfile);
7384 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7386 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7390 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7394 char *arg1, *arg2, *arg3;
7399 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7402 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7405 cpp_print_containing_files (pfile);
7407 ip = cpp_file_buffer (pfile);
7410 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7412 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7416 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7420 char *arg1, *arg2, *arg3;
7422 if (CPP_OPTIONS (pfile)->pedantic_errors)
7423 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7425 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7428 /* Report a warning (or an error if pedantic_errors)
7429 giving specified file name and line number, not current. */
7432 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7437 char *arg1, *arg2, *arg3;
7439 if (!CPP_OPTIONS (pfile)->pedantic_errors
7440 && CPP_OPTIONS (pfile)->inhibit_warnings)
7443 cpp_file_line_for_message (pfile, file, line, -1);
7444 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7445 msg, arg1, arg2, arg3);
7448 /* This defines "errno" properly for VMS, and gives us EACCES. */
7455 #ifndef HAVE_STRERROR
7456 extern int sys_nerr;
7458 extern const char *const sys_errlist[];
7461 extern char *sys_errlist[];
7464 #else /* HAVE_STRERROR */
7468 char *strerror (int,...);
7472 * my_strerror - return the descriptive text associated with an `errno' code.
7476 my_strerror (errnum)
7482 #ifndef HAVE_STRERROR
7483 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7485 result = strerror (errnum);
7488 /* VAXCRTL's strerror() takes an optional second argument, which only
7489 matters when the first argument is EVMSERR. However, it's simplest
7490 just to pass it unconditionally. `vaxc$errno' is declared in
7491 <errno.h>, and maintained by the library in parallel with `errno'.
7492 We assume that caller's `errnum' either matches the last setting of
7493 `errno' by the library or else does not have the value `EVMSERR'. */
7495 result = strerror (errnum, vaxc$errno);
7499 result = "undocumented I/O error";
7504 /* Error including a message from `errno'. */
7507 cpp_error_from_errno (
7514 cpp_buffer *ip = cpp_file_buffer (pfile);
7516 cpp_print_containing_files (pfile);
7519 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7521 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7525 cpp_perror_with_name (
7529 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7533 * No pre-compiled header file support.
7535 * Possibly different enum token codes for each C/C++ token.
7537 * Should clean up remaining directives to that do_XXX functions
7538 * only take two arguments and all have command_reads_line.
7540 * Find and cleanup remaining uses of static variables,
7542 * Support for trigraphs.
7544 * Support -dM flag (dump_all_macros).
7546 * Support for_lint flag.