2 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
4 Based on CCCP program by by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
27 #include "..\..\sdcc_vc.h"
38 #include "../src/config.h"
47 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
48 for the sake of machines with limited C compilers. */
53 #endif /* not EMACS */
54 #define GCC_INCLUDE_DIR "/usr/local/lib/gcc-lib/i386-unknown-freebsd_1.0/2.5.8/include"
55 #ifndef STANDARD_INCLUDE_DIR
56 #define STANDARD_INCLUDE_DIR "/usr/include"
59 #ifndef LOCAL_INCLUDE_DIR
60 #define LOCAL_INCLUDE_DIR "/usr/local/include"
63 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
65 #define PTR_INT_TYPE ptrdiff_t
67 #define PTR_INT_TYPE long
78 /* By default, colon separates directories in a path. */
79 #ifndef PATH_SEPARATOR
80 #define PATH_SEPARATOR ':'
91 // PENDING: Straighten this out into configure
100 #if !defined(_MSC_VER)
103 #include <sys/time.h> /* for __DATE__ and __TIME__ */
104 #include <sys/resource.h>
106 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
107 // #include <sys/times.h>
116 /* This defines "errno" properly for VMS, and gives us EACCES. */
119 extern char *index ();
120 extern char *rindex ();
128 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
129 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
131 /* Find the largest host integer type and set its size and type. */
133 #ifndef HOST_BITS_PER_WIDE_INT
135 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
136 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
137 #define HOST_WIDE_INT long
139 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
140 #define HOST_WIDE_INT int
146 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
150 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
153 /* Define a generic NULL if one hasn't already been defined. */
160 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
161 #define GENERIC_PTR void *
163 #define GENERIC_PTR char *
168 #define NULL_PTR ((GENERIC_PTR)0)
171 #ifndef INCLUDE_LEN_FUDGE
172 #define INCLUDE_LEN_FUDGE 0
175 /* Symbols to predefine. */
177 #ifdef CPP_PREDEFINES
178 static char *predefs = CPP_PREDEFINES;
180 static char *predefs = "";
183 /* We let tm.h override the types used here, to handle trivial differences
184 such as the choice of unsigned int or long unsigned int for size_t.
185 When machines start needing nontrivial differences in the size type,
186 it would be best to do something here to figure out automatically
187 from other information what type to use. */
189 /* The string value for __SIZE_TYPE__. */
192 #define SIZE_TYPE "long unsigned int"
195 /* The string value for __PTRDIFF_TYPE__. */
198 #define PTRDIFF_TYPE "long int"
201 /* The string value for __WCHAR_TYPE__. */
204 #define WCHAR_TYPE "int"
206 #define CPP_WCHAR_TYPE(PFILE) \
207 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
209 /* The string value for __USER_LABEL_PREFIX__ */
211 #ifndef USER_LABEL_PREFIX
212 #define USER_LABEL_PREFIX ""
215 /* The string value for __REGISTER_PREFIX__ */
217 #ifndef REGISTER_PREFIX
218 #define REGISTER_PREFIX ""
222 /* In the definition of a #assert name, this structure forms
223 a list of the individual values asserted.
224 Each value is itself a list of "tokens".
225 These are strings that are compared by name. */
227 struct tokenlist_list {
228 struct tokenlist_list *next;
229 struct arglist *tokens;
232 struct assertion_hashnode {
233 struct assertion_hashnode *next; /* double links for easy deletion */
234 struct assertion_hashnode *prev;
235 /* also, a back pointer to this node's hash
236 chain is kept, in case the node is the head
237 of the chain and gets deleted. */
238 struct assertion_hashnode **bucket_hdr;
239 int length; /* length of token, for quick comparison */
240 U_CHAR *name; /* the actual name */
241 /* List of token-sequences. */
242 struct tokenlist_list *value;
245 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
246 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
248 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
249 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
250 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
251 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
252 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
253 (Note that it is false while we're expanding marco *arguments*.) */
254 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
256 /* Move all backslash-newline pairs out of embarrassing places.
257 Exchange all such pairs following BP
258 with any potentially-embarrassing characters that follow them.
259 Potentially-embarrassing characters are / and *
260 (because a backslash-newline inside a comment delimiter
261 would cause it not to be recognized). */
263 #define NEWLINE_FIX \
264 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
266 /* Same, but assume we've already read the potential '\\' into C. */
267 #define NEWLINE_FIX1(C) do { \
268 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
272 struct cpp_pending *next;
277 /* Forward declarations. */
279 //extern char *Safe_malloc ();
281 static void add_import ();
282 static void append_include_chain ();
283 static void make_assertion ();
284 static void path_include ();
285 static void initialize_builtins ();
286 static void initialize_char_syntax ();
287 extern void delete_macro ();
288 static int finclude ();
289 static void validate_else ();
290 static int comp_def_part ();
291 extern void fancy_abort ();
292 static int lookup_import ();
293 static int redundant_include_p ();
294 static int is_system_include (cpp_reader *, char *);
295 static struct file_name_map *read_name_map ();
296 static char *read_filename_string ();
297 static int open_include_file ();
298 static int check_macro_name ();
299 static int compare_defs ();
300 static int compare_token_lists ();
301 static HOST_WIDE_INT eval_if_expression ();
302 static int change_newlines ();
304 static int file_size_and_mode ();
305 static struct arglist *read_token_list ();
306 static void free_token_list ();
307 static int safe_read ();
308 static void push_macro_expansion PARAMS ((cpp_reader *,
309 U_CHAR*, int, HASHNODE*));
310 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
311 //extern char *xrealloc ();
312 //extern char *xcalloc ();
313 static char *savestring ();
315 static void conditional_skip ();
316 static void skip_if_group ();
318 /* Last arg to output_line_command. */
319 enum file_change_code {same_file, enter_file, leave_file};
321 /* External declarations. */
323 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
325 extern char *getenv ();
326 extern FILE *fdopen ();
327 extern char *version_string;
328 extern struct tm *localtime ();
330 /* These functions are declared to return int instead of void since they
331 are going to be placed in a table and some old compilers have trouble with
332 pointers to functions returning void. */
334 static int do_define ();
335 static int do_line ();
336 static int do_include ();
337 static int do_undef ();
338 static int do_error ();
339 static int do_pragma ();
340 static int do_ident ();
342 static int do_xifdef ();
343 static int do_else ();
344 static int do_elif ();
345 static int do_endif ();
346 static int do_sccs ();
347 static int do_once ();
348 static int do_assert ();
349 static int do_unassert ();
350 static int do_warning ();
352 struct file_name_list
354 struct file_name_list *next;
356 /* If the following is nonzero, it is a macro name.
357 Don't include the file again if that macro is defined. */
358 U_CHAR *control_macro;
359 /* If the following is nonzero, it is a C-language system include
361 int c_system_include_path;
362 /* Mapping of file names for this directory. */
363 struct file_name_map *name_map;
364 /* Non-zero if name_map is valid. */
368 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
369 via the same directory as the file that #included it. */
370 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
372 /* #include "file" looks in source file dir, then stack. */
373 /* #include <file> just looks in the stack. */
374 /* -I directories are added to the end, then the defaults are added. */
376 static struct default_include {
377 char *fname; /* The name of the directory. */
378 int cplusplus; /* Only look here if we're compiling C++. */
379 int cxx_aware; /* Includes in this directory don't need to
380 be wrapped in extern "C" when compiling
382 } include_defaults_array[]
383 #ifdef INCLUDE_DEFAULTS
387 /* Pick up GNU C++ specific include files. */
388 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
390 /* This is the dir for fixincludes. Put it just before
391 the files that we fix. */
392 { GCC_INCLUDE_DIR, 0, 0 },
393 /* For cross-compilation, this dir name is generated
394 automatically in Makefile.in. */
395 { CROSS_INCLUDE_DIR, 0, 0 },
396 /* This is another place that the target system's headers might be. */
397 { TOOL_INCLUDE_DIR, 0, 1 },
398 { LOCAL_INCLUDE_DIR, 0, 1 },
399 #else /* not CROSS_COMPILE */
400 /* This should be /usr/local/include and should come before
401 the fixincludes-fixed header files. */
402 { LOCAL_INCLUDE_DIR, 0, 1 },
403 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
404 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
405 { TOOL_INCLUDE_DIR, 0, 1 },
406 /* This is the dir for fixincludes. Put it just before
407 the files that we fix. */
408 { GCC_INCLUDE_DIR, 0, 0 },
409 /* Some systems have an extra dir of include files. */
410 #ifdef SYSTEM_INCLUDE_DIR
411 { SYSTEM_INCLUDE_DIR, 0, 0 },
413 { STANDARD_INCLUDE_DIR, 0, 0 },
414 #endif /* not CROSS_COMPILE */
417 #endif /* no INCLUDE_DEFAULTS */
419 /* `struct directive' defines one #-directive, including how to handle it. */
422 int length; /* Length of name */
423 int (*func)(); /* Function to handle directive */
424 char *name; /* Name of directive */
425 enum node_type type; /* Code which describes which directive. */
426 char command_reads_line; /* One if rest of line is read by func. */
427 char traditional_comments; /* Nonzero: keep comments if -traditional. */
428 char pass_thru; /* Copy preprocessed directive to output file.*/
431 /* Here is the actual list of #-directives, most-often-used first.
432 The initialize_builtins function assumes #define is the very first. */
434 static struct directive directive_table[] = {
435 { 6, do_define, "define", T_DEFINE, 0, 1},
436 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
437 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
438 { 7, do_include, "include", T_INCLUDE, 1},
439 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
440 { 6, do_include, "import", T_IMPORT, 1},
441 { 5, do_endif, "endif", T_ENDIF, 1},
442 { 4, do_else, "else", T_ELSE, 1},
443 { 2, do_if, "if", T_IF, 1},
444 { 4, do_elif, "elif", T_ELIF, 1},
445 { 5, do_undef, "undef", T_UNDEF},
446 { 5, do_error, "error", T_ERROR},
447 { 7, do_warning, "warning", T_WARNING},
448 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
449 { 4, do_line, "line", T_LINE, 1},
450 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
451 #ifdef SCCS_DIRECTIVE
452 { 4, do_sccs, "sccs", T_SCCS},
454 { 6, do_assert, "assert", T_ASSERT, 1},
455 { 8, do_unassert, "unassert", T_UNASSERT, 1},
456 { -1, 0, "", T_UNUSED},
459 /* table to tell if char can be part of a C identifier. */
460 U_CHAR is_idchar[256];
461 /* table to tell if char can be first char of a c identifier. */
462 U_CHAR is_idstart[256];
463 /* table to tell if c is horizontal space. */
464 U_CHAR is_hor_space[256];
465 /* table to tell if c is horizontal or vertical space. */
466 static U_CHAR is_space[256];
468 /* Initialize syntactic classifications of characters. */
471 initialize_char_syntax (
472 struct cpp_options *opts)
477 * Set up is_idchar and is_idstart tables. These should be
478 * faster than saying (is_alpha (c) || c == '_'), etc.
479 * Set up these things before calling any routines tthat
482 for (i = 'a'; i <= 'z'; i++) {
483 is_idchar[i - 'a' + 'A'] = 1;
485 is_idstart[i - 'a' + 'A'] = 1;
488 for (i = '0'; i <= '9'; i++)
492 is_idchar['$'] = opts->dollars_in_ident;
493 is_idstart['$'] = opts->dollars_in_ident;
495 /* horizontal space table */
496 is_hor_space[' '] = 1;
497 is_hor_space['\t'] = 1;
498 is_hor_space['\v'] = 1;
499 is_hor_space['\f'] = 1;
500 is_hor_space['\r'] = 1;
511 /* Place into PFILE a quoted string representing the string SRC.
512 Caller must reserve enough space in pfile->token_buffer. */
520 CPP_PUTC_Q (pfile, '\"');
522 switch ((c = *src++))
526 CPP_PUTC_Q (pfile, c);
529 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
530 CPP_ADJUST_WRITTEN (pfile, 4);
536 CPP_PUTC_Q (pfile, '\\');
537 CPP_PUTC_Q (pfile, c);
541 CPP_PUTC_Q (pfile, '\"');
542 CPP_NUL_TERMINATE_Q (pfile);
547 /* Make sure PFILE->token_buffer will hold at least N more chars. */
554 long old_written = CPP_WRITTEN (pfile);
555 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
556 pfile->token_buffer = (U_CHAR*) Safe_realloc(pfile->token_buffer, pfile->token_buffer_size);
557 CPP_SET_WRITTEN (pfile, old_written);
562 * process a given definition string, for initialization
563 * If STR is just an identifier, define it with value 1.
564 * If STR has anything after the identifier, then it should
565 * be identifier=definition.
579 cpp_error (pfile, "malformed option `-D %s'", str);
582 while (is_idchar[*++p])
586 buf = (U_CHAR *) alloca (p - buf + 4);
587 strcpy ((char *)buf, str);
588 strcat ((char *)buf, " 1");
592 cpp_error (pfile, "malformed option `-D %s'", str);
598 /* Copy the entire option so we can modify it. */
599 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
600 strncpy (buf, str, p - str);
601 /* Change the = to a space. */
603 /* Scan for any backslash-newline and remove it. */
608 if (*p == '\\' && p[1] == '\n')
616 do_define (pfile, NULL, buf, buf + strlen (buf));
619 /* Process the string STR as if it appeared as the body of a #assert.
620 OPTION is the option name for which STR was the argument. */
629 /* struct directive *kt; */
630 U_CHAR *buf, *p /*, *q */ ;
632 /* Copy the entire option so we can modify it. */
633 buf = (U_CHAR *) alloca (strlen (str) + 1);
634 strcpy ((char *) buf, str);
637 /* Scan for any backslash-newline and remove it. */
640 if (*p == '\\' && p[1] == '\n')
649 if (!is_idstart[*p]) {
650 cpp_error (pfile, "malformed option `%s %s'", option, str);
653 while (is_idchar[*++p])
655 while (*p == ' ' || *p == '\t') p++;
656 if (! (*p == 0 || *p == '(')) {
657 cpp_error (pfile, "malformed option `%s %s'", option, str);
661 ip = cpp_push_buffer (pfile, buf, strlen (buf));
662 do_assert (pfile, NULL, NULL, NULL);
663 cpp_pop_buffer (pfile);
666 /* Append a chain of `struct file_name_list's
667 to the end of the main include chain.
668 FIRST is the beginning of the chain to append, and LAST is the end. */
671 append_include_chain (
673 struct file_name_list *first,struct file_name_list *last)
675 struct cpp_options *opts = CPP_OPTIONS (pfile);
676 struct file_name_list *dir;
681 if (opts->include == 0)
682 opts->include = first;
684 opts->last_include->next = first;
686 if (opts->first_bracket_include == 0)
687 opts->first_bracket_include = first;
689 for (dir = first; ; dir = dir->next) {
690 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
691 if (len > pfile->max_include_len)
692 pfile->max_include_len = len;
698 opts->last_include = last;
701 /* Add output to `deps_buffer' for the -M switch.
702 STRING points to the text to be output.
703 SPACER is ':' for targets, ' ' for dependencies, zero for text
704 to be inserted literally. */
712 int size = strlen (string);
717 #ifndef MAX_OUTPUT_COLUMNS
718 #define MAX_OUTPUT_COLUMNS 72
721 && pfile->deps_column > 0
722 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
724 deps_output (pfile, " \\\n ", 0);
725 pfile->deps_column = 0;
728 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
730 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
731 pfile->deps_buffer = (char *) Safe_realloc(pfile->deps_buffer,
732 pfile->deps_allocated_size);
734 if (spacer == ' ' && pfile->deps_column > 0)
735 pfile->deps_buffer[pfile->deps_size++] = ' ';
736 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
737 pfile->deps_size += size;
738 pfile->deps_column += size;
740 pfile->deps_buffer[pfile->deps_size++] = ':';
741 pfile->deps_buffer[pfile->deps_size] = 0;
744 /* Given a colon-separated list of file names PATH,
745 add all the names to the search path for include files. */
760 struct file_name_list *dirtmp;
762 /* Find the end of this name. */
763 while (*q != 0 && *q != PATH_SEPARATOR) q++;
765 /* An empty name in the path stands for the current directory. */
766 name = (char *) Safe_malloc (2);
770 /* Otherwise use the directory that is named. */
771 name = (char *) Safe_malloc (q - p + 1);
772 bcopy (p, name, q - p);
776 dirtmp = (struct file_name_list *)
777 Safe_malloc (sizeof (struct file_name_list));
778 dirtmp->next = 0; /* New one goes on the end */
779 dirtmp->control_macro = 0;
780 dirtmp->c_system_include_path = 0;
781 dirtmp->fname = name;
782 dirtmp->got_name_map = 0;
783 append_include_chain (pfile, dirtmp, dirtmp);
785 /* Advance past this name. */
789 /* Skip the colon. */
796 struct cpp_options *opts)
798 bzero ((char *) opts, sizeof *opts);
799 opts->in_fname = NULL;
800 opts->out_fname = NULL;
802 /* Initialize is_idchar to allow $. */
803 opts->dollars_in_ident = 1;
804 initialize_char_syntax (opts);
805 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
807 opts->no_line_commands = 0;
808 opts->no_trigraphs = 1;
809 opts->put_out_comments = 0;
810 opts->print_include_names = 0;
811 opts->dump_macros = dump_none;
814 opts->cplusplus_comments = 0;
821 opts->pedantic_errors = 0;
822 opts->inhibit_warnings = 0;
823 opts->warn_comments = 0;
824 opts->warn_import = 1;
825 opts->warnings_are_errors = 0;
848 HASHNODE *macro = (HASHNODE*)pbuf->data;
849 if (macro->type == T_DISABLED)
850 macro->type = T_MACRO;
851 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
869 /* Assuming we have read '/'.
870 If this is the start of a comment (followed by '*' or '/'),
871 skip to the end of the comment, and return ' '.
872 Return EOF if we reached the end of file before the end of the comment.
873 If not the start of a comment, return '/'. */
881 while (PEEKC() == '\\' && PEEKN(1) == '\n')
896 while (c == '\\' && PEEKC() == '\n')
900 FORWARD(1), c = GETC();
902 if (prev_c == '*' && c == '/')
904 if (c == '\n' && linep)
908 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
915 return ' '; /* Allow // to be terminated by EOF. */
916 while (c == '\\' && PEEKC() == '\n')
925 /* Don't consider final '\n' to be part of comment. */
935 /* Skip whitespace \-newline and comments. Does not macro-expand. */
947 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
948 cpp_pedwarn (pfile, "%s in preprocessing directive",
949 c == '\f' ? "formfeed" : "vertical tab");
955 c = skip_comment (pfile, NULL);
958 if (c == EOF || c == '/')
961 else if (c == '\\' && PEEKN(1) == '\n') {
964 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
965 && is_hor_space[PEEKN(1)])
971 /* Read the rest of the current line.
972 The line is appended to PFILE's output buffer. */
978 struct cpp_options *opts = CPP_OPTIONS (pfile);
995 goto scan_directive_token;
999 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
1000 goto scan_directive_token;
1004 if (CPP_PEDANTIC (pfile))
1005 cpp_pedwarn (pfile, "%s in preprocessing directive",
1006 c == '\f' ? "formfeed" : "vertical tab");
1012 scan_directive_token:
1014 cpp_get_token (pfile);
1017 CPP_PUTC (pfile, c);
1020 CPP_NUL_TERMINATE (pfile);
1027 long old = CPP_WRITTEN (pfile);
1028 copy_rest_of_line (pfile);
1029 CPP_SET_WRITTEN (pfile, old);
1032 /* Handle a possible # directive.
1033 '#' has already been read. */
1039 register struct directive *kt;
1041 long after_ident = 0;
1042 U_CHAR *ident, *line_end;
1043 long old_written = CPP_WRITTEN (pfile);
1045 cpp_skip_hspace (pfile);
1048 if (c >= '0' && c <= '9')
1050 /* Handle # followed by a line number. */
1051 if (CPP_PEDANTIC (pfile))
1052 cpp_pedwarn (pfile, "`#' followed by integer");
1053 do_line (pfile, NULL);
1054 goto done_a_directive;
1057 /* Now find the directive name. */
1058 CPP_PUTC (pfile, '#');
1059 parse_name (pfile, GETC());
1060 ident = pfile->token_buffer + old_written + 1;
1061 ident_length = CPP_PWRITTEN (pfile) - ident;
1062 if (ident_length == 0 && PEEKC() == '\n')
1064 /* A line of just `#' becomes blank. */
1065 goto done_a_directive;
1069 if (ident_length == 0 || !is_idstart[*ident]) {
1071 while (is_idchar[*p]) {
1072 if (*p < '0' || *p > '9')
1076 /* Avoid error for `###' and similar cases unless -pedantic. */
1078 while (*p == '#' || is_hor_space[*p]) p++;
1080 if (pedantic && !lang_asm)
1081 cpp_warning (pfile, "invalid preprocessor directive");
1087 cpp_error (pfile, "invalid preprocessor directive name");
1093 * Decode the keyword and call the appropriate expansion
1094 * routine, after moving the input pointer up to the next line.
1096 for (kt = directive_table; ; kt++) {
1097 if (kt->length <= 0)
1098 goto not_a_directive;
1099 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1103 if (! kt->command_reads_line)
1105 /* Nonzero means do not delete comments within the directive.
1106 #define needs this when -traditional. */
1107 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1108 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1109 CPP_OPTIONS (pfile)->put_out_comments = comments;
1110 after_ident = CPP_WRITTEN (pfile);
1111 copy_rest_of_line (pfile);
1112 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1115 /* For #pragma and #define, we may want to pass through the directive.
1116 Other directives may create output, but we don't want the directive
1117 itself out, so we pop it now. For example #include may write a
1118 command (see comment in do_include), and conditionals may emit
1119 #failed ... #endfailed stuff. But note that popping the buffer
1120 means the parameters to kt->func may point after pfile->limit
1121 so these parameters are invalid as soon as something gets appended
1122 to the token_buffer. */
1124 line_end = CPP_PWRITTEN (pfile);
1125 if (!kt->pass_thru && kt->type != T_DEFINE)
1126 CPP_SET_WRITTEN (pfile, old_written);
1128 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1130 || (kt->type == T_DEFINE
1131 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1133 /* Just leave the entire #define in the output stack. */
1135 else if (kt->type == T_DEFINE
1136 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1138 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1139 SKIP_WHITE_SPACE (p);
1140 while (is_idchar[*p]) p++;
1142 CPP_PUTC (pfile, '\n');
1144 else if (kt->type == T_DEFINE)
1145 CPP_SET_WRITTEN (pfile, old_written);
1153 /* Pass a directive through to the output file.
1154 BUF points to the contents of the directive, as a contiguous string.
1155 LIMIT points to the first character past the end of the directive.
1156 KEYWORD is the keyword-table entry for the directive. */
1159 pass_thru_directive (
1160 U_CHAR *buf, U_CHAR *limit,
1162 struct directive *keyword)
1164 register unsigned keyword_length = keyword->length;
1166 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1167 CPP_PUTC_Q (pfile, '#');
1168 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1169 if (limit != buf && buf[0] != ' ')
1170 CPP_PUTC_Q (pfile, ' ');
1171 CPP_PUTS_Q (pfile, buf, limit - buf);
1173 CPP_PUTS_Q (pfile, '\n');
1174 /* Count the line we have just made in the output,
1175 to get in sync properly. */
1180 /* The arglist structure is built by do_define to tell
1181 collect_definition where the argument names begin. That
1182 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1183 would contain pointers to the strings x, y, and z.
1184 Collect_definition would then build a DEFINITION node,
1185 with reflist nodes pointing to the places x, y, and z had
1186 appeared. So the arglist is just convenience data passed
1187 between these two routines. It is not kept around after
1188 the current #define has been processed and entered into the
1192 struct arglist *next;
1199 /* Read a replacement list for a macro with parameters.
1200 Build the DEFINITION structure.
1201 Reads characters of text starting at BUF until END.
1202 ARGLIST specifies the formal parameters to look for
1203 in the text of the definition; NARGS is the number of args
1204 in that list, or -1 for a macro name that wants no argument list.
1205 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1206 and NAMELEN is its length in characters.
1208 Note that comments, backslash-newlines, and leading white space
1209 have already been deleted from the argument. */
1214 U_CHAR *buf, U_CHAR *limit,
1216 struct arglist *arglist)
1219 register U_CHAR *p, *lastp, *exp_p;
1220 struct reflist *endpat = NULL;
1221 /* Pointer to first nonspace after last ## seen. */
1223 /* Pointer to first nonspace after last single-# seen. */
1224 U_CHAR *stringify = 0;
1226 int expected_delimiter = '\0';
1228 /* Scan thru the replacement list, ignoring comments and quoted
1229 strings, picking up on the macro calls. It does a linear search
1230 thru the arg list on every potential symbol. Profiling might say
1231 that something smarter should happen. */
1236 /* Find the beginning of the trailing whitespace. */
1238 while (p < limit && is_space[limit[-1]]) limit--;
1240 /* Allocate space for the text in the macro definition.
1241 Leading and trailing whitespace chars need 2 bytes each.
1242 Each other input char may or may not need 1 byte,
1243 so this is an upper bound. The extra 5 are for invented
1244 leading and trailing newline-marker and final null. */
1245 maxsize = (sizeof (DEFINITION)
1247 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1251 defn = (DEFINITION *) Safe_calloc(1,maxsize);
1253 defn->nargs = nargs;
1254 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1259 /* Add one initial space escape-marker to prevent accidental
1260 token-pasting (often removed by macroexpand). */
1264 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1265 cpp_error (pfile, "`##' at start of macro definition");
1269 /* Process the main body of the definition. */
1271 int skipped_arg = 0;
1272 register U_CHAR c = *p++;
1276 if (!CPP_TRADITIONAL (pfile)) {
1280 if (expected_delimiter != '\0') {
1281 if (c == expected_delimiter)
1282 expected_delimiter = '\0';
1284 expected_delimiter = c;
1288 if (p < limit && expected_delimiter) {
1289 /* In a string, backslash goes through
1290 and makes next char ordinary. */
1296 /* An '@' in a string or character constant stands for itself,
1297 and does not need to be escaped. */
1298 if (!expected_delimiter)
1303 /* # is ordinary inside a string. */
1304 if (expected_delimiter)
1306 if (p < limit && *p == '#') {
1307 /* ##: concatenate preceding and following tokens. */
1308 /* Take out the first #, discard preceding whitespace. */
1310 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1312 /* Skip the second #. */
1314 /* Discard following whitespace. */
1315 SKIP_WHITE_SPACE (p);
1318 cpp_error (pfile, "`##' at end of macro definition");
1319 } else if (nargs >= 0) {
1320 /* Single #: stringify following argument ref.
1321 Don't leave the # in the expansion. */
1323 SKIP_WHITE_SPACE (p);
1324 if (p == limit || ! is_idstart[*p])
1326 "`#' operator is not followed by a macro argument name");
1333 /* In -traditional mode, recognize arguments inside strings and
1334 and character constants, and ignore special properties of #.
1335 Arguments inside strings are considered "stringified", but no
1336 extra quote marks are supplied. */
1340 if (expected_delimiter != '\0') {
1341 if (c == expected_delimiter)
1342 expected_delimiter = '\0';
1344 expected_delimiter = c;
1348 /* Backslash quotes delimiters and itself, but not macro args. */
1349 if (expected_delimiter != 0 && p < limit
1350 && (*p == expected_delimiter || *p == '\\')) {
1357 if (expected_delimiter != '\0') /* No comments inside strings. */
1360 /* If we find a comment that wasn't removed by handle_directive,
1361 this must be -traditional. So replace the comment with
1365 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1368 /* Mark this as a concatenation-point, as if it had been ##. */
1372 else if (*p == '/') {
1373 /* A c++ comment. Discard to the end of line */
1381 /* Handle the start of a symbol. */
1382 if (is_idchar[c] && nargs > 0) {
1383 U_CHAR *id_beg = p - 1;
1387 while (p != limit && is_idchar[*p]) p++;
1388 id_len = p - id_beg;
1390 if (is_idstart[c]) {
1391 register struct arglist *arg;
1393 for (arg = arglist; arg != NULL; arg = arg->next) {
1394 struct reflist *tpat;
1396 if (arg->name[0] == c
1397 && arg->length == id_len
1398 && strncmp (arg->name, id_beg, id_len) == 0) {
1399 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1400 if (CPP_TRADITIONAL (pfile)) {
1401 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1405 "macro arg `%.*s' would be stringified with -traditional.",
1409 /* If ANSI, don't actually substitute inside a string. */
1410 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1412 /* make a pat node for this arg and append it to the end of
1414 tpat = (struct reflist *) Safe_malloc (sizeof (struct reflist));
1416 tpat->raw_before = concat == id_beg;
1417 tpat->raw_after = 0;
1418 tpat->rest_args = arg->rest_args;
1419 tpat->stringify = (CPP_TRADITIONAL (pfile)
1420 ? expected_delimiter != '\0'
1421 : stringify == id_beg);
1424 defn->pattern = tpat;
1426 endpat->next = tpat;
1429 tpat->argno = arg->argno;
1430 tpat->nchars = exp_p - lastp;
1432 register U_CHAR *p1 = p;
1433 SKIP_WHITE_SPACE (p1);
1434 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1435 tpat->raw_after = 1;
1437 lastp = exp_p; /* place to start copying from next time */
1444 /* If this was not a macro arg, copy it into the expansion. */
1445 if (! skipped_arg) {
1446 register U_CHAR *lim1 = p;
1450 if (stringify == id_beg)
1452 "`#' operator should be followed by a macro argument name");
1457 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1459 /* If ANSI, put in a "@ " marker to prevent token pasting.
1460 But not if "inside a string" (which in ANSI mode
1461 happens only for -D option). */
1468 defn->length = exp_p - defn->expansion;
1470 /* Crash now if we overrun the allocated size. */
1471 if (defn->length + 1 > maxsize)
1475 /* This isn't worth the time it takes. */
1476 /* give back excess storage */
1477 defn->expansion = (U_CHAR *) Safe_realloc(defn->expansion, defn->length + 1);
1484 * special extension string that can be added to the last macro argument to
1485 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1486 * #define wow(a, b...) process (b, a, b)
1487 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1488 * { wow (one, two); } -> { process (two, one, two); }
1489 * if this "rest_arg" is used with the concat token '##' and if it is not
1490 * supplied then the token attached to with ## will not be outputted. Ex:
1491 * #define wow (a, b...) process (b ## , a, ## b)
1492 * { wow (1, 2); } -> { process (2, 1, 2); }
1493 * { wow (one); } -> { process (one); {
1495 static char rest_extension[] = "...";
1496 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1498 /* Create a DEFINITION node from a #define directive. Arguments are
1499 as for do_define. */
1502 U_CHAR *buf, U_CHAR *limit,
1506 U_CHAR *bp; /* temp ptr into input buffer */
1507 U_CHAR *symname; /* remember where symbol name starts */
1508 int sym_length; /* and how long it is */
1511 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1513 int arglengths = 0; /* Accumulate lengths of arg names
1514 plus number of args. */
1516 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1520 while (is_hor_space[*bp])
1523 symname = bp; /* remember where it starts */
1525 sym_length = check_macro_name (pfile, bp, "macro");
1528 /* Lossage will occur if identifiers or control keywords are broken
1529 across lines using backslash. This is not the right place to take
1533 struct arglist *arg_ptrs = NULL;
1536 bp++; /* skip '(' */
1537 SKIP_WHITE_SPACE (bp);
1539 /* Loop over macro argument names. */
1540 while (*bp != ')') {
1541 struct arglist *temp;
1543 temp = (struct arglist *) alloca (sizeof (struct arglist));
1545 temp->next = arg_ptrs;
1546 temp->argno = argno++;
1547 temp->rest_args = 0;
1551 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1553 if (!is_idstart[*bp])
1554 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1556 /* Find the end of the arg name. */
1557 while (is_idchar[*bp]) {
1559 /* do we have a "special" rest-args extension here? */
1560 if (limit - bp > REST_EXTENSION_LENGTH &&
1561 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1563 temp->rest_args = 1;
1567 temp->length = bp - temp->name;
1569 bp += REST_EXTENSION_LENGTH;
1570 arglengths += temp->length + 2;
1571 SKIP_WHITE_SPACE (bp);
1572 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1573 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1578 SKIP_WHITE_SPACE (bp);
1581 cpp_error (pfile, "unterminated parameter list in `#define'");
1585 struct arglist *otemp;
1587 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1588 if (temp->length == otemp->length &&
1589 strncmp (temp->name, otemp->name, temp->length) == 0) {
1592 name = (U_CHAR *) alloca (temp->length + 1);
1593 (void) strncpy (name, temp->name, temp->length);
1594 name[temp->length] = '\0';
1596 "duplicate argument name `%s' in `#define'", name);
1602 ++bp; /* skip paren */
1603 SKIP_WHITE_SPACE (bp);
1604 /* now everything from bp before limit is the definition. */
1605 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1606 defn->rest_args = rest_args;
1608 /* Now set defn->args.argnames to the result of concatenating
1609 the argument names in reverse order
1610 with comma-space between them. */
1611 defn->args.argnames = (U_CHAR *) Safe_malloc (arglengths + 1);
1613 struct arglist *temp;
1615 for (temp = arg_ptrs; temp; temp = temp->next) {
1616 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1618 if (temp->next != 0) {
1619 defn->args.argnames[i++] = ',';
1620 defn->args.argnames[i++] = ' ';
1623 defn->args.argnames[i] = 0;
1626 /* Simple expansion or empty definition. */
1630 if (is_hor_space[*bp]) {
1632 SKIP_WHITE_SPACE (bp);
1635 case '!': case '"': case '#': case '%': case '&': case '\'':
1636 case ')': case '*': case '+': case ',': case '-': case '.':
1637 case '/': case ':': case ';': case '<': case '=': case '>':
1638 case '?': case '[': case '\\': case ']': case '^': case '{':
1639 case '|': case '}': case '~':
1640 cpp_warning (pfile, "missing white space after `#define %.*s'",
1641 sym_length, symname);
1645 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1646 sym_length, symname);
1651 /* now everything from bp before limit is the definition. */
1652 defn = collect_expansion (pfile, bp, limit, -1,(struct arglist *) NULL_PTR);
1653 defn->args.argnames = (U_CHAR *) "";
1659 /* OP is null if this is a predefinition */
1660 defn->predefined = predefinition;
1662 mdef.symnam = symname;
1663 mdef.symlen = sym_length;
1672 /* Check a purported macro name SYMNAME, and yield its length.
1673 USAGE is the kind of name this is intended for. */
1684 for (p = symname; is_idchar[*p]; p++)
1686 sym_length = p - symname;
1687 if (sym_length == 0)
1688 cpp_error (pfile, "invalid %s name", usage);
1689 else if (!is_idstart[*symname]) {
1690 U_CHAR *msg; /* what pain... */
1691 msg = (U_CHAR *) alloca (sym_length + 1);
1692 bcopy (symname, msg, sym_length);
1693 msg[sym_length] = 0;
1694 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1696 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1697 cpp_error (pfile, "invalid %s name `defined'", usage);
1703 * return zero if two DEFINITIONs are isomorphic
1707 DEFINITION *d1, DEFINITION *d2)
1709 register struct reflist *a1, *a2;
1710 register U_CHAR *p1 = d1->expansion;
1711 register U_CHAR *p2 = d2->expansion;
1714 if (d1->nargs != d2->nargs)
1716 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1718 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1719 a1 = a1->next, a2 = a2->next) {
1720 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1721 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1722 || a1->argno != a2->argno
1723 || a1->stringify != a2->stringify
1724 || a1->raw_before != a2->raw_before
1725 || a1->raw_after != a2->raw_after)
1733 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1734 p2, d2->length - (p2 - d2->expansion), 1))
1739 /* Return 1 if two parts of two macro definitions are effectively different.
1740 One of the parts starts at BEG1 and has LEN1 chars;
1741 the other has LEN2 chars at BEG2.
1742 Any sequence of whitespace matches any other sequence of whitespace.
1743 FIRST means these parts are the first of a macro definition;
1744 so ignore leading whitespace entirely.
1745 LAST means these parts are the last of a macro definition;
1746 so ignore trailing whitespace entirely. */
1751 U_CHAR *beg1, int len1,
1752 U_CHAR *beg2, int len2 ,
1755 register U_CHAR *end1 = beg1 + len1;
1756 register U_CHAR *end2 = beg2 + len2;
1758 while (beg1 != end1 && is_space[*beg1]) beg1++;
1759 while (beg2 != end2 && is_space[*beg2]) beg2++;
1762 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1763 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1765 while (beg1 != end1 && beg2 != end2) {
1766 if (is_space[*beg1] && is_space[*beg2]) {
1767 while (beg1 != end1 && is_space[*beg1]) beg1++;
1768 while (beg2 != end2 && is_space[*beg2]) beg2++;
1769 } else if (*beg1 == *beg2) {
1773 return (beg1 != end1) || (beg2 != end2);
1776 /* Process a #define command.
1777 BUF points to the contents of the #define command, as a contiguous string.
1778 LIMIT points to the first character past the end of the definition.
1779 KEYWORD is the keyword-table entry for #define,
1780 or NULL for a "predefined" macro. */
1785 struct directive *keyword,
1786 U_CHAR *buf, U_CHAR *limit)
1793 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1794 if (pcp_outfile && keyword)
1795 pass_thru_directive (buf, limit, pfile, keyword);
1798 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1802 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1804 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1807 /* Redefining a precompiled key is ok. */
1808 if (hp->type == T_PCSTRING)
1810 /* Redefining a macro is ok if the definitions are the same. */
1811 else if (hp->type == T_MACRO)
1812 ok = ! compare_defs (mdef.defn, hp->value.defn);
1813 /* Redefining a constant is ok with -D. */
1814 else if (hp->type == T_CONST)
1815 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1816 /* Print the warning if it's not ok. */
1819 U_CHAR *msg; /* what pain... */
1821 /* If we are passing through #define and #undef directives, do
1822 that for this re-definition now. */
1823 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1824 pass_thru_directive (buf, limit, pfile, keyword);
1826 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1828 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1829 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1830 cpp_pedwarn (pfile, msg);
1831 if (hp->type == T_MACRO)
1832 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1833 "this is the location of the previous definition");
1835 /* Replace the old definition. */
1837 hp->value.defn = mdef.defn;
1841 /* If we are passing through #define and #undef directives, do
1842 that for this new definition now. */
1843 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1844 pass_thru_directive (buf, limit, pfile, keyword);
1845 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1846 (char *) mdef.defn, hashcode);
1856 /* This structure represents one parsed argument in a macro call.
1857 `raw' points to the argument text as written (`raw_length' is its length).
1858 `expanded' points to the argument's macro-expansion
1859 (its length is `expand_length').
1860 `stringified_length' is the length the argument would have
1862 `use_count' is the number of times this macro arg is substituted
1863 into the macro. If the actual use count exceeds 10,
1864 the value stored is 10. */
1866 /* raw and expanded are relative to ARG_BASE */
1867 #define ARG_BASE ((pfile)->token_buffer)
1870 /* Strings relative to pfile->token_buffer */
1871 long raw, expanded, stringified;
1872 int raw_length, expand_length;
1873 int stringified_length;
1885 #ifdef STATIC_BUFFERS
1886 register cpp_buffer *buf = CPP_BUFFER (pfile);
1887 if (buf == pfile->buffer_stack)
1888 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1890 bzero ((char *) buf, sizeof (cpp_buffer));
1891 CPP_BUFFER (pfile) = buf;
1893 register cpp_buffer *buf = (cpp_buffer*) Safe_malloc (sizeof(cpp_buffer));
1894 bzero ((char *) buf, sizeof (cpp_buffer));
1895 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1896 CPP_BUFFER (pfile) = buf;
1898 buf->if_stack = pfile->if_stack;
1899 buf->cleanup = null_cleanup;
1900 buf->underflow = null_underflow;
1901 buf->buf = buf->cur = buffer;
1902 buf->alimit = buf->rlimit = buffer + length;
1908 cpp_pop_buffer (pfile)
1911 cpp_buffer *buf = CPP_BUFFER (pfile);
1912 #ifdef STATIC_BUFFERS
1913 (*buf->cleanup) (buf, pfile);
1914 return ++CPP_BUFFER (pfile);
1916 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1917 (*buf->cleanup) (buf, pfile);
1918 CPP_BUFFER (pfile) = next_buf;
1924 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1925 Pop the buffer when done. */
1931 cpp_buffer *buffer = CPP_BUFFER (pfile);
1934 enum cpp_token token = cpp_get_token (pfile);
1935 if (token == CPP_EOF) /* Should not happen ... */
1937 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1939 cpp_pop_buffer (pfile);
1946 * Rescan a string (which may have escape marks) into pfile's buffer.
1947 * Place the result in pfile->token_buffer.
1949 * The input is copied before it is scanned, so it is safe to pass
1950 * it something from the token_buffer that will get overwritten
1951 * (because it follows CPP_WRITTEN). This is used by do_include.
1955 cpp_expand_to_buffer (
1960 register cpp_buffer *ip;
1961 U_CHAR *limit = buf + length;
1965 int odepth = indepth;
1971 /* Set up the input on the input stack. */
1973 buf1 = (U_CHAR *) alloca (length + 1);
1975 register U_CHAR *p1 = buf;
1976 register U_CHAR *p2 = buf1;
1983 ip = cpp_push_buffer (pfile, buf1, length);
1984 ip->has_escapes = 1;
1986 ip->lineno = obuf.lineno = 1;
1989 /* Scan the input, create the output. */
1990 cpp_scan_buffer (pfile);
1993 if (indepth != odepth)
1997 CPP_NUL_TERMINATE (pfile);
2012 (*linep)++, (*colp) = 1;
2018 /* Move line_base forward, updating lineno and colno. */
2022 register cpp_buffer *pbuf)
2024 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2025 unsigned char *new_pos = pbuf->cur;
2026 register struct parse_marker *mark;
2027 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2029 if (pbuf->buf + mark->position < new_pos)
2030 new_pos = pbuf->buf + mark->position;
2032 pbuf->line_base += new_pos - old_pos;
2033 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2037 cpp_buf_line_and_col (
2038 register cpp_buffer *pbuf,
2039 long *linep,long *colp)
2046 *linep = pbuf->lineno;
2047 *colp = pbuf->colno;
2048 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2057 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2063 cpp_buffer *ip = CPP_BUFFER (pfile);
2065 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2066 if (ip->fname != NULL)
2073 register U_CHAR *buf,
2074 register U_CHAR *limit)
2076 register long count = 0;
2087 * write out a #line command, for instance, after an #include file.
2088 * If CONDITIONAL is nonzero, we can omit the #line if it would
2089 * appear to be a no-op, and we can output a few newlines instead
2090 * if we want to increase the line number by a small amount.
2091 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2095 output_line_command (
2098 enum file_change_code file_change)
2101 /* char *line_cmd_buf, *line_end;*/
2103 cpp_buffer *ip = CPP_BUFFER (pfile);
2105 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2109 update_position (ip);
2110 line = CPP_BUFFER (pfile)->lineno;
2111 col = CPP_BUFFER (pfile)->colno;
2112 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2114 if (CPP_OPTIONS (pfile)->no_line_commands)
2118 if (line == pfile->lineno)
2121 /* If the inherited line number is a little too small,
2122 output some newlines instead of a #line command. */
2123 if (line > pfile->lineno && line < pfile->lineno + 8) {
2124 CPP_RESERVE (pfile, 20);
2125 while (line > pfile->lineno) {
2126 CPP_PUTC_Q (pfile, '\n');
2134 /* Don't output a line number of 0 if we can help it. */
2135 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2136 && *ip->bufp == '\n') {
2142 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2144 static char sharp_line[] = "#line ";
2146 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2149 sprintf (CPP_PWRITTEN (pfile), "%ld ", line+2);
2150 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2152 // modification for SDC51
2153 if (*ip->nominal_fname == '\0')
2154 quote_string (pfile,"standard input");
2156 quote_string (pfile, ip->nominal_fname);
2157 if (file_change != same_file) {
2158 CPP_PUTC_Q (pfile, ' ');
2159 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2161 /* Tell cc1 if following text comes from a system header file. */
2162 if (ip->system_header_p) {
2163 CPP_PUTC_Q (pfile, ' ');
2164 CPP_PUTC_Q (pfile, '3');
2166 #ifndef NO_IMPLICIT_EXTERN_C
2167 /* Tell cc1plus if following text should be treated as C. */
2168 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2169 CPP_PUTC_Q (pfile, ' ');
2170 CPP_PUTC_Q (pfile, '4');
2173 CPP_PUTC_Q (pfile, '\n');
2174 pfile->lineno = line;
2178 * Parse a macro argument and append the info on PFILE's token_buffer.
2179 * REST_ARGS means to absorb the rest of the args.
2180 * Return nonzero to indicate a syntax error.
2183 static enum cpp_token
2189 enum cpp_token token = CPP_EOF;
2190 /* long arg_start = CPP_WRITTEN (pfile); */
2191 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2192 CPP_OPTIONS (pfile)->put_out_comments = 0;
2194 /* Try to parse as much of the argument as exists at this
2195 input stack level. */
2196 pfile->no_macro_expand++;
2199 token = cpp_get_token (pfile);
2205 /* If we've hit end of file, it's an error (reported by caller).
2206 Ditto if it's the end of cpp_expand_to_buffer text.
2207 If we've hit end of macro, just continue. */
2208 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2219 /* if we've returned to lowest level and
2220 we aren't absorbing all args */
2221 if (paren == 0 && rest_args == 0)
2225 /* Remove ',' or ')' from argument buffer. */
2226 CPP_ADJUST_WRITTEN (pfile, -1);
2233 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2234 pfile->no_macro_expand--;
2239 /* Turn newlines to spaces in the string of length LENGTH at START,
2240 except inside of string constants.
2241 The string is copied into itself with its beginning staying fixed. */
2248 register U_CHAR *ibp;
2249 register U_CHAR *obp;
2250 register U_CHAR *limit;
2254 limit = start + length;
2257 while (ibp < limit) {
2258 *obp++ = c = *ibp++;
2263 /* Notice and skip strings, so that we don't delete newlines in them. */
2266 while (ibp < limit) {
2267 *obp++ = c = *ibp++;
2270 if (c == '\n' && quotec == '\'')
2286 if (!pfile->timebuf) {
2287 time_t t = time ((time_t *)0);
2288 pfile->timebuf = localtime (&t);
2290 return pfile->timebuf;
2293 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2294 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2298 * expand things like __FILE__. Place the expansion into the output
2299 * buffer *without* rescanning.
2310 cpp_buffer *ip = NULL;
2313 int paren = 0; /* For special `defined' keyword */
2316 if (pcp_outfile && pcp_inside_if
2317 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2319 "Predefined macro `%s' used inside `#if' during precompilation",
2323 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2327 cpp_error (pfile, "cccp error: not in any file?!");
2328 return; /* the show must go on */
2330 if (ip->fname != NULL)
2340 if (hp->type == T_BASE_FILE)
2342 while (CPP_PREV_BUFFER (ip))
2343 ip = CPP_PREV_BUFFER (ip);
2345 string = ip->nominal_fname;
2349 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2350 quote_string (pfile, string);
2354 case T_INCLUDE_LEVEL:
2356 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2357 if (ip->fname != NULL)
2360 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2361 sprintf (buf, "%d", true_indepth - 1);
2365 buf = (char *) alloca (3 + strlen (version_string));
2366 sprintf (buf, "\"%s\"", version_string);
2369 #ifndef NO_BUILTIN_SIZE_TYPE
2375 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2376 case T_PTRDIFF_TYPE:
2382 buf = CPP_WCHAR_TYPE (pfile);
2385 case T_USER_LABEL_PREFIX_TYPE:
2386 buf = USER_LABEL_PREFIX;
2389 case T_REGISTER_PREFIX_TYPE:
2390 buf = REGISTER_PREFIX;
2394 buf = (char *) alloca (4 * sizeof (int));
2395 sprintf (buf, "%d", hp->value.ival);
2397 if (pcp_inside_if && pcp_outfile)
2398 /* Output a precondition for this macro use */
2399 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2405 long line = ip->lineno;
2406 long col = ip->colno;
2407 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2409 buf = (char *) alloca (10);
2410 sprintf (buf, "%ld", line);
2416 buf = (char *) alloca (20);
2417 timebuf = timestamp (pfile);
2418 if (hp->type == T_DATE)
2419 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2420 timebuf->tm_mday, timebuf->tm_year + 1900);
2422 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2426 case T_SPEC_DEFINED:
2427 buf = " 0 "; /* Assume symbol is not defined */
2428 ip = CPP_BUFFER (pfile);
2429 SKIP_WHITE_SPACE (ip->cur);
2430 if (*ip->cur == '(')
2433 ip->cur++; /* Skip over the paren */
2434 SKIP_WHITE_SPACE (ip->cur);
2437 if (!is_idstart[*ip->cur])
2439 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)) != 0)
2442 if (pcp_outfile && pcp_inside_if
2443 && (hp->type == T_CONST
2444 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2445 /* Output a precondition for this macro use. */
2446 fprintf (pcp_outfile, "#define %s\n", hp->name);
2452 if (pcp_outfile && pcp_inside_if)
2454 /* Output a precondition for this macro use */
2455 U_CHAR *cp = ip->bufp;
2456 fprintf (pcp_outfile, "#undef ");
2457 while (is_idchar[*cp]) /* Ick! */
2458 fputc (*cp++, pcp_outfile);
2459 putc ('\n', pcp_outfile);
2462 while (is_idchar[*ip->cur])
2464 SKIP_WHITE_SPACE (ip->cur);
2467 if (*ip->cur != ')')
2475 cpp_error (pfile, "`defined' without an identifier");
2479 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2483 CPP_RESERVE (pfile, len + 1);
2484 CPP_PUTS_Q (pfile, buf, len);
2485 CPP_NUL_TERMINATE_Q (pfile);
2490 /* Initialize the built-in macros. */
2493 initialize_builtins (
2496 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2497 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2498 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2499 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2500 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2501 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2502 #ifndef NO_BUILTIN_SIZE_TYPE
2503 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2505 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2506 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2508 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2509 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2510 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2511 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2512 if (!CPP_TRADITIONAL (pfile))
2513 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2514 if (CPP_OPTIONS (pfile)->objc)
2515 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2516 /* This is supplied using a -D by the compiler driver
2517 so that it is present only when truly compiling with GNU C. */
2518 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2520 if (CPP_OPTIONS (pfile)->debug_output)
2522 char directive[2048];
2523 register struct directive *dp = &directive_table[0];
2524 struct tm *timebuf = timestamp (pfile);
2525 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2527 while (CPP_PREV_BUFFER (pbuffer))
2528 pbuffer = CPP_PREV_BUFFER (pbuffer);
2529 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2530 pbuffer->nominal_fname);
2531 output_line_command (pfile, 0, same_file);
2532 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2534 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2535 output_line_command (pfile, 0, same_file);
2536 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2538 #ifndef NO_BUILTIN_SIZE_TYPE
2539 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2540 output_line_command (pfile, 0, same_file);
2541 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2544 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2545 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2546 output_line_command (pfile, 0, same_file);
2547 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2550 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2551 output_line_command (pfile, 0, same_file);
2552 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2554 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2555 monthnames[timebuf->tm_mon],
2556 timebuf->tm_mday, timebuf->tm_year + 1900);
2557 output_line_command (pfile, 0, same_file);
2558 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2560 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2561 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2562 output_line_command (pfile, 0, same_file);
2563 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2565 if (!CPP_TRADITIONAL (pfile))
2567 sprintf (directive, " __STDC__ 1");
2568 output_line_command (pfile, 0, same_file);
2569 pass_thru_directive (directive, &directive[strlen (directive)],
2572 if (CPP_OPTIONS (pfile)->objc)
2574 sprintf (directive, " __OBJC__ 1");
2575 output_line_command (pfile, 0, same_file);
2576 pass_thru_directive (directive, &directive[strlen (directive)],
2582 /* Return 1 iff a token ending in C1 followed directly by a token C2
2583 could cause mis-tokenization. */
2592 if (c2 == c1 || c2 == '=')
2596 case '0': case '1': case '2': case '3': case '4':
2597 case '5': case '6': case '7': case '8': case '9':
2599 if (c2 == '-' || c2 == '+')
2600 return 1; /* could extend a pre-processing number */
2603 if (c2 == '\'' || c2 == '\"')
2604 return 1; /* Could turn into L"xxx" or L'xxx'. */
2608 case 'a': case 'b': case 'c': case 'd': case 'f':
2609 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2610 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2611 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2613 case 'A': case 'B': case 'C': case 'D': case 'F':
2614 case 'G': case 'H': case 'I': case 'J': case 'K':
2615 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2616 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2618 /* We're in the middle of either a name or a pre-processing number. */
2619 return (is_idchar[c2] || c2 == '.');
2620 case '<': case '>': case '!': case '%': case '#': case ':':
2621 case '^': case '&': case '|': case '*': case '/': case '=':
2622 return (c2 == c1 || c2 == '=');
2627 /* Expand a macro call.
2628 HP points to the symbol that is the macro being called.
2629 Put the result of expansion onto the input stack
2630 so that subsequent input by our caller will use it.
2632 If macro wants arguments, caller has already verified that
2633 an argument list follows; arguments come from the input stack. */
2641 DEFINITION *defn = hp->value.defn;
2642 register U_CHAR *xbuf;
2643 long start_line, start_column;
2645 struct argdata *args;
2646 long old_written = CPP_WRITTEN (pfile);
2648 int start_line = instack[indepth].lineno;
2650 int rest_args, rest_zero;
2654 CHECK_DEPTH (return;);
2658 /* This macro is being used inside a #if, which means it must be */
2659 /* recorded as a precondition. */
2660 if (pcp_inside_if && pcp_outfile && defn->predefined)
2661 dump_single_macro (hp, pcp_outfile);
2664 pfile->output_escapes++;
2665 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2667 nargs = defn->nargs;
2671 enum cpp_token token = CPP_EOF;
2673 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2675 for (i = 0; i < nargs; i++)
2677 args[i].raw = args[i].expanded = 0;
2678 args[i].raw_length = 0;
2679 args[i].expand_length = args[i].stringified_length = -1;
2680 args[i].use_count = 0;
2683 /* Parse all the macro args that are supplied. I counts them.
2684 The first NARGS args are stored in ARGS.
2685 The rest are discarded. If rest_args is set then we assume
2686 macarg absorbed the rest of the args. */
2690 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2695 if (i < nargs || (nargs == 0 && i == 0))
2697 /* if we are working on last arg which absorbs rest of args... */
2698 if (i == nargs - 1 && defn->rest_args)
2700 args[i].raw = CPP_WRITTEN (pfile);
2701 token = macarg (pfile, rest_args);
2702 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2703 args[i].newlines = 0; /* FIXME */
2706 token = macarg (pfile, 0);
2707 if (token == CPP_EOF || token == CPP_POP)
2709 cpp_error_with_line (pfile, start_line, start_column,
2710 "unterminated macro call");
2714 } while (token == CPP_COMMA);
2716 /* If we got one arg but it was just whitespace, call that 0 args. */
2719 register U_CHAR *bp = ARG_BASE + args[0].raw;
2720 register U_CHAR *lim = bp + args[0].raw_length;
2721 /* cpp.texi says for foo ( ) we provide one argument.
2722 However, if foo wants just 0 arguments, treat this as 0. */
2724 while (bp != lim && is_space[*bp]) bp++;
2729 /* Don't output an error message if we have already output one for
2730 a parse error above. */
2732 if (nargs == 0 && i > 0)
2734 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2738 /* traditional C allows foo() if foo wants one argument. */
2739 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2741 /* the rest args token is allowed to absorb 0 tokens */
2742 else if (i == nargs - 1 && defn->rest_args)
2745 cpp_error (pfile, "macro `%s' used without args", hp->name);
2747 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2749 cpp_error (pfile, "macro `%s' used with only %d args",
2755 "macro `%s' used with too many (%d) args", hp->name, i);
2759 /* If macro wants zero args, we parsed the arglist for checking only.
2760 Read directly from the macro definition. */
2763 xbuf = defn->expansion;
2764 xbuf_len = defn->length;
2768 register U_CHAR *exp = defn->expansion;
2769 register int offset; /* offset in expansion,
2770 copied a piece at a time */
2771 register int totlen; /* total amount of exp buffer filled so far */
2773 register struct reflist *ap, *last_ap;
2775 /* Macro really takes args. Compute the expansion of this call. */
2777 /* Compute length in characters of the macro's expansion.
2778 Also count number of times each arg is used. */
2779 xbuf_len = defn->length;
2780 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2784 register struct argdata *arg = &args[ap->argno];
2785 /* Stringify it it hasn't already been */
2786 if (arg->stringified_length < 0)
2788 int arglen = arg->raw_length;
2792 /* Initially need_space is -1. Otherwise, 1 means the
2793 previous character was a space, but we suppressed it;
2794 0 means the previous character was a non-space. */
2795 int need_space = -1;
2797 arg->stringified = CPP_WRITTEN (pfile);
2798 if (!CPP_TRADITIONAL (pfile))
2799 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2800 for (; i < arglen; i++)
2802 c = (ARG_BASE + arg->raw)[i];
2806 /* Internal sequences of whitespace are replaced by
2807 one space except within an string or char token.*/
2810 if (CPP_WRITTEN (pfile) > arg->stringified
2811 && (CPP_PWRITTEN (pfile))[-1] == '@')
2813 /* "@ " escape markers are removed */
2814 CPP_ADJUST_WRITTEN (pfile, -1);
2817 if (need_space == 0)
2821 else if (need_space > 0)
2822 CPP_PUTC (pfile, ' ');
2837 else if (c == '\"' || c == '\'')
2841 /* Escape these chars */
2842 if (c == '\"' || (in_string && c == '\\'))
2843 CPP_PUTC (pfile, '\\');
2845 CPP_PUTC (pfile, c);
2848 CPP_RESERVE (pfile, 4);
2849 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2851 CPP_ADJUST_WRITTEN (pfile, 4);
2854 if (!CPP_TRADITIONAL (pfile))
2855 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2856 arg->stringified_length
2857 = CPP_WRITTEN (pfile) - arg->stringified;
2859 xbuf_len += args[ap->argno].stringified_length;
2861 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2862 /* Add 4 for two newline-space markers to prevent
2863 token concatenation. */
2864 xbuf_len += args[ap->argno].raw_length + 4;
2867 /* We have an ordinary (expanded) occurrence of the arg.
2868 So compute its expansion, if we have not already. */
2869 if (args[ap->argno].expand_length < 0)
2871 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2872 cpp_expand_to_buffer (pfile,
2873 ARG_BASE + args[ap->argno].raw,
2874 args[ap->argno].raw_length);
2876 args[ap->argno].expand_length
2877 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2880 /* Add 4 for two newline-space markers to prevent
2881 token concatenation. */
2882 xbuf_len += args[ap->argno].expand_length + 4;
2884 if (args[ap->argno].use_count < 10)
2885 args[ap->argno].use_count++;
2888 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
2890 /* Generate in XBUF the complete expansion
2891 with arguments substituted in.
2892 TOTLEN is the total size generated so far.
2893 OFFSET is the index in the definition
2894 of where we are copying from. */
2895 offset = totlen = 0;
2896 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2897 last_ap = ap, ap = ap->next)
2899 register struct argdata *arg = &args[ap->argno];
2900 int count_before = totlen;
2902 /* Add chars to XBUF. */
2903 for (i = 0; i < ap->nchars; i++, offset++)
2904 xbuf[totlen++] = exp[offset];
2906 /* If followed by an empty rest arg with concatenation,
2907 delete the last run of nonwhite chars. */
2908 if (rest_zero && totlen > count_before
2909 && ((ap->rest_args && ap->raw_before)
2910 || (last_ap != NULL && last_ap->rest_args
2911 && last_ap->raw_after)))
2913 /* Delete final whitespace. */
2914 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2917 /* Delete the nonwhites before them. */
2918 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2922 if (ap->stringify != 0)
2924 bcopy (ARG_BASE + arg->stringified,
2925 xbuf + totlen, arg->stringified_length);
2926 totlen += arg->stringified_length;
2928 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2930 U_CHAR *p1 = ARG_BASE + arg->raw;
2931 U_CHAR *l1 = p1 + arg->raw_length;
2934 while (p1 != l1 && is_space[*p1]) p1++;
2935 while (p1 != l1 && is_idchar[*p1])
2936 xbuf[totlen++] = *p1++;
2940 /* Arg is concatenated after: delete trailing whitespace,
2941 whitespace markers, and no-reexpansion markers. */
2944 if (is_space[l1[-1]]) l1--;
2945 else if (l1[-1] == '-')
2947 U_CHAR *p2 = l1 - 1;
2948 /* If a `-' is preceded by an odd number of newlines then it
2949 and the last newline are a no-reexpansion marker. */
2950 while (p2 != p1 && p2[-1] == '\n') p2--;
2951 if ((l1 - 1 - p2) & 1) {
2960 bcopy (p1, xbuf + totlen, l1 - p1);
2965 U_CHAR *expanded = ARG_BASE + arg->expanded;
2966 if (!ap->raw_before && totlen > 0 && arg->expand_length
2967 && !CPP_TRADITIONAL(pfile)
2968 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2970 xbuf[totlen++] = '@';
2971 xbuf[totlen++] = ' ';
2974 bcopy (expanded, xbuf + totlen, arg->expand_length);
2975 totlen += arg->expand_length;
2977 if (!ap->raw_after && totlen > 0 && offset < defn->length
2978 && !CPP_TRADITIONAL(pfile)
2979 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2981 xbuf[totlen++] = '@';
2982 xbuf[totlen++] = ' ';
2985 /* If a macro argument with newlines is used multiple times,
2986 then only expand the newlines once. This avoids creating
2987 output lines which don't correspond to any input line,
2988 which confuses gdb and gcov. */
2989 if (arg->use_count > 1 && arg->newlines > 0)
2991 /* Don't bother doing change_newlines for subsequent
2995 = change_newlines (expanded, arg->expand_length);
2999 if (totlen > xbuf_len)
3003 /* if there is anything left of the definition
3004 after handling the arg list, copy that in too. */
3006 for (i = offset; i < defn->length; i++)
3008 /* if we've reached the end of the macro */
3011 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3012 && last_ap->raw_after))
3013 xbuf[totlen++] = exp[i];
3021 pfile->output_escapes--;
3023 /* Now put the expansion on the input stack
3024 so our caller will commence reading from it. */
3025 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3026 CPP_BUFFER (pfile)->has_escapes = 1;
3028 /* Pop the space we've used in the token_buffer for argument expansion. */
3029 CPP_SET_WRITTEN (pfile, old_written);
3031 /* Recursive macro use sometimes works traditionally.
3032 #define foo(x,y) bar (x (y,0), y)
3035 if (!CPP_TRADITIONAL (pfile))
3036 hp->type = T_DISABLED;
3040 push_macro_expansion (
3042 register U_CHAR *xbuf,
3046 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3047 mbuf->cleanup = macro_cleanup;
3050 /* The first chars of the expansion should be a "@ " added by
3051 collect_expansion. This is to prevent accidental token-pasting
3052 between the text preceding the macro invocation, and the macro
3055 We would like to avoid adding unneeded spaces (for the sake of
3056 tools that use cpp, such as imake). In some common cases we can
3057 tell that it is safe to omit the space.
3059 The character before the macro invocation cannot have been an
3060 idchar (or else it would have been pasted with the idchars of
3061 the macro name). Therefore, if the first non-space character
3062 of the expansion is an idchar, we do not need the extra space
3063 to prevent token pasting.
3065 Also, we don't need the extra space if the first char is '(',
3066 or some other (less common) characters. */
3068 if (xbuf[0] == '@' && xbuf[1] == ' '
3069 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3070 || xbuf[2] == '\"'))
3074 /* Like cpp_get_token, except that it does not read past end-of-line.
3075 Also, horizontal space is skipped, and macros are popped. */
3077 static enum cpp_token
3078 get_directive_token (
3083 long old_written = CPP_WRITTEN (pfile);
3084 enum cpp_token token;
3085 cpp_skip_hspace (pfile);
3086 if (PEEKC () == '\n')
3088 token = cpp_get_token (pfile);
3092 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3094 /* ... else fall though ... */
3095 case CPP_HSPACE: case CPP_COMMENT:
3096 CPP_SET_WRITTEN (pfile, old_written);
3104 /* Handle #include and #import.
3105 This function expects to see "fname" or <fname> on the input.
3107 The input is normally in part of the output_buffer following
3108 CPP_WRITTEN, and will get overwritten by output_line_command.
3109 I.e. in input file specification has been popped by handle_directive.
3115 struct directive *keyword,
3116 U_CHAR *unused1, U_CHAR *unused2)
3118 int importing = (keyword->type == T_IMPORT);
3119 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3120 char *fname; /* Dynamically allocated fname buffer */
3123 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3124 enum cpp_token token;
3126 /* Chain of dirs to search */
3127 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3128 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3129 struct file_name_list *searchptr = 0;
3130 long old_written = CPP_WRITTEN (pfile);
3134 int f; /* file number */
3136 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3139 /* char *pcfbuflimit;*/
3141 f= -1; /* JF we iz paranoid! */
3143 if (importing && CPP_OPTIONS (pfile)->warn_import
3144 && !CPP_OPTIONS (pfile)->inhibit_warnings
3145 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3147 pfile->import_warning = 1;
3148 cpp_warning (pfile, "using `#import' is not recommended");
3149 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3150 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3151 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3152 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3153 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3154 fprintf (stderr, " ... <real contents of file> ...\n");
3155 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3156 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3157 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3158 fprintf (stderr, "when it is equipped with such a conditional.\n");
3161 pfile->parsing_include_directive++;
3162 token = get_directive_token (pfile);
3163 pfile->parsing_include_directive--;
3165 if (token == CPP_STRING)
3167 /* FIXME - check no trailing garbage */
3168 fbeg = pfile->token_buffer + old_written + 1;
3169 fend = CPP_PWRITTEN (pfile) - 1;
3170 if (fbeg[-1] == '<')
3173 /* If -I-, start with the first -I dir after the -I-. */
3174 if (CPP_OPTIONS (pfile)->first_bracket_include)
3175 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3177 /* If -I- was specified, don't search current dir, only spec'd ones. */
3178 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3181 /* We have "filename". Figure out directory this source
3182 file is coming from and put it on the front of the list. */
3184 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3189 if ((nam = fp->nominal_fname) != NULL)
3191 /* Found a named file. Figure out dir of the file,
3192 and put it in front of the search list. */
3193 dsp[0].next = search_start;
3196 ep = rindex (nam, '/');
3198 ep = rindex (nam, ']');
3199 if (ep == NULL) ep = rindex (nam, '>');
3200 if (ep == NULL) ep = rindex (nam, ':');
3201 if (ep != NULL) ep++;
3206 dsp[0].fname = (char *) alloca (n + 1);
3207 strncpy (dsp[0].fname, nam, n);
3208 dsp[0].fname[n] = '\0';
3209 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3210 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3214 dsp[0].fname = 0; /* Current directory */
3216 dsp[0].got_name_map = 0;
3223 else if (token == CPP_NAME)
3226 * Support '#include xyz' like VAX-C to allow for easy use of all the
3227 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3228 * code from case '<' is repeated here) and generates a warning.
3231 "VAX-C-style include specification found, use '#include <filename.h>' !");
3233 /* If -I-, start with the first -I dir after the -I-. */
3234 if (CPP_OPTIONS (pfile)->first_bracket_include)
3235 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3236 fbeg = pfile->token_buffer + old_written;
3237 fend = CPP_PWRITTEN (pfile);
3243 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3244 CPP_SET_WRITTEN (pfile, old_written);
3245 skip_rest_of_line (pfile);
3251 token = get_directive_token (pfile);
3252 if (token != CPP_VSPACE)
3254 cpp_error (pfile, "junk at end of `#include'");
3255 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3256 token = get_directive_token (pfile);
3259 /* For #include_next, skip in the search path
3260 past the dir in which the containing file was found. */
3264 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3265 if (fp->fname != NULL)
3267 /* fp->dir is null if the containing file was specified with
3268 an absolute file name. In that case, don't skip anything. */
3269 if (fp->dir == SELF_DIR_DUMMY)
3270 search_start = CPP_OPTIONS (pfile)->include;
3272 search_start = fp->dir->next;
3277 CPP_SET_WRITTEN (pfile, old_written);
3283 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3287 /* Allocate this permanently, because it gets stored in the definitions
3289 fname = (char *) Safe_malloc (pfile->max_include_len + flen + 4);
3290 /* + 2 above for slash and terminating null. */
3291 /* + 2 added for '.h' on VMS (to support '#include filename') */
3293 /* If specified file name is absolute, just open it. */
3296 strncpy (fname, fbeg, flen);
3298 if (redundant_include_p (pfile, fname))
3301 f = lookup_import (pfile, fname, NULL_PTR);
3303 f = open_include_file (pfile, fname, NULL_PTR);
3305 return 0; /* Already included this file */
3307 /* Search directory path, trying to open the file.
3308 Copy each filename tried into FNAME. */
3310 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3311 if (searchptr->fname) {
3312 /* The empty string in a search path is ignored.
3313 This makes it possible to turn off entirely
3314 a standard piece of the list. */
3315 if (searchptr->fname[0] == 0)
3317 strcpy (fname, searchptr->fname);
3318 strcat (fname, "/");
3319 fname[strlen (fname) + flen] = 0;
3323 strncat (fname, fbeg, flen);
3325 /* Change this 1/2 Unix 1/2 VMS file specification into a
3326 full VMS file specification */
3327 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3328 /* Fix up the filename */
3329 hack_vms_include_specification (fname);
3331 /* This is a normal VMS filespec, so use it unchanged. */
3332 strncpy (fname, fbeg, flen);
3334 /* if it's '#include filename', add the missing .h */
3335 if (index(fname,'.')==NULL) {
3336 strcat (fname, ".h");
3340 /* ??? There are currently 3 separate mechanisms for avoiding processing
3341 of redundant include files: #import, #pragma once, and
3342 redundant_include_p. It would be nice if they were unified. */
3343 if (redundant_include_p (pfile, fname))
3346 f = lookup_import (pfile, fname, searchptr);
3348 f = open_include_file (pfile, fname, searchptr);
3350 return 0; /* Already included this file */
3352 else if (f == -1 && errno == EACCES)
3353 cpp_warning (pfile, "Header file %s exists, but is not readable",
3363 /* A file that was not found. */
3364 strncpy (fname, fbeg, flen);
3366 /* If generating dependencies and -MG was specified, we assume missing
3367 files are leaf files, living in the same directory as the source file
3368 or other similar place; these missing files may be generated from
3369 other files and may not exist yet (eg: y.tab.h). */
3371 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3372 && CPP_PRINT_DEPS (pfile)
3373 > (angle_brackets || (pfile->system_include_depth > 0)))
3375 /* If it was requested as a system header file,
3376 then assume it belongs in the first place to look for such. */
3379 for (searchptr = search_start; searchptr;
3380 searchptr = searchptr->next)
3382 if (searchptr->fname)
3386 if (searchptr->fname[0] == 0)
3388 p = (char *) alloca (strlen (searchptr->fname)
3389 + strlen (fname) + 2);
3390 strcpy (p, searchptr->fname);
3393 deps_output (pfile, p, ' ');
3400 /* Otherwise, omit the directory, as if the file existed
3401 in the directory with the source. */
3402 deps_output (pfile, fname, ' ');
3405 /* If -M was specified, and this header file won't be added to the
3406 dependency list, then don't count this as an error, because we can
3407 still produce correct output. Otherwise, we can't produce correct
3408 output, because there may be dependencies we need inside the missing
3409 file, and we don't know what directory this missing file exists in.*/
3410 else if (CPP_PRINT_DEPS (pfile)
3411 && (CPP_PRINT_DEPS (pfile)
3412 <= (angle_brackets || (pfile->system_include_depth > 0))))
3413 cpp_warning (pfile, "No include path in which to find %s", fname);
3414 else if (search_start)
3415 cpp_error_from_errno (pfile, fname);
3417 cpp_error (pfile, "No include path in which to find %s", fname);
3420 /* Check to see if this include file is a once-only include file.
3423 struct file_name_list* ptr;
3425 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3426 if (!strcmp (ptr->fname, fname)) {
3428 return 0; /* This file was once'd. */
3432 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3433 if (!strcmp (ptr->fname, fname))
3434 break; /* This file was included before. */
3438 /* This is the first time for this file. */
3439 /* Add it to list of files included. */
3441 ptr = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3442 ptr->control_macro = 0;
3443 ptr->c_system_include_path = 0;
3444 ptr->next = pfile->all_include_files;
3445 pfile->all_include_files = ptr;
3446 ptr->fname = savestring (fname);
3447 ptr->got_name_map = 0;
3449 /* For -M, add this file to the dependencies. */
3450 if (CPP_PRINT_DEPS (pfile)
3451 > (angle_brackets || (pfile->system_include_depth > 0)))
3452 deps_output (pfile, fname, ' ');
3455 /* Handle -H option. */
3456 if (CPP_OPTIONS(pfile)->print_include_names)
3458 cpp_buffer *buf = CPP_BUFFER (pfile);
3459 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3461 fprintf (stderr, "%s\n", fname);
3465 pfile->system_include_depth++;
3467 /* Actually process the file. */
3469 /* Record file on "seen" list for #import. */
3470 add_import (pfile, f, fname);
3472 pcftry = (char *) alloca (strlen (fname) + 30);
3484 sprintf (pcftry, "%s%d", fname, pcfnum++);
3486 pcf = open (pcftry, O_RDONLY, 0666);
3492 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3494 || stat_f.st_dev != s.st_dev)
3496 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3497 /* Don't need it any more. */
3502 /* Don't need it at all. */
3507 } while (pcf != -1 && !pcfbuf);
3511 /* Actually process the file */
3512 cpp_push_buffer (pfile, NULL, 0);
3513 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3514 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3516 output_line_command (pfile, 0, enter_file);
3517 pfile->only_seen_white = 2;
3521 pfile->system_include_depth--;
3526 /* Return nonzero if there is no need to include file NAME
3527 because it has already been included and it contains a conditional
3528 to make a repeated include do nothing. */
3531 redundant_include_p (
3535 struct file_name_list *l = pfile->all_include_files;
3536 for (; l; l = l->next)
3537 if (! strcmp (name, l->fname)
3539 && cpp_lookup (pfile, l->control_macro, -1, -1))
3544 /* Return nonzero if the given FILENAME is an absolute pathname which
3545 designates a file within one of the known "system" include file
3546 directories. We assume here that if the given FILENAME looks like
3547 it is the name of a file which resides either directly in a "system"
3548 include file directory, or within any subdirectory thereof, then the
3549 given file must be a "system" include file. This function tells us
3550 if we should suppress pedantic errors/warnings for the given FILENAME.
3552 The value is 2 if the file is a C-language system header file
3553 for which C++ should (on most systems) assume `extern "C"'. */
3558 register char *filename)
3560 struct file_name_list *searchptr;
3562 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3563 searchptr = searchptr->next)
3564 if (searchptr->fname) {
3565 register char *sys_dir = searchptr->fname;
3566 register unsigned length = strlen (sys_dir);
3568 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3570 if (searchptr->c_system_include_path)
3581 * Install a name in the assertion hash table.
3583 * If LEN is >= 0, it is the length of the name.
3584 * Otherwise, compute the length by scanning the entire name.
3586 * If HASH is >= 0, it is the precomputed hash code.
3587 * Otherwise, compute the hash code.
3589 static ASSERTION_HASHNODE *
3596 register ASSERTION_HASHNODE *hp;
3597 register int i, bucket;
3598 register U_CHAR *p, *q;
3600 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3601 hp = (ASSERTION_HASHNODE *) Safe_malloc (i);
3603 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3604 hp->next = pfile->assertion_hashtab[bucket];
3605 pfile->assertion_hashtab[bucket] = hp;
3607 if (hp->next != NULL)
3608 hp->next->prev = hp;
3611 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3614 for (i = 0; i < len; i++)
3620 * find the most recent hash node for name name (ending with first
3621 * non-identifier char) installed by install
3623 * If LEN is >= 0, it is the length of the name.
3624 * Otherwise, compute the length by scanning the entire name.
3626 * If HASH is >= 0, it is the precomputed hash code.
3627 * Otherwise, compute the hash code.
3630 static ASSERTION_HASHNODE *
3637 register ASSERTION_HASHNODE *bucket;
3639 bucket = pfile->assertion_hashtab[hash];
3641 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3643 bucket = bucket->next;
3650 ASSERTION_HASHNODE *hp)
3652 struct tokenlist_list *tail;
3653 if (hp->prev != NULL)
3654 hp->prev->next = hp->next;
3655 if (hp->next != NULL)
3656 hp->next->prev = hp->prev;
3658 for (tail = hp->value; tail; )
3660 struct tokenlist_list *next = tail->next;
3661 free_token_list (tail->tokens);
3666 /* make sure that the bucket chain header that
3667 the deleted guy was on points to the right thing afterwards. */
3668 if (hp == *hp->bucket_hdr)
3669 *hp->bucket_hdr = hp->next;
3674 /* Convert a character string literal into a nul-terminated string.
3675 The input string is [IN ... LIMIT).
3676 The result is placed in RESULT. RESULT can be the same as IN.
3677 The value returned in the end of the string written to RESULT,
3678 or NULL on error. */
3683 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3703 char *bpc = (char *) in;
3704 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3705 in = (U_CHAR *) bpc;
3707 *result++ = (U_CHAR)c;
3710 /* else fall through */
3720 * interpret #line command. Remembers previously seen fnames
3721 * in its very own hash table.
3723 #define FNAME_HASHSIZE 37
3728 struct directive *keyword)
3730 cpp_buffer *ip = CPP_BUFFER (pfile);
3732 long old_written = CPP_WRITTEN (pfile);
3733 enum file_change_code file_change = same_file;
3734 enum cpp_token token;
3737 token = get_directive_token (pfile);
3739 if (token != CPP_NUMBER
3740 || !isdigit(pfile->token_buffer[old_written]))
3742 cpp_error (pfile, "invalid format `#line' command");
3743 goto bad_line_directive;
3746 /* The Newline at the end of this line remains to be processed.
3747 To put the next line at the specified line number,
3748 we must store a line number now that is one less. */
3749 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3750 CPP_SET_WRITTEN (pfile, old_written);
3752 /* NEW_LINENO is one less than the actual line number here. */
3753 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3754 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3756 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3757 if (PEEKC() && !is_space[PEEKC()]) {
3758 cpp_error (pfile, "invalid format `#line' command");
3759 goto bad_line_directive;
3763 token = get_directive_token (pfile);
3765 if (token == CPP_STRING) {
3766 U_CHAR *fname = pfile->token_buffer + old_written;
3768 static HASHNODE *fname_table[FNAME_HASHSIZE];
3769 HASHNODE *hp, **hash_bucket;
3774 /* Turn the file name, which is a character string literal,
3775 into a null-terminated string. Do this in place. */
3776 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3777 if (end_name == NULL)
3779 cpp_error (pfile, "invalid format `#line' command");
3780 goto bad_line_directive;
3783 fname_length = end_name - fname;
3785 num_start = CPP_WRITTEN (pfile);
3786 token = get_directive_token (pfile);
3787 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3788 p = pfile->token_buffer + num_start;
3789 if (CPP_PEDANTIC (pfile))
3790 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3792 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3794 cpp_error (pfile, "invalid format `#line' command");
3795 goto bad_line_directive;
3798 file_change = enter_file;
3800 file_change = leave_file;
3802 ip->system_header_p = 1;
3803 else /* if (*p == 4) */
3804 ip->system_header_p = 2;
3806 CPP_SET_WRITTEN (pfile, num_start);
3807 token = get_directive_token (pfile);
3808 p = pfile->token_buffer + num_start;
3809 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3810 ip->system_header_p = *p == 3 ? 1 : 2;
3811 token = get_directive_token (pfile);
3813 if (token != CPP_VSPACE) {
3814 cpp_error (pfile, "invalid format `#line' command");
3815 goto bad_line_directive;
3820 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3821 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3822 if (hp->length == fname_length &&
3823 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3824 ip->nominal_fname = hp->value.cpval;
3828 /* Didn't find it; cons up a new one. */
3829 hp = (HASHNODE *) Safe_calloc(1,sizeof (HASHNODE) + fname_length + 1);
3830 hp->next = *hash_bucket;
3833 hp->length = fname_length;
3834 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3835 bcopy (fname, hp->value.cpval, fname_length);
3838 else if (token != CPP_VSPACE && token != CPP_EOF) {
3839 cpp_error (pfile, "invalid format `#line' command");
3840 goto bad_line_directive;
3843 ip->lineno = new_lineno;
3845 skip_rest_of_line (pfile);
3846 CPP_SET_WRITTEN (pfile, old_written);
3847 output_line_command (pfile, 0, file_change);
3852 * remove the definition of a symbol from the symbol table.
3853 * according to un*x /lib/cpp, it is not an error to undef
3854 * something that has no definitions, so it isn't one here either.
3860 struct directive *keyword,
3861 U_CHAR *buf, U_CHAR *limit)
3865 U_CHAR *orig_buf = buf;
3868 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3869 if (pcp_outfile && keyword)
3870 pass_thru_directive (buf, limit, pfile, keyword);
3873 SKIP_WHITE_SPACE (buf);
3874 sym_length = check_macro_name (pfile, buf, "macro");
3876 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3878 /* If we are generating additional info for debugging (with -g) we
3879 need to pass through all effective #undef commands. */
3880 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3881 pass_thru_directive (orig_buf, limit, pfile, keyword);
3882 if (hp->type != T_MACRO)
3883 cpp_warning (pfile, "undefining `%s'", hp->name);
3887 if (CPP_PEDANTIC (pfile)) {
3889 SKIP_WHITE_SPACE (buf);
3891 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3897 * Report an error detected by the program we are processing.
3898 * Use the text of the line in the error message.
3899 * (We use error because it prints the filename & line#.)
3905 struct directive *keyword,
3906 U_CHAR *buf,U_CHAR *limit)
3908 int length = limit - buf;
3909 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3910 bcopy (buf, copy, length);
3912 SKIP_WHITE_SPACE (copy);
3913 cpp_error (pfile, "#error %s", copy);
3918 * Report a warning detected by the program we are processing.
3919 * Use the text of the line in the warning message, then continue.
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 *) Safe_malloc (length + 1);
3931 bcopy (buf, copy, length);
3933 SKIP_WHITE_SPACE (copy);
3934 cpp_warning (pfile, "#warning %s", copy);
3938 /* Remember the name of the current file being read from so that we can
3939 avoid ever including it again. */
3945 cpp_buffer *ip = NULL;
3946 struct file_name_list *new;
3948 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3952 if (ip->fname != NULL)
3957 new = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3958 new->next = pfile->dont_repeat_files;
3959 pfile->dont_repeat_files = new;
3960 new->fname = savestring (ip->fname);
3961 new->control_macro = 0;
3962 new->got_name_map = 0;
3963 new->c_system_include_path = 0;
3968 /* #ident has already been copied to the output file, so just ignore it. */
3973 struct directive *keyword,
3974 U_CHAR *buf, U_CHAR *limit)
3976 /* long old_written = CPP_WRITTEN (pfile);*/
3979 /* Allow #ident in system headers, since that's not user's fault. */
3980 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3981 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3983 /* Leave rest of line to be read by later calls to cpp_get_token. */
3988 /* #pragma and its argument line have already been copied to the output file.
3989 Just check for some recognized pragmas that need validation here. */
3994 struct directive *keyword,
3995 U_CHAR *buf,U_CHAR *limit)
3997 while (*buf == ' ' || *buf == '\t')
3999 if (!strncmp (buf, "once", 4)) {
4000 /* Allow #pragma once in system headers, since that's not the user's
4002 if (!CPP_BUFFER (pfile)->system_header_p)
4003 cpp_warning (pfile, "`#pragma once' is obsolete");
4007 if (!strncmp (buf, "implementation", 14)) {
4008 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4009 been included yet. */
4010 struct file_name_list *ptr;
4011 U_CHAR *p = buf + 14, *fname, *inc_fname;
4013 SKIP_WHITE_SPACE (p);
4014 if (*p == '\n' || *p != '\"')
4018 p = (U_CHAR *) index (fname, '\"');
4019 fname_len = p != NULL ? p - fname : strlen (fname);
4021 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4022 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4023 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4024 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4026 "`#pragma implementation' for `%s' appears after file is included",
4035 /* This was a fun hack, but #pragma seems to start to be useful.
4036 By failing to recognize it, we pass it through unchanged to cc1. */
4039 * the behavior of the #pragma directive is implementation defined.
4040 * this implementation defines it as follows.
4047 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4050 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4052 execl ("/usr/games/hack", "#pragma", 0);
4053 execl ("/usr/games/rogue", "#pragma", 0);
4054 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4055 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4057 fatal ("You are in a maze of twisty compiler features, all different");
4061 /* Just ignore #sccs, on systems where we define it at all. */
4066 struct directive *keyword,
4067 U_CHAR *buf,U_CHAR *limit)
4069 if (CPP_PEDANTIC (pfile))
4070 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4075 * handle #if command by
4076 * 1) inserting special `defined' keyword into the hash table
4077 * that gets turned into 0 or 1 by special_symbol (thus,
4078 * if the luser has a symbol called `defined' already, it won't
4079 * work inside the #if command)
4080 * 2) rescan the input into a temporary output buffer
4081 * 3) pass the output buffer to the yacc parser and collect a value
4082 * 4) clean up the mess left from steps 1 and 2.
4083 * 5) call conditional_skip to skip til the next #endif (etc.),
4084 * or not, depending on the value from step 3.
4090 struct directive *keyword,
4091 U_CHAR *buf,U_CHAR *limit)
4093 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4094 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4099 * handle a #elif directive by not changing if_stack either.
4100 * see the comment above do_else.
4106 struct directive *keyword,
4107 U_CHAR *buf,U_CHAR *limit)
4109 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4110 cpp_error (pfile, "`#elif' not within a conditional");
4113 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4114 cpp_error (pfile, "`#elif' after `#else'");
4116 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4118 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4119 && strcmp (pfile->if_stack->fname,
4120 CPP_BUFFER (pfile)->nominal_fname) != 0)
4121 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4122 fprintf (stderr, ")\n");
4124 pfile->if_stack->type = T_ELIF;
4127 if (pfile->if_stack->if_succeeded)
4128 skip_if_group (pfile, 0);
4130 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4132 skip_if_group (pfile, 0);
4134 ++pfile->if_stack->if_succeeded; /* continue processing input */
4135 output_line_command (pfile, 1, same_file);
4142 * evaluate a #if expression in BUF, of length LENGTH,
4143 * then parse the result as a C expression and return the value as an int.
4145 static HOST_WIDE_INT
4146 eval_if_expression (
4151 HASHNODE *save_defined;
4152 HOST_WIDE_INT value;
4153 long old_written = CPP_WRITTEN (pfile);
4155 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4156 pfile->pcp_inside_if = 1;
4158 value = cpp_parse_expr (pfile);
4159 pfile->pcp_inside_if = 0;
4160 delete_macro (save_defined); /* clean up special symbol */
4162 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4168 * routine to handle ifdef/ifndef. Try to look up the symbol,
4169 * then do or don't skip to the #endif/#else/#elif depending
4170 * on what directive is actually being processed.
4176 struct directive *keyword,
4177 U_CHAR *unused1, U_CHAR *unused2)
4180 cpp_buffer *ip = CPP_BUFFER (pfile);
4183 enum cpp_token token;
4184 int start_of_file = 0;
4185 U_CHAR *control_macro = 0;
4186 int old_written = CPP_WRITTEN (pfile);
4188 /* Detect a #ifndef at start of file (not counting comments). */
4189 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4190 start_of_file = pfile->only_seen_white == 2;
4192 pfile->no_macro_expand++;
4193 token = get_directive_token (pfile);
4194 pfile->no_macro_expand--;
4196 ident = pfile->token_buffer + old_written;
4197 ident_length = CPP_WRITTEN (pfile) - old_written;
4198 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4200 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4202 skip = (keyword->type == T_IFDEF);
4203 if (! CPP_TRADITIONAL (pfile))
4204 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4206 else if (token == CPP_NAME)
4208 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4209 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4210 if (start_of_file && !skip)
4212 control_macro = (U_CHAR *) Safe_malloc (ident_length + 1);
4213 bcopy (ident, control_macro, ident_length + 1);
4218 skip = (keyword->type == T_IFDEF);
4219 if (! CPP_TRADITIONAL (pfile))
4220 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4223 if (!CPP_TRADITIONAL (pfile))
4225 cpp_skip_hspace (pfile);
4227 if (c != EOF && c != '\n')
4228 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4230 skip_rest_of_line (pfile);
4234 /* Output a precondition for this macro. */
4235 if (hp && hp->value.defn->predefined)
4236 fprintf (pcp_outfile, "#define %s\n", hp->name);
4239 fprintf (pcp_outfile, "#undef ");
4240 while (is_idchar[*cp]) /* Ick! */
4241 fputc (*cp++, pcp_outfile);
4242 putc ('\n', pcp_outfile);
4246 conditional_skip (pfile, skip, T_IF, control_macro);
4250 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4251 If this is a #ifndef starting at the beginning of a file,
4252 CONTROL_MACRO is the macro name tested by the #ifndef.
4253 Otherwise, CONTROL_MACRO is 0. */
4259 enum node_type type,
4260 U_CHAR *control_macro)
4262 IF_STACK_FRAME *temp;
4264 temp = (IF_STACK_FRAME *) Safe_calloc(1,sizeof (IF_STACK_FRAME));
4265 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4267 temp->lineno = CPP_BUFFER (pfile)->lineno;
4269 temp->next = pfile->if_stack;
4270 temp->control_macro = control_macro;
4271 pfile->if_stack = temp;
4273 pfile->if_stack->type = type;
4276 skip_if_group (pfile, 0);
4279 ++pfile->if_stack->if_succeeded;
4280 output_line_command (pfile, 1, same_file);
4285 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4286 * leaves input ptr at the sharp sign found.
4287 * If ANY is nonzero, return at next directive of any sort.
4295 struct directive *kt;
4296 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4298 U_CHAR *beg_of_line = bp;
4300 register int ident_length;
4301 U_CHAR *ident /*, *after_ident */ ;
4302 struct parse_marker line_start_mark;
4304 parse_set_mark (&line_start_mark, pfile);
4306 if (CPP_OPTIONS (pfile)->output_conditionals) {
4307 static char failed[] = "#failed\n";
4308 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4310 output_line_command (pfile, 1, same_file);
4314 if (CPP_OPTIONS (pfile)->output_conditionals)
4316 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4317 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4318 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4320 parse_move_mark (&line_start_mark, pfile);
4321 if (!CPP_TRADITIONAL (pfile))
4322 cpp_skip_hspace (pfile);
4326 int old_written = CPP_WRITTEN (pfile);
4327 cpp_skip_hspace (pfile);
4329 parse_name (pfile, GETC());
4330 ident_length = CPP_WRITTEN (pfile) - old_written;
4331 ident = pfile->token_buffer + old_written;
4332 pfile->limit = ident;
4334 if (ident_length == 0)
4335 goto not_a_directive;
4337 /* Handle # followed by a line number. */
4339 /* Avoid error for `###' and similar cases unless -pedantic. */
4342 for (kt = directive_table; kt->length >= 0; kt++)
4344 IF_STACK_FRAME *temp;
4345 if (ident_length == kt->length
4346 && strncmp (ident, kt->name, kt->length) == 0)
4348 /* If we are asked to return on next directive, do so now. */
4358 = (IF_STACK_FRAME *) Safe_calloc(1,sizeof (IF_STACK_FRAME));
4359 temp->next = pfile->if_stack;
4360 pfile->if_stack = temp;
4362 temp->lineno = CPP_BUFFER(pfile)->lineno;
4364 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4365 temp->type = kt->type;
4369 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4370 validate_else (pfile,
4371 kt->type == T_ELSE ? "#else" : "#endif");
4373 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4376 "`#%s' not within a conditional", kt->name);
4379 else if (pfile->if_stack == save_if_stack)
4380 goto done; /* found what we came for */
4382 if (kt->type != T_ENDIF)
4384 if (pfile->if_stack->type == T_ELSE)
4385 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4386 pfile->if_stack->type = kt->type;
4390 temp = pfile->if_stack;
4391 pfile->if_stack = temp->next;
4398 /* Don't let erroneous code go by. */
4399 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4400 && CPP_PEDANTIC (pfile))
4401 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4405 /* We're in the middle of a line. Skip the rest of it. */
4412 case '/': /* possible comment */
4413 c = skip_comment (pfile, NULL);
4420 old = CPP_WRITTEN (pfile);
4421 cpp_get_token (pfile);
4422 CPP_SET_WRITTEN (pfile, old);
4425 /* Char after backslash loses its special meaning. */
4426 if (PEEKC() == '\n')
4436 if (CPP_OPTIONS (pfile)->output_conditionals) {
4437 static char end_failed[] = "#endfailed\n";
4438 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4441 pfile->only_seen_white = 1;
4442 parse_goto_mark (&line_start_mark, pfile);
4443 parse_clear_mark (&line_start_mark);
4447 * handle a #else directive. Do this by just continuing processing
4448 * without changing if_stack ; this is so that the error message
4449 * for missing #endif's etc. will point to the original #if. It
4450 * is possible that something different would be better.
4456 struct directive *keyword,
4457 U_CHAR *buf, U_CHAR *limit)
4459 cpp_buffer *ip = CPP_BUFFER (pfile);
4461 if (CPP_PEDANTIC (pfile))
4462 validate_else (pfile, "#else");
4463 skip_rest_of_line (pfile);
4465 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4466 cpp_error (pfile, "`#else' not within a conditional");
4469 /* #ifndef can't have its special treatment for containing the whole file
4470 if it has a #else clause. */
4471 pfile->if_stack->control_macro = 0;
4473 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4474 cpp_error (pfile, "`#else' after `#else'");
4475 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4476 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4477 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4478 fprintf (stderr, ")\n");
4480 pfile->if_stack->type = T_ELSE;
4483 if (pfile->if_stack->if_succeeded)
4484 skip_if_group (pfile, 0);
4486 ++pfile->if_stack->if_succeeded; /* continue processing input */
4487 output_line_command (pfile, 1, same_file);
4493 * unstack after #endif command
4499 struct directive *keyword,
4500 U_CHAR *buf, U_CHAR *limit)
4502 if (CPP_PEDANTIC (pfile))
4503 validate_else (pfile, "#endif");
4504 skip_rest_of_line (pfile);
4506 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4507 cpp_error (pfile, "unbalanced `#endif'");
4510 IF_STACK_FRAME *temp = pfile->if_stack;
4511 pfile->if_stack = temp->next;
4512 if (temp->control_macro != 0)
4514 /* This #endif matched a #ifndef at the start of the file.
4515 See if it is at the end of the file. */
4516 struct parse_marker start_mark;
4519 parse_set_mark (&start_mark, pfile);
4523 cpp_skip_hspace (pfile);
4528 parse_goto_mark (&start_mark, pfile);
4529 parse_clear_mark (&start_mark);
4533 /* If we get here, this #endif ends a #ifndef
4534 that contains all of the file (aside from whitespace).
4535 Arrange not to include the file again
4536 if the macro that was tested is defined.
4538 Do not do this for the top-level file in a -include or any
4539 file in a -imacros. */
4543 && ! (indepth == 1 && pfile->no_record_file)
4544 && ! (pfile->no_record_file && no_output))
4547 struct file_name_list *ifile = pfile->all_include_files;
4549 for ( ; ifile != NULL; ifile = ifile->next)
4551 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4553 ifile->control_macro = temp->control_macro;
4561 output_line_command (pfile, 1, same_file);
4566 /* When an #else or #endif is found while skipping failed conditional,
4567 if -pedantic was specified, this is called to warn about text after
4568 the command name. P points to the first char after the command name. */
4576 cpp_skip_hspace (pfile);
4578 if (c != EOF && c != '\n')
4580 "text following `%s' violates ANSI standard", directive);
4583 /* Get the next token, and add it to the text in pfile->token_buffer.
4584 Return the kind of token we got. */
4591 register int c, c2, c3;
4592 long old_written = 0;
4593 long start_line, start_column;
4594 enum cpp_token token;
4595 struct cpp_options *opts = CPP_OPTIONS (pfile);
4596 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4602 if (CPP_BUFFER (pfile)->seen_eof)
4604 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4611 cpp_buffer *next_buf
4612 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4613 CPP_BUFFER (pfile)->seen_eof = 1;
4614 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4616 /* We're about to return from an #include file.
4617 Emit #line information now (as part of the CPP_POP) result.
4618 But the #line refers to the file we will pop to. */
4619 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4620 CPP_BUFFER (pfile) = next_buf;
4621 pfile->input_stack_listing_current = 0;
4622 output_line_command (pfile, 0, leave_file);
4623 CPP_BUFFER (pfile) = cur_buffer;
4633 struct parse_marker start_mark;
4635 if (PEEKC () == '=')
4637 if (opts->put_out_comments)
4638 parse_set_mark (&start_mark, pfile);
4640 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4641 &start_line, &start_column);
4642 c = skip_comment (pfile, &newlines);
4643 if (opts->put_out_comments && (c == '/' || c == EOF))
4644 parse_clear_mark (&start_mark);
4649 cpp_error_with_line (pfile, start_line, start_column,
4650 "unterminated comment");
4653 c = '/'; /* Initial letter of comment. */
4655 /* Comments are equivalent to spaces.
4656 For -traditional, a comment is equivalent to nothing. */
4657 if (opts->put_out_comments)
4659 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4661 U_CHAR *start = pbuf->buf + start_mark.position;
4662 int len = pbuf->cur - start;
4663 CPP_RESERVE(pfile, 1 + len);
4664 CPP_PUTC_Q (pfile, c);
4665 CPP_PUTS_Q (pfile, start, len);
4666 pfile->lineno += newlines;
4667 parse_clear_mark (&start_mark);
4670 else if (CPP_TRADITIONAL (pfile))
4677 /* This may not work if cpp_get_token is called recursively,
4678 since many places look for horizontal space. */
4681 /* Copy the newlines into the output buffer, in order to
4682 avoid the pain of a #line every time a multiline comment
4684 CPP_RESERVE(pfile, newlines);
4685 while (--newlines >= 0)
4687 CPP_PUTC_Q (pfile, '\n');
4693 CPP_RESERVE(pfile, 1);
4694 CPP_PUTC_Q (pfile, ' ');
4698 if (opts->for_lint) {
4701 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4703 if (lintcmd != NULL) {
4704 /* I believe it is always safe to emit this newline: */
4706 bcopy ("#pragma lint ", (char *) obp, 13);
4708 bcopy (lintcmd, (char *) obp, cmdlen);
4713 bcopy (argbp, (char *) obp, arglen);
4717 /* OK, now bring us back to the state we were in before we entered
4718 this branch. We need #line b/c the newline for the pragma
4719 could fuck things up. */
4720 output_line_command (pfile, 0, same_file);
4721 *(obp++) = ' '; /* just in case, if comments are copied thru */
4729 /* If this is expanding a macro definition, don't recognize
4730 preprocessor directives. */
4733 /* If this is expand_into_temp_buffer, recognize them
4734 only after an actual newline at this level,
4735 not at the beginning of the input level. */
4736 if (ip->fname == 0 && beg_of_line == ip->buf)
4742 if (!pfile->only_seen_white)
4744 if (handle_directive (pfile))
4745 return CPP_DIRECTIVE;
4746 pfile->only_seen_white = 0;
4751 /* A single quoted string is treated like a double -- some
4752 programs (e.g., troff) are perverse this way */
4753 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4754 &start_line, &start_column);
4755 old_written = CPP_WRITTEN (pfile);
4757 CPP_PUTC (pfile, c);
4763 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4765 /* try harder: this string crosses a macro expansion
4766 boundary. This can happen naturally if -traditional.
4767 Otherwise, only -D can make a macro with an unmatched
4769 cpp_buffer *next_buf
4770 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4771 (*CPP_BUFFER (pfile)->cleanup)
4772 (CPP_BUFFER (pfile), pfile);
4773 CPP_BUFFER (pfile) = next_buf;
4776 if (!CPP_TRADITIONAL (pfile))
4778 cpp_error_with_line (pfile, start_line, start_column,
4779 "unterminated string or character constant");
4780 if (pfile->multiline_string_line != start_line
4781 && pfile->multiline_string_line != 0)
4782 cpp_error_with_line (pfile,
4783 pfile->multiline_string_line, -1,
4784 "possible real start of unterminated constant");
4785 pfile->multiline_string_line = 0;
4789 CPP_PUTC (pfile, cc);
4793 /* Traditionally, end of line ends a string constant with
4794 no error. So exit the loop and record the new line. */
4795 if (CPP_TRADITIONAL (pfile))
4799 cpp_error_with_line (pfile, start_line, start_column,
4800 "unterminated character constant");
4803 if (CPP_PEDANTIC (pfile)
4804 && pfile->multiline_string_line == 0)
4806 cpp_pedwarn_with_line (pfile, start_line, start_column,
4807 "string constant runs past end of line");
4809 if (pfile->multiline_string_line == 0)
4810 pfile->multiline_string_line = start_line;
4817 /* Backslash newline is replaced by nothing at all. */
4818 CPP_ADJUST_WRITTEN (pfile, -1);
4823 /* ANSI stupidly requires that in \\ the second \
4824 is *not* prevented from combining with a newline. */
4827 CPP_PUTC (pfile, cc);
4839 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4840 CPP_PWRITTEN (pfile));
4841 pfile->only_seen_white = 0;
4842 return c == '\'' ? CPP_CHAR : CPP_STRING;
4845 if (!opts->dollars_in_ident)
4850 if (opts->cplusplus && PEEKC () == ':')
4859 if (c2 == c || c2 == '=')
4869 if (PEEKC () == '=')
4876 if (c2 == '-' && opts->chill)
4878 /* Chill style comment */
4879 if (opts->put_out_comments)
4880 parse_set_mark (&start_mark, pfile);
4881 FORWARD(1); /* Skip second '-'. */
4889 /* Don't consider final '\n' to be part of comment. */
4895 goto return_comment;
4897 if (c2 == '-' || c2 == '=' || c2 == '>')
4902 if (pfile->parsing_include_directive)
4906 CPP_PUTC (pfile, c);
4911 if (c == '\n' || c == EOF)
4914 "missing '>' in `#include <FILENAME>'");
4920 /* else fall through */
4929 CPP_RESERVE (pfile, 4);
4930 CPP_PUTC (pfile, c);
4931 CPP_PUTC (pfile, c2);
4935 CPP_PUTC_Q (pfile, GETC ());
4936 CPP_NUL_TERMINATE_Q (pfile);
4937 pfile->only_seen_white = 0;
4941 if (CPP_BUFFER (pfile)->has_escapes)
4946 if (pfile->output_escapes)
4947 CPP_PUTS (pfile, "@-", 2);
4948 parse_name (pfile, GETC ());
4951 else if (is_space [c])
4953 CPP_RESERVE (pfile, 2);
4954 if (pfile->output_escapes)
4955 CPP_PUTC_Q (pfile, '@');
4956 CPP_PUTC_Q (pfile, c);
4960 if (pfile->output_escapes)
4962 CPP_PUTS (pfile, "@@", 2);
4972 CPP_RESERVE(pfile, 2);
4973 CPP_PUTC_Q (pfile, '.');
4977 /* FIXME - misses the case "..\\\n." */
4978 if (c2 == '.' && PEEKN(1) == '.')
4980 CPP_RESERVE(pfile, 4);
4981 CPP_PUTC_Q (pfile, '.');
4982 CPP_PUTC_Q (pfile, '.');
4983 CPP_PUTC_Q (pfile, '.');
4985 CPP_NUL_TERMINATE_Q (pfile);
4986 pfile->only_seen_white = 0;
4993 pfile->only_seen_white = 0;
4995 CPP_RESERVE(pfile, 3);
4996 CPP_PUTC_Q (pfile, c);
4997 CPP_PUTC_Q (pfile, GETC ());
4998 CPP_NUL_TERMINATE_Q (pfile);
5004 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5006 CPP_PUTC (pfile, c);
5012 case '0': case '1': case '2': case '3': case '4':
5013 case '5': case '6': case '7': case '8': case '9':
5018 CPP_RESERVE (pfile, 2);
5019 CPP_PUTC_Q (pfile, c);
5024 if (!is_idchar[c] && c != '.'
5025 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5030 CPP_NUL_TERMINATE_Q (pfile);
5031 pfile->only_seen_white = 0;
5033 case 'b': case 'c': case 'd': case 'h': case 'o':
5034 case 'B': case 'C': case 'D': case 'H': case 'O':
5035 if (opts->chill && PEEKC () == '\'')
5037 pfile->only_seen_white = 0;
5038 CPP_RESERVE (pfile, 2);
5039 CPP_PUTC_Q (pfile, c);
5040 CPP_PUTC_Q (pfile, '\'');
5046 goto chill_number_eof;
5049 if (c == '\\' && PEEKC() == '\n')
5056 CPP_PUTC (pfile, c);
5060 CPP_RESERVE (pfile, 2);
5061 CPP_PUTC_Q (pfile, c);
5062 CPP_NUL_TERMINATE_Q (pfile);
5069 CPP_NUL_TERMINATE (pfile);
5076 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5077 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5078 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5079 case 'x': case 'y': case 'z':
5080 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5081 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5082 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5087 unsigned char *ident;
5088 int before_name_written = CPP_WRITTEN (pfile);
5090 parse_name (pfile, c);
5091 pfile->only_seen_white = 0;
5092 if (pfile->no_macro_expand)
5094 ident = pfile->token_buffer + before_name_written;
5095 ident_len = CPP_PWRITTEN (pfile) - ident;
5096 hp = cpp_lookup (pfile, ident, ident_len, -1);
5099 if (hp->type == T_DISABLED)
5101 if (pfile->output_escapes)
5102 { /* Return "@-IDENT", followed by '\0'. */
5104 CPP_RESERVE (pfile, 3);
5105 ident = pfile->token_buffer + before_name_written;
5106 CPP_ADJUST_WRITTEN (pfile, 2);
5107 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5114 /* If macro wants an arglist, verify that a '(' follows.
5115 first skip all whitespace, copying it to the output
5116 after the macro name. Then, if there is no '(',
5117 decide this is not a macro call and leave things that way. */
5118 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5120 struct parse_marker macro_mark;
5122 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5124 cpp_buffer *next_buf;
5125 cpp_skip_hspace (pfile);
5126 if (PEEKC () != EOF)
5128 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5129 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5130 CPP_BUFFER (pfile) = next_buf;
5132 parse_set_mark (¯o_mark, pfile);
5135 cpp_skip_hspace (pfile);
5137 is_macro_call = c == '(';
5143 parse_goto_mark (¯o_mark, pfile);
5144 parse_clear_mark (¯o_mark);
5148 /* This is now known to be a macro call. */
5150 /* it might not actually be a macro. */
5151 if (hp->type != T_MACRO) {
5152 int xbuf_len; U_CHAR *xbuf;
5153 CPP_SET_WRITTEN (pfile, before_name_written);
5154 special_symbol (hp, pfile);
5155 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5156 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
5157 CPP_SET_WRITTEN (pfile, before_name_written);
5158 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5159 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5163 /* Expand the macro, reading arguments as needed,
5164 and push the expansion on the input stack. */
5165 macroexpand (pfile, hp);
5166 CPP_SET_WRITTEN (pfile, before_name_written);
5169 /* An extra "@ " is added to the end of a macro expansion
5170 to prevent accidental token pasting. We prefer to avoid
5171 unneeded extra spaces (for the sake of cpp-using tools like
5172 imake). Here we remove the space if it is safe to do so. */
5173 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5174 && pfile->buffer->rlimit[-2] == '@'
5175 && pfile->buffer->rlimit[-1] == ' ')
5177 int c1 = pfile->buffer->rlimit[-3];
5178 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5179 if (c2 == EOF || ! unsafe_chars (c1, c2))
5180 pfile->buffer->rlimit -= 2;
5185 case ' ': case '\t': case '\v': case '\r':
5188 CPP_PUTC (pfile, c);
5190 if (c == EOF || !is_hor_space[c])
5204 CPP_PUTC (pfile, c);
5205 if (pfile->only_seen_white == 0)
5206 pfile->only_seen_white = 1;
5208 output_line_command (pfile, 1, same_file);
5211 case '(': token = CPP_LPAREN; goto char1;
5212 case ')': token = CPP_RPAREN; goto char1;
5213 case '{': token = CPP_LBRACE; goto char1;
5214 case '}': token = CPP_RBRACE; goto char1;
5215 case ',': token = CPP_COMMA; goto char1;
5216 case ';': token = CPP_SEMICOLON; goto char1;
5222 pfile->only_seen_white = 0;
5223 CPP_PUTC (pfile, c);
5229 /* Like cpp_get_token, but skip spaces and comments. */
5231 cpp_get_non_space_token (
5234 int old_written = CPP_WRITTEN (pfile);
5237 enum cpp_token token = cpp_get_token (pfile);
5238 if (token != CPP_COMMENT && token != CPP_POP
5239 && token != CPP_HSPACE && token != CPP_VSPACE)
5241 CPP_SET_WRITTEN (pfile, old_written);
5245 /* Parse an identifier starting with C. */
5249 cpp_reader *pfile, int c)
5255 if (c == '\\' && PEEKC() == '\n')
5264 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5265 CPP_PUTC_Q (pfile, c);
5270 CPP_NUL_TERMINATE_Q (pfile);
5275 /* Maintain and search list of included files, for #import. */
5277 /* Hash a file name for import_hash_table. */
5285 while (*f) val += *f++;
5286 return (val%IMPORT_HASH_SIZE);
5289 /* Search for file FILENAME in import_hash_table.
5290 Return -2 if found, either a matching name or a matching inode.
5291 Otherwise, open the file and return a file descriptor if successful
5292 or -1 if unsuccessful. */
5298 struct file_name_list *searchptr)
5300 struct import_file *i;
5306 hashval = import_hash (filename);
5308 /* Attempt to find file in list of already included files */
5309 i = pfile->import_hash_table[hashval];
5312 if (!strcmp (filename, i->name))
5313 return -2; /* return found */
5316 /* Open it and try a match on inode/dev */
5317 fd = open_include_file (pfile, filename, searchptr);
5321 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5322 i = pfile->import_hash_table[h];
5324 /* Compare the inode and the device.
5325 Supposedly on some systems the inode is not a scalar. */
5326 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5327 && i->dev == sb.st_dev) {
5329 return -2; /* return found */
5334 return fd; /* Not found, return open file */
5337 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5345 struct import_file *i;
5349 hashval = import_hash (fname);
5351 i = (struct import_file *)Safe_malloc (sizeof (struct import_file));
5352 i->name = (char *)Safe_malloc (strlen (fname)+1);
5353 strcpy (i->name, fname);
5354 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5356 i->next = pfile->import_hash_table[hashval];
5357 pfile->import_hash_table[hashval] = i;
5360 /* The file_name_map structure holds a mapping of file names for a
5361 particular directory. This mapping is read from the file named
5362 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5363 map filenames on a file system with severe filename restrictions,
5364 such as DOS. The format of the file name map file is just a series
5365 of lines with two tokens on each line. The first token is the name
5366 to map, and the second token is the actual name to use. */
5368 struct file_name_map
5370 struct file_name_map *map_next;
5375 #define FILE_NAME_MAP_FILE "header.gcc"
5377 /* Read a space delimited string of unlimited length from a stdio
5381 read_filename_string (
5389 set = alloc = Safe_malloc (len + 1);
5393 while ((ch = getc (f)) != EOF && ! is_space[ch])
5395 if (set - alloc == len)
5398 alloc = Safe_realloc(alloc, len + 1);
5399 set = alloc + len / 2;
5409 /* This structure holds a linked list of file name maps, one per directory. */
5410 struct file_name_map_list
5412 struct file_name_map_list *map_list_next;
5413 char *map_list_name;
5414 struct file_name_map *map_list_map;
5417 /* Read the file name map file for DIRNAME. */
5419 static struct file_name_map *
5424 register struct file_name_map_list *map_list_ptr;
5428 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5429 map_list_ptr = map_list_ptr->map_list_next)
5430 if (! strcmp (map_list_ptr->map_list_name, dirname))
5431 return map_list_ptr->map_list_map;
5433 map_list_ptr = ((struct file_name_map_list *)
5434 Safe_malloc (sizeof (struct file_name_map_list)));
5435 map_list_ptr->map_list_name = savestring (dirname);
5436 map_list_ptr->map_list_map = NULL;
5438 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5439 strcpy (name, dirname);
5442 strcat (name, FILE_NAME_MAP_FILE);
5443 f = fopen (name, "r");
5445 map_list_ptr->map_list_map = NULL;
5449 int dirlen = strlen (dirname);
5451 while ((ch = getc (f)) != EOF)
5454 struct file_name_map *ptr;
5458 from = read_filename_string (ch, f);
5459 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5461 to = read_filename_string (ch, f);
5463 ptr = ((struct file_name_map *)
5464 Safe_malloc (sizeof (struct file_name_map)));
5465 ptr->map_from = from;
5467 /* Make the real filename absolute. */
5472 ptr->map_to = Safe_malloc (dirlen + strlen (to) + 2);
5473 strcpy (ptr->map_to, dirname);
5474 ptr->map_to[dirlen] = '/';
5475 strcpy (ptr->map_to + dirlen + 1, to);
5479 ptr->map_next = map_list_ptr->map_list_map;
5480 map_list_ptr->map_list_map = ptr;
5482 while ((ch = getc (f)) != '\n')
5489 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5490 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5492 return map_list_ptr->map_list_map;
5495 /* Try to open include file FILENAME. SEARCHPTR is the directory
5496 being tried from the include file search path. This function maps
5497 filenames on file systems based on information read by
5504 struct file_name_list *searchptr)
5506 register struct file_name_map *map;
5507 register char *from;
5510 if (searchptr && ! searchptr->got_name_map)
5512 searchptr->name_map = read_name_map (pfile,
5514 ? searchptr->fname : ".");
5515 searchptr->got_name_map = 1;
5518 /* First check the mapping for the directory we are using. */
5519 if (searchptr && searchptr->name_map)
5522 if (searchptr->fname)
5523 from += strlen (searchptr->fname) + 1;
5524 for (map = searchptr->name_map; map; map = map->map_next)
5526 if (! strcmp (map->map_from, from))
5528 /* Found a match. */
5529 return open (map->map_to, O_RDONLY, 0666);
5534 /* Try to find a mapping file for the particular directory we are
5535 looking in. Thus #include <sys/types.h> will look up sys/types.h
5536 in /usr/include/header.gcc and look up types.h in
5537 /usr/include/sys/header.gcc. */
5538 p = rindex (filename, '/');
5543 && (int) strlen (searchptr->fname) == p - filename
5544 && ! strncmp (searchptr->fname, filename, p - filename))
5546 /* FILENAME is in SEARCHPTR, which we've already checked. */
5547 return open (filename, O_RDONLY, 0666);
5557 dir = (char *) alloca (p - filename + 1);
5558 bcopy (filename, dir, p - filename);
5559 dir[p - filename] = '\0';
5562 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5563 if (! strcmp (map->map_from, from))
5564 return open (map->map_to, O_RDONLY, 0666);
5566 return open (filename, O_RDONLY, 0666);
5569 /* Process the contents of include file FNAME, already open on descriptor F,
5571 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5572 "system" include directories (as decided by the `is_system_include'
5574 DIRPTR is the link in the dir path through which this file was found,
5575 or 0 if the file name was absolute or via the current directory.
5576 Return 1 on success, 0 on failure.
5578 The caller is responsible for the cpp_push_buffer. */
5585 int system_header_p,
5586 struct file_name_list *dirptr)
5592 cpp_buffer *fp; /* For input stack frame */
5593 /* int missing_newline = 0; # KILL */
5595 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5597 cpp_perror_with_name (pfile, fname);
5599 cpp_pop_buffer (pfile);
5603 fp = CPP_BUFFER (pfile);
5604 fp->nominal_fname = fp->fname = fname;
5609 fp->system_header_p = system_header_p;
5612 fp->cleanup = file_cleanup;
5614 if (S_ISREG (st_mode)) {
5615 fp->buf = (U_CHAR *) Safe_malloc (st_size + 2);
5616 fp->alimit = fp->buf + st_size + 2;
5619 /* Read the file contents, knowing that st_size is an upper bound
5620 on the number of bytes we can read. */
5621 length = safe_read (f, fp->buf, st_size);
5622 fp->rlimit = fp->buf + length;
5623 if (length < 0) goto nope;
5625 else if (S_ISDIR (st_mode)) {
5626 cpp_error (pfile, "directory `%s' specified in #include", fname);
5630 /* Cannot count its file size before reading.
5631 First read the entire file into heap and
5632 copy them into buffer on stack. */
5637 fp->buf = (U_CHAR *) Safe_malloc (bsize + 2);
5640 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5642 goto nope; /* error! */
5644 if (st_size != bsize)
5645 break; /* End of file */
5647 fp->buf = (U_CHAR *) Safe_realloc(fp->buf, bsize + 2);
5653 if ((length > 0 && fp->buf[length - 1] != '\n')
5654 /* Backslash-newline at end is not good enough. */
5655 || (length > 1 && fp->buf[length - 2] == '\\')) {
5656 fp->buf[length++] = '\n';
5658 missing_newline = 1;
5661 fp->buf[length] = '\0';
5662 fp->rlimit = fp->buf + length;
5664 /* Close descriptor now, so nesting does not use lots of descriptors. */
5667 /* Must do this before calling trigraph_pcp, so that the correct file name
5668 will be printed in warning messages. */
5670 pfile->input_stack_listing_current = 0;
5680 if (missing_newline)
5683 if (CPP_PEDANTIC (pfile) && missing_newline)
5684 pedwarn ("file does not end in newline");
5687 input_file_stack_tick++;
5694 cpp_perror_with_name (pfile, fname);
5705 struct cpp_options *opts = CPP_OPTIONS (pfile);
5706 struct cpp_pending *pend;
5711 /* The code looks at the defaults through this pointer, rather than through
5712 the constant structure above. This pointer gets changed if an environment
5713 variable specifies other defaults. */
5714 struct default_include *include_defaults = include_defaults_array;
5716 /* Add dirs from CPATH after dirs from -I. */
5717 /* There seems to be confusion about what CPATH should do,
5718 so for the moment it is not documented. */
5719 /* Some people say that CPATH should replace the standard include dirs,
5720 but that seems pointless: it comes before them, so it overrides them
5722 p = (char *) getenv ("CPATH");
5723 if (p != 0 && ! opts->no_standard_includes)
5724 path_include (pfile, p);
5726 /* Now that dollars_in_ident is known, initialize is_idchar. */
5727 initialize_char_syntax (opts);
5729 /* Do partial setup of input buffer for the sake of generating
5730 early #line directives (when -g is in effect). */
5731 fp = cpp_push_buffer (pfile, NULL, 0);
5732 if (opts->in_fname == NULL)
5733 opts->in_fname = "";
5734 fp->nominal_fname = fp->fname = opts->in_fname;
5737 /* Install __LINE__, etc. Must follow initialize_char_syntax
5738 and option processing. */
5739 initialize_builtins (pfile);
5741 /* Do standard #defines and assertions
5742 that identify system and machine type. */
5744 if (!opts->inhibit_predefs) {
5745 char *p = (char *) alloca (strlen (predefs) + 1);
5746 strcpy (p, predefs);
5749 while (*p == ' ' || *p == '\t')
5751 /* Handle -D options. */
5752 if (p[0] == '-' && p[1] == 'D') {
5754 while (*p && *p != ' ' && *p != '\t')
5758 if (opts->debug_output)
5759 output_line_command (pfile, 0, same_file);
5760 cpp_define (pfile, q);
5761 while (*p == ' ' || *p == '\t')
5763 } else if (p[0] == '-' && p[1] == 'A') {
5764 /* Handle -A options (assertions). */
5773 past_name = assertion;
5774 /* Locate end of name. */
5775 while (*past_name && *past_name != ' '
5776 && *past_name != '\t' && *past_name != '(')
5778 /* Locate `(' at start of value. */
5780 while (*value && (*value == ' ' || *value == '\t'))
5782 if (*value++ != '(')
5784 while (*value && (*value == ' ' || *value == '\t'))
5787 /* Locate end of value. */
5788 while (*past_value && *past_value != ' '
5789 && *past_value != '\t' && *past_value != ')')
5791 termination = past_value;
5792 while (*termination && (*termination == ' ' || *termination == '\t'))
5794 if (*termination++ != ')')
5796 if (*termination && *termination != ' ' && *termination != '\t')
5798 /* Temporarily null-terminate the value. */
5799 save_char = *termination;
5800 *termination = '\0';
5801 /* Install the assertion. */
5802 make_assertion (pfile, "-A", assertion);
5803 *termination = (char) save_char;
5805 while (*p == ' ' || *p == '\t')
5813 /* Now handle the command line options. */
5815 /* Do -U's, -D's and -A's in the order they were seen. */
5816 /* First reverse the list. */
5817 opts->pending = nreverse_pending (opts->pending);
5819 for (pend = opts->pending; pend; pend = pend->next)
5821 if (pend->cmd != NULL && pend->cmd[0] == '-')
5823 switch (pend->cmd[1])
5826 if (opts->debug_output)
5827 output_line_command (pfile, 0, same_file);
5828 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5831 if (opts->debug_output)
5832 output_line_command (pfile, 0, same_file);
5833 cpp_define (pfile, pend->arg);
5836 make_assertion (pfile, "-A", pend->arg);
5842 opts->done_initializing = 1;
5844 { /* read the appropriate environment variable and if it exists
5845 replace include_defaults with the listed path. */
5847 switch ((opts->objc << 1) + opts->cplusplus)
5850 epath = getenv ("C_INCLUDE_PATH");
5853 epath = getenv ("CPLUS_INCLUDE_PATH");
5856 epath = getenv ("OBJC_INCLUDE_PATH");
5859 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5862 /* If the environment var for this language is set,
5863 add to the default list of include directories. */
5865 char *nstore = (char *) alloca (strlen (epath) + 2);
5867 char *startp, *endp;
5869 for (num_dirs = 1, startp = epath; *startp; startp++)
5870 if (*startp == PATH_SEPARATOR)
5873 = (struct default_include *) Safe_malloc ((num_dirs
5874 * sizeof (struct default_include))
5875 + sizeof (include_defaults_array));
5876 startp = endp = epath;
5879 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5880 if ((*endp == PATH_SEPARATOR)
5882 strncpy (nstore, startp, endp-startp);
5884 strcpy (nstore, ".");
5886 nstore[endp-startp] = '\0';
5888 include_defaults[num_dirs].fname = savestring (nstore);
5889 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5890 include_defaults[num_dirs].cxx_aware = 1;
5894 endp = startp = endp + 1;
5898 /* Put the usual defaults back in at the end. */
5899 bcopy ((char *) include_defaults_array,
5900 (char *) &include_defaults[num_dirs],
5901 sizeof (include_defaults_array));
5905 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5906 opts->first_system_include = opts->before_system;
5908 /* Unless -fnostdinc,
5909 tack on the standard include file dirs to the specified list */
5910 if (!opts->no_standard_includes) {
5911 struct default_include *p = include_defaults;
5912 char *specd_prefix = opts->include_prefix;
5913 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5914 int default_len = 0;
5915 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5916 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5917 default_len = strlen (default_prefix) - 7;
5918 default_prefix[default_len] = 0;
5920 /* Search "translated" versions of GNU directories.
5921 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5922 if (specd_prefix != 0 && default_len != 0)
5923 for (p = include_defaults; p->fname; p++) {
5924 /* Some standard dirs are only for C++. */
5926 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5927 /* Does this dir start with the prefix? */
5928 if (!strncmp (p->fname, default_prefix, default_len)) {
5929 /* Yes; change prefix and add to search list. */
5930 struct file_name_list *new
5931 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5932 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5933 char *str = (char *) Safe_malloc (this_len + 1);
5934 strcpy (str, specd_prefix);
5935 strcat (str, p->fname + default_len);
5937 new->control_macro = 0;
5938 new->c_system_include_path = !p->cxx_aware;
5939 new->got_name_map = 0;
5940 append_include_chain (pfile, new, new);
5941 if (opts->first_system_include == 0)
5942 opts->first_system_include = new;
5946 /* Search ordinary names for GNU include directories. */
5947 for (p = include_defaults; p->fname; p++) {
5948 /* Some standard dirs are only for C++. */
5950 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5951 struct file_name_list *new
5952 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5953 new->control_macro = 0;
5954 new->c_system_include_path = !p->cxx_aware;
5955 new->fname = p->fname;
5956 new->got_name_map = 0;
5957 append_include_chain (pfile, new, new);
5958 if (opts->first_system_include == 0)
5959 opts->first_system_include = new;
5964 /* Tack the after_include chain at the end of the include chain. */
5965 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5966 if (opts->first_system_include == 0)
5967 opts->first_system_include = opts->after_include;
5969 /* With -v, print the list of dirs to search. */
5970 if (opts->verbose) {
5971 struct file_name_list *p;
5972 fprintf (stderr, "#include \"...\" search starts here:\n");
5973 for (p = opts->include; p; p = p->next) {
5974 if (p == opts->first_bracket_include)
5975 fprintf (stderr, "#include <...> search starts here:\n");
5976 fprintf (stderr, " %s\n", p->fname);
5978 fprintf (stderr, "End of search list.\n");
5981 /* Scan the -imacros files before the main input.
5982 Much like #including them, but with no_output set
5983 so that only their macro definitions matter. */
5985 opts->no_output++; pfile->no_record_file++;
5986 for (pend = opts->pending; pend; pend = pend->next)
5988 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5990 int fd = open (pend->arg, O_RDONLY, 0666);
5993 cpp_perror_with_name (pfile, pend->arg);
5994 return FATAL_EXIT_CODE;
5996 cpp_push_buffer (pfile, NULL, 0);
5997 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
5998 cpp_scan_buffer (pfile);
6001 opts->no_output--; pfile->no_record_file--;
6003 /* Copy the entire contents of the main input file into
6004 the stacked input buffer previously allocated for it. */
6005 if (fname == NULL || *fname == 0) {
6008 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6009 cpp_pfatal_with_name (pfile, fname);
6011 /* -MG doesn't select the form of output and must be specified with one of
6012 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6013 inhibit compilation. */
6014 if (opts->print_deps_missing_files
6015 && (opts->print_deps == 0 || !opts->no_output))
6016 fatal (pfile, "-MG must be specified with one of -M or -MM");
6018 /* Either of two environment variables can specify output of deps.
6019 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6020 where OUTPUT_FILE is the file to write deps info to
6021 and DEPS_TARGET is the target to mention in the deps. */
6023 if (opts->print_deps == 0
6024 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6025 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6026 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6032 spec = getenv ("SUNPRO_DEPENDENCIES");
6033 opts->print_deps = 2;
6036 opts->print_deps = 1;
6039 /* Find the space before the DEPS_TARGET, if there is one. */
6040 /* This should use index. (mrs) */
6041 while (*s != 0 && *s != ' ') s++;
6044 opts->deps_target = s + 1;
6045 output_file = (char *) Safe_malloc (s - spec + 1);
6046 bcopy (spec, output_file, s - spec);
6047 output_file[s - spec] = 0;
6051 opts->deps_target = 0;
6055 opts->deps_file = output_file;
6056 opts->print_deps_append = 1;
6059 /* For -M, print the expected object file name
6060 as the target of this Make-rule. */
6061 if (opts->print_deps)
6063 pfile->deps_allocated_size = 200;
6064 pfile->deps_buffer = (char *) Safe_malloc (pfile->deps_allocated_size);
6065 pfile->deps_buffer[0] = 0;
6066 pfile->deps_size = 0;
6067 pfile->deps_column = 0;
6069 if (opts->deps_target)
6070 deps_output (pfile, opts->deps_target, ':');
6071 else if (*opts->in_fname == 0)
6072 deps_output (pfile, "-", ':');
6078 /* Discard all directory prefixes from filename. */
6079 if ((q = rindex (opts->in_fname, '/')) != NULL
6080 #ifdef DIR_SEPARATOR
6081 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6088 /* Copy remainder to mungable area. */
6089 p = (char *) alloca (strlen(q) + 8);
6092 /* Output P, but remove known suffixes. */
6096 && p[len - 2] == '.'
6097 && index("cCsSm", p[len - 1]))
6100 && p[len - 3] == '.'
6101 && p[len - 2] == 'c'
6102 && p[len - 1] == 'c')
6105 && p[len - 4] == '.'
6106 && p[len - 3] == 'c'
6107 && p[len - 2] == 'x'
6108 && p[len - 1] == 'x')
6111 && p[len - 4] == '.'
6112 && p[len - 3] == 'c'
6113 && p[len - 2] == 'p'
6114 && p[len - 1] == 'p')
6117 /* Supply our own suffix. */
6128 deps_output (pfile, p, ':');
6129 deps_output (pfile, opts->in_fname, ' ');
6134 /* Make sure data ends with a newline. And put a null after it. */
6136 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6137 /* Backslash-newline at end is not good enough. */
6138 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6139 fp->buf[fp->length++] = '\n';
6140 missing_newline = 1;
6142 fp->buf[fp->length] = '\0';
6144 /* Unless inhibited, convert trigraphs in the input. */
6150 /* Scan the -include files before the main input.
6151 We push these in reverse order, so that the first one is handled first. */
6153 pfile->no_record_file++;
6154 opts->pending = nreverse_pending (opts->pending);
6155 for (pend = opts->pending; pend; pend = pend->next)
6157 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6159 int fd = open (pend->arg, O_RDONLY, 0666);
6162 cpp_perror_with_name (pfile, pend->arg);
6163 return FATAL_EXIT_CODE;
6165 cpp_push_buffer (pfile, NULL, 0);
6166 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6169 pfile->no_record_file--;
6171 /* Free the pending list. */
6172 for (pend = opts->pending; pend; )
6174 struct cpp_pending *next = pend->next;
6178 opts->pending = NULL;
6181 /* Scan the input, processing macros and directives. */
6183 rescan (&outbuf, 0);
6185 if (missing_newline)
6188 if (CPP_PEDANTIC (pfile) && missing_newline)
6189 pedwarn ("file does not end in newline");
6192 if (finclude (pfile, f, fname, 0, NULL_PTR))
6193 output_line_command (pfile, 0, same_file);
6194 return SUCCESS_EXIT_CODE;
6201 bzero ((char *) pfile, sizeof (cpp_reader));
6202 pfile->get_token = cpp_get_token;
6204 pfile->token_buffer_size = 200;
6205 pfile->token_buffer = (U_CHAR*)Safe_malloc (pfile->token_buffer_size);
6206 CPP_SET_WRITTEN (pfile, 0);
6208 pfile->system_include_depth = 0;
6209 pfile->dont_repeat_files = 0;
6210 pfile->all_include_files = 0;
6211 pfile->max_include_len = 0;
6212 pfile->timebuf = NULL;
6213 pfile->only_seen_white = 1;
6214 pfile->buffer = CPP_NULL_BUFFER(pfile);
6217 static struct cpp_pending *
6219 struct cpp_pending *list)
6222 register struct cpp_pending *prev = 0, *next, *pend;
6223 for (pend = list; pend; pend = next)
6238 struct cpp_pending *pend
6239 = (struct cpp_pending*)Safe_malloc (sizeof (struct cpp_pending));
6242 pend->next = CPP_OPTIONS (pfile)->pending;
6243 CPP_OPTIONS (pfile)->pending = pend;
6246 /* Handle command-line options in (argc, argv).
6247 Can be called multiple times, to handle multiple sets of options.
6248 Returns if an unrecognized option is seen.
6249 Returns number of handled arguments. */
6252 cpp_handle_options (
6258 struct cpp_options *opts = CPP_OPTIONS (pfile);
6259 for (i = 0; i < argc; i++) {
6260 if (argv[i][0] != '-') {
6261 if (opts->out_fname != NULL)
6262 fatal ("Usage: %s [switches] input output", argv[0]);
6263 else if (opts->in_fname != NULL)
6264 opts->out_fname = argv[i];
6266 opts->in_fname = argv[i];
6268 switch (argv[i][1]) {
6271 if (!strcmp (argv[i], "-include")
6272 || !strcmp (argv[i], "-imacros")) {
6274 fatal ("Filename missing after `%s' option", argv[i]);
6276 push_pending (pfile, argv[i], argv[i+1]), i++;
6278 if (!strcmp (argv[i], "-iprefix")) {
6280 fatal ("Filename missing after `-iprefix' option");
6282 opts->include_prefix = argv[++i];
6284 if (!strcmp (argv[i], "-ifoutput")) {
6285 opts->output_conditionals = 1;
6287 if (!strcmp (argv[i], "-isystem")) {
6288 struct file_name_list *dirtmp;
6291 fatal ("Filename missing after `-isystem' option");
6293 dirtmp = (struct file_name_list *)
6294 Safe_malloc (sizeof (struct file_name_list));
6296 dirtmp->control_macro = 0;
6297 dirtmp->c_system_include_path = 1;
6298 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1]) + 1);
6299 strcpy (dirtmp->fname, argv[++i]);
6300 dirtmp->got_name_map = 0;
6302 if (opts->before_system == 0)
6303 opts->before_system = dirtmp;
6305 opts->last_before_system->next = dirtmp;
6306 opts->last_before_system = dirtmp; /* Tail follows the last one */
6308 /* Add directory to end of path for includes,
6309 with the default prefix at the front of its name. */
6310 if (!strcmp (argv[i], "-iwithprefix")) {
6311 struct file_name_list *dirtmp;
6314 if (opts->include_prefix != 0)
6315 prefix = opts->include_prefix;
6317 prefix = savestring (GCC_INCLUDE_DIR);
6318 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6319 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6320 prefix[strlen (prefix) - 7] = 0;
6323 dirtmp = (struct file_name_list *)
6324 Safe_malloc (sizeof (struct file_name_list));
6325 dirtmp->next = 0; /* New one goes on the end */
6326 dirtmp->control_macro = 0;
6327 dirtmp->c_system_include_path = 0;
6329 fatal ("Directory name missing after `-iwithprefix' option");
6331 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6332 + strlen (prefix) + 1);
6333 strcpy (dirtmp->fname, prefix);
6334 strcat (dirtmp->fname, argv[++i]);
6335 dirtmp->got_name_map = 0;
6337 if (opts->after_include == 0)
6338 opts->after_include = dirtmp;
6340 opts->last_after_include->next = dirtmp;
6341 opts->last_after_include = dirtmp; /* Tail follows the last one */
6343 /* Add directory to main path for includes,
6344 with the default prefix at the front of its name. */
6345 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6346 struct file_name_list *dirtmp;
6349 if (opts->include_prefix != 0)
6350 prefix = opts->include_prefix;
6352 prefix = savestring (GCC_INCLUDE_DIR);
6353 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6354 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6355 prefix[strlen (prefix) - 7] = 0;
6358 dirtmp = (struct file_name_list *)
6359 Safe_malloc (sizeof (struct file_name_list));
6360 dirtmp->next = 0; /* New one goes on the end */
6361 dirtmp->control_macro = 0;
6362 dirtmp->c_system_include_path = 0;
6364 fatal ("Directory name missing after `-iwithprefixbefore' option");
6366 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6367 + strlen (prefix) + 1);
6368 strcpy (dirtmp->fname, prefix);
6369 strcat (dirtmp->fname, argv[++i]);
6370 dirtmp->got_name_map = 0;
6372 append_include_chain (pfile, dirtmp, dirtmp);
6374 /* Add directory to end of path for includes. */
6375 if (!strcmp (argv[i], "-idirafter")) {
6376 struct file_name_list *dirtmp;
6378 dirtmp = (struct file_name_list *)
6379 Safe_malloc (sizeof (struct file_name_list));
6380 dirtmp->next = 0; /* New one goes on the end */
6381 dirtmp->control_macro = 0;
6382 dirtmp->c_system_include_path = 0;
6384 fatal ("Directory name missing after `-idirafter' option");
6386 dirtmp->fname = argv[++i];
6387 dirtmp->got_name_map = 0;
6389 if (opts->after_include == 0)
6390 opts->after_include = dirtmp;
6392 opts->last_after_include->next = dirtmp;
6393 opts->last_after_include = dirtmp; /* Tail follows the last one */
6398 if (opts->out_fname != NULL)
6399 fatal ("Output filename specified twice");
6401 fatal ("Filename missing after -o option");
6402 opts->out_fname = argv[++i];
6403 if (!strcmp (opts->out_fname, "-"))
6404 opts->out_fname = "";
6408 if (!strcmp (argv[i], "-pedantic"))
6409 CPP_PEDANTIC (pfile) = 1;
6410 else if (!strcmp (argv[i], "-pedantic-errors")) {
6411 CPP_PEDANTIC (pfile) = 1;
6412 opts->pedantic_errors = 1;
6415 else if (!strcmp (argv[i], "-pcp")) {
6416 char *pcp_fname = argv[++i];
6418 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6419 ? fopen (pcp_fname, "w")
6420 : fdopen (dup (fileno (stdout)), "w"));
6421 if (pcp_outfile == 0)
6422 cpp_pfatal_with_name (pfile, pcp_fname);
6429 if (!strcmp (argv[i], "-traditional")) {
6430 opts->traditional = 1;
6431 if (opts->dollars_in_ident > 0)
6432 opts->dollars_in_ident = 1;
6433 } else if (!strcmp (argv[i], "-trigraphs")) {
6435 opts->no_trigraphs = 0;
6440 if (! strcmp (argv[i], "-lang-c"))
6441 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6442 if (! strcmp (argv[i], "-lang-c++"))
6443 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6444 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6445 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6446 if (! strcmp (argv[i], "-lang-objc"))
6447 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6448 if (! strcmp (argv[i], "-lang-objc++"))
6449 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6450 if (! strcmp (argv[i], "-lang-asm"))
6452 if (! strcmp (argv[i], "-lint"))
6454 if (! strcmp (argv[i], "-lang-chill"))
6455 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6456 opts->traditional = 1, opts->no_trigraphs = 1;
6460 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6464 opts->inhibit_warnings = 1;
6468 if (!strcmp (argv[i], "-Wtrigraphs"))
6469 opts->warn_trigraphs = 1;
6470 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6471 opts->warn_trigraphs = 0;
6472 else if (!strcmp (argv[i], "-Wcomment"))
6473 opts->warn_comments = 1;
6474 else if (!strcmp (argv[i], "-Wno-comment"))
6475 opts->warn_comments = 0;
6476 else if (!strcmp (argv[i], "-Wcomments"))
6477 opts->warn_comments = 1;
6478 else if (!strcmp (argv[i], "-Wno-comments"))
6479 opts->warn_comments = 0;
6480 else if (!strcmp (argv[i], "-Wtraditional"))
6481 opts->warn_stringify = 1;
6482 else if (!strcmp (argv[i], "-Wno-traditional"))
6483 opts->warn_stringify = 0;
6484 else if (!strcmp (argv[i], "-Wimport"))
6485 opts->warn_import = 1;
6486 else if (!strcmp (argv[i], "-Wno-import"))
6487 opts->warn_import = 0;
6488 else if (!strcmp (argv[i], "-Werror"))
6489 opts->warnings_are_errors = 1;
6490 else if (!strcmp (argv[i], "-Wno-error"))
6491 opts->warnings_are_errors = 0;
6492 else if (!strcmp (argv[i], "-Wall"))
6494 opts->warn_trigraphs = 1;
6495 opts->warn_comments = 1;
6500 /* The style of the choices here is a bit mixed.
6501 The chosen scheme is a hybrid of keeping all options in one string
6502 and specifying each option in a separate argument:
6503 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6504 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6505 -M[M][G][D file]. This is awkward to handle in specs, and is not
6507 /* ??? -MG must be specified in addition to one of -M or -MM.
6508 This can be relaxed in the future without breaking anything.
6509 The converse isn't true. */
6511 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6512 if (!strcmp (argv[i], "-MG"))
6514 opts->print_deps_missing_files = 1;
6517 if (!strcmp (argv[i], "-M"))
6518 opts->print_deps = 2;
6519 else if (!strcmp (argv[i], "-MM"))
6520 opts->print_deps = 1;
6521 else if (!strcmp (argv[i], "-MD"))
6522 opts->print_deps = 2;
6523 else if (!strcmp (argv[i], "-MMD"))
6524 opts->print_deps = 1;
6525 /* For -MD and -MMD options, write deps on file named by next arg. */
6526 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6529 fatal ("Filename missing after %s option", argv[i]);
6530 opts->deps_file = argv[++i];
6534 /* For -M and -MM, write deps on standard output
6535 and suppress the usual output. */
6536 opts->no_output = 1;
6542 char *p = argv[i] + 2;
6544 while ((c = *p++) != 0) {
6545 /* Arg to -d specifies what parts of macros to dump */
6548 opts->dump_macros = dump_only;
6549 opts->no_output = 1;
6552 opts->dump_macros = dump_names;
6555 opts->dump_macros = dump_definitions;
6563 if (argv[i][2] == '3')
6564 opts->debug_output = 1;
6568 fprintf (stderr, "GNU CPP version %s", version_string);
6569 #ifdef TARGET_VERSION
6572 fprintf (stderr, "\n");
6573 // opts->verbose = 1;
6577 opts->print_include_names = 1;
6581 if (argv[i][2] != 0)
6582 push_pending (pfile, "-D", argv[i] + 2);
6583 else if (i + 1 == argc)
6584 fatal ("Macro name missing after -D option");
6586 i++, push_pending (pfile, "-D", argv[i]);
6593 if (argv[i][2] != 0)
6601 fatal ("Assertion missing after -A option");
6606 if (!strcmp (p, "-")) {
6607 struct cpp_pending **ptr;
6608 /* -A- eliminates all predefined macros and assertions.
6609 Let's include also any that were specified earlier
6610 on the command line. That way we can get rid of any
6611 that were passed automatically in from GCC. */
6613 opts->inhibit_predefs = 1;
6614 for (ptr = &opts->pending; *ptr != NULL; )
6616 struct cpp_pending *pend = *ptr;
6617 if (pend->cmd && pend->cmd[0] == '-'
6618 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6627 push_pending (pfile, "-A", p);
6632 case 'U': /* JF #undef something */
6633 if (argv[i][2] != 0)
6634 push_pending (pfile, "-U", argv[i] + 2);
6635 else if (i + 1 == argc)
6636 fatal ("Macro name missing after -U option");
6638 push_pending (pfile, "-U", argv[i+1]), i++;
6642 opts->put_out_comments = 1;
6645 case 'E': /* -E comes from cc -E; ignore it. */
6649 opts->no_line_commands = 1;
6652 case '$': /* Don't include $ in identifiers. */
6653 opts->dollars_in_ident = 0;
6656 case 'I': /* Add directory to path for includes. */
6658 struct file_name_list *dirtmp;
6660 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6661 && !strcmp (argv[i] + 2, "-")) {
6662 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6663 /* Don't use any preceding -I directories for #include <...>. */
6664 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6667 dirtmp = (struct file_name_list *)
6668 Safe_malloc (sizeof (struct file_name_list));
6669 dirtmp->next = 0; /* New one goes on the end */
6670 dirtmp->control_macro = 0;
6671 dirtmp->c_system_include_path = 0;
6672 if (argv[i][2] != 0)
6673 dirtmp->fname = argv[i] + 2;
6674 else if (i + 1 == argc)
6675 fatal ("Directory name missing after -I option");
6677 dirtmp->fname = argv[++i];
6678 dirtmp->got_name_map = 0;
6679 append_include_chain (pfile, dirtmp, dirtmp);
6685 if (!strcmp (argv[i], "-nostdinc"))
6686 /* -nostdinc causes no default include directories.
6687 You must specify all include-file directories with -I. */
6688 opts->no_standard_includes = 1;
6689 else if (!strcmp (argv[i], "-nostdinc++"))
6690 /* -nostdinc++ causes no default C++-specific include directories. */
6691 opts->no_standard_cplusplus_includes = 1;
6693 else if (!strcmp (argv[i], "-noprecomp"))
6699 /* Sun compiler passes undocumented switch "-undef".
6700 Let's assume it means to inhibit the predefined symbols. */
6701 opts->inhibit_predefs = 1;
6704 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6705 if (opts->in_fname == NULL) {
6706 opts->in_fname = "";
6708 } else if (opts->out_fname == NULL) {
6709 opts->out_fname = "";
6711 } /* else fall through into error */
6725 struct cpp_options *opts = CPP_OPTIONS (pfile);
6727 if (opts->print_deps)
6729 /* Stream on which to print the dependency information. */
6732 /* Don't actually write the deps file if compilation has failed. */
6733 if (pfile->errors == 0)
6735 char *deps_mode = opts->print_deps_append ? "a" : "w";
6736 if (opts->deps_file == 0)
6737 deps_stream = stdout;
6738 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6739 cpp_pfatal_with_name (pfile, opts->deps_file);
6740 fputs (pfile->deps_buffer, deps_stream);
6741 putc ('\n', deps_stream);
6742 if (opts->deps_file)
6744 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6745 fatal ("I/O error on output");
6751 /* Free resources used by PFILE. */
6758 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6759 cpp_pop_buffer (pfile);
6761 if (pfile->token_buffer)
6763 free (pfile->token_buffer);
6764 pfile->token_buffer = NULL;
6767 if (pfile->deps_buffer)
6769 free (pfile->deps_buffer);
6770 pfile->deps_buffer = NULL;
6771 pfile->deps_allocated_size = 0;
6774 while (pfile->if_stack)
6776 IF_STACK_FRAME *temp = pfile->if_stack;
6777 pfile->if_stack = temp->next;
6781 while (pfile->dont_repeat_files)
6783 struct file_name_list *temp = pfile->dont_repeat_files;
6784 pfile->dont_repeat_files = temp->next;
6789 while (pfile->all_include_files)
6791 struct file_name_list *temp = pfile->all_include_files;
6792 pfile->all_include_files = temp->next;
6797 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6799 register struct import_file *imp = pfile->import_hash_table[i];
6802 struct import_file *next = imp->next;
6807 pfile->import_hash_table[i] = 0;
6810 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6812 while (pfile->assertion_hashtab[i])
6813 delete_assertion (pfile->assertion_hashtab[i]);
6816 cpp_hash_cleanup (pfile);
6822 struct directive *keyword,
6823 U_CHAR *buf, U_CHAR *limit)
6825 long symstart; /* remember where symbol name starts */
6827 int sym_length; /* and how long it is */
6828 struct arglist *tokens = NULL;
6830 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6831 && !CPP_BUFFER (pfile)->system_header_p)
6832 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6834 cpp_skip_hspace (pfile);
6835 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6836 parse_name (pfile, GETC());
6837 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6840 cpp_skip_hspace (pfile);
6841 if (PEEKC() != '(') {
6842 cpp_error (pfile, "missing token-sequence in `#assert'");
6848 tokens = read_token_list (pfile, &error_flag);
6852 cpp_error (pfile, "empty token-sequence in `#assert'");
6855 cpp_skip_hspace (pfile);
6857 if (c != EOF && c != '\n')
6858 cpp_pedwarn (pfile, "junk at end of `#assert'");
6859 skip_rest_of_line (pfile);
6862 /* If this name isn't already an assertion name, make it one.
6863 Error if it was already in use in some other way. */
6866 ASSERTION_HASHNODE *hp;
6867 U_CHAR *symname = pfile->token_buffer + symstart;
6868 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6869 struct tokenlist_list *value
6870 = (struct tokenlist_list *) Safe_malloc (sizeof (struct tokenlist_list));
6872 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6874 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6875 cpp_error (pfile, "`defined' redefined as assertion");
6876 hp = assertion_install (pfile, symname, sym_length, hashcode);
6879 /* Add the spec'd token-sequence to the list of such. */
6880 value->tokens = tokens;
6881 value->next = hp->value;
6884 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6887 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6888 skip_rest_of_line (pfile);
6895 struct directive *keyword,
6896 U_CHAR *buf, U_CHAR *limit)
6898 long symstart; /* remember where symbol name starts */
6899 int sym_length; /* and how long it is */
6902 struct arglist *tokens = NULL;
6903 int tokens_specified = 0;
6905 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6906 && !CPP_BUFFER (pfile)->system_header_p)
6907 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6909 cpp_skip_hspace (pfile);
6911 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6912 parse_name (pfile, GETC());
6913 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6916 cpp_skip_hspace (pfile);
6917 if (PEEKC() == '(') {
6920 tokens = read_token_list (pfile, &error_flag);
6924 cpp_error (pfile, "empty token list in `#unassert'");
6928 tokens_specified = 1;
6931 cpp_skip_hspace (pfile);
6933 if (c != EOF && c != '\n')
6934 cpp_error (pfile, "junk at end of `#unassert'");
6935 skip_rest_of_line (pfile);
6938 ASSERTION_HASHNODE *hp;
6939 U_CHAR *symname = pfile->token_buffer + symstart;
6940 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6941 struct tokenlist_list *tail, *prev;
6943 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6947 /* If no token list was specified, then eliminate this assertion
6949 if (! tokens_specified)
6950 delete_assertion (hp);
6952 /* If a list of tokens was given, then delete any matching list. */
6957 struct tokenlist_list *next = tail->next;
6958 if (compare_token_lists (tail->tokens, tokens)) {
6962 hp->value = tail->next;
6963 free_token_list (tail->tokens);
6973 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6976 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6977 skip_rest_of_line (pfile);
6981 /* Test whether there is an assertion named NAME
6982 and optionally whether it has an asserted token list TOKENS.
6983 NAME is not null terminated; its length is SYM_LENGTH.
6984 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6991 int tokens_specified,
6992 struct arglist *tokens)
6994 ASSERTION_HASHNODE *hp;
6995 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6997 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6998 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7000 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7002 /* It is not an assertion; just return false. */
7005 /* If no token list was specified, then value is 1. */
7006 if (! tokens_specified)
7010 struct tokenlist_list *tail;
7014 /* If a list of tokens was given,
7015 then succeed if the assertion records a matching list. */
7018 if (compare_token_lists (tail->tokens, tokens))
7023 /* Fail if the assertion has no matching list. */
7028 /* Compare two lists of tokens for equality including order of tokens. */
7031 compare_token_lists (
7032 struct arglist *l1, struct arglist *l2 )
7035 if (l1->length != l2->length)
7037 if (strncmp (l1->name, l2->name, l1->length))
7043 /* Succeed if both lists end at the same time. */
7048 reverse_token_list (
7049 struct arglist *tokens)
7051 register struct arglist *prev = 0, *this, *next;
7052 for (this = tokens; this; this = next)
7061 /* Read a space-separated list of tokens ending in a close parenthesis.
7062 Return a list of strings, in the order they were written.
7063 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7065 static struct arglist *
7070 struct arglist *token_ptrs = 0;
7075 FORWARD (1); /* Skip '(' */
7077 /* Loop over the assertion value tokens. */
7080 struct arglist *temp;
7081 long name_written = CPP_WRITTEN (pfile);
7084 cpp_skip_hspace (pfile);
7088 /* Find the end of the token. */
7091 CPP_PUTC (pfile, c);
7099 CPP_PUTC (pfile, c);
7101 else if (c == '"' || c == '\'')
7104 cpp_get_token (pfile);
7110 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7111 && c != '"' && c != '\'')
7113 CPP_PUTC (pfile, c);
7116 if (c != EOF) FORWARD(-1);
7119 length = CPP_WRITTEN (pfile) - name_written;
7120 temp = (struct arglist *)
7121 Safe_malloc (sizeof (struct arglist) + length + 1);
7122 temp->name = (U_CHAR *) (temp + 1);
7123 bcopy ((char *) (pfile->token_buffer + name_written),
7124 (char *) temp->name, length);
7125 temp->name[length] = 0;
7126 temp->next = token_ptrs;
7128 temp->length = length;
7130 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7132 if (c == EOF || c == '\n')
7135 "unterminated token sequence following `#' operator");
7140 /* We accumulated the names in reverse order.
7141 Now reverse them to get the proper order. */
7142 return reverse_token_list (token_ptrs);
7147 struct arglist *tokens)
7150 struct arglist *next = tokens->next;
7151 free (tokens->name);
7157 /* Get the file-mode and data size of the file open on FD
7158 and store them in *MODE_POINTER and *SIZE_POINTER. */
7161 file_size_and_mode (
7164 long int *size_pointer)
7168 if (fstat (fd, &sbuf) < 0) return (-1);
7169 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7170 if (size_pointer) *size_pointer = sbuf.st_size;
7174 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7175 retrying if necessary. Return a negative value if an error occurs,
7176 otherwise return the actual number of bytes read,
7177 which must be LEN unless end-of-file was reached. */
7187 int nchars = read (desc, ptr, left);
7208 unsigned size = strlen (input);
7209 char *output = Safe_malloc (size + 1);
7210 strcpy (output, input);
7214 /* Initialize PMARK to remember the current position of PFILE. */
7217 struct parse_marker *pmark,
7220 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7221 pmark->next = pbuf->marks;
7222 pbuf->marks = pmark;
7224 pmark->position = pbuf->cur - pbuf->buf;
7227 /* Cleanup PMARK - we no longer need it. */
7230 struct parse_marker *pmark)
7232 struct parse_marker **pp = &pmark->buf->marks;
7233 for (; ; pp = &(*pp)->next) {
7234 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7235 if (*pp == pmark) break;
7240 /* Backup the current position of PFILE to that saved in PMARK. */
7244 struct parse_marker *pmark,
7247 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7248 if (pbuf != pmark->buf)
7249 fatal ("internal error %s", "parse_goto_mark");
7250 pbuf->cur = pbuf->buf + pmark->position;
7253 /* Reset PMARK to point to the current position of PFILE. (Same
7254 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7258 struct parse_marker *pmark,
7261 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7262 if (pbuf != pmark->buf)
7263 fatal ("internal error %s", "parse_move_mark");
7264 pmark->position = pbuf->cur - pbuf->buf;
7268 cpp_read_check_assertion (
7271 int name_start = CPP_WRITTEN (pfile);
7272 int name_length, name_written;
7274 FORWARD (1); /* Skip '#' */
7275 cpp_skip_hspace (pfile);
7276 parse_name (pfile, GETC ());
7277 name_written = CPP_WRITTEN (pfile);
7278 name_length = name_written - name_start;
7279 cpp_skip_hspace (pfile);
7280 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7283 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7284 result = check_assertion (pfile,
7285 pfile->token_buffer + name_start, name_length,
7289 result = check_assertion (pfile,
7290 pfile->token_buffer + name_start, name_length,
7291 0, (struct arglist *)NULL_PTR);
7292 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7297 cpp_print_file_and_line (pfile)
7300 cpp_buffer *ip = cpp_file_buffer (pfile);
7305 cpp_buf_line_and_col (ip, &line, &col);
7306 cpp_file_line_for_message (pfile, ip->nominal_fname,
7307 line, pfile->show_column ? col : -1);
7312 cpp_error (pfile, msg, arg1, arg2, arg3)
7315 char *arg1, *arg2, *arg3;
7317 cpp_print_containing_files (pfile);
7318 cpp_print_file_and_line (pfile);
7319 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7322 /* Print error message but don't count it. */
7325 cpp_warning (pfile, msg, arg1, arg2, arg3)
7328 char *arg1, *arg2, *arg3;
7330 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7333 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7336 cpp_print_containing_files (pfile);
7337 cpp_print_file_and_line (pfile);
7338 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7341 /* Print an error message and maybe count it. */
7344 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7347 char *arg1, *arg2, *arg3;
7349 if (CPP_OPTIONS (pfile)->pedantic_errors)
7350 cpp_error (pfile, msg, arg1, arg2, arg3);
7352 cpp_warning (pfile, msg, arg1, arg2, arg3);
7356 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7360 char *arg1, *arg2, *arg3;
7362 cpp_buffer *ip = cpp_file_buffer (pfile);
7364 cpp_print_containing_files (pfile);
7367 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7369 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7373 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7377 char *arg1, *arg2, *arg3;
7381 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7384 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7387 cpp_print_containing_files (pfile);
7389 ip = cpp_file_buffer (pfile);
7392 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7394 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7398 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7402 char *arg1, *arg2, *arg3;
7404 if (CPP_OPTIONS (pfile)->pedantic_errors)
7405 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7407 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7410 /* Report a warning (or an error if pedantic_errors)
7411 giving specified file name and line number, not current. */
7414 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7419 char *arg1, *arg2, *arg3;
7421 if (!CPP_OPTIONS (pfile)->pedantic_errors
7422 && CPP_OPTIONS (pfile)->inhibit_warnings)
7425 cpp_file_line_for_message (pfile, file, line, -1);
7426 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7427 msg, arg1, arg2, arg3);
7430 /* This defines "errno" properly for VMS, and gives us EACCES. */
7437 #ifndef HAVE_STRERROR
7438 extern int sys_nerr;
7440 extern const char *const sys_errlist[];
7443 extern char *sys_errlist[];
7446 #else /* HAVE_STRERROR */
7450 char *strerror (int,...);
7454 * my_strerror - return the descriptive text associated with an `errno' code.
7458 my_strerror (errnum)
7464 #ifndef HAVE_STRERROR
7465 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7467 result = strerror (errnum);
7470 /* VAXCRTL's strerror() takes an optional second argument, which only
7471 matters when the first argument is EVMSERR. However, it's simplest
7472 just to pass it unconditionally. `vaxc$errno' is declared in
7473 <errno.h>, and maintained by the library in parallel with `errno'.
7474 We assume that caller's `errnum' either matches the last setting of
7475 `errno' by the library or else does not have the value `EVMSERR'. */
7477 result = strerror (errnum, vaxc$errno);
7481 result = "undocumented I/O error";
7486 /* Error including a message from `errno'. */
7489 cpp_error_from_errno (
7493 cpp_buffer *ip = cpp_file_buffer (pfile);
7495 cpp_print_containing_files (pfile);
7498 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7500 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno),NULL);
7504 cpp_perror_with_name (
7508 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7512 * No pre-compiled header file support.
7514 * Possibly different enum token codes for each C/C++ token.
7516 * Should clean up remaining directives to that do_XXX functions
7517 * only take two arguments and all have command_reads_line.
7519 * Find and cleanup remaining uses of static variables,
7521 * Support for trigraphs.
7523 * Support -dM flag (dump_all_macros).
7525 * Support for_lint flag.