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 ':'
90 // PENDING: Straighten this out into configure
99 #if !defined(_MSC_VER)
102 #include <sys/time.h> /* for __DATE__ and __TIME__ */
103 #include <sys/resource.h>
105 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
106 // #include <sys/times.h>
115 /* This defines "errno" properly for VMS, and gives us EACCES. */
118 extern char *index ();
119 extern char *rindex ();
127 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
128 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
130 /* Find the largest host integer type and set its size and type. */
132 #ifndef HOST_BITS_PER_WIDE_INT
134 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
135 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
136 #define HOST_WIDE_INT long
138 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
139 #define HOST_WIDE_INT int
145 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
149 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
152 /* Define a generic NULL if one hasn't already been defined. */
159 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
160 #define GENERIC_PTR void *
162 #define GENERIC_PTR char *
167 #define NULL_PTR ((GENERIC_PTR)0)
170 #ifndef INCLUDE_LEN_FUDGE
171 #define INCLUDE_LEN_FUDGE 0
174 /* Symbols to predefine. */
176 #ifdef CPP_PREDEFINES
177 static char *predefs = CPP_PREDEFINES;
179 static char *predefs = "";
182 /* We let tm.h override the types used here, to handle trivial differences
183 such as the choice of unsigned int or long unsigned int for size_t.
184 When machines start needing nontrivial differences in the size type,
185 it would be best to do something here to figure out automatically
186 from other information what type to use. */
188 /* The string value for __SIZE_TYPE__. */
191 #define SIZE_TYPE "long unsigned int"
194 /* The string value for __PTRDIFF_TYPE__. */
197 #define PTRDIFF_TYPE "long int"
200 /* The string value for __WCHAR_TYPE__. */
203 #define WCHAR_TYPE "int"
205 #define CPP_WCHAR_TYPE(PFILE) \
206 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
208 /* The string value for __USER_LABEL_PREFIX__ */
210 #ifndef USER_LABEL_PREFIX
211 #define USER_LABEL_PREFIX ""
214 /* The string value for __REGISTER_PREFIX__ */
216 #ifndef REGISTER_PREFIX
217 #define REGISTER_PREFIX ""
221 /* In the definition of a #assert name, this structure forms
222 a list of the individual values asserted.
223 Each value is itself a list of "tokens".
224 These are strings that are compared by name. */
226 struct tokenlist_list {
227 struct tokenlist_list *next;
228 struct arglist *tokens;
231 struct assertion_hashnode {
232 struct assertion_hashnode *next; /* double links for easy deletion */
233 struct assertion_hashnode *prev;
234 /* also, a back pointer to this node's hash
235 chain is kept, in case the node is the head
236 of the chain and gets deleted. */
237 struct assertion_hashnode **bucket_hdr;
238 int length; /* length of token, for quick comparison */
239 U_CHAR *name; /* the actual name */
240 /* List of token-sequences. */
241 struct tokenlist_list *value;
244 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
245 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
247 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
248 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
249 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
250 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
251 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
252 (Note that it is false while we're expanding marco *arguments*.) */
253 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
255 /* Move all backslash-newline pairs out of embarrassing places.
256 Exchange all such pairs following BP
257 with any potentially-embarrassing characters that follow them.
258 Potentially-embarrassing characters are / and *
259 (because a backslash-newline inside a comment delimiter
260 would cause it not to be recognized). */
262 #define NEWLINE_FIX \
263 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
265 /* Same, but assume we've already read the potential '\\' into C. */
266 #define NEWLINE_FIX1(C) do { \
267 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
271 struct cpp_pending *next;
276 /* Forward declarations. */
278 //extern char *Safe_malloc ();
280 static void add_import ();
281 static void append_include_chain ();
282 static void make_undef ();
283 static void make_assertion ();
284 static void path_include ();
285 static void initialize_builtins ();
286 static void initialize_char_syntax ();
287 static void dump_arg_n ();
288 static void dump_defn_1 ();
289 extern void delete_macro ();
290 static void trigraph_pcp ();
291 static int finclude ();
292 static void validate_else ();
293 static int comp_def_part ();
294 extern void fancy_abort ();
295 static void pipe_closed ();
296 static void print_containing_files ();
297 static int lookup_import ();
298 static int redundant_include_p ();
299 static is_system_include ();
300 static struct file_name_map *read_name_map ();
301 static char *read_filename_string ();
302 static int open_include_file ();
303 static int check_preconditions ();
304 static void pcfinclude ();
305 static void pcstring_used ();
306 static int check_macro_name ();
307 static int compare_defs ();
308 static int compare_token_lists ();
309 static HOST_WIDE_INT eval_if_expression ();
310 static int change_newlines ();
312 static int file_size_and_mode ();
313 static struct arglist *read_token_list ();
314 static void free_token_list ();
315 static int safe_read ();
316 static void push_macro_expansion PARAMS ((cpp_reader *,
317 U_CHAR*, int, HASHNODE*));
318 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
319 //extern char *xrealloc ();
320 //extern char *xcalloc ();
321 static char *savestring ();
323 static void conditional_skip ();
324 static void skip_if_group ();
326 /* Last arg to output_line_command. */
327 enum file_change_code {same_file, enter_file, leave_file};
329 /* External declarations. */
331 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
333 extern char *getenv ();
334 extern FILE *fdopen ();
335 extern char *version_string;
336 extern struct tm *localtime ();
338 /* These functions are declared to return int instead of void since they
339 are going to be placed in a table and some old compilers have trouble with
340 pointers to functions returning void. */
342 static int do_define ();
343 static int do_line ();
344 static int do_include ();
345 static int do_undef ();
346 static int do_error ();
347 static int do_pragma ();
348 static int do_ident ();
350 static int do_xifdef ();
351 static int do_else ();
352 static int do_elif ();
353 static int do_endif ();
354 static int do_sccs ();
355 static int do_once ();
356 static int do_assert ();
357 static int do_unassert ();
358 static int do_warning ();
360 struct file_name_list
362 struct file_name_list *next;
364 /* If the following is nonzero, it is a macro name.
365 Don't include the file again if that macro is defined. */
366 U_CHAR *control_macro;
367 /* If the following is nonzero, it is a C-language system include
369 int c_system_include_path;
370 /* Mapping of file names for this directory. */
371 struct file_name_map *name_map;
372 /* Non-zero if name_map is valid. */
376 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
377 via the same directory as the file that #included it. */
378 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
380 /* #include "file" looks in source file dir, then stack. */
381 /* #include <file> just looks in the stack. */
382 /* -I directories are added to the end, then the defaults are added. */
384 static struct default_include {
385 char *fname; /* The name of the directory. */
386 int cplusplus; /* Only look here if we're compiling C++. */
387 int cxx_aware; /* Includes in this directory don't need to
388 be wrapped in extern "C" when compiling
390 } include_defaults_array[]
391 #ifdef INCLUDE_DEFAULTS
395 /* Pick up GNU C++ specific include files. */
396 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
398 /* This is the dir for fixincludes. Put it just before
399 the files that we fix. */
400 { GCC_INCLUDE_DIR, 0, 0 },
401 /* For cross-compilation, this dir name is generated
402 automatically in Makefile.in. */
403 { CROSS_INCLUDE_DIR, 0, 0 },
404 /* This is another place that the target system's headers might be. */
405 { TOOL_INCLUDE_DIR, 0, 1 },
406 { LOCAL_INCLUDE_DIR, 0, 1 },
407 #else /* not CROSS_COMPILE */
408 /* This should be /usr/local/include and should come before
409 the fixincludes-fixed header files. */
410 { LOCAL_INCLUDE_DIR, 0, 1 },
411 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
412 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
413 { TOOL_INCLUDE_DIR, 0, 1 },
414 /* This is the dir for fixincludes. Put it just before
415 the files that we fix. */
416 { GCC_INCLUDE_DIR, 0, 0 },
417 /* Some systems have an extra dir of include files. */
418 #ifdef SYSTEM_INCLUDE_DIR
419 { SYSTEM_INCLUDE_DIR, 0, 0 },
421 { STANDARD_INCLUDE_DIR, 0, 0 },
422 #endif /* not CROSS_COMPILE */
425 #endif /* no INCLUDE_DEFAULTS */
427 /* `struct directive' defines one #-directive, including how to handle it. */
430 int length; /* Length of name */
431 int (*func)(); /* Function to handle directive */
432 char *name; /* Name of directive */
433 enum node_type type; /* Code which describes which directive. */
434 char command_reads_line; /* One if rest of line is read by func. */
435 char traditional_comments; /* Nonzero: keep comments if -traditional. */
436 char pass_thru; /* Copy preprocessed directive to output file.*/
439 /* Here is the actual list of #-directives, most-often-used first.
440 The initialize_builtins function assumes #define is the very first. */
442 static struct directive directive_table[] = {
443 { 6, do_define, "define", T_DEFINE, 0, 1},
444 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
445 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
446 { 7, do_include, "include", T_INCLUDE, 1},
447 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
448 { 6, do_include, "import", T_IMPORT, 1},
449 { 5, do_endif, "endif", T_ENDIF, 1},
450 { 4, do_else, "else", T_ELSE, 1},
451 { 2, do_if, "if", T_IF, 1},
452 { 4, do_elif, "elif", T_ELIF, 1},
453 { 5, do_undef, "undef", T_UNDEF},
454 { 5, do_error, "error", T_ERROR},
455 { 7, do_warning, "warning", T_WARNING},
456 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
457 { 4, do_line, "line", T_LINE, 1},
458 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
459 #ifdef SCCS_DIRECTIVE
460 { 4, do_sccs, "sccs", T_SCCS},
462 { 6, do_assert, "assert", T_ASSERT, 1},
463 { 8, do_unassert, "unassert", T_UNASSERT, 1},
464 { -1, 0, "", T_UNUSED},
467 /* table to tell if char can be part of a C identifier. */
468 U_CHAR is_idchar[256];
469 /* table to tell if char can be first char of a c identifier. */
470 U_CHAR is_idstart[256];
471 /* table to tell if c is horizontal space. */
472 U_CHAR is_hor_space[256];
473 /* table to tell if c is horizontal or vertical space. */
474 static U_CHAR is_space[256];
476 /* Initialize syntactic classifications of characters. */
479 initialize_char_syntax (
480 struct cpp_options *opts)
485 * Set up is_idchar and is_idstart tables. These should be
486 * faster than saying (is_alpha (c) || c == '_'), etc.
487 * Set up these things before calling any routines tthat
490 for (i = 'a'; i <= 'z'; i++) {
491 is_idchar[i - 'a' + 'A'] = 1;
493 is_idstart[i - 'a' + 'A'] = 1;
496 for (i = '0'; i <= '9'; i++)
500 is_idchar['$'] = opts->dollars_in_ident;
501 is_idstart['$'] = opts->dollars_in_ident;
503 /* horizontal space table */
504 is_hor_space[' '] = 1;
505 is_hor_space['\t'] = 1;
506 is_hor_space['\v'] = 1;
507 is_hor_space['\f'] = 1;
508 is_hor_space['\r'] = 1;
519 /* Place into PFILE a quoted string representing the string SRC.
520 Caller must reserve enough space in pfile->token_buffer. */
528 CPP_PUTC_Q (pfile, '\"');
530 switch ((c = *src++))
534 CPP_PUTC_Q (pfile, c);
537 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
538 CPP_ADJUST_WRITTEN (pfile, 4);
544 CPP_PUTC_Q (pfile, '\\');
545 CPP_PUTC_Q (pfile, c);
549 CPP_PUTC_Q (pfile, '\"');
550 CPP_NUL_TERMINATE_Q (pfile);
555 /* Make sure PFILE->token_buffer will hold at least N more chars. */
562 long old_written = CPP_WRITTEN (pfile);
563 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
564 pfile->token_buffer = (U_CHAR*) Safe_realloc(pfile->token_buffer, pfile->token_buffer_size);
565 CPP_SET_WRITTEN (pfile, old_written);
570 * process a given definition string, for initialization
571 * If STR is just an identifier, define it with value 1.
572 * If STR has anything after the identifier, then it should
573 * be identifier=definition.
587 cpp_error (pfile, "malformed option `-D %s'", str);
590 while (is_idchar[*++p])
594 buf = (U_CHAR *) alloca (p - buf + 4);
595 strcpy ((char *)buf, str);
596 strcat ((char *)buf, " 1");
600 cpp_error (pfile, "malformed option `-D %s'", str);
606 /* Copy the entire option so we can modify it. */
607 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
608 strncpy (buf, str, p - str);
609 /* Change the = to a space. */
611 /* Scan for any backslash-newline and remove it. */
616 if (*p == '\\' && p[1] == '\n')
624 do_define (pfile, NULL, buf, buf + strlen (buf));
627 /* Process the string STR as if it appeared as the body of a #assert.
628 OPTION is the option name for which STR was the argument. */
637 /* struct directive *kt; */
638 U_CHAR *buf, *p /*, *q */ ;
640 /* Copy the entire option so we can modify it. */
641 buf = (U_CHAR *) alloca (strlen (str) + 1);
642 strcpy ((char *) buf, str);
645 /* Scan for any backslash-newline and remove it. */
648 if (*p == '\\' && p[1] == '\n')
657 if (!is_idstart[*p]) {
658 cpp_error (pfile, "malformed option `%s %s'", option, str);
661 while (is_idchar[*++p])
663 while (*p == ' ' || *p == '\t') p++;
664 if (! (*p == 0 || *p == '(')) {
665 cpp_error (pfile, "malformed option `%s %s'", option, str);
669 ip = cpp_push_buffer (pfile, buf, strlen (buf));
670 do_assert (pfile, NULL, NULL, NULL);
671 cpp_pop_buffer (pfile);
674 /* Append a chain of `struct file_name_list's
675 to the end of the main include chain.
676 FIRST is the beginning of the chain to append, and LAST is the end. */
679 append_include_chain (
681 struct file_name_list *first,struct file_name_list *last)
683 struct cpp_options *opts = CPP_OPTIONS (pfile);
684 struct file_name_list *dir;
689 if (opts->include == 0)
690 opts->include = first;
692 opts->last_include->next = first;
694 if (opts->first_bracket_include == 0)
695 opts->first_bracket_include = first;
697 for (dir = first; ; dir = dir->next) {
698 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
699 if (len > pfile->max_include_len)
700 pfile->max_include_len = len;
706 opts->last_include = last;
709 /* Add output to `deps_buffer' for the -M switch.
710 STRING points to the text to be output.
711 SPACER is ':' for targets, ' ' for dependencies, zero for text
712 to be inserted literally. */
720 int size = strlen (string);
725 #ifndef MAX_OUTPUT_COLUMNS
726 #define MAX_OUTPUT_COLUMNS 72
729 && pfile->deps_column > 0
730 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
732 deps_output (pfile, " \\\n ", 0);
733 pfile->deps_column = 0;
736 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
738 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
739 pfile->deps_buffer = (char *) Safe_realloc(pfile->deps_buffer,
740 pfile->deps_allocated_size);
742 if (spacer == ' ' && pfile->deps_column > 0)
743 pfile->deps_buffer[pfile->deps_size++] = ' ';
744 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
745 pfile->deps_size += size;
746 pfile->deps_column += size;
748 pfile->deps_buffer[pfile->deps_size++] = ':';
749 pfile->deps_buffer[pfile->deps_size] = 0;
752 /* Given a colon-separated list of file names PATH,
753 add all the names to the search path for include files. */
768 struct file_name_list *dirtmp;
770 /* Find the end of this name. */
771 while (*q != 0 && *q != PATH_SEPARATOR) q++;
773 /* An empty name in the path stands for the current directory. */
774 name = (char *) Safe_malloc (2);
778 /* Otherwise use the directory that is named. */
779 name = (char *) Safe_malloc (q - p + 1);
780 bcopy (p, name, q - p);
784 dirtmp = (struct file_name_list *)
785 Safe_malloc (sizeof (struct file_name_list));
786 dirtmp->next = 0; /* New one goes on the end */
787 dirtmp->control_macro = 0;
788 dirtmp->c_system_include_path = 0;
789 dirtmp->fname = name;
790 dirtmp->got_name_map = 0;
791 append_include_chain (pfile, dirtmp, dirtmp);
793 /* Advance past this name. */
797 /* Skip the colon. */
804 struct cpp_options *opts)
806 bzero ((char *) opts, sizeof *opts);
807 opts->in_fname = NULL;
808 opts->out_fname = NULL;
810 /* Initialize is_idchar to allow $. */
811 opts->dollars_in_ident = 1;
812 initialize_char_syntax (opts);
813 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
815 opts->no_line_commands = 0;
816 opts->no_trigraphs = 1;
817 opts->put_out_comments = 0;
818 opts->print_include_names = 0;
819 opts->dump_macros = dump_none;
822 opts->cplusplus_comments = 0;
829 opts->pedantic_errors = 0;
830 opts->inhibit_warnings = 0;
831 opts->warn_comments = 0;
832 opts->warn_import = 1;
833 opts->warnings_are_errors = 0;
856 HASHNODE *macro = (HASHNODE*)pbuf->data;
857 if (macro->type == T_DISABLED)
858 macro->type = T_MACRO;
859 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
884 register U_CHAR *p = bp;
886 /* First count the backslash-newline pairs here. */
888 while (p[0] == '\\' && p[1] == '\n')
891 /* What follows the backslash-newlines is not embarrassing. */
893 if (*p != '/' && *p != '*')
896 /* Copy all potentially embarrassing characters
897 that follow the backslash-newline pairs
898 down to where the pairs originally started. */
900 while (*p == '*' || *p == '/')
903 /* Now write the same number of pairs after the embarrassing chars. */
911 /* Assuming we have read '/'.
912 If this is the start of a comment (followed by '*' or '/'),
913 skip to the end of the comment, and return ' '.
914 Return EOF if we reached the end of file before the end of the comment.
915 If not the start of a comment, return '/'. */
923 while (PEEKC() == '\\' && PEEKN(1) == '\n')
938 while (c == '\\' && PEEKC() == '\n')
942 FORWARD(1), c = GETC();
944 if (prev_c == '*' && c == '/')
946 if (c == '\n' && linep)
950 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
957 return ' '; /* Allow // to be terminated by EOF. */
958 while (c == '\\' && PEEKC() == '\n')
967 /* Don't consider final '\n' to be part of comment. */
977 /* Skip whitespace \-newline and comments. Does not macro-expand. */
989 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
990 cpp_pedwarn (pfile, "%s in preprocessing directive",
991 c == '\f' ? "formfeed" : "vertical tab");
997 c = skip_comment (pfile, NULL);
1000 if (c == EOF || c == '/')
1003 else if (c == '\\' && PEEKN(1) == '\n') {
1006 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
1007 && is_hor_space[PEEKN(1)])
1013 /* Read the rest of the current line.
1014 The line is appended to PFILE's output buffer. */
1020 struct cpp_options *opts = CPP_OPTIONS (pfile);
1030 if (PEEKC() == '\n')
1037 goto scan_directive_token;
1041 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
1042 goto scan_directive_token;
1046 if (CPP_PEDANTIC (pfile))
1047 cpp_pedwarn (pfile, "%s in preprocessing directive",
1048 c == '\f' ? "formfeed" : "vertical tab");
1054 scan_directive_token:
1056 cpp_get_token (pfile);
1059 CPP_PUTC (pfile, c);
1062 CPP_NUL_TERMINATE (pfile);
1069 long old = CPP_WRITTEN (pfile);
1070 copy_rest_of_line (pfile);
1071 CPP_SET_WRITTEN (pfile, old);
1074 /* Handle a possible # directive.
1075 '#' has already been read. */
1081 register struct directive *kt;
1084 U_CHAR *ident, *line_end;
1085 long old_written = CPP_WRITTEN (pfile);
1087 cpp_skip_hspace (pfile);
1090 if (c >= '0' && c <= '9')
1092 /* Handle # followed by a line number. */
1093 if (CPP_PEDANTIC (pfile))
1094 cpp_pedwarn (pfile, "`#' followed by integer");
1095 do_line (pfile, NULL);
1096 goto done_a_directive;
1099 /* Now find the directive name. */
1100 CPP_PUTC (pfile, '#');
1101 parse_name (pfile, GETC());
1102 ident = pfile->token_buffer + old_written + 1;
1103 ident_length = CPP_PWRITTEN (pfile) - ident;
1104 if (ident_length == 0 && PEEKC() == '\n')
1106 /* A line of just `#' becomes blank. */
1107 goto done_a_directive;
1111 if (ident_length == 0 || !is_idstart[*ident]) {
1113 while (is_idchar[*p]) {
1114 if (*p < '0' || *p > '9')
1118 /* Avoid error for `###' and similar cases unless -pedantic. */
1120 while (*p == '#' || is_hor_space[*p]) p++;
1122 if (pedantic && !lang_asm)
1123 cpp_warning (pfile, "invalid preprocessor directive");
1129 cpp_error (pfile, "invalid preprocessor directive name");
1135 * Decode the keyword and call the appropriate expansion
1136 * routine, after moving the input pointer up to the next line.
1138 for (kt = directive_table; ; kt++) {
1139 if (kt->length <= 0)
1140 goto not_a_directive;
1141 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1145 if (! kt->command_reads_line)
1147 /* Nonzero means do not delete comments within the directive.
1148 #define needs this when -traditional. */
1149 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1150 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1151 CPP_OPTIONS (pfile)->put_out_comments = comments;
1152 after_ident = CPP_WRITTEN (pfile);
1153 copy_rest_of_line (pfile);
1154 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1157 /* For #pragma and #define, we may want to pass through the directive.
1158 Other directives may create output, but we don't want the directive
1159 itself out, so we pop it now. For example #include may write a
1160 command (see comment in do_include), and conditionals may emit
1161 #failed ... #endfailed stuff. But note that popping the buffer
1162 means the parameters to kt->func may point after pfile->limit
1163 so these parameters are invalid as soon as something gets appended
1164 to the token_buffer. */
1166 line_end = CPP_PWRITTEN (pfile);
1167 if (!kt->pass_thru && kt->type != T_DEFINE)
1168 CPP_SET_WRITTEN (pfile, old_written);
1170 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1172 || (kt->type == T_DEFINE
1173 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1175 /* Just leave the entire #define in the output stack. */
1177 else if (kt->type == T_DEFINE
1178 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1180 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1181 SKIP_WHITE_SPACE (p);
1182 while (is_idchar[*p]) p++;
1184 CPP_PUTC (pfile, '\n');
1186 else if (kt->type == T_DEFINE)
1187 CPP_SET_WRITTEN (pfile, old_written);
1195 /* Pass a directive through to the output file.
1196 BUF points to the contents of the directive, as a contiguous string.
1197 LIMIT points to the first character past the end of the directive.
1198 KEYWORD is the keyword-table entry for the directive. */
1201 pass_thru_directive (
1202 U_CHAR *buf, U_CHAR *limit,
1204 struct directive *keyword)
1206 register unsigned keyword_length = keyword->length;
1208 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1209 CPP_PUTC_Q (pfile, '#');
1210 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1211 if (limit != buf && buf[0] != ' ')
1212 CPP_PUTC_Q (pfile, ' ');
1213 CPP_PUTS_Q (pfile, buf, limit - buf);
1215 CPP_PUTS_Q (pfile, '\n');
1216 /* Count the line we have just made in the output,
1217 to get in sync properly. */
1222 /* The arglist structure is built by do_define to tell
1223 collect_definition where the argument names begin. That
1224 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1225 would contain pointers to the strings x, y, and z.
1226 Collect_definition would then build a DEFINITION node,
1227 with reflist nodes pointing to the places x, y, and z had
1228 appeared. So the arglist is just convenience data passed
1229 between these two routines. It is not kept around after
1230 the current #define has been processed and entered into the
1234 struct arglist *next;
1241 /* Read a replacement list for a macro with parameters.
1242 Build the DEFINITION structure.
1243 Reads characters of text starting at BUF until END.
1244 ARGLIST specifies the formal parameters to look for
1245 in the text of the definition; NARGS is the number of args
1246 in that list, or -1 for a macro name that wants no argument list.
1247 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1248 and NAMELEN is its length in characters.
1250 Note that comments, backslash-newlines, and leading white space
1251 have already been deleted from the argument. */
1256 U_CHAR *buf, U_CHAR *limit,
1258 struct arglist *arglist)
1261 register U_CHAR *p, *lastp, *exp_p;
1262 struct reflist *endpat = NULL;
1263 /* Pointer to first nonspace after last ## seen. */
1265 /* Pointer to first nonspace after last single-# seen. */
1266 U_CHAR *stringify = 0;
1268 int expected_delimiter = '\0';
1270 /* Scan thru the replacement list, ignoring comments and quoted
1271 strings, picking up on the macro calls. It does a linear search
1272 thru the arg list on every potential symbol. Profiling might say
1273 that something smarter should happen. */
1278 /* Find the beginning of the trailing whitespace. */
1280 while (p < limit && is_space[limit[-1]]) limit--;
1282 /* Allocate space for the text in the macro definition.
1283 Leading and trailing whitespace chars need 2 bytes each.
1284 Each other input char may or may not need 1 byte,
1285 so this is an upper bound. The extra 5 are for invented
1286 leading and trailing newline-marker and final null. */
1287 maxsize = (sizeof (DEFINITION)
1289 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1293 defn = (DEFINITION *) Safe_calloc(1,maxsize);
1295 defn->nargs = nargs;
1296 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1301 /* Add one initial space escape-marker to prevent accidental
1302 token-pasting (often removed by macroexpand). */
1306 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1307 cpp_error (pfile, "`##' at start of macro definition");
1311 /* Process the main body of the definition. */
1313 int skipped_arg = 0;
1314 register U_CHAR c = *p++;
1318 if (!CPP_TRADITIONAL (pfile)) {
1322 if (expected_delimiter != '\0') {
1323 if (c == expected_delimiter)
1324 expected_delimiter = '\0';
1326 expected_delimiter = c;
1330 if (p < limit && expected_delimiter) {
1331 /* In a string, backslash goes through
1332 and makes next char ordinary. */
1338 /* An '@' in a string or character constant stands for itself,
1339 and does not need to be escaped. */
1340 if (!expected_delimiter)
1345 /* # is ordinary inside a string. */
1346 if (expected_delimiter)
1348 if (p < limit && *p == '#') {
1349 /* ##: concatenate preceding and following tokens. */
1350 /* Take out the first #, discard preceding whitespace. */
1352 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1354 /* Skip the second #. */
1356 /* Discard following whitespace. */
1357 SKIP_WHITE_SPACE (p);
1360 cpp_error (pfile, "`##' at end of macro definition");
1361 } else if (nargs >= 0) {
1362 /* Single #: stringify following argument ref.
1363 Don't leave the # in the expansion. */
1365 SKIP_WHITE_SPACE (p);
1366 if (p == limit || ! is_idstart[*p])
1368 "`#' operator is not followed by a macro argument name");
1375 /* In -traditional mode, recognize arguments inside strings and
1376 and character constants, and ignore special properties of #.
1377 Arguments inside strings are considered "stringified", but no
1378 extra quote marks are supplied. */
1382 if (expected_delimiter != '\0') {
1383 if (c == expected_delimiter)
1384 expected_delimiter = '\0';
1386 expected_delimiter = c;
1390 /* Backslash quotes delimiters and itself, but not macro args. */
1391 if (expected_delimiter != 0 && p < limit
1392 && (*p == expected_delimiter || *p == '\\')) {
1399 if (expected_delimiter != '\0') /* No comments inside strings. */
1402 /* If we find a comment that wasn't removed by handle_directive,
1403 this must be -traditional. So replace the comment with
1407 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1410 /* Mark this as a concatenation-point, as if it had been ##. */
1414 else if (*p == '/') {
1415 /* A c++ comment. Discard to the end of line */
1423 /* Handle the start of a symbol. */
1424 if (is_idchar[c] && nargs > 0) {
1425 U_CHAR *id_beg = p - 1;
1429 while (p != limit && is_idchar[*p]) p++;
1430 id_len = p - id_beg;
1432 if (is_idstart[c]) {
1433 register struct arglist *arg;
1435 for (arg = arglist; arg != NULL; arg = arg->next) {
1436 struct reflist *tpat;
1438 if (arg->name[0] == c
1439 && arg->length == id_len
1440 && strncmp (arg->name, id_beg, id_len) == 0) {
1441 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1442 if (CPP_TRADITIONAL (pfile)) {
1443 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1447 "macro arg `%.*s' would be stringified with -traditional.",
1451 /* If ANSI, don't actually substitute inside a string. */
1452 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1454 /* make a pat node for this arg and append it to the end of
1456 tpat = (struct reflist *) Safe_malloc (sizeof (struct reflist));
1458 tpat->raw_before = concat == id_beg;
1459 tpat->raw_after = 0;
1460 tpat->rest_args = arg->rest_args;
1461 tpat->stringify = (CPP_TRADITIONAL (pfile)
1462 ? expected_delimiter != '\0'
1463 : stringify == id_beg);
1466 defn->pattern = tpat;
1468 endpat->next = tpat;
1471 tpat->argno = arg->argno;
1472 tpat->nchars = exp_p - lastp;
1474 register U_CHAR *p1 = p;
1475 SKIP_WHITE_SPACE (p1);
1476 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1477 tpat->raw_after = 1;
1479 lastp = exp_p; /* place to start copying from next time */
1486 /* If this was not a macro arg, copy it into the expansion. */
1487 if (! skipped_arg) {
1488 register U_CHAR *lim1 = p;
1492 if (stringify == id_beg)
1494 "`#' operator should be followed by a macro argument name");
1499 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1501 /* If ANSI, put in a "@ " marker to prevent token pasting.
1502 But not if "inside a string" (which in ANSI mode
1503 happens only for -D option). */
1510 defn->length = exp_p - defn->expansion;
1512 /* Crash now if we overrun the allocated size. */
1513 if (defn->length + 1 > maxsize)
1517 /* This isn't worth the time it takes. */
1518 /* give back excess storage */
1519 defn->expansion = (U_CHAR *) Safe_realloc(defn->expansion, defn->length + 1);
1526 * special extension string that can be added to the last macro argument to
1527 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1528 * #define wow(a, b...) process (b, a, b)
1529 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1530 * { wow (one, two); } -> { process (two, one, two); }
1531 * if this "rest_arg" is used with the concat token '##' and if it is not
1532 * supplied then the token attached to with ## will not be outputted. Ex:
1533 * #define wow (a, b...) process (b ## , a, ## b)
1534 * { wow (1, 2); } -> { process (2, 1, 2); }
1535 * { wow (one); } -> { process (one); {
1537 static char rest_extension[] = "...";
1538 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1540 /* Create a DEFINITION node from a #define directive. Arguments are
1541 as for do_define. */
1544 U_CHAR *buf, U_CHAR *limit,
1548 U_CHAR *bp; /* temp ptr into input buffer */
1549 U_CHAR *symname; /* remember where symbol name starts */
1550 int sym_length; /* and how long it is */
1553 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1555 int arglengths = 0; /* Accumulate lengths of arg names
1556 plus number of args. */
1558 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1562 while (is_hor_space[*bp])
1565 symname = bp; /* remember where it starts */
1567 sym_length = check_macro_name (pfile, bp, "macro");
1570 /* Lossage will occur if identifiers or control keywords are broken
1571 across lines using backslash. This is not the right place to take
1575 struct arglist *arg_ptrs = NULL;
1578 bp++; /* skip '(' */
1579 SKIP_WHITE_SPACE (bp);
1581 /* Loop over macro argument names. */
1582 while (*bp != ')') {
1583 struct arglist *temp;
1585 temp = (struct arglist *) alloca (sizeof (struct arglist));
1587 temp->next = arg_ptrs;
1588 temp->argno = argno++;
1589 temp->rest_args = 0;
1593 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1595 if (!is_idstart[*bp])
1596 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1598 /* Find the end of the arg name. */
1599 while (is_idchar[*bp]) {
1601 /* do we have a "special" rest-args extension here? */
1602 if (limit - bp > REST_EXTENSION_LENGTH &&
1603 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1605 temp->rest_args = 1;
1609 temp->length = bp - temp->name;
1611 bp += REST_EXTENSION_LENGTH;
1612 arglengths += temp->length + 2;
1613 SKIP_WHITE_SPACE (bp);
1614 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1615 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1620 SKIP_WHITE_SPACE (bp);
1623 cpp_error (pfile, "unterminated parameter list in `#define'");
1627 struct arglist *otemp;
1629 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1630 if (temp->length == otemp->length &&
1631 strncmp (temp->name, otemp->name, temp->length) == 0) {
1634 name = (U_CHAR *) alloca (temp->length + 1);
1635 (void) strncpy (name, temp->name, temp->length);
1636 name[temp->length] = '\0';
1638 "duplicate argument name `%s' in `#define'", name);
1644 ++bp; /* skip paren */
1645 SKIP_WHITE_SPACE (bp);
1646 /* now everything from bp before limit is the definition. */
1647 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1648 defn->rest_args = rest_args;
1650 /* Now set defn->args.argnames to the result of concatenating
1651 the argument names in reverse order
1652 with comma-space between them. */
1653 defn->args.argnames = (U_CHAR *) Safe_malloc (arglengths + 1);
1655 struct arglist *temp;
1657 for (temp = arg_ptrs; temp; temp = temp->next) {
1658 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1660 if (temp->next != 0) {
1661 defn->args.argnames[i++] = ',';
1662 defn->args.argnames[i++] = ' ';
1665 defn->args.argnames[i] = 0;
1668 /* Simple expansion or empty definition. */
1672 if (is_hor_space[*bp]) {
1674 SKIP_WHITE_SPACE (bp);
1677 case '!': case '"': case '#': case '%': case '&': case '\'':
1678 case ')': case '*': case '+': case ',': case '-': case '.':
1679 case '/': case ':': case ';': case '<': case '=': case '>':
1680 case '?': case '[': case '\\': case ']': case '^': case '{':
1681 case '|': case '}': case '~':
1682 cpp_warning (pfile, "missing white space after `#define %.*s'",
1683 sym_length, symname);
1687 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1688 sym_length, symname);
1693 /* now everything from bp before limit is the definition. */
1694 defn = collect_expansion (pfile, bp, limit, -1,(struct arglist *) NULL_PTR);
1695 defn->args.argnames = (U_CHAR *) "";
1701 /* OP is null if this is a predefinition */
1702 defn->predefined = predefinition;
1704 mdef.symnam = symname;
1705 mdef.symlen = sym_length;
1714 /* Check a purported macro name SYMNAME, and yield its length.
1715 USAGE is the kind of name this is intended for. */
1726 for (p = symname; is_idchar[*p]; p++)
1728 sym_length = p - symname;
1729 if (sym_length == 0)
1730 cpp_error (pfile, "invalid %s name", usage);
1731 else if (!is_idstart[*symname]) {
1732 U_CHAR *msg; /* what pain... */
1733 msg = (U_CHAR *) alloca (sym_length + 1);
1734 bcopy (symname, msg, sym_length);
1735 msg[sym_length] = 0;
1736 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1738 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1739 cpp_error (pfile, "invalid %s name `defined'", usage);
1745 * return zero if two DEFINITIONs are isomorphic
1749 DEFINITION *d1, DEFINITION *d2)
1751 register struct reflist *a1, *a2;
1752 register U_CHAR *p1 = d1->expansion;
1753 register U_CHAR *p2 = d2->expansion;
1756 if (d1->nargs != d2->nargs)
1758 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1760 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1761 a1 = a1->next, a2 = a2->next) {
1762 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1763 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1764 || a1->argno != a2->argno
1765 || a1->stringify != a2->stringify
1766 || a1->raw_before != a2->raw_before
1767 || a1->raw_after != a2->raw_after)
1775 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1776 p2, d2->length - (p2 - d2->expansion), 1))
1781 /* Return 1 if two parts of two macro definitions are effectively different.
1782 One of the parts starts at BEG1 and has LEN1 chars;
1783 the other has LEN2 chars at BEG2.
1784 Any sequence of whitespace matches any other sequence of whitespace.
1785 FIRST means these parts are the first of a macro definition;
1786 so ignore leading whitespace entirely.
1787 LAST means these parts are the last of a macro definition;
1788 so ignore trailing whitespace entirely. */
1793 U_CHAR *beg1, int len1,
1794 U_CHAR *beg2, int len2 ,
1797 register U_CHAR *end1 = beg1 + len1;
1798 register U_CHAR *end2 = beg2 + len2;
1800 while (beg1 != end1 && is_space[*beg1]) beg1++;
1801 while (beg2 != end2 && is_space[*beg2]) beg2++;
1804 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1805 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1807 while (beg1 != end1 && beg2 != end2) {
1808 if (is_space[*beg1] && is_space[*beg2]) {
1809 while (beg1 != end1 && is_space[*beg1]) beg1++;
1810 while (beg2 != end2 && is_space[*beg2]) beg2++;
1811 } else if (*beg1 == *beg2) {
1815 return (beg1 != end1) || (beg2 != end2);
1818 /* Process a #define command.
1819 BUF points to the contents of the #define command, as a contiguous string.
1820 LIMIT points to the first character past the end of the definition.
1821 KEYWORD is the keyword-table entry for #define,
1822 or NULL for a "predefined" macro. */
1827 struct directive *keyword,
1828 U_CHAR *buf, U_CHAR *limit)
1835 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1836 if (pcp_outfile && keyword)
1837 pass_thru_directive (buf, limit, pfile, keyword);
1840 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1844 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1846 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1849 /* Redefining a precompiled key is ok. */
1850 if (hp->type == T_PCSTRING)
1852 /* Redefining a macro is ok if the definitions are the same. */
1853 else if (hp->type == T_MACRO)
1854 ok = ! compare_defs (mdef.defn, hp->value.defn);
1855 /* Redefining a constant is ok with -D. */
1856 else if (hp->type == T_CONST)
1857 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1858 /* Print the warning if it's not ok. */
1861 U_CHAR *msg; /* what pain... */
1863 /* If we are passing through #define and #undef directives, do
1864 that for this re-definition now. */
1865 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1866 pass_thru_directive (buf, limit, pfile, keyword);
1868 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1870 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1871 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1872 cpp_pedwarn (pfile, msg);
1873 if (hp->type == T_MACRO)
1874 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1875 "this is the location of the previous definition");
1877 /* Replace the old definition. */
1879 hp->value.defn = mdef.defn;
1883 /* If we are passing through #define and #undef directives, do
1884 that for this new definition now. */
1885 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1886 pass_thru_directive (buf, limit, pfile, keyword);
1887 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1888 (char *) mdef.defn, hashcode);
1898 /* This structure represents one parsed argument in a macro call.
1899 `raw' points to the argument text as written (`raw_length' is its length).
1900 `expanded' points to the argument's macro-expansion
1901 (its length is `expand_length').
1902 `stringified_length' is the length the argument would have
1904 `use_count' is the number of times this macro arg is substituted
1905 into the macro. If the actual use count exceeds 10,
1906 the value stored is 10. */
1908 /* raw and expanded are relative to ARG_BASE */
1909 #define ARG_BASE ((pfile)->token_buffer)
1912 /* Strings relative to pfile->token_buffer */
1913 long raw, expanded, stringified;
1914 int raw_length, expand_length;
1915 int stringified_length;
1927 #ifdef STATIC_BUFFERS
1928 register cpp_buffer *buf = CPP_BUFFER (pfile);
1929 if (buf == pfile->buffer_stack)
1930 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1932 bzero ((char *) buf, sizeof (cpp_buffer));
1933 CPP_BUFFER (pfile) = buf;
1935 register cpp_buffer *buf = (cpp_buffer*) Safe_malloc (sizeof(cpp_buffer));
1936 bzero ((char *) buf, sizeof (cpp_buffer));
1937 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1938 CPP_BUFFER (pfile) = buf;
1940 buf->if_stack = pfile->if_stack;
1941 buf->cleanup = null_cleanup;
1942 buf->underflow = null_underflow;
1943 buf->buf = buf->cur = buffer;
1944 buf->alimit = buf->rlimit = buffer + length;
1950 cpp_pop_buffer (pfile)
1953 cpp_buffer *buf = CPP_BUFFER (pfile);
1954 #ifdef STATIC_BUFFERS
1955 (*buf->cleanup) (buf, pfile);
1956 return ++CPP_BUFFER (pfile);
1958 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1959 (*buf->cleanup) (buf, pfile);
1960 CPP_BUFFER (pfile) = next_buf;
1966 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1967 Pop the buffer when done. */
1973 cpp_buffer *buffer = CPP_BUFFER (pfile);
1976 enum cpp_token token = cpp_get_token (pfile);
1977 if (token == CPP_EOF) /* Should not happen ... */
1979 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1981 cpp_pop_buffer (pfile);
1988 * Rescan a string (which may have escape marks) into pfile's buffer.
1989 * Place the result in pfile->token_buffer.
1991 * The input is copied before it is scanned, so it is safe to pass
1992 * it something from the token_buffer that will get overwritten
1993 * (because it follows CPP_WRITTEN). This is used by do_include.
1997 cpp_expand_to_buffer (
2002 register cpp_buffer *ip;
2003 U_CHAR *limit = buf + length;
2007 int odepth = indepth;
2013 /* Set up the input on the input stack. */
2015 buf1 = (U_CHAR *) alloca (length + 1);
2017 register U_CHAR *p1 = buf;
2018 register U_CHAR *p2 = buf1;
2025 ip = cpp_push_buffer (pfile, buf1, length);
2026 ip->has_escapes = 1;
2028 ip->lineno = obuf.lineno = 1;
2031 /* Scan the input, create the output. */
2032 cpp_scan_buffer (pfile);
2035 if (indepth != odepth)
2039 CPP_NUL_TERMINATE (pfile);
2054 (*linep)++, (*colp) = 1;
2060 /* Move line_base forward, updating lineno and colno. */
2064 register cpp_buffer *pbuf)
2066 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2067 unsigned char *new_pos = pbuf->cur;
2068 register struct parse_marker *mark;
2069 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2071 if (pbuf->buf + mark->position < new_pos)
2072 new_pos = pbuf->buf + mark->position;
2074 pbuf->line_base += new_pos - old_pos;
2075 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2079 cpp_buf_line_and_col (
2080 register cpp_buffer *pbuf,
2081 long *linep,long *colp)
2088 *linep = pbuf->lineno;
2089 *colp = pbuf->colno;
2090 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2099 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2105 cpp_buffer *ip = CPP_BUFFER (pfile);
2107 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2108 if (ip->fname != NULL)
2115 register U_CHAR *buf,
2116 register U_CHAR *limit)
2118 register long count = 0;
2129 * write out a #line command, for instance, after an #include file.
2130 * If CONDITIONAL is nonzero, we can omit the #line if it would
2131 * appear to be a no-op, and we can output a few newlines instead
2132 * if we want to increase the line number by a small amount.
2133 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2137 output_line_command (
2140 enum file_change_code file_change)
2143 /* char *line_cmd_buf, *line_end;*/
2145 cpp_buffer *ip = CPP_BUFFER (pfile);
2147 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2151 update_position (ip);
2152 line = CPP_BUFFER (pfile)->lineno;
2153 col = CPP_BUFFER (pfile)->colno;
2154 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2156 if (CPP_OPTIONS (pfile)->no_line_commands)
2160 if (line == pfile->lineno)
2163 /* If the inherited line number is a little too small,
2164 output some newlines instead of a #line command. */
2165 if (line > pfile->lineno && line < pfile->lineno + 8) {
2166 CPP_RESERVE (pfile, 20);
2167 while (line > pfile->lineno) {
2168 CPP_PUTC_Q (pfile, '\n');
2176 /* Don't output a line number of 0 if we can help it. */
2177 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2178 && *ip->bufp == '\n') {
2184 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2186 static char sharp_line[] = "#line ";
2188 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2191 sprintf (CPP_PWRITTEN (pfile), "%d ", line+2);
2192 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2194 // modification for SDC51
2195 if (*ip->nominal_fname == '\0')
2196 quote_string (pfile,"standard input");
2198 quote_string (pfile, ip->nominal_fname);
2199 if (file_change != same_file) {
2200 CPP_PUTC_Q (pfile, ' ');
2201 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2203 /* Tell cc1 if following text comes from a system header file. */
2204 if (ip->system_header_p) {
2205 CPP_PUTC_Q (pfile, ' ');
2206 CPP_PUTC_Q (pfile, '3');
2208 #ifndef NO_IMPLICIT_EXTERN_C
2209 /* Tell cc1plus if following text should be treated as C. */
2210 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2211 CPP_PUTC_Q (pfile, ' ');
2212 CPP_PUTC_Q (pfile, '4');
2215 CPP_PUTC_Q (pfile, '\n');
2216 pfile->lineno = line;
2220 * Parse a macro argument and append the info on PFILE's token_buffer.
2221 * REST_ARGS means to absorb the rest of the args.
2222 * Return nonzero to indicate a syntax error.
2225 static enum cpp_token
2231 enum cpp_token token;
2232 long arg_start = CPP_WRITTEN (pfile);
2233 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2234 CPP_OPTIONS (pfile)->put_out_comments = 0;
2236 /* Try to parse as much of the argument as exists at this
2237 input stack level. */
2238 pfile->no_macro_expand++;
2241 token = cpp_get_token (pfile);
2247 /* If we've hit end of file, it's an error (reported by caller).
2248 Ditto if it's the end of cpp_expand_to_buffer text.
2249 If we've hit end of macro, just continue. */
2250 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2261 /* if we've returned to lowest level and
2262 we aren't absorbing all args */
2263 if (paren == 0 && rest_args == 0)
2267 /* Remove ',' or ')' from argument buffer. */
2268 CPP_ADJUST_WRITTEN (pfile, -1);
2275 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2276 pfile->no_macro_expand--;
2281 /* Turn newlines to spaces in the string of length LENGTH at START,
2282 except inside of string constants.
2283 The string is copied into itself with its beginning staying fixed. */
2290 register U_CHAR *ibp;
2291 register U_CHAR *obp;
2292 register U_CHAR *limit;
2296 limit = start + length;
2299 while (ibp < limit) {
2300 *obp++ = c = *ibp++;
2305 /* Notice and skip strings, so that we don't delete newlines in them. */
2308 while (ibp < limit) {
2309 *obp++ = c = *ibp++;
2312 if (c == '\n' && quotec == '\'')
2328 if (!pfile->timebuf) {
2329 time_t t = time ((time_t *)0);
2330 pfile->timebuf = localtime (&t);
2332 return pfile->timebuf;
2335 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2336 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2340 * expand things like __FILE__. Place the expansion into the output
2341 * buffer *without* rescanning.
2352 cpp_buffer *ip = NULL;
2355 int paren = 0; /* For special `defined' keyword */
2358 if (pcp_outfile && pcp_inside_if
2359 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2361 "Predefined macro `%s' used inside `#if' during precompilation",
2365 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2369 cpp_error (pfile, "cccp error: not in any file?!");
2370 return; /* the show must go on */
2372 if (ip->fname != NULL)
2382 if (hp->type == T_BASE_FILE)
2384 while (CPP_PREV_BUFFER (ip))
2385 ip = CPP_PREV_BUFFER (ip);
2387 string = ip->nominal_fname;
2391 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2392 quote_string (pfile, string);
2396 case T_INCLUDE_LEVEL:
2398 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2399 if (ip->fname != NULL)
2402 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2403 sprintf (buf, "%d", true_indepth - 1);
2407 buf = (char *) alloca (3 + strlen (version_string));
2408 sprintf (buf, "\"%s\"", version_string);
2411 #ifndef NO_BUILTIN_SIZE_TYPE
2417 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2418 case T_PTRDIFF_TYPE:
2424 buf = CPP_WCHAR_TYPE (pfile);
2427 case T_USER_LABEL_PREFIX_TYPE:
2428 buf = USER_LABEL_PREFIX;
2431 case T_REGISTER_PREFIX_TYPE:
2432 buf = REGISTER_PREFIX;
2436 buf = (char *) alloca (4 * sizeof (int));
2437 sprintf (buf, "%d", hp->value.ival);
2439 if (pcp_inside_if && pcp_outfile)
2440 /* Output a precondition for this macro use */
2441 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2447 long line = ip->lineno;
2448 long col = ip->colno;
2449 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2451 buf = (char *) alloca (10);
2452 sprintf (buf, "%d", line);
2458 buf = (char *) alloca (20);
2459 timebuf = timestamp (pfile);
2460 if (hp->type == T_DATE)
2461 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2462 timebuf->tm_mday, timebuf->tm_year + 1900);
2464 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2468 case T_SPEC_DEFINED:
2469 buf = " 0 "; /* Assume symbol is not defined */
2470 ip = CPP_BUFFER (pfile);
2471 SKIP_WHITE_SPACE (ip->cur);
2472 if (*ip->cur == '(')
2475 ip->cur++; /* Skip over the paren */
2476 SKIP_WHITE_SPACE (ip->cur);
2479 if (!is_idstart[*ip->cur])
2481 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2484 if (pcp_outfile && pcp_inside_if
2485 && (hp->type == T_CONST
2486 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2487 /* Output a precondition for this macro use. */
2488 fprintf (pcp_outfile, "#define %s\n", hp->name);
2494 if (pcp_outfile && pcp_inside_if)
2496 /* Output a precondition for this macro use */
2497 U_CHAR *cp = ip->bufp;
2498 fprintf (pcp_outfile, "#undef ");
2499 while (is_idchar[*cp]) /* Ick! */
2500 fputc (*cp++, pcp_outfile);
2501 putc ('\n', pcp_outfile);
2504 while (is_idchar[*ip->cur])
2506 SKIP_WHITE_SPACE (ip->cur);
2509 if (*ip->cur != ')')
2517 cpp_error (pfile, "`defined' without an identifier");
2521 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2525 CPP_RESERVE (pfile, len + 1);
2526 CPP_PUTS_Q (pfile, buf, len);
2527 CPP_NUL_TERMINATE_Q (pfile);
2532 /* Initialize the built-in macros. */
2535 initialize_builtins (
2538 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2539 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2540 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2541 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2542 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2543 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2544 #ifndef NO_BUILTIN_SIZE_TYPE
2545 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2547 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2548 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2550 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2551 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2552 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2553 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2554 if (!CPP_TRADITIONAL (pfile))
2555 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2556 if (CPP_OPTIONS (pfile)->objc)
2557 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2558 /* This is supplied using a -D by the compiler driver
2559 so that it is present only when truly compiling with GNU C. */
2560 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2562 if (CPP_OPTIONS (pfile)->debug_output)
2564 char directive[2048];
2565 register struct directive *dp = &directive_table[0];
2566 struct tm *timebuf = timestamp (pfile);
2567 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2569 while (CPP_PREV_BUFFER (pbuffer))
2570 pbuffer = CPP_PREV_BUFFER (pbuffer);
2571 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2572 pbuffer->nominal_fname);
2573 output_line_command (pfile, 0, same_file);
2574 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2576 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2577 output_line_command (pfile, 0, same_file);
2578 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2580 #ifndef NO_BUILTIN_SIZE_TYPE
2581 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2582 output_line_command (pfile, 0, same_file);
2583 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2586 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2587 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2588 output_line_command (pfile, 0, same_file);
2589 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2592 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2593 output_line_command (pfile, 0, same_file);
2594 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2596 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2597 monthnames[timebuf->tm_mon],
2598 timebuf->tm_mday, timebuf->tm_year + 1900);
2599 output_line_command (pfile, 0, same_file);
2600 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2602 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2603 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2604 output_line_command (pfile, 0, same_file);
2605 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2607 if (!CPP_TRADITIONAL (pfile))
2609 sprintf (directive, " __STDC__ 1");
2610 output_line_command (pfile, 0, same_file);
2611 pass_thru_directive (directive, &directive[strlen (directive)],
2614 if (CPP_OPTIONS (pfile)->objc)
2616 sprintf (directive, " __OBJC__ 1");
2617 output_line_command (pfile, 0, same_file);
2618 pass_thru_directive (directive, &directive[strlen (directive)],
2624 /* Return 1 iff a token ending in C1 followed directly by a token C2
2625 could cause mis-tokenization. */
2634 if (c2 == c1 || c2 == '=')
2638 case '0': case '1': case '2': case '3': case '4':
2639 case '5': case '6': case '7': case '8': case '9':
2641 if (c2 == '-' || c2 == '+')
2642 return 1; /* could extend a pre-processing number */
2645 if (c2 == '\'' || c2 == '\"')
2646 return 1; /* Could turn into L"xxx" or L'xxx'. */
2650 case 'a': case 'b': case 'c': case 'd': case 'f':
2651 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2652 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2653 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2655 case 'A': case 'B': case 'C': case 'D': case 'F':
2656 case 'G': case 'H': case 'I': case 'J': case 'K':
2657 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2658 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2660 /* We're in the middle of either a name or a pre-processing number. */
2661 return (is_idchar[c2] || c2 == '.');
2662 case '<': case '>': case '!': case '%': case '#': case ':':
2663 case '^': case '&': case '|': case '*': case '/': case '=':
2664 return (c2 == c1 || c2 == '=');
2669 /* Expand a macro call.
2670 HP points to the symbol that is the macro being called.
2671 Put the result of expansion onto the input stack
2672 so that subsequent input by our caller will use it.
2674 If macro wants arguments, caller has already verified that
2675 an argument list follows; arguments come from the input stack. */
2683 DEFINITION *defn = hp->value.defn;
2684 register U_CHAR *xbuf;
2685 long start_line, start_column;
2687 struct argdata *args;
2688 long old_written = CPP_WRITTEN (pfile);
2690 int start_line = instack[indepth].lineno;
2692 int rest_args, rest_zero;
2696 CHECK_DEPTH (return;);
2700 /* This macro is being used inside a #if, which means it must be */
2701 /* recorded as a precondition. */
2702 if (pcp_inside_if && pcp_outfile && defn->predefined)
2703 dump_single_macro (hp, pcp_outfile);
2706 pfile->output_escapes++;
2707 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2709 nargs = defn->nargs;
2713 enum cpp_token token;
2715 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2717 for (i = 0; i < nargs; i++)
2719 args[i].raw = args[i].expanded = 0;
2720 args[i].raw_length = 0;
2721 args[i].expand_length = args[i].stringified_length = -1;
2722 args[i].use_count = 0;
2725 /* Parse all the macro args that are supplied. I counts them.
2726 The first NARGS args are stored in ARGS.
2727 The rest are discarded. If rest_args is set then we assume
2728 macarg absorbed the rest of the args. */
2732 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2737 if (i < nargs || (nargs == 0 && i == 0))
2739 /* if we are working on last arg which absorbs rest of args... */
2740 if (i == nargs - 1 && defn->rest_args)
2742 args[i].raw = CPP_WRITTEN (pfile);
2743 token = macarg (pfile, rest_args);
2744 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2745 args[i].newlines = 0; /* FIXME */
2748 token = macarg (pfile, 0);
2749 if (token == CPP_EOF || token == CPP_POP)
2751 cpp_error_with_line (pfile, start_line, start_column,
2752 "unterminated macro call");
2756 } while (token == CPP_COMMA);
2758 /* If we got one arg but it was just whitespace, call that 0 args. */
2761 register U_CHAR *bp = ARG_BASE + args[0].raw;
2762 register U_CHAR *lim = bp + args[0].raw_length;
2763 /* cpp.texi says for foo ( ) we provide one argument.
2764 However, if foo wants just 0 arguments, treat this as 0. */
2766 while (bp != lim && is_space[*bp]) bp++;
2771 /* Don't output an error message if we have already output one for
2772 a parse error above. */
2774 if (nargs == 0 && i > 0)
2776 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2780 /* traditional C allows foo() if foo wants one argument. */
2781 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2783 /* the rest args token is allowed to absorb 0 tokens */
2784 else if (i == nargs - 1 && defn->rest_args)
2787 cpp_error (pfile, "macro `%s' used without args", hp->name);
2789 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2791 cpp_error (pfile, "macro `%s' used with only %d args",
2797 "macro `%s' used with too many (%d) args", hp->name, i);
2801 /* If macro wants zero args, we parsed the arglist for checking only.
2802 Read directly from the macro definition. */
2805 xbuf = defn->expansion;
2806 xbuf_len = defn->length;
2810 register U_CHAR *exp = defn->expansion;
2811 register int offset; /* offset in expansion,
2812 copied a piece at a time */
2813 register int totlen; /* total amount of exp buffer filled so far */
2815 register struct reflist *ap, *last_ap;
2817 /* Macro really takes args. Compute the expansion of this call. */
2819 /* Compute length in characters of the macro's expansion.
2820 Also count number of times each arg is used. */
2821 xbuf_len = defn->length;
2822 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2826 register struct argdata *arg = &args[ap->argno];
2827 /* Stringify it it hasn't already been */
2828 if (arg->stringified_length < 0)
2830 int arglen = arg->raw_length;
2834 /* Initially need_space is -1. Otherwise, 1 means the
2835 previous character was a space, but we suppressed it;
2836 0 means the previous character was a non-space. */
2837 int need_space = -1;
2839 arg->stringified = CPP_WRITTEN (pfile);
2840 if (!CPP_TRADITIONAL (pfile))
2841 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2842 for (; i < arglen; i++)
2844 c = (ARG_BASE + arg->raw)[i];
2848 /* Internal sequences of whitespace are replaced by
2849 one space except within an string or char token.*/
2852 if (CPP_WRITTEN (pfile) > arg->stringified
2853 && (CPP_PWRITTEN (pfile))[-1] == '@')
2855 /* "@ " escape markers are removed */
2856 CPP_ADJUST_WRITTEN (pfile, -1);
2859 if (need_space == 0)
2863 else if (need_space > 0)
2864 CPP_PUTC (pfile, ' ');
2879 else if (c == '\"' || c == '\'')
2883 /* Escape these chars */
2884 if (c == '\"' || (in_string && c == '\\'))
2885 CPP_PUTC (pfile, '\\');
2887 CPP_PUTC (pfile, c);
2890 CPP_RESERVE (pfile, 4);
2891 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2893 CPP_ADJUST_WRITTEN (pfile, 4);
2896 if (!CPP_TRADITIONAL (pfile))
2897 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2898 arg->stringified_length
2899 = CPP_WRITTEN (pfile) - arg->stringified;
2901 xbuf_len += args[ap->argno].stringified_length;
2903 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2904 /* Add 4 for two newline-space markers to prevent
2905 token concatenation. */
2906 xbuf_len += args[ap->argno].raw_length + 4;
2909 /* We have an ordinary (expanded) occurrence of the arg.
2910 So compute its expansion, if we have not already. */
2911 if (args[ap->argno].expand_length < 0)
2913 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2914 cpp_expand_to_buffer (pfile,
2915 ARG_BASE + args[ap->argno].raw,
2916 args[ap->argno].raw_length);
2918 args[ap->argno].expand_length
2919 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2922 /* Add 4 for two newline-space markers to prevent
2923 token concatenation. */
2924 xbuf_len += args[ap->argno].expand_length + 4;
2926 if (args[ap->argno].use_count < 10)
2927 args[ap->argno].use_count++;
2930 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
2932 /* Generate in XBUF the complete expansion
2933 with arguments substituted in.
2934 TOTLEN is the total size generated so far.
2935 OFFSET is the index in the definition
2936 of where we are copying from. */
2937 offset = totlen = 0;
2938 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2939 last_ap = ap, ap = ap->next)
2941 register struct argdata *arg = &args[ap->argno];
2942 int count_before = totlen;
2944 /* Add chars to XBUF. */
2945 for (i = 0; i < ap->nchars; i++, offset++)
2946 xbuf[totlen++] = exp[offset];
2948 /* If followed by an empty rest arg with concatenation,
2949 delete the last run of nonwhite chars. */
2950 if (rest_zero && totlen > count_before
2951 && ((ap->rest_args && ap->raw_before)
2952 || (last_ap != NULL && last_ap->rest_args
2953 && last_ap->raw_after)))
2955 /* Delete final whitespace. */
2956 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2959 /* Delete the nonwhites before them. */
2960 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2964 if (ap->stringify != 0)
2966 bcopy (ARG_BASE + arg->stringified,
2967 xbuf + totlen, arg->stringified_length);
2968 totlen += arg->stringified_length;
2970 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2972 U_CHAR *p1 = ARG_BASE + arg->raw;
2973 U_CHAR *l1 = p1 + arg->raw_length;
2976 while (p1 != l1 && is_space[*p1]) p1++;
2977 while (p1 != l1 && is_idchar[*p1])
2978 xbuf[totlen++] = *p1++;
2982 /* Arg is concatenated after: delete trailing whitespace,
2983 whitespace markers, and no-reexpansion markers. */
2986 if (is_space[l1[-1]]) l1--;
2987 else if (l1[-1] == '-')
2989 U_CHAR *p2 = l1 - 1;
2990 /* If a `-' is preceded by an odd number of newlines then it
2991 and the last newline are a no-reexpansion marker. */
2992 while (p2 != p1 && p2[-1] == '\n') p2--;
2993 if ((l1 - 1 - p2) & 1) {
3002 bcopy (p1, xbuf + totlen, l1 - p1);
3007 U_CHAR *expanded = ARG_BASE + arg->expanded;
3008 if (!ap->raw_before && totlen > 0 && arg->expand_length
3009 && !CPP_TRADITIONAL(pfile)
3010 && unsafe_chars (xbuf[totlen-1], expanded[0]))
3012 xbuf[totlen++] = '@';
3013 xbuf[totlen++] = ' ';
3016 bcopy (expanded, xbuf + totlen, arg->expand_length);
3017 totlen += arg->expand_length;
3019 if (!ap->raw_after && totlen > 0 && offset < defn->length
3020 && !CPP_TRADITIONAL(pfile)
3021 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3023 xbuf[totlen++] = '@';
3024 xbuf[totlen++] = ' ';
3027 /* If a macro argument with newlines is used multiple times,
3028 then only expand the newlines once. This avoids creating
3029 output lines which don't correspond to any input line,
3030 which confuses gdb and gcov. */
3031 if (arg->use_count > 1 && arg->newlines > 0)
3033 /* Don't bother doing change_newlines for subsequent
3037 = change_newlines (expanded, arg->expand_length);
3041 if (totlen > xbuf_len)
3045 /* if there is anything left of the definition
3046 after handling the arg list, copy that in too. */
3048 for (i = offset; i < defn->length; i++)
3050 /* if we've reached the end of the macro */
3053 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3054 && last_ap->raw_after))
3055 xbuf[totlen++] = exp[i];
3063 pfile->output_escapes--;
3065 /* Now put the expansion on the input stack
3066 so our caller will commence reading from it. */
3067 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3068 CPP_BUFFER (pfile)->has_escapes = 1;
3070 /* Pop the space we've used in the token_buffer for argument expansion. */
3071 CPP_SET_WRITTEN (pfile, old_written);
3073 /* Recursive macro use sometimes works traditionally.
3074 #define foo(x,y) bar (x (y,0), y)
3077 if (!CPP_TRADITIONAL (pfile))
3078 hp->type = T_DISABLED;
3082 push_macro_expansion (
3084 register U_CHAR *xbuf,
3088 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3089 mbuf->cleanup = macro_cleanup;
3092 /* The first chars of the expansion should be a "@ " added by
3093 collect_expansion. This is to prevent accidental token-pasting
3094 between the text preceding the macro invocation, and the macro
3097 We would like to avoid adding unneeded spaces (for the sake of
3098 tools that use cpp, such as imake). In some common cases we can
3099 tell that it is safe to omit the space.
3101 The character before the macro invocation cannot have been an
3102 idchar (or else it would have been pasted with the idchars of
3103 the macro name). Therefore, if the first non-space character
3104 of the expansion is an idchar, we do not need the extra space
3105 to prevent token pasting.
3107 Also, we don't need the extra space if the first char is '(',
3108 or some other (less common) characters. */
3110 if (xbuf[0] == '@' && xbuf[1] == ' '
3111 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3112 || xbuf[2] == '\"'))
3116 /* Like cpp_get_token, except that it does not read past end-of-line.
3117 Also, horizontal space is skipped, and macros are popped. */
3119 static enum cpp_token
3120 get_directive_token (
3125 long old_written = CPP_WRITTEN (pfile);
3126 enum cpp_token token;
3127 cpp_skip_hspace (pfile);
3128 if (PEEKC () == '\n')
3130 token = cpp_get_token (pfile);
3134 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3136 /* ... else fall though ... */
3137 case CPP_HSPACE: case CPP_COMMENT:
3138 CPP_SET_WRITTEN (pfile, old_written);
3146 /* Handle #include and #import.
3147 This function expects to see "fname" or <fname> on the input.
3149 The input is normally in part of the output_buffer following
3150 CPP_WRITTEN, and will get overwritten by output_line_command.
3151 I.e. in input file specification has been popped by handle_directive.
3157 struct directive *keyword,
3158 U_CHAR *unused1, U_CHAR *unused2)
3160 int importing = (keyword->type == T_IMPORT);
3161 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3162 char *fname; /* Dynamically allocated fname buffer */
3165 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3166 enum cpp_token token;
3168 /* Chain of dirs to search */
3169 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3170 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3171 struct file_name_list *searchptr = 0;
3172 long old_written = CPP_WRITTEN (pfile);
3176 int f; /* file number */
3178 int retried = 0; /* Have already tried macro
3179 expanding the include line*/
3180 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3183 /* char *pcfbuflimit;*/
3185 f= -1; /* JF we iz paranoid! */
3187 if (importing && CPP_OPTIONS (pfile)->warn_import
3188 && !CPP_OPTIONS (pfile)->inhibit_warnings
3189 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3191 pfile->import_warning = 1;
3192 cpp_warning (pfile, "using `#import' is not recommended");
3193 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3194 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3195 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3196 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3197 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3198 fprintf (stderr, " ... <real contents of file> ...\n");
3199 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3200 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3201 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3202 fprintf (stderr, "when it is equipped with such a conditional.\n");
3205 pfile->parsing_include_directive++;
3206 token = get_directive_token (pfile);
3207 pfile->parsing_include_directive--;
3209 if (token == CPP_STRING)
3211 /* FIXME - check no trailing garbage */
3212 fbeg = pfile->token_buffer + old_written + 1;
3213 fend = CPP_PWRITTEN (pfile) - 1;
3214 if (fbeg[-1] == '<')
3217 /* If -I-, start with the first -I dir after the -I-. */
3218 if (CPP_OPTIONS (pfile)->first_bracket_include)
3219 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3221 /* If -I- was specified, don't search current dir, only spec'd ones. */
3222 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3225 /* We have "filename". Figure out directory this source
3226 file is coming from and put it on the front of the list. */
3228 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3233 if ((nam = fp->nominal_fname) != NULL)
3235 /* Found a named file. Figure out dir of the file,
3236 and put it in front of the search list. */
3237 dsp[0].next = search_start;
3240 ep = rindex (nam, '/');
3242 ep = rindex (nam, ']');
3243 if (ep == NULL) ep = rindex (nam, '>');
3244 if (ep == NULL) ep = rindex (nam, ':');
3245 if (ep != NULL) ep++;
3250 dsp[0].fname = (char *) alloca (n + 1);
3251 strncpy (dsp[0].fname, nam, n);
3252 dsp[0].fname[n] = '\0';
3253 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3254 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3258 dsp[0].fname = 0; /* Current directory */
3260 dsp[0].got_name_map = 0;
3267 else if (token == CPP_NAME)
3270 * Support '#include xyz' like VAX-C to allow for easy use of all the
3271 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3272 * code from case '<' is repeated here) and generates a warning.
3275 "VAX-C-style include specification found, use '#include <filename.h>' !");
3277 /* If -I-, start with the first -I dir after the -I-. */
3278 if (CPP_OPTIONS (pfile)->first_bracket_include)
3279 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3280 fbeg = pfile->token_buffer + old_written;
3281 fend = CPP_PWRITTEN (pfile);
3287 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3288 CPP_SET_WRITTEN (pfile, old_written);
3289 skip_rest_of_line (pfile);
3295 token = get_directive_token (pfile);
3296 if (token != CPP_VSPACE)
3298 cpp_error (pfile, "junk at end of `#include'");
3299 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3300 token = get_directive_token (pfile);
3303 /* For #include_next, skip in the search path
3304 past the dir in which the containing file was found. */
3308 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3309 if (fp->fname != NULL)
3311 /* fp->dir is null if the containing file was specified with
3312 an absolute file name. In that case, don't skip anything. */
3313 if (fp->dir == SELF_DIR_DUMMY)
3314 search_start = CPP_OPTIONS (pfile)->include;
3316 search_start = fp->dir->next;
3321 CPP_SET_WRITTEN (pfile, old_written);
3327 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3331 /* Allocate this permanently, because it gets stored in the definitions
3333 fname = (char *) Safe_malloc (pfile->max_include_len + flen + 4);
3334 /* + 2 above for slash and terminating null. */
3335 /* + 2 added for '.h' on VMS (to support '#include filename') */
3337 /* If specified file name is absolute, just open it. */
3340 strncpy (fname, fbeg, flen);
3342 if (redundant_include_p (pfile, fname))
3345 f = lookup_import (pfile, fname, NULL_PTR);
3347 f = open_include_file (pfile, fname, NULL_PTR);
3349 return 0; /* Already included this file */
3351 /* Search directory path, trying to open the file.
3352 Copy each filename tried into FNAME. */
3354 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3355 if (searchptr->fname) {
3356 /* The empty string in a search path is ignored.
3357 This makes it possible to turn off entirely
3358 a standard piece of the list. */
3359 if (searchptr->fname[0] == 0)
3361 strcpy (fname, searchptr->fname);
3362 strcat (fname, "/");
3363 fname[strlen (fname) + flen] = 0;
3367 strncat (fname, fbeg, flen);
3369 /* Change this 1/2 Unix 1/2 VMS file specification into a
3370 full VMS file specification */
3371 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3372 /* Fix up the filename */
3373 hack_vms_include_specification (fname);
3375 /* This is a normal VMS filespec, so use it unchanged. */
3376 strncpy (fname, fbeg, flen);
3378 /* if it's '#include filename', add the missing .h */
3379 if (index(fname,'.')==NULL) {
3380 strcat (fname, ".h");
3384 /* ??? There are currently 3 separate mechanisms for avoiding processing
3385 of redundant include files: #import, #pragma once, and
3386 redundant_include_p. It would be nice if they were unified. */
3387 if (redundant_include_p (pfile, fname))
3390 f = lookup_import (pfile, fname, searchptr);
3392 f = open_include_file (pfile, fname, searchptr);
3394 return 0; /* Already included this file */
3396 else if (f == -1 && errno == EACCES)
3397 cpp_warning (pfile, "Header file %s exists, but is not readable",
3407 /* A file that was not found. */
3408 strncpy (fname, fbeg, flen);
3410 /* If generating dependencies and -MG was specified, we assume missing
3411 files are leaf files, living in the same directory as the source file
3412 or other similar place; these missing files may be generated from
3413 other files and may not exist yet (eg: y.tab.h). */
3415 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3416 && CPP_PRINT_DEPS (pfile)
3417 > (angle_brackets || (pfile->system_include_depth > 0)))
3419 /* If it was requested as a system header file,
3420 then assume it belongs in the first place to look for such. */
3423 for (searchptr = search_start; searchptr;
3424 searchptr = searchptr->next)
3426 if (searchptr->fname)
3430 if (searchptr->fname[0] == 0)
3432 p = (char *) alloca (strlen (searchptr->fname)
3433 + strlen (fname) + 2);
3434 strcpy (p, searchptr->fname);
3437 deps_output (pfile, p, ' ');
3444 /* Otherwise, omit the directory, as if the file existed
3445 in the directory with the source. */
3446 deps_output (pfile, fname, ' ');
3449 /* If -M was specified, and this header file won't be added to the
3450 dependency list, then don't count this as an error, because we can
3451 still produce correct output. Otherwise, we can't produce correct
3452 output, because there may be dependencies we need inside the missing
3453 file, and we don't know what directory this missing file exists in.*/
3454 else if (CPP_PRINT_DEPS (pfile)
3455 && (CPP_PRINT_DEPS (pfile)
3456 <= (angle_brackets || (pfile->system_include_depth > 0))))
3457 cpp_warning (pfile, "No include path in which to find %s", fname);
3458 else if (search_start)
3459 cpp_error_from_errno (pfile, fname);
3461 cpp_error (pfile, "No include path in which to find %s", fname);
3464 /* Check to see if this include file is a once-only include file.
3467 struct file_name_list* ptr;
3469 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3470 if (!strcmp (ptr->fname, fname)) {
3472 return 0; /* This file was once'd. */
3476 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3477 if (!strcmp (ptr->fname, fname))
3478 break; /* This file was included before. */
3482 /* This is the first time for this file. */
3483 /* Add it to list of files included. */
3485 ptr = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
3486 ptr->control_macro = 0;
3487 ptr->c_system_include_path = 0;
3488 ptr->next = pfile->all_include_files;
3489 pfile->all_include_files = ptr;
3490 ptr->fname = savestring (fname);
3491 ptr->got_name_map = 0;
3493 /* For -M, add this file to the dependencies. */
3494 if (CPP_PRINT_DEPS (pfile)
3495 > (angle_brackets || (pfile->system_include_depth > 0)))
3496 deps_output (pfile, fname, ' ');
3499 /* Handle -H option. */
3500 if (CPP_OPTIONS(pfile)->print_include_names)
3502 cpp_buffer *buf = CPP_BUFFER (pfile);
3503 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3505 fprintf (stderr, "%s\n", fname);
3509 pfile->system_include_depth++;
3511 /* Actually process the file. */
3513 /* Record file on "seen" list for #import. */
3514 add_import (pfile, f, fname);
3516 pcftry = (char *) alloca (strlen (fname) + 30);
3528 sprintf (pcftry, "%s%d", fname, pcfnum++);
3530 pcf = open (pcftry, O_RDONLY, 0666);
3536 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3538 || stat_f.st_dev != s.st_dev)
3540 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3541 /* Don't need it any more. */
3546 /* Don't need it at all. */
3551 } while (pcf != -1 && !pcfbuf);
3555 /* Actually process the file */
3556 cpp_push_buffer (pfile, NULL, 0);
3557 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3558 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3560 output_line_command (pfile, 0, enter_file);
3561 pfile->only_seen_white = 2;
3565 pfile->system_include_depth--;
3570 /* Return nonzero if there is no need to include file NAME
3571 because it has already been included and it contains a conditional
3572 to make a repeated include do nothing. */
3575 redundant_include_p (
3579 struct file_name_list *l = pfile->all_include_files;
3580 for (; l; l = l->next)
3581 if (! strcmp (name, l->fname)
3583 && cpp_lookup (pfile, l->control_macro, -1, -1))
3588 /* Return nonzero if the given FILENAME is an absolute pathname which
3589 designates a file within one of the known "system" include file
3590 directories. We assume here that if the given FILENAME looks like
3591 it is the name of a file which resides either directly in a "system"
3592 include file directory, or within any subdirectory thereof, then the
3593 given file must be a "system" include file. This function tells us
3594 if we should suppress pedantic errors/warnings for the given FILENAME.
3596 The value is 2 if the file is a C-language system header file
3597 for which C++ should (on most systems) assume `extern "C"'. */
3602 register char *filename)
3604 struct file_name_list *searchptr;
3606 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3607 searchptr = searchptr->next)
3608 if (searchptr->fname) {
3609 register char *sys_dir = searchptr->fname;
3610 register unsigned length = strlen (sys_dir);
3612 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3614 if (searchptr->c_system_include_path)
3625 * Install a name in the assertion hash table.
3627 * If LEN is >= 0, it is the length of the name.
3628 * Otherwise, compute the length by scanning the entire name.
3630 * If HASH is >= 0, it is the precomputed hash code.
3631 * Otherwise, compute the hash code.
3633 static ASSERTION_HASHNODE *
3640 register ASSERTION_HASHNODE *hp;
3641 register int i, bucket;
3642 register U_CHAR *p, *q;
3644 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3645 hp = (ASSERTION_HASHNODE *) Safe_malloc (i);
3647 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3648 hp->next = pfile->assertion_hashtab[bucket];
3649 pfile->assertion_hashtab[bucket] = hp;
3651 if (hp->next != NULL)
3652 hp->next->prev = hp;
3655 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3658 for (i = 0; i < len; i++)
3664 * find the most recent hash node for name name (ending with first
3665 * non-identifier char) installed by install
3667 * If LEN is >= 0, it is the length of the name.
3668 * Otherwise, compute the length by scanning the entire name.
3670 * If HASH is >= 0, it is the precomputed hash code.
3671 * Otherwise, compute the hash code.
3674 static ASSERTION_HASHNODE *
3681 register ASSERTION_HASHNODE *bucket;
3683 bucket = pfile->assertion_hashtab[hash];
3685 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3687 bucket = bucket->next;
3694 ASSERTION_HASHNODE *hp)
3696 struct tokenlist_list *tail;
3697 if (hp->prev != NULL)
3698 hp->prev->next = hp->next;
3699 if (hp->next != NULL)
3700 hp->next->prev = hp->prev;
3702 for (tail = hp->value; tail; )
3704 struct tokenlist_list *next = tail->next;
3705 free_token_list (tail->tokens);
3710 /* make sure that the bucket chain header that
3711 the deleted guy was on points to the right thing afterwards. */
3712 if (hp == *hp->bucket_hdr)
3713 *hp->bucket_hdr = hp->next;
3718 /* Convert a character string literal into a nul-terminated string.
3719 The input string is [IN ... LIMIT).
3720 The result is placed in RESULT. RESULT can be the same as IN.
3721 The value returned in the end of the string written to RESULT,
3722 or NULL on error. */
3727 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3747 char *bpc = (char *) in;
3748 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3749 in = (U_CHAR *) bpc;
3751 *result++ = (U_CHAR)c;
3754 /* else fall through */
3764 * interpret #line command. Remembers previously seen fnames
3765 * in its very own hash table.
3767 #define FNAME_HASHSIZE 37
3772 struct directive *keyword)
3774 cpp_buffer *ip = CPP_BUFFER (pfile);
3776 long old_written = CPP_WRITTEN (pfile);
3777 enum file_change_code file_change = same_file;
3778 enum cpp_token token;
3781 token = get_directive_token (pfile);
3783 if (token != CPP_NUMBER
3784 || !isdigit(pfile->token_buffer[old_written]))
3786 cpp_error (pfile, "invalid format `#line' command");
3787 goto bad_line_directive;
3790 /* The Newline at the end of this line remains to be processed.
3791 To put the next line at the specified line number,
3792 we must store a line number now that is one less. */
3793 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3794 CPP_SET_WRITTEN (pfile, old_written);
3796 /* NEW_LINENO is one less than the actual line number here. */
3797 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3798 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3800 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3801 if (PEEKC() && !is_space[PEEKC()]) {
3802 cpp_error (pfile, "invalid format `#line' command");
3803 goto bad_line_directive;
3807 token = get_directive_token (pfile);
3809 if (token == CPP_STRING) {
3810 U_CHAR *fname = pfile->token_buffer + old_written;
3812 static HASHNODE *fname_table[FNAME_HASHSIZE];
3813 HASHNODE *hp, **hash_bucket;
3818 /* Turn the file name, which is a character string literal,
3819 into a null-terminated string. Do this in place. */
3820 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3821 if (end_name == NULL)
3823 cpp_error (pfile, "invalid format `#line' command");
3824 goto bad_line_directive;
3827 fname_length = end_name - fname;
3829 num_start = CPP_WRITTEN (pfile);
3830 token = get_directive_token (pfile);
3831 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3832 p = pfile->token_buffer + num_start;
3833 if (CPP_PEDANTIC (pfile))
3834 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3836 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3838 cpp_error (pfile, "invalid format `#line' command");
3839 goto bad_line_directive;
3842 file_change = enter_file;
3844 file_change = leave_file;
3846 ip->system_header_p = 1;
3847 else /* if (*p == 4) */
3848 ip->system_header_p = 2;
3850 CPP_SET_WRITTEN (pfile, num_start);
3851 token = get_directive_token (pfile);
3852 p = pfile->token_buffer + num_start;
3853 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3854 ip->system_header_p = *p == 3 ? 1 : 2;
3855 token = get_directive_token (pfile);
3857 if (token != CPP_VSPACE) {
3858 cpp_error (pfile, "invalid format `#line' command");
3859 goto bad_line_directive;
3864 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3865 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3866 if (hp->length == fname_length &&
3867 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3868 ip->nominal_fname = hp->value.cpval;
3872 /* Didn't find it; cons up a new one. */
3873 hp = (HASHNODE *) Safe_calloc(1,sizeof (HASHNODE) + fname_length + 1);
3874 hp->next = *hash_bucket;
3877 hp->length = fname_length;
3878 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3879 bcopy (fname, hp->value.cpval, fname_length);
3882 else if (token != CPP_VSPACE && token != CPP_EOF) {
3883 cpp_error (pfile, "invalid format `#line' command");
3884 goto bad_line_directive;
3887 ip->lineno = new_lineno;
3889 skip_rest_of_line (pfile);
3890 CPP_SET_WRITTEN (pfile, old_written);
3891 output_line_command (pfile, 0, file_change);
3896 * remove the definition of a symbol from the symbol table.
3897 * according to un*x /lib/cpp, it is not an error to undef
3898 * something that has no definitions, so it isn't one here either.
3904 struct directive *keyword,
3905 U_CHAR *buf, U_CHAR *limit)
3909 U_CHAR *orig_buf = buf;
3912 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3913 if (pcp_outfile && keyword)
3914 pass_thru_directive (buf, limit, pfile, keyword);
3917 SKIP_WHITE_SPACE (buf);
3918 sym_length = check_macro_name (pfile, buf, "macro");
3920 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3922 /* If we are generating additional info for debugging (with -g) we
3923 need to pass through all effective #undef commands. */
3924 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3925 pass_thru_directive (orig_buf, limit, pfile, keyword);
3926 if (hp->type != T_MACRO)
3927 cpp_warning (pfile, "undefining `%s'", hp->name);
3931 if (CPP_PEDANTIC (pfile)) {
3933 SKIP_WHITE_SPACE (buf);
3935 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3941 * Report an error detected by the program we are processing.
3942 * Use the text of the line in the error message.
3943 * (We use error because it prints the filename & line#.)
3949 struct directive *keyword,
3950 U_CHAR *buf,U_CHAR *limit)
3952 int length = limit - buf;
3953 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3954 bcopy (buf, copy, length);
3956 SKIP_WHITE_SPACE (copy);
3957 cpp_error (pfile, "#error %s", copy);
3962 * Report a warning detected by the program we are processing.
3963 * Use the text of the line in the warning message, then continue.
3964 * (We use error because it prints the filename & line#.)
3970 struct directive *keyword,
3971 U_CHAR *buf,U_CHAR *limit)
3973 int length = limit - buf;
3974 U_CHAR *copy = (U_CHAR *) Safe_malloc (length + 1);
3975 bcopy (buf, copy, length);
3977 SKIP_WHITE_SPACE (copy);
3978 cpp_warning (pfile, "#warning %s", copy);
3982 /* Remember the name of the current file being read from so that we can
3983 avoid ever including it again. */
3989 cpp_buffer *ip = NULL;
3990 struct file_name_list *new;
3992 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3996 if (ip->fname != NULL)
4001 new = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
4002 new->next = pfile->dont_repeat_files;
4003 pfile->dont_repeat_files = new;
4004 new->fname = savestring (ip->fname);
4005 new->control_macro = 0;
4006 new->got_name_map = 0;
4007 new->c_system_include_path = 0;
4012 /* #ident has already been copied to the output file, so just ignore it. */
4017 struct directive *keyword,
4018 U_CHAR *buf, U_CHAR *limit)
4020 /* long old_written = CPP_WRITTEN (pfile);*/
4023 /* Allow #ident in system headers, since that's not user's fault. */
4024 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
4025 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
4027 /* Leave rest of line to be read by later calls to cpp_get_token. */
4032 /* #pragma and its argument line have already been copied to the output file.
4033 Just check for some recognized pragmas that need validation here. */
4038 struct directive *keyword,
4039 U_CHAR *buf,U_CHAR *limit)
4041 while (*buf == ' ' || *buf == '\t')
4043 if (!strncmp (buf, "once", 4)) {
4044 /* Allow #pragma once in system headers, since that's not the user's
4046 if (!CPP_BUFFER (pfile)->system_header_p)
4047 cpp_warning (pfile, "`#pragma once' is obsolete");
4051 if (!strncmp (buf, "implementation", 14)) {
4052 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4053 been included yet. */
4054 struct file_name_list *ptr;
4055 U_CHAR *p = buf + 14, *fname, *inc_fname;
4057 SKIP_WHITE_SPACE (p);
4058 if (*p == '\n' || *p != '\"')
4062 p = (U_CHAR *) index (fname, '\"');
4063 fname_len = p != NULL ? p - fname : strlen (fname);
4065 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4066 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4067 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4068 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4070 "`#pragma implementation' for `%s' appears after file is included",
4079 /* This was a fun hack, but #pragma seems to start to be useful.
4080 By failing to recognize it, we pass it through unchanged to cc1. */
4083 * the behavior of the #pragma directive is implementation defined.
4084 * this implementation defines it as follows.
4091 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4094 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4096 execl ("/usr/games/hack", "#pragma", 0);
4097 execl ("/usr/games/rogue", "#pragma", 0);
4098 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4099 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4101 fatal ("You are in a maze of twisty compiler features, all different");
4105 /* Just ignore #sccs, on systems where we define it at all. */
4110 struct directive *keyword,
4111 U_CHAR *buf,U_CHAR *limit)
4113 if (CPP_PEDANTIC (pfile))
4114 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4119 * handle #if command by
4120 * 1) inserting special `defined' keyword into the hash table
4121 * that gets turned into 0 or 1 by special_symbol (thus,
4122 * if the luser has a symbol called `defined' already, it won't
4123 * work inside the #if command)
4124 * 2) rescan the input into a temporary output buffer
4125 * 3) pass the output buffer to the yacc parser and collect a value
4126 * 4) clean up the mess left from steps 1 and 2.
4127 * 5) call conditional_skip to skip til the next #endif (etc.),
4128 * or not, depending on the value from step 3.
4134 struct directive *keyword,
4135 U_CHAR *buf,U_CHAR *limit)
4137 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4138 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4143 * handle a #elif directive by not changing if_stack either.
4144 * see the comment above do_else.
4150 struct directive *keyword,
4151 U_CHAR *buf,U_CHAR *limit)
4153 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4154 cpp_error (pfile, "`#elif' not within a conditional");
4157 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4158 cpp_error (pfile, "`#elif' after `#else'");
4160 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4162 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4163 && strcmp (pfile->if_stack->fname,
4164 CPP_BUFFER (pfile)->nominal_fname) != 0)
4165 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4166 fprintf (stderr, ")\n");
4168 pfile->if_stack->type = T_ELIF;
4171 if (pfile->if_stack->if_succeeded)
4172 skip_if_group (pfile, 0);
4174 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4176 skip_if_group (pfile, 0);
4178 ++pfile->if_stack->if_succeeded; /* continue processing input */
4179 output_line_command (pfile, 1, same_file);
4186 * evaluate a #if expression in BUF, of length LENGTH,
4187 * then parse the result as a C expression and return the value as an int.
4189 static HOST_WIDE_INT
4190 eval_if_expression (
4195 HASHNODE *save_defined;
4196 HOST_WIDE_INT value;
4197 long old_written = CPP_WRITTEN (pfile);
4199 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4200 pfile->pcp_inside_if = 1;
4202 value = cpp_parse_expr (pfile);
4203 pfile->pcp_inside_if = 0;
4204 delete_macro (save_defined); /* clean up special symbol */
4206 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4212 * routine to handle ifdef/ifndef. Try to look up the symbol,
4213 * then do or don't skip to the #endif/#else/#elif depending
4214 * on what directive is actually being processed.
4220 struct directive *keyword,
4221 U_CHAR *unused1, U_CHAR *unused2)
4224 cpp_buffer *ip = CPP_BUFFER (pfile);
4227 enum cpp_token token;
4228 int start_of_file = 0;
4229 U_CHAR *control_macro = 0;
4230 int old_written = CPP_WRITTEN (pfile);
4232 /* Detect a #ifndef at start of file (not counting comments). */
4233 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4234 start_of_file = pfile->only_seen_white == 2;
4236 pfile->no_macro_expand++;
4237 token = get_directive_token (pfile);
4238 pfile->no_macro_expand--;
4240 ident = pfile->token_buffer + old_written;
4241 ident_length = CPP_WRITTEN (pfile) - old_written;
4242 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4244 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4246 skip = (keyword->type == T_IFDEF);
4247 if (! CPP_TRADITIONAL (pfile))
4248 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4250 else if (token == CPP_NAME)
4252 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4253 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4254 if (start_of_file && !skip)
4256 control_macro = (U_CHAR *) Safe_malloc (ident_length + 1);
4257 bcopy (ident, control_macro, ident_length + 1);
4262 skip = (keyword->type == T_IFDEF);
4263 if (! CPP_TRADITIONAL (pfile))
4264 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4267 if (!CPP_TRADITIONAL (pfile))
4269 cpp_skip_hspace (pfile);
4271 if (c != EOF && c != '\n')
4272 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4274 skip_rest_of_line (pfile);
4278 /* Output a precondition for this macro. */
4279 if (hp && hp->value.defn->predefined)
4280 fprintf (pcp_outfile, "#define %s\n", hp->name);
4283 fprintf (pcp_outfile, "#undef ");
4284 while (is_idchar[*cp]) /* Ick! */
4285 fputc (*cp++, pcp_outfile);
4286 putc ('\n', pcp_outfile);
4290 conditional_skip (pfile, skip, T_IF, control_macro);
4294 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4295 If this is a #ifndef starting at the beginning of a file,
4296 CONTROL_MACRO is the macro name tested by the #ifndef.
4297 Otherwise, CONTROL_MACRO is 0. */
4303 enum node_type type,
4304 U_CHAR *control_macro)
4306 IF_STACK_FRAME *temp;
4308 temp = (IF_STACK_FRAME *) Safe_calloc(1,sizeof (IF_STACK_FRAME));
4309 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4311 temp->lineno = CPP_BUFFER (pfile)->lineno;
4313 temp->next = pfile->if_stack;
4314 temp->control_macro = control_macro;
4315 pfile->if_stack = temp;
4317 pfile->if_stack->type = type;
4320 skip_if_group (pfile, 0);
4323 ++pfile->if_stack->if_succeeded;
4324 output_line_command (pfile, 1, same_file);
4329 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4330 * leaves input ptr at the sharp sign found.
4331 * If ANY is nonzero, return at next directive of any sort.
4339 int at_beg_of_line = 1;
4340 struct directive *kt;
4341 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4343 U_CHAR *beg_of_line = bp;
4345 register int ident_length;
4346 U_CHAR *ident /*, *after_ident */ ;
4347 struct parse_marker line_start_mark;
4349 parse_set_mark (&line_start_mark, pfile);
4351 if (CPP_OPTIONS (pfile)->output_conditionals) {
4352 static char failed[] = "#failed\n";
4353 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4355 output_line_command (pfile, 1, same_file);
4359 if (CPP_OPTIONS (pfile)->output_conditionals)
4361 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4362 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4363 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4365 parse_move_mark (&line_start_mark, pfile);
4366 if (!CPP_TRADITIONAL (pfile))
4367 cpp_skip_hspace (pfile);
4371 int old_written = CPP_WRITTEN (pfile);
4372 cpp_skip_hspace (pfile);
4374 parse_name (pfile, GETC());
4375 ident_length = CPP_WRITTEN (pfile) - old_written;
4376 ident = pfile->token_buffer + old_written;
4377 pfile->limit = ident;
4379 if (ident_length == 0)
4380 goto not_a_directive;
4382 /* Handle # followed by a line number. */
4384 /* Avoid error for `###' and similar cases unless -pedantic. */
4387 for (kt = directive_table; kt->length >= 0; kt++)
4389 IF_STACK_FRAME *temp;
4390 if (ident_length == kt->length
4391 && strncmp (ident, kt->name, kt->length) == 0)
4393 /* If we are asked to return on next directive, do so now. */
4403 = (IF_STACK_FRAME *) Safe_calloc(1,sizeof (IF_STACK_FRAME));
4404 temp->next = pfile->if_stack;
4405 pfile->if_stack = temp;
4407 temp->lineno = CPP_BUFFER(pfile)->lineno;
4409 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4410 temp->type = kt->type;
4414 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4415 validate_else (pfile,
4416 kt->type == T_ELSE ? "#else" : "#endif");
4418 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4421 "`#%s' not within a conditional", kt->name);
4424 else if (pfile->if_stack == save_if_stack)
4425 goto done; /* found what we came for */
4427 if (kt->type != T_ENDIF)
4429 if (pfile->if_stack->type == T_ELSE)
4430 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4431 pfile->if_stack->type = kt->type;
4435 temp = pfile->if_stack;
4436 pfile->if_stack = temp->next;
4443 /* Don't let erroneous code go by. */
4444 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4445 && CPP_PEDANTIC (pfile))
4446 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4450 /* We're in the middle of a line. Skip the rest of it. */
4457 case '/': /* possible comment */
4458 c = skip_comment (pfile, NULL);
4465 old = CPP_WRITTEN (pfile);
4466 cpp_get_token (pfile);
4467 CPP_SET_WRITTEN (pfile, old);
4470 /* Char after backslash loses its special meaning. */
4471 if (PEEKC() == '\n')
4481 if (CPP_OPTIONS (pfile)->output_conditionals) {
4482 static char end_failed[] = "#endfailed\n";
4483 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4486 pfile->only_seen_white = 1;
4487 parse_goto_mark (&line_start_mark, pfile);
4488 parse_clear_mark (&line_start_mark);
4492 * handle a #else directive. Do this by just continuing processing
4493 * without changing if_stack ; this is so that the error message
4494 * for missing #endif's etc. will point to the original #if. It
4495 * is possible that something different would be better.
4501 struct directive *keyword,
4502 U_CHAR *buf, U_CHAR *limit)
4504 cpp_buffer *ip = CPP_BUFFER (pfile);
4506 if (CPP_PEDANTIC (pfile))
4507 validate_else (pfile, "#else");
4508 skip_rest_of_line (pfile);
4510 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4511 cpp_error (pfile, "`#else' not within a conditional");
4514 /* #ifndef can't have its special treatment for containing the whole file
4515 if it has a #else clause. */
4516 pfile->if_stack->control_macro = 0;
4518 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4519 cpp_error (pfile, "`#else' after `#else'");
4520 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4521 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4522 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4523 fprintf (stderr, ")\n");
4525 pfile->if_stack->type = T_ELSE;
4528 if (pfile->if_stack->if_succeeded)
4529 skip_if_group (pfile, 0);
4531 ++pfile->if_stack->if_succeeded; /* continue processing input */
4532 output_line_command (pfile, 1, same_file);
4538 * unstack after #endif command
4544 struct directive *keyword,
4545 U_CHAR *buf, U_CHAR *limit)
4547 if (CPP_PEDANTIC (pfile))
4548 validate_else (pfile, "#endif");
4549 skip_rest_of_line (pfile);
4551 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4552 cpp_error (pfile, "unbalanced `#endif'");
4555 IF_STACK_FRAME *temp = pfile->if_stack;
4556 pfile->if_stack = temp->next;
4557 if (temp->control_macro != 0)
4559 /* This #endif matched a #ifndef at the start of the file.
4560 See if it is at the end of the file. */
4561 struct parse_marker start_mark;
4564 parse_set_mark (&start_mark, pfile);
4568 cpp_skip_hspace (pfile);
4573 parse_goto_mark (&start_mark, pfile);
4574 parse_clear_mark (&start_mark);
4578 /* If we get here, this #endif ends a #ifndef
4579 that contains all of the file (aside from whitespace).
4580 Arrange not to include the file again
4581 if the macro that was tested is defined.
4583 Do not do this for the top-level file in a -include or any
4584 file in a -imacros. */
4588 && ! (indepth == 1 && pfile->no_record_file)
4589 && ! (pfile->no_record_file && no_output))
4592 struct file_name_list *ifile = pfile->all_include_files;
4594 for ( ; ifile != NULL; ifile = ifile->next)
4596 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4598 ifile->control_macro = temp->control_macro;
4606 output_line_command (pfile, 1, same_file);
4611 /* When an #else or #endif is found while skipping failed conditional,
4612 if -pedantic was specified, this is called to warn about text after
4613 the command name. P points to the first char after the command name. */
4621 cpp_skip_hspace (pfile);
4623 if (c != EOF && c != '\n')
4625 "text following `%s' violates ANSI standard", directive);
4628 /* Get the next token, and add it to the text in pfile->token_buffer.
4629 Return the kind of token we got. */
4636 register int c, c2, c3;
4638 long start_line, start_column;
4639 enum cpp_token token;
4640 struct cpp_options *opts = CPP_OPTIONS (pfile);
4641 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4647 if (CPP_BUFFER (pfile)->seen_eof)
4649 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4656 cpp_buffer *next_buf
4657 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4658 CPP_BUFFER (pfile)->seen_eof = 1;
4659 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4661 /* We're about to return from an #include file.
4662 Emit #line information now (as part of the CPP_POP) result.
4663 But the #line refers to the file we will pop to. */
4664 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4665 CPP_BUFFER (pfile) = next_buf;
4666 pfile->input_stack_listing_current = 0;
4667 output_line_command (pfile, 0, leave_file);
4668 CPP_BUFFER (pfile) = cur_buffer;
4678 struct parse_marker start_mark;
4680 if (PEEKC () == '=')
4682 if (opts->put_out_comments)
4683 parse_set_mark (&start_mark, pfile);
4685 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4686 &start_line, &start_column);
4687 c = skip_comment (pfile, &newlines);
4688 if (opts->put_out_comments && (c == '/' || c == EOF))
4689 parse_clear_mark (&start_mark);
4694 cpp_error_with_line (pfile, start_line, start_column,
4695 "unterminated comment");
4698 c = '/'; /* Initial letter of comment. */
4700 /* Comments are equivalent to spaces.
4701 For -traditional, a comment is equivalent to nothing. */
4702 if (opts->put_out_comments)
4704 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4706 U_CHAR *start = pbuf->buf + start_mark.position;
4707 int len = pbuf->cur - start;
4708 CPP_RESERVE(pfile, 1 + len);
4709 CPP_PUTC_Q (pfile, c);
4710 CPP_PUTS_Q (pfile, start, len);
4711 pfile->lineno += newlines;
4712 parse_clear_mark (&start_mark);
4715 else if (CPP_TRADITIONAL (pfile))
4722 /* This may not work if cpp_get_token is called recursively,
4723 since many places look for horizontal space. */
4726 /* Copy the newlines into the output buffer, in order to
4727 avoid the pain of a #line every time a multiline comment
4729 CPP_RESERVE(pfile, newlines);
4730 while (--newlines >= 0)
4732 CPP_PUTC_Q (pfile, '\n');
4738 CPP_RESERVE(pfile, 1);
4739 CPP_PUTC_Q (pfile, ' ');
4743 if (opts->for_lint) {
4746 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4748 if (lintcmd != NULL) {
4749 /* I believe it is always safe to emit this newline: */
4751 bcopy ("#pragma lint ", (char *) obp, 13);
4753 bcopy (lintcmd, (char *) obp, cmdlen);
4758 bcopy (argbp, (char *) obp, arglen);
4762 /* OK, now bring us back to the state we were in before we entered
4763 this branch. We need #line b/c the newline for the pragma
4764 could fuck things up. */
4765 output_line_command (pfile, 0, same_file);
4766 *(obp++) = ' '; /* just in case, if comments are copied thru */
4774 /* If this is expanding a macro definition, don't recognize
4775 preprocessor directives. */
4778 /* If this is expand_into_temp_buffer, recognize them
4779 only after an actual newline at this level,
4780 not at the beginning of the input level. */
4781 if (ip->fname == 0 && beg_of_line == ip->buf)
4787 if (!pfile->only_seen_white)
4789 if (handle_directive (pfile))
4790 return CPP_DIRECTIVE;
4791 pfile->only_seen_white = 0;
4796 /* A single quoted string is treated like a double -- some
4797 programs (e.g., troff) are perverse this way */
4798 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4799 &start_line, &start_column);
4800 old_written = CPP_WRITTEN (pfile);
4802 CPP_PUTC (pfile, c);
4808 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4810 /* try harder: this string crosses a macro expansion
4811 boundary. This can happen naturally if -traditional.
4812 Otherwise, only -D can make a macro with an unmatched
4814 cpp_buffer *next_buf
4815 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4816 (*CPP_BUFFER (pfile)->cleanup)
4817 (CPP_BUFFER (pfile), pfile);
4818 CPP_BUFFER (pfile) = next_buf;
4821 if (!CPP_TRADITIONAL (pfile))
4823 cpp_error_with_line (pfile, start_line, start_column,
4824 "unterminated string or character constant");
4825 if (pfile->multiline_string_line != start_line
4826 && pfile->multiline_string_line != 0)
4827 cpp_error_with_line (pfile,
4828 pfile->multiline_string_line, -1,
4829 "possible real start of unterminated constant");
4830 pfile->multiline_string_line = 0;
4834 CPP_PUTC (pfile, cc);
4838 /* Traditionally, end of line ends a string constant with
4839 no error. So exit the loop and record the new line. */
4840 if (CPP_TRADITIONAL (pfile))
4844 cpp_error_with_line (pfile, start_line, start_column,
4845 "unterminated character constant");
4848 if (CPP_PEDANTIC (pfile)
4849 && pfile->multiline_string_line == 0)
4851 cpp_pedwarn_with_line (pfile, start_line, start_column,
4852 "string constant runs past end of line");
4854 if (pfile->multiline_string_line == 0)
4855 pfile->multiline_string_line = start_line;
4862 /* Backslash newline is replaced by nothing at all. */
4863 CPP_ADJUST_WRITTEN (pfile, -1);
4868 /* ANSI stupidly requires that in \\ the second \
4869 is *not* prevented from combining with a newline. */
4872 CPP_PUTC (pfile, cc);
4884 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4885 CPP_PWRITTEN (pfile));
4886 pfile->only_seen_white = 0;
4887 return c == '\'' ? CPP_CHAR : CPP_STRING;
4890 if (!opts->dollars_in_ident)
4895 if (opts->cplusplus && PEEKC () == ':')
4904 if (c2 == c || c2 == '=')
4914 if (PEEKC () == '=')
4921 if (c2 == '-' && opts->chill)
4923 /* Chill style comment */
4924 if (opts->put_out_comments)
4925 parse_set_mark (&start_mark, pfile);
4926 FORWARD(1); /* Skip second '-'. */
4934 /* Don't consider final '\n' to be part of comment. */
4940 goto return_comment;
4942 if (c2 == '-' || c2 == '=' || c2 == '>')
4947 if (pfile->parsing_include_directive)
4951 CPP_PUTC (pfile, c);
4956 if (c == '\n' || c == EOF)
4959 "missing '>' in `#include <FILENAME>'");
4965 /* else fall through */
4974 CPP_RESERVE (pfile, 4);
4975 CPP_PUTC (pfile, c);
4976 CPP_PUTC (pfile, c2);
4980 CPP_PUTC_Q (pfile, GETC ());
4981 CPP_NUL_TERMINATE_Q (pfile);
4982 pfile->only_seen_white = 0;
4986 if (CPP_BUFFER (pfile)->has_escapes)
4991 if (pfile->output_escapes)
4992 CPP_PUTS (pfile, "@-", 2);
4993 parse_name (pfile, GETC ());
4996 else if (is_space [c])
4998 CPP_RESERVE (pfile, 2);
4999 if (pfile->output_escapes)
5000 CPP_PUTC_Q (pfile, '@');
5001 CPP_PUTC_Q (pfile, c);
5005 if (pfile->output_escapes)
5007 CPP_PUTS (pfile, "@@", 2);
5017 CPP_RESERVE(pfile, 2);
5018 CPP_PUTC_Q (pfile, '.');
5022 /* FIXME - misses the case "..\\\n." */
5023 if (c2 == '.' && PEEKN(1) == '.')
5025 CPP_RESERVE(pfile, 4);
5026 CPP_PUTC_Q (pfile, '.');
5027 CPP_PUTC_Q (pfile, '.');
5028 CPP_PUTC_Q (pfile, '.');
5030 CPP_NUL_TERMINATE_Q (pfile);
5031 pfile->only_seen_white = 0;
5038 pfile->only_seen_white = 0;
5040 CPP_RESERVE(pfile, 3);
5041 CPP_PUTC_Q (pfile, c);
5042 CPP_PUTC_Q (pfile, GETC ());
5043 CPP_NUL_TERMINATE_Q (pfile);
5049 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5051 CPP_PUTC (pfile, c);
5057 case '0': case '1': case '2': case '3': case '4':
5058 case '5': case '6': case '7': case '8': case '9':
5063 CPP_RESERVE (pfile, 2);
5064 CPP_PUTC_Q (pfile, c);
5069 if (!is_idchar[c] && c != '.'
5070 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5075 CPP_NUL_TERMINATE_Q (pfile);
5076 pfile->only_seen_white = 0;
5078 case 'b': case 'c': case 'd': case 'h': case 'o':
5079 case 'B': case 'C': case 'D': case 'H': case 'O':
5080 if (opts->chill && PEEKC () == '\'')
5082 pfile->only_seen_white = 0;
5083 CPP_RESERVE (pfile, 2);
5084 CPP_PUTC_Q (pfile, c);
5085 CPP_PUTC_Q (pfile, '\'');
5091 goto chill_number_eof;
5094 if (c == '\\' && PEEKC() == '\n')
5101 CPP_PUTC (pfile, c);
5105 CPP_RESERVE (pfile, 2);
5106 CPP_PUTC_Q (pfile, c);
5107 CPP_NUL_TERMINATE_Q (pfile);
5114 CPP_NUL_TERMINATE (pfile);
5121 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5122 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5123 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5124 case 'x': case 'y': case 'z':
5125 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5126 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5127 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5132 unsigned char *ident;
5133 int before_name_written = CPP_WRITTEN (pfile);
5135 parse_name (pfile, c);
5136 pfile->only_seen_white = 0;
5137 if (pfile->no_macro_expand)
5139 ident = pfile->token_buffer + before_name_written;
5140 ident_len = CPP_PWRITTEN (pfile) - ident;
5141 hp = cpp_lookup (pfile, ident, ident_len, -1);
5144 if (hp->type == T_DISABLED)
5146 if (pfile->output_escapes)
5147 { /* Return "@-IDENT", followed by '\0'. */
5149 CPP_RESERVE (pfile, 3);
5150 ident = pfile->token_buffer + before_name_written;
5151 CPP_ADJUST_WRITTEN (pfile, 2);
5152 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5159 /* If macro wants an arglist, verify that a '(' follows.
5160 first skip all whitespace, copying it to the output
5161 after the macro name. Then, if there is no '(',
5162 decide this is not a macro call and leave things that way. */
5163 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5165 struct parse_marker macro_mark;
5167 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5169 cpp_buffer *next_buf;
5170 cpp_skip_hspace (pfile);
5171 if (PEEKC () != EOF)
5173 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5174 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5175 CPP_BUFFER (pfile) = next_buf;
5177 parse_set_mark (¯o_mark, pfile);
5180 cpp_skip_hspace (pfile);
5182 is_macro_call = c == '(';
5188 parse_goto_mark (¯o_mark, pfile);
5189 parse_clear_mark (¯o_mark);
5193 /* This is now known to be a macro call. */
5195 /* it might not actually be a macro. */
5196 if (hp->type != T_MACRO) {
5197 int xbuf_len; U_CHAR *xbuf;
5198 CPP_SET_WRITTEN (pfile, before_name_written);
5199 special_symbol (hp, pfile);
5200 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5201 xbuf = (U_CHAR *) Safe_malloc (xbuf_len + 1);
5202 CPP_SET_WRITTEN (pfile, before_name_written);
5203 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5204 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5208 /* Expand the macro, reading arguments as needed,
5209 and push the expansion on the input stack. */
5210 macroexpand (pfile, hp);
5211 CPP_SET_WRITTEN (pfile, before_name_written);
5214 /* An extra "@ " is added to the end of a macro expansion
5215 to prevent accidental token pasting. We prefer to avoid
5216 unneeded extra spaces (for the sake of cpp-using tools like
5217 imake). Here we remove the space if it is safe to do so. */
5218 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5219 && pfile->buffer->rlimit[-2] == '@'
5220 && pfile->buffer->rlimit[-1] == ' ')
5222 int c1 = pfile->buffer->rlimit[-3];
5223 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5224 if (c2 == EOF || ! unsafe_chars (c1, c2))
5225 pfile->buffer->rlimit -= 2;
5230 case ' ': case '\t': case '\v': case '\r':
5233 CPP_PUTC (pfile, c);
5235 if (c == EOF || !is_hor_space[c])
5249 CPP_PUTC (pfile, c);
5250 if (pfile->only_seen_white == 0)
5251 pfile->only_seen_white = 1;
5253 output_line_command (pfile, 1, same_file);
5256 case '(': token = CPP_LPAREN; goto char1;
5257 case ')': token = CPP_RPAREN; goto char1;
5258 case '{': token = CPP_LBRACE; goto char1;
5259 case '}': token = CPP_RBRACE; goto char1;
5260 case ',': token = CPP_COMMA; goto char1;
5261 case ';': token = CPP_SEMICOLON; goto char1;
5267 pfile->only_seen_white = 0;
5268 CPP_PUTC (pfile, c);
5274 /* Like cpp_get_token, but skip spaces and comments. */
5276 cpp_get_non_space_token (
5279 int old_written = CPP_WRITTEN (pfile);
5282 enum cpp_token token = cpp_get_token (pfile);
5283 if (token != CPP_COMMENT && token != CPP_POP
5284 && token != CPP_HSPACE && token != CPP_VSPACE)
5286 CPP_SET_WRITTEN (pfile, old_written);
5290 /* Parse an identifier starting with C. */
5294 cpp_reader *pfile, int c)
5300 if (c == '\\' && PEEKC() == '\n')
5309 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5310 CPP_PUTC_Q (pfile, c);
5315 CPP_NUL_TERMINATE_Q (pfile);
5320 /* Maintain and search list of included files, for #import. */
5322 /* Hash a file name for import_hash_table. */
5330 while (*f) val += *f++;
5331 return (val%IMPORT_HASH_SIZE);
5334 /* Search for file FILENAME in import_hash_table.
5335 Return -2 if found, either a matching name or a matching inode.
5336 Otherwise, open the file and return a file descriptor if successful
5337 or -1 if unsuccessful. */
5343 struct file_name_list *searchptr)
5345 struct import_file *i;
5351 hashval = import_hash (filename);
5353 /* Attempt to find file in list of already included files */
5354 i = pfile->import_hash_table[hashval];
5357 if (!strcmp (filename, i->name))
5358 return -2; /* return found */
5361 /* Open it and try a match on inode/dev */
5362 fd = open_include_file (pfile, filename, searchptr);
5366 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5367 i = pfile->import_hash_table[h];
5369 /* Compare the inode and the device.
5370 Supposedly on some systems the inode is not a scalar. */
5371 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5372 && i->dev == sb.st_dev) {
5374 return -2; /* return found */
5379 return fd; /* Not found, return open file */
5382 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5390 struct import_file *i;
5394 hashval = import_hash (fname);
5396 i = (struct import_file *)Safe_malloc (sizeof (struct import_file));
5397 i->name = (char *)Safe_malloc (strlen (fname)+1);
5398 strcpy (i->name, fname);
5399 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5401 i->next = pfile->import_hash_table[hashval];
5402 pfile->import_hash_table[hashval] = i;
5405 /* The file_name_map structure holds a mapping of file names for a
5406 particular directory. This mapping is read from the file named
5407 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5408 map filenames on a file system with severe filename restrictions,
5409 such as DOS. The format of the file name map file is just a series
5410 of lines with two tokens on each line. The first token is the name
5411 to map, and the second token is the actual name to use. */
5413 struct file_name_map
5415 struct file_name_map *map_next;
5420 #define FILE_NAME_MAP_FILE "header.gcc"
5422 /* Read a space delimited string of unlimited length from a stdio
5426 read_filename_string (
5434 set = alloc = Safe_malloc (len + 1);
5438 while ((ch = getc (f)) != EOF && ! is_space[ch])
5440 if (set - alloc == len)
5443 alloc = Safe_realloc(alloc, len + 1);
5444 set = alloc + len / 2;
5454 /* This structure holds a linked list of file name maps, one per directory. */
5455 struct file_name_map_list
5457 struct file_name_map_list *map_list_next;
5458 char *map_list_name;
5459 struct file_name_map *map_list_map;
5462 /* Read the file name map file for DIRNAME. */
5464 static struct file_name_map *
5469 register struct file_name_map_list *map_list_ptr;
5473 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5474 map_list_ptr = map_list_ptr->map_list_next)
5475 if (! strcmp (map_list_ptr->map_list_name, dirname))
5476 return map_list_ptr->map_list_map;
5478 map_list_ptr = ((struct file_name_map_list *)
5479 Safe_malloc (sizeof (struct file_name_map_list)));
5480 map_list_ptr->map_list_name = savestring (dirname);
5481 map_list_ptr->map_list_map = NULL;
5483 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5484 strcpy (name, dirname);
5487 strcat (name, FILE_NAME_MAP_FILE);
5488 f = fopen (name, "r");
5490 map_list_ptr->map_list_map = NULL;
5494 int dirlen = strlen (dirname);
5496 while ((ch = getc (f)) != EOF)
5499 struct file_name_map *ptr;
5503 from = read_filename_string (ch, f);
5504 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5506 to = read_filename_string (ch, f);
5508 ptr = ((struct file_name_map *)
5509 Safe_malloc (sizeof (struct file_name_map)));
5510 ptr->map_from = from;
5512 /* Make the real filename absolute. */
5517 ptr->map_to = Safe_malloc (dirlen + strlen (to) + 2);
5518 strcpy (ptr->map_to, dirname);
5519 ptr->map_to[dirlen] = '/';
5520 strcpy (ptr->map_to + dirlen + 1, to);
5524 ptr->map_next = map_list_ptr->map_list_map;
5525 map_list_ptr->map_list_map = ptr;
5527 while ((ch = getc (f)) != '\n')
5534 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5535 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5537 return map_list_ptr->map_list_map;
5540 /* Try to open include file FILENAME. SEARCHPTR is the directory
5541 being tried from the include file search path. This function maps
5542 filenames on file systems based on information read by
5549 struct file_name_list *searchptr)
5551 register struct file_name_map *map;
5552 register char *from;
5555 if (searchptr && ! searchptr->got_name_map)
5557 searchptr->name_map = read_name_map (pfile,
5559 ? searchptr->fname : ".");
5560 searchptr->got_name_map = 1;
5563 /* First check the mapping for the directory we are using. */
5564 if (searchptr && searchptr->name_map)
5567 if (searchptr->fname)
5568 from += strlen (searchptr->fname) + 1;
5569 for (map = searchptr->name_map; map; map = map->map_next)
5571 if (! strcmp (map->map_from, from))
5573 /* Found a match. */
5574 return open (map->map_to, O_RDONLY, 0666);
5579 /* Try to find a mapping file for the particular directory we are
5580 looking in. Thus #include <sys/types.h> will look up sys/types.h
5581 in /usr/include/header.gcc and look up types.h in
5582 /usr/include/sys/header.gcc. */
5583 p = rindex (filename, '/');
5588 && (int) strlen (searchptr->fname) == p - filename
5589 && ! strncmp (searchptr->fname, filename, p - filename))
5591 /* FILENAME is in SEARCHPTR, which we've already checked. */
5592 return open (filename, O_RDONLY, 0666);
5602 dir = (char *) alloca (p - filename + 1);
5603 bcopy (filename, dir, p - filename);
5604 dir[p - filename] = '\0';
5607 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5608 if (! strcmp (map->map_from, from))
5609 return open (map->map_to, O_RDONLY, 0666);
5611 return open (filename, O_RDONLY, 0666);
5614 /* Process the contents of include file FNAME, already open on descriptor F,
5616 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5617 "system" include directories (as decided by the `is_system_include'
5619 DIRPTR is the link in the dir path through which this file was found,
5620 or 0 if the file name was absolute or via the current directory.
5621 Return 1 on success, 0 on failure.
5623 The caller is responsible for the cpp_push_buffer. */
5630 int system_header_p,
5631 struct file_name_list *dirptr)
5637 cpp_buffer *fp; /* For input stack frame */
5638 int missing_newline = 0;
5640 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5642 cpp_perror_with_name (pfile, fname);
5644 cpp_pop_buffer (pfile);
5648 fp = CPP_BUFFER (pfile);
5649 fp->nominal_fname = fp->fname = fname;
5654 fp->system_header_p = system_header_p;
5657 fp->cleanup = file_cleanup;
5659 if (S_ISREG (st_mode)) {
5660 fp->buf = (U_CHAR *) Safe_malloc (st_size + 2);
5661 fp->alimit = fp->buf + st_size + 2;
5664 /* Read the file contents, knowing that st_size is an upper bound
5665 on the number of bytes we can read. */
5666 length = safe_read (f, fp->buf, st_size);
5667 fp->rlimit = fp->buf + length;
5668 if (length < 0) goto nope;
5670 else if (S_ISDIR (st_mode)) {
5671 cpp_error (pfile, "directory `%s' specified in #include", fname);
5675 /* Cannot count its file size before reading.
5676 First read the entire file into heap and
5677 copy them into buffer on stack. */
5682 fp->buf = (U_CHAR *) Safe_malloc (bsize + 2);
5685 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5687 goto nope; /* error! */
5689 if (st_size != bsize)
5690 break; /* End of file */
5692 fp->buf = (U_CHAR *) Safe_realloc(fp->buf, bsize + 2);
5698 if ((length > 0 && fp->buf[length - 1] != '\n')
5699 /* Backslash-newline at end is not good enough. */
5700 || (length > 1 && fp->buf[length - 2] == '\\')) {
5701 fp->buf[length++] = '\n';
5703 missing_newline = 1;
5706 fp->buf[length] = '\0';
5707 fp->rlimit = fp->buf + length;
5709 /* Close descriptor now, so nesting does not use lots of descriptors. */
5712 /* Must do this before calling trigraph_pcp, so that the correct file name
5713 will be printed in warning messages. */
5715 pfile->input_stack_listing_current = 0;
5725 if (missing_newline)
5728 if (CPP_PEDANTIC (pfile) && missing_newline)
5729 pedwarn ("file does not end in newline");
5732 input_file_stack_tick++;
5739 cpp_perror_with_name (pfile, fname);
5750 struct cpp_options *opts = CPP_OPTIONS (pfile);
5751 struct cpp_pending *pend;
5756 /* The code looks at the defaults through this pointer, rather than through
5757 the constant structure above. This pointer gets changed if an environment
5758 variable specifies other defaults. */
5759 struct default_include *include_defaults = include_defaults_array;
5761 /* Add dirs from CPATH after dirs from -I. */
5762 /* There seems to be confusion about what CPATH should do,
5763 so for the moment it is not documented. */
5764 /* Some people say that CPATH should replace the standard include dirs,
5765 but that seems pointless: it comes before them, so it overrides them
5767 p = (char *) getenv ("CPATH");
5768 if (p != 0 && ! opts->no_standard_includes)
5769 path_include (pfile, p);
5771 /* Now that dollars_in_ident is known, initialize is_idchar. */
5772 initialize_char_syntax (opts);
5774 /* Do partial setup of input buffer for the sake of generating
5775 early #line directives (when -g is in effect). */
5776 fp = cpp_push_buffer (pfile, NULL, 0);
5777 if (opts->in_fname == NULL)
5778 opts->in_fname = "";
5779 fp->nominal_fname = fp->fname = opts->in_fname;
5782 /* Install __LINE__, etc. Must follow initialize_char_syntax
5783 and option processing. */
5784 initialize_builtins (pfile);
5786 /* Do standard #defines and assertions
5787 that identify system and machine type. */
5789 if (!opts->inhibit_predefs) {
5790 char *p = (char *) alloca (strlen (predefs) + 1);
5791 strcpy (p, predefs);
5794 while (*p == ' ' || *p == '\t')
5796 /* Handle -D options. */
5797 if (p[0] == '-' && p[1] == 'D') {
5799 while (*p && *p != ' ' && *p != '\t')
5803 if (opts->debug_output)
5804 output_line_command (pfile, 0, same_file);
5805 cpp_define (pfile, q);
5806 while (*p == ' ' || *p == '\t')
5808 } else if (p[0] == '-' && p[1] == 'A') {
5809 /* Handle -A options (assertions). */
5818 past_name = assertion;
5819 /* Locate end of name. */
5820 while (*past_name && *past_name != ' '
5821 && *past_name != '\t' && *past_name != '(')
5823 /* Locate `(' at start of value. */
5825 while (*value && (*value == ' ' || *value == '\t'))
5827 if (*value++ != '(')
5829 while (*value && (*value == ' ' || *value == '\t'))
5832 /* Locate end of value. */
5833 while (*past_value && *past_value != ' '
5834 && *past_value != '\t' && *past_value != ')')
5836 termination = past_value;
5837 while (*termination && (*termination == ' ' || *termination == '\t'))
5839 if (*termination++ != ')')
5841 if (*termination && *termination != ' ' && *termination != '\t')
5843 /* Temporarily null-terminate the value. */
5844 save_char = *termination;
5845 *termination = '\0';
5846 /* Install the assertion. */
5847 make_assertion (pfile, "-A", assertion);
5848 *termination = (char) save_char;
5850 while (*p == ' ' || *p == '\t')
5858 /* Now handle the command line options. */
5860 /* Do -U's, -D's and -A's in the order they were seen. */
5861 /* First reverse the list. */
5862 opts->pending = nreverse_pending (opts->pending);
5864 for (pend = opts->pending; pend; pend = pend->next)
5866 if (pend->cmd != NULL && pend->cmd[0] == '-')
5868 switch (pend->cmd[1])
5871 if (opts->debug_output)
5872 output_line_command (pfile, 0, same_file);
5873 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5876 if (opts->debug_output)
5877 output_line_command (pfile, 0, same_file);
5878 cpp_define (pfile, pend->arg);
5881 make_assertion (pfile, "-A", pend->arg);
5887 opts->done_initializing = 1;
5889 { /* read the appropriate environment variable and if it exists
5890 replace include_defaults with the listed path. */
5892 switch ((opts->objc << 1) + opts->cplusplus)
5895 epath = getenv ("C_INCLUDE_PATH");
5898 epath = getenv ("CPLUS_INCLUDE_PATH");
5901 epath = getenv ("OBJC_INCLUDE_PATH");
5904 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5907 /* If the environment var for this language is set,
5908 add to the default list of include directories. */
5910 char *nstore = (char *) alloca (strlen (epath) + 2);
5912 char *startp, *endp;
5914 for (num_dirs = 1, startp = epath; *startp; startp++)
5915 if (*startp == PATH_SEPARATOR)
5918 = (struct default_include *) Safe_malloc ((num_dirs
5919 * sizeof (struct default_include))
5920 + sizeof (include_defaults_array));
5921 startp = endp = epath;
5924 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5925 if ((*endp == PATH_SEPARATOR)
5927 strncpy (nstore, startp, endp-startp);
5929 strcpy (nstore, ".");
5931 nstore[endp-startp] = '\0';
5933 include_defaults[num_dirs].fname = savestring (nstore);
5934 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5935 include_defaults[num_dirs].cxx_aware = 1;
5939 endp = startp = endp + 1;
5943 /* Put the usual defaults back in at the end. */
5944 bcopy ((char *) include_defaults_array,
5945 (char *) &include_defaults[num_dirs],
5946 sizeof (include_defaults_array));
5950 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5951 opts->first_system_include = opts->before_system;
5953 /* Unless -fnostdinc,
5954 tack on the standard include file dirs to the specified list */
5955 if (!opts->no_standard_includes) {
5956 struct default_include *p = include_defaults;
5957 char *specd_prefix = opts->include_prefix;
5958 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5959 int default_len = 0;
5960 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5961 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5962 default_len = strlen (default_prefix) - 7;
5963 default_prefix[default_len] = 0;
5965 /* Search "translated" versions of GNU directories.
5966 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5967 if (specd_prefix != 0 && default_len != 0)
5968 for (p = include_defaults; p->fname; p++) {
5969 /* Some standard dirs are only for C++. */
5971 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5972 /* Does this dir start with the prefix? */
5973 if (!strncmp (p->fname, default_prefix, default_len)) {
5974 /* Yes; change prefix and add to search list. */
5975 struct file_name_list *new
5976 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5977 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5978 char *str = (char *) Safe_malloc (this_len + 1);
5979 strcpy (str, specd_prefix);
5980 strcat (str, p->fname + default_len);
5982 new->control_macro = 0;
5983 new->c_system_include_path = !p->cxx_aware;
5984 new->got_name_map = 0;
5985 append_include_chain (pfile, new, new);
5986 if (opts->first_system_include == 0)
5987 opts->first_system_include = new;
5991 /* Search ordinary names for GNU include directories. */
5992 for (p = include_defaults; p->fname; p++) {
5993 /* Some standard dirs are only for C++. */
5995 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5996 struct file_name_list *new
5997 = (struct file_name_list *) Safe_malloc (sizeof (struct file_name_list));
5998 new->control_macro = 0;
5999 new->c_system_include_path = !p->cxx_aware;
6000 new->fname = p->fname;
6001 new->got_name_map = 0;
6002 append_include_chain (pfile, new, new);
6003 if (opts->first_system_include == 0)
6004 opts->first_system_include = new;
6009 /* Tack the after_include chain at the end of the include chain. */
6010 append_include_chain (pfile, opts->after_include, opts->last_after_include);
6011 if (opts->first_system_include == 0)
6012 opts->first_system_include = opts->after_include;
6014 /* With -v, print the list of dirs to search. */
6015 if (opts->verbose) {
6016 struct file_name_list *p;
6017 fprintf (stderr, "#include \"...\" search starts here:\n");
6018 for (p = opts->include; p; p = p->next) {
6019 if (p == opts->first_bracket_include)
6020 fprintf (stderr, "#include <...> search starts here:\n");
6021 fprintf (stderr, " %s\n", p->fname);
6023 fprintf (stderr, "End of search list.\n");
6026 /* Scan the -imacros files before the main input.
6027 Much like #including them, but with no_output set
6028 so that only their macro definitions matter. */
6030 opts->no_output++; pfile->no_record_file++;
6031 for (pend = opts->pending; pend; pend = pend->next)
6033 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6035 int fd = open (pend->arg, O_RDONLY, 0666);
6038 cpp_perror_with_name (pfile, pend->arg);
6039 return FATAL_EXIT_CODE;
6041 cpp_push_buffer (pfile, NULL, 0);
6042 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6043 cpp_scan_buffer (pfile);
6046 opts->no_output--; pfile->no_record_file--;
6048 /* Copy the entire contents of the main input file into
6049 the stacked input buffer previously allocated for it. */
6050 if (fname == NULL || *fname == 0) {
6053 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6054 cpp_pfatal_with_name (pfile, fname);
6056 /* -MG doesn't select the form of output and must be specified with one of
6057 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6058 inhibit compilation. */
6059 if (opts->print_deps_missing_files
6060 && (opts->print_deps == 0 || !opts->no_output))
6061 fatal (pfile, "-MG must be specified with one of -M or -MM");
6063 /* Either of two environment variables can specify output of deps.
6064 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6065 where OUTPUT_FILE is the file to write deps info to
6066 and DEPS_TARGET is the target to mention in the deps. */
6068 if (opts->print_deps == 0
6069 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6070 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6071 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6077 spec = getenv ("SUNPRO_DEPENDENCIES");
6078 opts->print_deps = 2;
6081 opts->print_deps = 1;
6084 /* Find the space before the DEPS_TARGET, if there is one. */
6085 /* This should use index. (mrs) */
6086 while (*s != 0 && *s != ' ') s++;
6089 opts->deps_target = s + 1;
6090 output_file = (char *) Safe_malloc (s - spec + 1);
6091 bcopy (spec, output_file, s - spec);
6092 output_file[s - spec] = 0;
6096 opts->deps_target = 0;
6100 opts->deps_file = output_file;
6101 opts->print_deps_append = 1;
6104 /* For -M, print the expected object file name
6105 as the target of this Make-rule. */
6106 if (opts->print_deps)
6108 pfile->deps_allocated_size = 200;
6109 pfile->deps_buffer = (char *) Safe_malloc (pfile->deps_allocated_size);
6110 pfile->deps_buffer[0] = 0;
6111 pfile->deps_size = 0;
6112 pfile->deps_column = 0;
6114 if (opts->deps_target)
6115 deps_output (pfile, opts->deps_target, ':');
6116 else if (*opts->in_fname == 0)
6117 deps_output (pfile, "-", ':');
6123 /* Discard all directory prefixes from filename. */
6124 if ((q = rindex (opts->in_fname, '/')) != NULL
6125 #ifdef DIR_SEPARATOR
6126 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6133 /* Copy remainder to mungable area. */
6134 p = (char *) alloca (strlen(q) + 8);
6137 /* Output P, but remove known suffixes. */
6141 && p[len - 2] == '.'
6142 && index("cCsSm", p[len - 1]))
6145 && p[len - 3] == '.'
6146 && p[len - 2] == 'c'
6147 && p[len - 1] == 'c')
6150 && p[len - 4] == '.'
6151 && p[len - 3] == 'c'
6152 && p[len - 2] == 'x'
6153 && p[len - 1] == 'x')
6156 && p[len - 4] == '.'
6157 && p[len - 3] == 'c'
6158 && p[len - 2] == 'p'
6159 && p[len - 1] == 'p')
6162 /* Supply our own suffix. */
6173 deps_output (pfile, p, ':');
6174 deps_output (pfile, opts->in_fname, ' ');
6179 /* Make sure data ends with a newline. And put a null after it. */
6181 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6182 /* Backslash-newline at end is not good enough. */
6183 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6184 fp->buf[fp->length++] = '\n';
6185 missing_newline = 1;
6187 fp->buf[fp->length] = '\0';
6189 /* Unless inhibited, convert trigraphs in the input. */
6195 /* Scan the -include files before the main input.
6196 We push these in reverse order, so that the first one is handled first. */
6198 pfile->no_record_file++;
6199 opts->pending = nreverse_pending (opts->pending);
6200 for (pend = opts->pending; pend; pend = pend->next)
6202 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6204 int fd = open (pend->arg, O_RDONLY, 0666);
6207 cpp_perror_with_name (pfile, pend->arg);
6208 return FATAL_EXIT_CODE;
6210 cpp_push_buffer (pfile, NULL, 0);
6211 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6214 pfile->no_record_file--;
6216 /* Free the pending list. */
6217 for (pend = opts->pending; pend; )
6219 struct cpp_pending *next = pend->next;
6223 opts->pending = NULL;
6226 /* Scan the input, processing macros and directives. */
6228 rescan (&outbuf, 0);
6230 if (missing_newline)
6233 if (CPP_PEDANTIC (pfile) && missing_newline)
6234 pedwarn ("file does not end in newline");
6237 if (finclude (pfile, f, fname, 0, NULL_PTR))
6238 output_line_command (pfile, 0, same_file);
6239 return SUCCESS_EXIT_CODE;
6246 bzero ((char *) pfile, sizeof (cpp_reader));
6247 pfile->get_token = cpp_get_token;
6249 pfile->token_buffer_size = 200;
6250 pfile->token_buffer = (U_CHAR*)Safe_malloc (pfile->token_buffer_size);
6251 CPP_SET_WRITTEN (pfile, 0);
6253 pfile->system_include_depth = 0;
6254 pfile->dont_repeat_files = 0;
6255 pfile->all_include_files = 0;
6256 pfile->max_include_len = 0;
6257 pfile->timebuf = NULL;
6258 pfile->only_seen_white = 1;
6259 pfile->buffer = CPP_NULL_BUFFER(pfile);
6262 static struct cpp_pending *
6264 struct cpp_pending *list)
6267 register struct cpp_pending *prev = 0, *next, *pend;
6268 for (pend = list; pend; pend = next)
6283 struct cpp_pending *pend
6284 = (struct cpp_pending*)Safe_malloc (sizeof (struct cpp_pending));
6287 pend->next = CPP_OPTIONS (pfile)->pending;
6288 CPP_OPTIONS (pfile)->pending = pend;
6291 /* Handle command-line options in (argc, argv).
6292 Can be called multiple times, to handle multiple sets of options.
6293 Returns if an unrecognized option is seen.
6294 Returns number of handled arguments. */
6297 cpp_handle_options (
6303 struct cpp_options *opts = CPP_OPTIONS (pfile);
6304 for (i = 0; i < argc; i++) {
6305 if (argv[i][0] != '-') {
6306 if (opts->out_fname != NULL)
6307 fatal ("Usage: %s [switches] input output", argv[0]);
6308 else if (opts->in_fname != NULL)
6309 opts->out_fname = argv[i];
6311 opts->in_fname = argv[i];
6313 switch (argv[i][1]) {
6316 if (!strcmp (argv[i], "-include")
6317 || !strcmp (argv[i], "-imacros")) {
6319 fatal ("Filename missing after `%s' option", argv[i]);
6321 push_pending (pfile, argv[i], argv[i+1]), i++;
6323 if (!strcmp (argv[i], "-iprefix")) {
6325 fatal ("Filename missing after `-iprefix' option");
6327 opts->include_prefix = argv[++i];
6329 if (!strcmp (argv[i], "-ifoutput")) {
6330 opts->output_conditionals = 1;
6332 if (!strcmp (argv[i], "-isystem")) {
6333 struct file_name_list *dirtmp;
6336 fatal ("Filename missing after `-isystem' option");
6338 dirtmp = (struct file_name_list *)
6339 Safe_malloc (sizeof (struct file_name_list));
6341 dirtmp->control_macro = 0;
6342 dirtmp->c_system_include_path = 1;
6343 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1]) + 1);
6344 strcpy (dirtmp->fname, argv[++i]);
6345 dirtmp->got_name_map = 0;
6347 if (opts->before_system == 0)
6348 opts->before_system = dirtmp;
6350 opts->last_before_system->next = dirtmp;
6351 opts->last_before_system = dirtmp; /* Tail follows the last one */
6353 /* Add directory to end of path for includes,
6354 with the default prefix at the front of its name. */
6355 if (!strcmp (argv[i], "-iwithprefix")) {
6356 struct file_name_list *dirtmp;
6359 if (opts->include_prefix != 0)
6360 prefix = opts->include_prefix;
6362 prefix = savestring (GCC_INCLUDE_DIR);
6363 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6364 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6365 prefix[strlen (prefix) - 7] = 0;
6368 dirtmp = (struct file_name_list *)
6369 Safe_malloc (sizeof (struct file_name_list));
6370 dirtmp->next = 0; /* New one goes on the end */
6371 dirtmp->control_macro = 0;
6372 dirtmp->c_system_include_path = 0;
6374 fatal ("Directory name missing after `-iwithprefix' option");
6376 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6377 + strlen (prefix) + 1);
6378 strcpy (dirtmp->fname, prefix);
6379 strcat (dirtmp->fname, argv[++i]);
6380 dirtmp->got_name_map = 0;
6382 if (opts->after_include == 0)
6383 opts->after_include = dirtmp;
6385 opts->last_after_include->next = dirtmp;
6386 opts->last_after_include = dirtmp; /* Tail follows the last one */
6388 /* Add directory to main path for includes,
6389 with the default prefix at the front of its name. */
6390 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6391 struct file_name_list *dirtmp;
6394 if (opts->include_prefix != 0)
6395 prefix = opts->include_prefix;
6397 prefix = savestring (GCC_INCLUDE_DIR);
6398 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6399 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6400 prefix[strlen (prefix) - 7] = 0;
6403 dirtmp = (struct file_name_list *)
6404 Safe_malloc (sizeof (struct file_name_list));
6405 dirtmp->next = 0; /* New one goes on the end */
6406 dirtmp->control_macro = 0;
6407 dirtmp->c_system_include_path = 0;
6409 fatal ("Directory name missing after `-iwithprefixbefore' option");
6411 dirtmp->fname = (char *) Safe_malloc (strlen (argv[i+1])
6412 + strlen (prefix) + 1);
6413 strcpy (dirtmp->fname, prefix);
6414 strcat (dirtmp->fname, argv[++i]);
6415 dirtmp->got_name_map = 0;
6417 append_include_chain (pfile, dirtmp, dirtmp);
6419 /* Add directory to end of path for includes. */
6420 if (!strcmp (argv[i], "-idirafter")) {
6421 struct file_name_list *dirtmp;
6423 dirtmp = (struct file_name_list *)
6424 Safe_malloc (sizeof (struct file_name_list));
6425 dirtmp->next = 0; /* New one goes on the end */
6426 dirtmp->control_macro = 0;
6427 dirtmp->c_system_include_path = 0;
6429 fatal ("Directory name missing after `-idirafter' option");
6431 dirtmp->fname = argv[++i];
6432 dirtmp->got_name_map = 0;
6434 if (opts->after_include == 0)
6435 opts->after_include = dirtmp;
6437 opts->last_after_include->next = dirtmp;
6438 opts->last_after_include = dirtmp; /* Tail follows the last one */
6443 if (opts->out_fname != NULL)
6444 fatal ("Output filename specified twice");
6446 fatal ("Filename missing after -o option");
6447 opts->out_fname = argv[++i];
6448 if (!strcmp (opts->out_fname, "-"))
6449 opts->out_fname = "";
6453 if (!strcmp (argv[i], "-pedantic"))
6454 CPP_PEDANTIC (pfile) = 1;
6455 else if (!strcmp (argv[i], "-pedantic-errors")) {
6456 CPP_PEDANTIC (pfile) = 1;
6457 opts->pedantic_errors = 1;
6460 else if (!strcmp (argv[i], "-pcp")) {
6461 char *pcp_fname = argv[++i];
6463 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6464 ? fopen (pcp_fname, "w")
6465 : fdopen (dup (fileno (stdout)), "w"));
6466 if (pcp_outfile == 0)
6467 cpp_pfatal_with_name (pfile, pcp_fname);
6474 if (!strcmp (argv[i], "-traditional")) {
6475 opts->traditional = 1;
6476 if (opts->dollars_in_ident > 0)
6477 opts->dollars_in_ident = 1;
6478 } else if (!strcmp (argv[i], "-trigraphs")) {
6480 opts->no_trigraphs = 0;
6485 if (! strcmp (argv[i], "-lang-c"))
6486 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6487 if (! strcmp (argv[i], "-lang-c++"))
6488 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6489 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6490 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6491 if (! strcmp (argv[i], "-lang-objc"))
6492 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6493 if (! strcmp (argv[i], "-lang-objc++"))
6494 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6495 if (! strcmp (argv[i], "-lang-asm"))
6497 if (! strcmp (argv[i], "-lint"))
6499 if (! strcmp (argv[i], "-lang-chill"))
6500 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6501 opts->traditional = 1, opts->no_trigraphs = 1;
6505 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6509 opts->inhibit_warnings = 1;
6513 if (!strcmp (argv[i], "-Wtrigraphs"))
6514 opts->warn_trigraphs = 1;
6515 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6516 opts->warn_trigraphs = 0;
6517 else if (!strcmp (argv[i], "-Wcomment"))
6518 opts->warn_comments = 1;
6519 else if (!strcmp (argv[i], "-Wno-comment"))
6520 opts->warn_comments = 0;
6521 else if (!strcmp (argv[i], "-Wcomments"))
6522 opts->warn_comments = 1;
6523 else if (!strcmp (argv[i], "-Wno-comments"))
6524 opts->warn_comments = 0;
6525 else if (!strcmp (argv[i], "-Wtraditional"))
6526 opts->warn_stringify = 1;
6527 else if (!strcmp (argv[i], "-Wno-traditional"))
6528 opts->warn_stringify = 0;
6529 else if (!strcmp (argv[i], "-Wimport"))
6530 opts->warn_import = 1;
6531 else if (!strcmp (argv[i], "-Wno-import"))
6532 opts->warn_import = 0;
6533 else if (!strcmp (argv[i], "-Werror"))
6534 opts->warnings_are_errors = 1;
6535 else if (!strcmp (argv[i], "-Wno-error"))
6536 opts->warnings_are_errors = 0;
6537 else if (!strcmp (argv[i], "-Wall"))
6539 opts->warn_trigraphs = 1;
6540 opts->warn_comments = 1;
6545 /* The style of the choices here is a bit mixed.
6546 The chosen scheme is a hybrid of keeping all options in one string
6547 and specifying each option in a separate argument:
6548 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6549 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6550 -M[M][G][D file]. This is awkward to handle in specs, and is not
6552 /* ??? -MG must be specified in addition to one of -M or -MM.
6553 This can be relaxed in the future without breaking anything.
6554 The converse isn't true. */
6556 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6557 if (!strcmp (argv[i], "-MG"))
6559 opts->print_deps_missing_files = 1;
6562 if (!strcmp (argv[i], "-M"))
6563 opts->print_deps = 2;
6564 else if (!strcmp (argv[i], "-MM"))
6565 opts->print_deps = 1;
6566 else if (!strcmp (argv[i], "-MD"))
6567 opts->print_deps = 2;
6568 else if (!strcmp (argv[i], "-MMD"))
6569 opts->print_deps = 1;
6570 /* For -MD and -MMD options, write deps on file named by next arg. */
6571 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6574 fatal ("Filename missing after %s option", argv[i]);
6575 opts->deps_file = argv[++i];
6579 /* For -M and -MM, write deps on standard output
6580 and suppress the usual output. */
6581 opts->no_output = 1;
6587 char *p = argv[i] + 2;
6589 while ((c = *p++) != 0) {
6590 /* Arg to -d specifies what parts of macros to dump */
6593 opts->dump_macros = dump_only;
6594 opts->no_output = 1;
6597 opts->dump_macros = dump_names;
6600 opts->dump_macros = dump_definitions;
6608 if (argv[i][2] == '3')
6609 opts->debug_output = 1;
6613 fprintf (stderr, "GNU CPP version %s", version_string);
6614 #ifdef TARGET_VERSION
6617 fprintf (stderr, "\n");
6618 // opts->verbose = 1;
6622 opts->print_include_names = 1;
6626 if (argv[i][2] != 0)
6627 push_pending (pfile, "-D", argv[i] + 2);
6628 else if (i + 1 == argc)
6629 fatal ("Macro name missing after -D option");
6631 i++, push_pending (pfile, "-D", argv[i]);
6638 if (argv[i][2] != 0)
6640 else if (i + 1 == argc)
6641 fatal ("Assertion missing after -A option");
6645 if (!strcmp (p, "-")) {
6646 struct cpp_pending **ptr;
6647 /* -A- eliminates all predefined macros and assertions.
6648 Let's include also any that were specified earlier
6649 on the command line. That way we can get rid of any
6650 that were passed automatically in from GCC. */
6652 opts->inhibit_predefs = 1;
6653 for (ptr = &opts->pending; *ptr != NULL; )
6655 struct cpp_pending *pend = *ptr;
6656 if (pend->cmd && pend->cmd[0] == '-'
6657 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6666 push_pending (pfile, "-A", p);
6671 case 'U': /* JF #undef something */
6672 if (argv[i][2] != 0)
6673 push_pending (pfile, "-U", argv[i] + 2);
6674 else if (i + 1 == argc)
6675 fatal ("Macro name missing after -U option");
6677 push_pending (pfile, "-U", argv[i+1]), i++;
6681 opts->put_out_comments = 1;
6684 case 'E': /* -E comes from cc -E; ignore it. */
6688 opts->no_line_commands = 1;
6691 case '$': /* Don't include $ in identifiers. */
6692 opts->dollars_in_ident = 0;
6695 case 'I': /* Add directory to path for includes. */
6697 struct file_name_list *dirtmp;
6699 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6700 && !strcmp (argv[i] + 2, "-")) {
6701 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6702 /* Don't use any preceding -I directories for #include <...>. */
6703 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6706 dirtmp = (struct file_name_list *)
6707 Safe_malloc (sizeof (struct file_name_list));
6708 dirtmp->next = 0; /* New one goes on the end */
6709 dirtmp->control_macro = 0;
6710 dirtmp->c_system_include_path = 0;
6711 if (argv[i][2] != 0)
6712 dirtmp->fname = argv[i] + 2;
6713 else if (i + 1 == argc)
6714 fatal ("Directory name missing after -I option");
6716 dirtmp->fname = argv[++i];
6717 dirtmp->got_name_map = 0;
6718 append_include_chain (pfile, dirtmp, dirtmp);
6724 if (!strcmp (argv[i], "-nostdinc"))
6725 /* -nostdinc causes no default include directories.
6726 You must specify all include-file directories with -I. */
6727 opts->no_standard_includes = 1;
6728 else if (!strcmp (argv[i], "-nostdinc++"))
6729 /* -nostdinc++ causes no default C++-specific include directories. */
6730 opts->no_standard_cplusplus_includes = 1;
6732 else if (!strcmp (argv[i], "-noprecomp"))
6738 /* Sun compiler passes undocumented switch "-undef".
6739 Let's assume it means to inhibit the predefined symbols. */
6740 opts->inhibit_predefs = 1;
6743 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6744 if (opts->in_fname == NULL) {
6745 opts->in_fname = "";
6747 } else if (opts->out_fname == NULL) {
6748 opts->out_fname = "";
6750 } /* else fall through into error */
6764 struct cpp_options *opts = CPP_OPTIONS (pfile);
6766 if (opts->print_deps)
6768 /* Stream on which to print the dependency information. */
6771 /* Don't actually write the deps file if compilation has failed. */
6772 if (pfile->errors == 0)
6774 char *deps_mode = opts->print_deps_append ? "a" : "w";
6775 if (opts->deps_file == 0)
6776 deps_stream = stdout;
6777 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6778 cpp_pfatal_with_name (pfile, opts->deps_file);
6779 fputs (pfile->deps_buffer, deps_stream);
6780 putc ('\n', deps_stream);
6781 if (opts->deps_file)
6783 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6784 fatal ("I/O error on output");
6790 /* Free resources used by PFILE. */
6797 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6798 cpp_pop_buffer (pfile);
6800 if (pfile->token_buffer)
6802 free (pfile->token_buffer);
6803 pfile->token_buffer = NULL;
6806 if (pfile->deps_buffer)
6808 free (pfile->deps_buffer);
6809 pfile->deps_buffer = NULL;
6810 pfile->deps_allocated_size = 0;
6813 while (pfile->if_stack)
6815 IF_STACK_FRAME *temp = pfile->if_stack;
6816 pfile->if_stack = temp->next;
6820 while (pfile->dont_repeat_files)
6822 struct file_name_list *temp = pfile->dont_repeat_files;
6823 pfile->dont_repeat_files = temp->next;
6828 while (pfile->all_include_files)
6830 struct file_name_list *temp = pfile->all_include_files;
6831 pfile->all_include_files = temp->next;
6836 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6838 register struct import_file *imp = pfile->import_hash_table[i];
6841 struct import_file *next = imp->next;
6846 pfile->import_hash_table[i] = 0;
6849 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6851 while (pfile->assertion_hashtab[i])
6852 delete_assertion (pfile->assertion_hashtab[i]);
6855 cpp_hash_cleanup (pfile);
6861 struct directive *keyword,
6862 U_CHAR *buf, U_CHAR *limit)
6864 long symstart; /* remember where symbol name starts */
6866 int sym_length; /* and how long it is */
6867 struct arglist *tokens = NULL;
6869 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6870 && !CPP_BUFFER (pfile)->system_header_p)
6871 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6873 cpp_skip_hspace (pfile);
6874 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6875 parse_name (pfile, GETC());
6876 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6879 cpp_skip_hspace (pfile);
6880 if (PEEKC() != '(') {
6881 cpp_error (pfile, "missing token-sequence in `#assert'");
6887 tokens = read_token_list (pfile, &error_flag);
6891 cpp_error (pfile, "empty token-sequence in `#assert'");
6894 cpp_skip_hspace (pfile);
6896 if (c != EOF && c != '\n')
6897 cpp_pedwarn (pfile, "junk at end of `#assert'");
6898 skip_rest_of_line (pfile);
6901 /* If this name isn't already an assertion name, make it one.
6902 Error if it was already in use in some other way. */
6905 ASSERTION_HASHNODE *hp;
6906 U_CHAR *symname = pfile->token_buffer + symstart;
6907 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6908 struct tokenlist_list *value
6909 = (struct tokenlist_list *) Safe_malloc (sizeof (struct tokenlist_list));
6911 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6913 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6914 cpp_error (pfile, "`defined' redefined as assertion");
6915 hp = assertion_install (pfile, symname, sym_length, hashcode);
6918 /* Add the spec'd token-sequence to the list of such. */
6919 value->tokens = tokens;
6920 value->next = hp->value;
6923 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6926 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6927 skip_rest_of_line (pfile);
6934 struct directive *keyword,
6935 U_CHAR *buf, U_CHAR *limit)
6937 long symstart; /* remember where symbol name starts */
6938 int sym_length; /* and how long it is */
6941 struct arglist *tokens = NULL;
6942 int tokens_specified = 0;
6944 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6945 && !CPP_BUFFER (pfile)->system_header_p)
6946 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6948 cpp_skip_hspace (pfile);
6950 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6951 parse_name (pfile, GETC());
6952 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6955 cpp_skip_hspace (pfile);
6956 if (PEEKC() == '(') {
6959 tokens = read_token_list (pfile, &error_flag);
6963 cpp_error (pfile, "empty token list in `#unassert'");
6967 tokens_specified = 1;
6970 cpp_skip_hspace (pfile);
6972 if (c != EOF && c != '\n')
6973 cpp_error (pfile, "junk at end of `#unassert'");
6974 skip_rest_of_line (pfile);
6977 ASSERTION_HASHNODE *hp;
6978 U_CHAR *symname = pfile->token_buffer + symstart;
6979 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6980 struct tokenlist_list *tail, *prev;
6982 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6986 /* If no token list was specified, then eliminate this assertion
6988 if (! tokens_specified)
6989 delete_assertion (hp);
6991 /* If a list of tokens was given, then delete any matching list. */
6996 struct tokenlist_list *next = tail->next;
6997 if (compare_token_lists (tail->tokens, tokens)) {
7001 hp->value = tail->next;
7002 free_token_list (tail->tokens);
7012 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7015 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7016 skip_rest_of_line (pfile);
7020 /* Test whether there is an assertion named NAME
7021 and optionally whether it has an asserted token list TOKENS.
7022 NAME is not null terminated; its length is SYM_LENGTH.
7023 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7030 int tokens_specified,
7031 struct arglist *tokens)
7033 ASSERTION_HASHNODE *hp;
7034 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7036 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7037 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7039 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7041 /* It is not an assertion; just return false. */
7044 /* If no token list was specified, then value is 1. */
7045 if (! tokens_specified)
7049 struct tokenlist_list *tail;
7053 /* If a list of tokens was given,
7054 then succeed if the assertion records a matching list. */
7057 if (compare_token_lists (tail->tokens, tokens))
7062 /* Fail if the assertion has no matching list. */
7067 /* Compare two lists of tokens for equality including order of tokens. */
7070 compare_token_lists (
7071 struct arglist *l1, struct arglist *l2 )
7074 if (l1->length != l2->length)
7076 if (strncmp (l1->name, l2->name, l1->length))
7082 /* Succeed if both lists end at the same time. */
7087 reverse_token_list (
7088 struct arglist *tokens)
7090 register struct arglist *prev = 0, *this, *next;
7091 for (this = tokens; this; this = next)
7100 /* Read a space-separated list of tokens ending in a close parenthesis.
7101 Return a list of strings, in the order they were written.
7102 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7104 static struct arglist *
7109 struct arglist *token_ptrs = 0;
7114 FORWARD (1); /* Skip '(' */
7116 /* Loop over the assertion value tokens. */
7119 struct arglist *temp;
7120 long name_written = CPP_WRITTEN (pfile);
7121 int eofp = 0; int c;
7123 cpp_skip_hspace (pfile);
7127 /* Find the end of the token. */
7130 CPP_PUTC (pfile, c);
7138 CPP_PUTC (pfile, c);
7140 else if (c == '"' || c == '\'')
7143 cpp_get_token (pfile);
7149 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7150 && c != '"' && c != '\'')
7152 CPP_PUTC (pfile, c);
7155 if (c != EOF) FORWARD(-1);
7158 length = CPP_WRITTEN (pfile) - name_written;
7159 temp = (struct arglist *)
7160 Safe_malloc (sizeof (struct arglist) + length + 1);
7161 temp->name = (U_CHAR *) (temp + 1);
7162 bcopy ((char *) (pfile->token_buffer + name_written),
7163 (char *) temp->name, length);
7164 temp->name[length] = 0;
7165 temp->next = token_ptrs;
7167 temp->length = length;
7169 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7171 if (c == EOF || c == '\n')
7174 "unterminated token sequence following `#' operator");
7179 /* We accumulated the names in reverse order.
7180 Now reverse them to get the proper order. */
7181 return reverse_token_list (token_ptrs);
7186 struct arglist *tokens)
7189 struct arglist *next = tokens->next;
7190 free (tokens->name);
7196 /* Get the file-mode and data size of the file open on FD
7197 and store them in *MODE_POINTER and *SIZE_POINTER. */
7200 file_size_and_mode (
7203 long int *size_pointer)
7207 if (fstat (fd, &sbuf) < 0) return (-1);
7208 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7209 if (size_pointer) *size_pointer = sbuf.st_size;
7213 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7214 retrying if necessary. Return a negative value if an error occurs,
7215 otherwise return the actual number of bytes read,
7216 which must be LEN unless end-of-file was reached. */
7226 int nchars = read (desc, ptr, left);
7247 unsigned size = strlen (input);
7248 char *output = Safe_malloc (size + 1);
7249 strcpy (output, input);
7253 /* Initialize PMARK to remember the current position of PFILE. */
7256 struct parse_marker *pmark,
7259 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7260 pmark->next = pbuf->marks;
7261 pbuf->marks = pmark;
7263 pmark->position = pbuf->cur - pbuf->buf;
7266 /* Cleanup PMARK - we no longer need it. */
7269 struct parse_marker *pmark)
7271 struct parse_marker **pp = &pmark->buf->marks;
7272 for (; ; pp = &(*pp)->next) {
7273 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7274 if (*pp == pmark) break;
7279 /* Backup the current position of PFILE to that saved in PMARK. */
7283 struct parse_marker *pmark,
7286 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7287 if (pbuf != pmark->buf)
7288 fatal ("internal error %s", "parse_goto_mark");
7289 pbuf->cur = pbuf->buf + pmark->position;
7292 /* Reset PMARK to point to the current position of PFILE. (Same
7293 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7297 struct parse_marker *pmark,
7300 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7301 if (pbuf != pmark->buf)
7302 fatal ("internal error %s", "parse_move_mark");
7303 pmark->position = pbuf->cur - pbuf->buf;
7307 cpp_read_check_assertion (
7310 int name_start = CPP_WRITTEN (pfile);
7311 int name_length, name_written;
7313 FORWARD (1); /* Skip '#' */
7314 cpp_skip_hspace (pfile);
7315 parse_name (pfile, GETC ());
7316 name_written = CPP_WRITTEN (pfile);
7317 name_length = name_written - name_start;
7318 cpp_skip_hspace (pfile);
7319 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7322 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7323 result = check_assertion (pfile,
7324 pfile->token_buffer + name_start, name_length,
7328 result = check_assertion (pfile,
7329 pfile->token_buffer + name_start, name_length,
7330 0, (struct arglist *)NULL_PTR);
7331 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7336 cpp_print_file_and_line (pfile)
7339 cpp_buffer *ip = cpp_file_buffer (pfile);
7344 cpp_buf_line_and_col (ip, &line, &col);
7345 cpp_file_line_for_message (pfile, ip->nominal_fname,
7346 line, pfile->show_column ? col : -1);
7351 cpp_error (pfile, msg, arg1, arg2, arg3)
7354 char *arg1, *arg2, *arg3;
7356 cpp_print_containing_files (pfile);
7357 cpp_print_file_and_line (pfile);
7358 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7361 /* Print error message but don't count it. */
7364 cpp_warning (pfile, msg, arg1, arg2, arg3)
7367 char *arg1, *arg2, *arg3;
7369 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7372 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7375 cpp_print_containing_files (pfile);
7376 cpp_print_file_and_line (pfile);
7377 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7380 /* Print an error message and maybe count it. */
7383 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7386 char *arg1, *arg2, *arg3;
7388 if (CPP_OPTIONS (pfile)->pedantic_errors)
7389 cpp_error (pfile, msg, arg1, arg2, arg3);
7391 cpp_warning (pfile, msg, arg1, arg2, arg3);
7395 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7399 char *arg1, *arg2, *arg3;
7401 cpp_buffer *ip = cpp_file_buffer (pfile);
7403 cpp_print_containing_files (pfile);
7406 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7408 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7412 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7416 char *arg1, *arg2, *arg3;
7420 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7423 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7426 cpp_print_containing_files (pfile);
7428 ip = cpp_file_buffer (pfile);
7431 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7433 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7437 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7441 char *arg1, *arg2, *arg3;
7443 if (CPP_OPTIONS (pfile)->pedantic_errors)
7444 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7446 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7449 /* Report a warning (or an error if pedantic_errors)
7450 giving specified file name and line number, not current. */
7453 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7458 char *arg1, *arg2, *arg3;
7460 if (!CPP_OPTIONS (pfile)->pedantic_errors
7461 && CPP_OPTIONS (pfile)->inhibit_warnings)
7464 cpp_file_line_for_message (pfile, file, line, -1);
7465 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7466 msg, arg1, arg2, arg3);
7469 /* This defines "errno" properly for VMS, and gives us EACCES. */
7476 #ifndef HAVE_STRERROR
7477 extern int sys_nerr;
7479 extern const char *const sys_errlist[];
7482 extern char *sys_errlist[];
7485 #else /* HAVE_STRERROR */
7489 char *strerror (int,...);
7493 * my_strerror - return the descriptive text associated with an `errno' code.
7497 my_strerror (errnum)
7503 #ifndef HAVE_STRERROR
7504 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7506 result = strerror (errnum);
7509 /* VAXCRTL's strerror() takes an optional second argument, which only
7510 matters when the first argument is EVMSERR. However, it's simplest
7511 just to pass it unconditionally. `vaxc$errno' is declared in
7512 <errno.h>, and maintained by the library in parallel with `errno'.
7513 We assume that caller's `errnum' either matches the last setting of
7514 `errno' by the library or else does not have the value `EVMSERR'. */
7516 result = strerror (errnum, vaxc$errno);
7520 result = "undocumented I/O error";
7525 /* Error including a message from `errno'. */
7528 cpp_error_from_errno (
7532 cpp_buffer *ip = cpp_file_buffer (pfile);
7534 cpp_print_containing_files (pfile);
7537 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7539 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno),NULL);
7543 cpp_perror_with_name (
7547 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7551 * No pre-compiled header file support.
7553 * Possibly different enum token codes for each C/C++ token.
7555 * Should clean up remaining directives to that do_XXX functions
7556 * only take two arguments and all have command_reads_line.
7558 * Find and cleanup remaining uses of static variables,
7560 * Support for trigraphs.
7562 * Support -dM flag (dump_all_macros).
7564 * Support for_lint flag.