2 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
4 Based on CCCP program by by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
29 #include "../src/config.h"
38 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
39 for the sake of machines with limited C compilers. */
44 #endif /* not EMACS */
45 #define GCC_INCLUDE_DIR "/usr/local/lib/gcc-lib/i386-unknown-freebsd_1.0/2.5.8/include"
46 #ifndef STANDARD_INCLUDE_DIR
47 #define STANDARD_INCLUDE_DIR "/usr/include"
50 #ifndef LOCAL_INCLUDE_DIR
51 #define LOCAL_INCLUDE_DIR "/usr/local/include"
54 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
56 #define PTR_INT_TYPE ptrdiff_t
58 #define PTR_INT_TYPE long
69 /* By default, colon separates directories in a path. */
70 #ifndef PATH_SEPARATOR
71 #define PATH_SEPARATOR ':'
83 #include <sys/time.h> /* for __DATE__ and __TIME__ */
84 #include <sys/resource.h>
86 /*#include <sys/param.h> CYGNUS LOCAL: shebs -noquiet */
87 // #include <sys/times.h>
93 /* This defines "errno" properly for VMS, and gives us EACCES. */
96 extern char *index ();
97 extern char *rindex ();
105 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
106 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
108 /* Find the largest host integer type and set its size and type. */
110 #ifndef HOST_BITS_PER_WIDE_INT
112 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
113 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
114 #define HOST_WIDE_INT long
116 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
117 #define HOST_WIDE_INT int
123 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
127 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
130 /* Define a generic NULL if one hasn't already been defined. */
137 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
138 #define GENERIC_PTR void *
140 #define GENERIC_PTR char *
145 #define NULL_PTR ((GENERIC_PTR)0)
148 #ifndef INCLUDE_LEN_FUDGE
149 #define INCLUDE_LEN_FUDGE 0
152 /* Symbols to predefine. */
154 #ifdef CPP_PREDEFINES
155 static char *predefs = CPP_PREDEFINES;
157 static char *predefs = "";
160 /* We let tm.h override the types used here, to handle trivial differences
161 such as the choice of unsigned int or long unsigned int for size_t.
162 When machines start needing nontrivial differences in the size type,
163 it would be best to do something here to figure out automatically
164 from other information what type to use. */
166 /* The string value for __SIZE_TYPE__. */
169 #define SIZE_TYPE "long unsigned int"
172 /* The string value for __PTRDIFF_TYPE__. */
175 #define PTRDIFF_TYPE "long int"
178 /* The string value for __WCHAR_TYPE__. */
181 #define WCHAR_TYPE "int"
183 #define CPP_WCHAR_TYPE(PFILE) \
184 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
186 /* The string value for __USER_LABEL_PREFIX__ */
188 #ifndef USER_LABEL_PREFIX
189 #define USER_LABEL_PREFIX ""
192 /* The string value for __REGISTER_PREFIX__ */
194 #ifndef REGISTER_PREFIX
195 #define REGISTER_PREFIX ""
199 /* In the definition of a #assert name, this structure forms
200 a list of the individual values asserted.
201 Each value is itself a list of "tokens".
202 These are strings that are compared by name. */
204 struct tokenlist_list {
205 struct tokenlist_list *next;
206 struct arglist *tokens;
209 struct assertion_hashnode {
210 struct assertion_hashnode *next; /* double links for easy deletion */
211 struct assertion_hashnode *prev;
212 /* also, a back pointer to this node's hash
213 chain is kept, in case the node is the head
214 of the chain and gets deleted. */
215 struct assertion_hashnode **bucket_hdr;
216 int length; /* length of token, for quick comparison */
217 U_CHAR *name; /* the actual name */
218 /* List of token-sequences. */
219 struct tokenlist_list *value;
222 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
223 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
225 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
226 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
227 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
228 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
229 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
230 (Note that it is false while we're expanding marco *arguments*.) */
231 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
233 /* Move all backslash-newline pairs out of embarrassing places.
234 Exchange all such pairs following BP
235 with any potentially-embarrassing characters that follow them.
236 Potentially-embarrassing characters are / and *
237 (because a backslash-newline inside a comment delimiter
238 would cause it not to be recognized). */
240 #define NEWLINE_FIX \
241 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
243 /* Same, but assume we've already read the potential '\\' into C. */
244 #define NEWLINE_FIX1(C) do { \
245 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
249 struct cpp_pending *next;
254 /* Forward declarations. */
256 extern char *xmalloc ();
258 static void add_import ();
259 static void append_include_chain ();
260 static void make_undef ();
261 static void make_assertion ();
262 static void path_include ();
263 static void initialize_builtins ();
264 static void initialize_char_syntax ();
265 static void dump_arg_n ();
266 static void dump_defn_1 ();
267 extern void delete_macro ();
268 static void trigraph_pcp ();
269 static int finclude ();
270 static void validate_else ();
271 static int comp_def_part ();
272 extern void fancy_abort ();
273 static void pipe_closed ();
274 static void print_containing_files ();
275 static int lookup_import ();
276 static int redundant_include_p ();
277 static is_system_include ();
278 static struct file_name_map *read_name_map ();
279 static char *read_filename_string ();
280 static int open_include_file ();
281 static int check_preconditions ();
282 static void pcfinclude ();
283 static void pcstring_used ();
284 static int check_macro_name ();
285 static int compare_defs ();
286 static int compare_token_lists ();
287 static HOST_WIDE_INT eval_if_expression ();
288 static int change_newlines ();
290 static int file_size_and_mode ();
291 static struct arglist *read_token_list ();
292 static void free_token_list ();
293 static int safe_read ();
294 static void push_macro_expansion PARAMS ((cpp_reader *,
295 U_CHAR*, int, HASHNODE*));
296 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending*));
297 extern char *xrealloc ();
298 extern char *xcalloc ();
299 static char *savestring ();
301 static void conditional_skip ();
302 static void skip_if_group ();
304 /* Last arg to output_line_command. */
305 enum file_change_code {same_file, enter_file, leave_file};
307 /* External declarations. */
309 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader*));
311 extern char *getenv ();
312 extern FILE *fdopen ();
313 extern char *version_string;
314 extern struct tm *localtime ();
316 /* These functions are declared to return int instead of void since they
317 are going to be placed in a table and some old compilers have trouble with
318 pointers to functions returning void. */
320 static int do_define ();
321 static int do_line ();
322 static int do_include ();
323 static int do_undef ();
324 static int do_error ();
325 static int do_pragma ();
326 static int do_ident ();
328 static int do_xifdef ();
329 static int do_else ();
330 static int do_elif ();
331 static int do_endif ();
332 static int do_sccs ();
333 static int do_once ();
334 static int do_assert ();
335 static int do_unassert ();
336 static int do_warning ();
338 struct file_name_list
340 struct file_name_list *next;
342 /* If the following is nonzero, it is a macro name.
343 Don't include the file again if that macro is defined. */
344 U_CHAR *control_macro;
345 /* If the following is nonzero, it is a C-language system include
347 int c_system_include_path;
348 /* Mapping of file names for this directory. */
349 struct file_name_map *name_map;
350 /* Non-zero if name_map is valid. */
354 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
355 via the same directory as the file that #included it. */
356 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
358 /* #include "file" looks in source file dir, then stack. */
359 /* #include <file> just looks in the stack. */
360 /* -I directories are added to the end, then the defaults are added. */
362 static struct default_include {
363 char *fname; /* The name of the directory. */
364 int cplusplus; /* Only look here if we're compiling C++. */
365 int cxx_aware; /* Includes in this directory don't need to
366 be wrapped in extern "C" when compiling
368 } include_defaults_array[]
369 #ifdef INCLUDE_DEFAULTS
373 /* Pick up GNU C++ specific include files. */
374 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
376 /* This is the dir for fixincludes. Put it just before
377 the files that we fix. */
378 { GCC_INCLUDE_DIR, 0, 0 },
379 /* For cross-compilation, this dir name is generated
380 automatically in Makefile.in. */
381 { CROSS_INCLUDE_DIR, 0, 0 },
382 /* This is another place that the target system's headers might be. */
383 { TOOL_INCLUDE_DIR, 0, 1 },
384 { LOCAL_INCLUDE_DIR, 0, 1 },
385 #else /* not CROSS_COMPILE */
386 /* This should be /usr/local/include and should come before
387 the fixincludes-fixed header files. */
388 { LOCAL_INCLUDE_DIR, 0, 1 },
389 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
390 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
391 { TOOL_INCLUDE_DIR, 0, 1 },
392 /* This is the dir for fixincludes. Put it just before
393 the files that we fix. */
394 { GCC_INCLUDE_DIR, 0, 0 },
395 /* Some systems have an extra dir of include files. */
396 #ifdef SYSTEM_INCLUDE_DIR
397 { SYSTEM_INCLUDE_DIR, 0, 0 },
399 { STANDARD_INCLUDE_DIR, 0, 0 },
400 #endif /* not CROSS_COMPILE */
403 #endif /* no INCLUDE_DEFAULTS */
405 /* `struct directive' defines one #-directive, including how to handle it. */
408 int length; /* Length of name */
409 int (*func)(); /* Function to handle directive */
410 char *name; /* Name of directive */
411 enum node_type type; /* Code which describes which directive. */
412 char command_reads_line; /* One if rest of line is read by func. */
413 char traditional_comments; /* Nonzero: keep comments if -traditional. */
414 char pass_thru; /* Copy preprocessed directive to output file.*/
417 /* Here is the actual list of #-directives, most-often-used first.
418 The initialize_builtins function assumes #define is the very first. */
420 static struct directive directive_table[] = {
421 { 6, do_define, "define", T_DEFINE, 0, 1},
422 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
423 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
424 { 7, do_include, "include", T_INCLUDE, 1},
425 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
426 { 6, do_include, "import", T_IMPORT, 1},
427 { 5, do_endif, "endif", T_ENDIF, 1},
428 { 4, do_else, "else", T_ELSE, 1},
429 { 2, do_if, "if", T_IF, 1},
430 { 4, do_elif, "elif", T_ELIF, 1},
431 { 5, do_undef, "undef", T_UNDEF},
432 { 5, do_error, "error", T_ERROR},
433 { 7, do_warning, "warning", T_WARNING},
434 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
435 { 4, do_line, "line", T_LINE, 1},
436 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
437 #ifdef SCCS_DIRECTIVE
438 { 4, do_sccs, "sccs", T_SCCS},
440 { 6, do_assert, "assert", T_ASSERT, 1},
441 { 8, do_unassert, "unassert", T_UNASSERT, 1},
442 { -1, 0, "", T_UNUSED},
445 /* table to tell if char can be part of a C identifier. */
446 U_CHAR is_idchar[256];
447 /* table to tell if char can be first char of a c identifier. */
448 U_CHAR is_idstart[256];
449 /* table to tell if c is horizontal space. */
450 U_CHAR is_hor_space[256];
451 /* table to tell if c is horizontal or vertical space. */
452 static U_CHAR is_space[256];
454 /* Initialize syntactic classifications of characters. */
457 initialize_char_syntax (
458 struct cpp_options *opts)
463 * Set up is_idchar and is_idstart tables. These should be
464 * faster than saying (is_alpha (c) || c == '_'), etc.
465 * Set up these things before calling any routines tthat
468 for (i = 'a'; i <= 'z'; i++) {
469 is_idchar[i - 'a' + 'A'] = 1;
471 is_idstart[i - 'a' + 'A'] = 1;
474 for (i = '0'; i <= '9'; i++)
478 is_idchar['$'] = opts->dollars_in_ident;
479 is_idstart['$'] = opts->dollars_in_ident;
481 /* horizontal space table */
482 is_hor_space[' '] = 1;
483 is_hor_space['\t'] = 1;
484 is_hor_space['\v'] = 1;
485 is_hor_space['\f'] = 1;
486 is_hor_space['\r'] = 1;
497 /* Place into PFILE a quoted string representing the string SRC.
498 Caller must reserve enough space in pfile->token_buffer. */
506 CPP_PUTC_Q (pfile, '\"');
508 switch ((c = *src++))
512 CPP_PUTC_Q (pfile, c);
515 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
516 CPP_ADJUST_WRITTEN (pfile, 4);
522 CPP_PUTC_Q (pfile, '\\');
523 CPP_PUTC_Q (pfile, c);
527 CPP_PUTC_Q (pfile, '\"');
528 CPP_NUL_TERMINATE_Q (pfile);
533 /* Make sure PFILE->token_buffer will hold at least N more chars. */
540 long old_written = CPP_WRITTEN (pfile);
541 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
542 pfile->token_buffer = (U_CHAR*)
543 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
544 CPP_SET_WRITTEN (pfile, old_written);
549 * process a given definition string, for initialization
550 * If STR is just an identifier, define it with value 1.
551 * If STR has anything after the identifier, then it should
552 * be identifier=definition.
566 cpp_error (pfile, "malformed option `-D %s'", str);
569 while (is_idchar[*++p])
573 buf = (U_CHAR *) alloca (p - buf + 4);
574 strcpy ((char *)buf, str);
575 strcat ((char *)buf, " 1");
579 cpp_error (pfile, "malformed option `-D %s'", str);
585 /* Copy the entire option so we can modify it. */
586 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
587 strncpy (buf, str, p - str);
588 /* Change the = to a space. */
590 /* Scan for any backslash-newline and remove it. */
595 if (*p == '\\' && p[1] == '\n')
603 do_define (pfile, NULL, buf, buf + strlen (buf));
606 /* Process the string STR as if it appeared as the body of a #assert.
607 OPTION is the option name for which STR was the argument. */
616 struct directive *kt;
619 /* Copy the entire option so we can modify it. */
620 buf = (U_CHAR *) alloca (strlen (str) + 1);
621 strcpy ((char *) buf, str);
622 /* Scan for any backslash-newline and remove it. */
626 if (*p == '\\' && p[1] == '\n')
635 if (!is_idstart[*p]) {
636 cpp_error (pfile, "malformed option `%s %s'", option, str);
639 while (is_idchar[*++p])
641 while (*p == ' ' || *p == '\t') p++;
642 if (! (*p == 0 || *p == '(')) {
643 cpp_error (pfile, "malformed option `%s %s'", option, str);
647 ip = cpp_push_buffer (pfile, buf, strlen (buf));
648 do_assert (pfile, NULL, NULL, NULL);
649 cpp_pop_buffer (pfile);
652 /* Append a chain of `struct file_name_list's
653 to the end of the main include chain.
654 FIRST is the beginning of the chain to append, and LAST is the end. */
657 append_include_chain (
659 struct file_name_list *first,struct file_name_list *last)
661 struct cpp_options *opts = CPP_OPTIONS (pfile);
662 struct file_name_list *dir;
667 if (opts->include == 0)
668 opts->include = first;
670 opts->last_include->next = first;
672 if (opts->first_bracket_include == 0)
673 opts->first_bracket_include = first;
675 for (dir = first; ; dir = dir->next) {
676 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
677 if (len > pfile->max_include_len)
678 pfile->max_include_len = len;
684 opts->last_include = last;
687 /* Add output to `deps_buffer' for the -M switch.
688 STRING points to the text to be output.
689 SPACER is ':' for targets, ' ' for dependencies, zero for text
690 to be inserted literally. */
698 int size = strlen (string);
703 #ifndef MAX_OUTPUT_COLUMNS
704 #define MAX_OUTPUT_COLUMNS 72
707 && pfile->deps_column > 0
708 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
710 deps_output (pfile, " \\\n ", 0);
711 pfile->deps_column = 0;
714 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
716 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
717 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
718 pfile->deps_allocated_size);
720 if (spacer == ' ' && pfile->deps_column > 0)
721 pfile->deps_buffer[pfile->deps_size++] = ' ';
722 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
723 pfile->deps_size += size;
724 pfile->deps_column += size;
726 pfile->deps_buffer[pfile->deps_size++] = ':';
727 pfile->deps_buffer[pfile->deps_size] = 0;
730 /* Given a colon-separated list of file names PATH,
731 add all the names to the search path for include files. */
746 struct file_name_list *dirtmp;
748 /* Find the end of this name. */
749 while (*q != 0 && *q != PATH_SEPARATOR) q++;
751 /* An empty name in the path stands for the current directory. */
752 name = (char *) xmalloc (2);
756 /* Otherwise use the directory that is named. */
757 name = (char *) xmalloc (q - p + 1);
758 bcopy (p, name, q - p);
762 dirtmp = (struct file_name_list *)
763 xmalloc (sizeof (struct file_name_list));
764 dirtmp->next = 0; /* New one goes on the end */
765 dirtmp->control_macro = 0;
766 dirtmp->c_system_include_path = 0;
767 dirtmp->fname = name;
768 dirtmp->got_name_map = 0;
769 append_include_chain (pfile, dirtmp, dirtmp);
771 /* Advance past this name. */
775 /* Skip the colon. */
782 struct cpp_options *opts)
784 bzero ((char *) opts, sizeof *opts);
785 opts->in_fname = NULL;
786 opts->out_fname = NULL;
788 /* Initialize is_idchar to allow $. */
789 opts->dollars_in_ident = 1;
790 initialize_char_syntax (opts);
791 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
793 opts->no_line_commands = 0;
794 opts->no_trigraphs = 1;
795 opts->put_out_comments = 0;
796 opts->print_include_names = 0;
797 opts->dump_macros = dump_none;
800 opts->cplusplus_comments = 0;
807 opts->pedantic_errors = 0;
808 opts->inhibit_warnings = 0;
809 opts->warn_comments = 0;
810 opts->warn_import = 1;
811 opts->warnings_are_errors = 0;
834 HASHNODE *macro = (HASHNODE*)pbuf->data;
835 if (macro->type == T_DISABLED)
836 macro->type = T_MACRO;
837 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
862 register U_CHAR *p = bp;
864 /* First count the backslash-newline pairs here. */
866 while (p[0] == '\\' && p[1] == '\n')
869 /* What follows the backslash-newlines is not embarrassing. */
871 if (*p != '/' && *p != '*')
874 /* Copy all potentially embarrassing characters
875 that follow the backslash-newline pairs
876 down to where the pairs originally started. */
878 while (*p == '*' || *p == '/')
881 /* Now write the same number of pairs after the embarrassing chars. */
889 /* Assuming we have read '/'.
890 If this is the start of a comment (followed by '*' or '/'),
891 skip to the end of the comment, and return ' '.
892 Return EOF if we reached the end of file before the end of the comment.
893 If not the start of a comment, return '/'. */
901 while (PEEKC() == '\\' && PEEKN(1) == '\n')
916 while (c == '\\' && PEEKC() == '\n')
920 FORWARD(1), c = GETC();
922 if (prev_c == '*' && c == '/')
924 if (c == '\n' && linep)
928 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
935 return ' '; /* Allow // to be terminated by EOF. */
936 while (c == '\\' && PEEKC() == '\n')
945 /* Don't consider final '\n' to be part of comment. */
955 /* Skip whitespace \-newline and comments. Does not macro-expand. */
967 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
968 cpp_pedwarn (pfile, "%s in preprocessing directive",
969 c == '\f' ? "formfeed" : "vertical tab");
975 c = skip_comment (pfile, NULL);
978 if (c == EOF || c == '/')
981 else if (c == '\\' && PEEKN(1) == '\n') {
984 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
985 && is_hor_space[PEEKN(1)])
991 /* Read the rest of the current line.
992 The line is appended to PFILE's output buffer. */
998 struct cpp_options *opts = CPP_OPTIONS (pfile);
1008 if (PEEKC() == '\n')
1015 goto scan_directive_token;
1019 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
1020 goto scan_directive_token;
1024 if (CPP_PEDANTIC (pfile))
1025 cpp_pedwarn (pfile, "%s in preprocessing directive",
1026 c == '\f' ? "formfeed" : "vertical tab");
1032 scan_directive_token:
1034 cpp_get_token (pfile);
1037 CPP_PUTC (pfile, c);
1040 CPP_NUL_TERMINATE (pfile);
1047 long old = CPP_WRITTEN (pfile);
1048 copy_rest_of_line (pfile);
1049 CPP_SET_WRITTEN (pfile, old);
1052 /* Handle a possible # directive.
1053 '#' has already been read. */
1059 register struct directive *kt;
1062 U_CHAR *ident, *line_end;
1063 long old_written = CPP_WRITTEN (pfile);
1065 cpp_skip_hspace (pfile);
1068 if (c >= '0' && c <= '9')
1070 /* Handle # followed by a line number. */
1071 if (CPP_PEDANTIC (pfile))
1072 cpp_pedwarn (pfile, "`#' followed by integer");
1073 do_line (pfile, NULL);
1074 goto done_a_directive;
1077 /* Now find the directive name. */
1078 CPP_PUTC (pfile, '#');
1079 parse_name (pfile, GETC());
1080 ident = pfile->token_buffer + old_written + 1;
1081 ident_length = CPP_PWRITTEN (pfile) - ident;
1082 if (ident_length == 0 && PEEKC() == '\n')
1084 /* A line of just `#' becomes blank. */
1085 goto done_a_directive;
1089 if (ident_length == 0 || !is_idstart[*ident]) {
1091 while (is_idchar[*p]) {
1092 if (*p < '0' || *p > '9')
1096 /* Avoid error for `###' and similar cases unless -pedantic. */
1098 while (*p == '#' || is_hor_space[*p]) p++;
1100 if (pedantic && !lang_asm)
1101 cpp_warning (pfile, "invalid preprocessor directive");
1107 cpp_error (pfile, "invalid preprocessor directive name");
1113 * Decode the keyword and call the appropriate expansion
1114 * routine, after moving the input pointer up to the next line.
1116 for (kt = directive_table; ; kt++) {
1117 if (kt->length <= 0)
1118 goto not_a_directive;
1119 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1123 if (! kt->command_reads_line)
1125 /* Nonzero means do not delete comments within the directive.
1126 #define needs this when -traditional. */
1127 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1128 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1129 CPP_OPTIONS (pfile)->put_out_comments = comments;
1130 after_ident = CPP_WRITTEN (pfile);
1131 copy_rest_of_line (pfile);
1132 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1135 /* For #pragma and #define, we may want to pass through the directive.
1136 Other directives may create output, but we don't want the directive
1137 itself out, so we pop it now. For example #include may write a
1138 command (see comment in do_include), and conditionals may emit
1139 #failed ... #endfailed stuff. But note that popping the buffer
1140 means the parameters to kt->func may point after pfile->limit
1141 so these parameters are invalid as soon as something gets appended
1142 to the token_buffer. */
1144 line_end = CPP_PWRITTEN (pfile);
1145 if (!kt->pass_thru && kt->type != T_DEFINE)
1146 CPP_SET_WRITTEN (pfile, old_written);
1148 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1150 || (kt->type == T_DEFINE
1151 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1153 /* Just leave the entire #define in the output stack. */
1155 else if (kt->type == T_DEFINE
1156 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1158 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1159 SKIP_WHITE_SPACE (p);
1160 while (is_idchar[*p]) p++;
1162 CPP_PUTC (pfile, '\n');
1164 else if (kt->type == T_DEFINE)
1165 CPP_SET_WRITTEN (pfile, old_written);
1173 /* Pass a directive through to the output file.
1174 BUF points to the contents of the directive, as a contiguous string.
1175 LIMIT points to the first character past the end of the directive.
1176 KEYWORD is the keyword-table entry for the directive. */
1179 pass_thru_directive (
1180 U_CHAR *buf, U_CHAR *limit,
1182 struct directive *keyword)
1184 register unsigned keyword_length = keyword->length;
1186 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1187 CPP_PUTC_Q (pfile, '#');
1188 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1189 if (limit != buf && buf[0] != ' ')
1190 CPP_PUTC_Q (pfile, ' ');
1191 CPP_PUTS_Q (pfile, buf, limit - buf);
1193 CPP_PUTS_Q (pfile, '\n');
1194 /* Count the line we have just made in the output,
1195 to get in sync properly. */
1200 /* The arglist structure is built by do_define to tell
1201 collect_definition where the argument names begin. That
1202 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1203 would contain pointers to the strings x, y, and z.
1204 Collect_definition would then build a DEFINITION node,
1205 with reflist nodes pointing to the places x, y, and z had
1206 appeared. So the arglist is just convenience data passed
1207 between these two routines. It is not kept around after
1208 the current #define has been processed and entered into the
1212 struct arglist *next;
1219 /* Read a replacement list for a macro with parameters.
1220 Build the DEFINITION structure.
1221 Reads characters of text starting at BUF until END.
1222 ARGLIST specifies the formal parameters to look for
1223 in the text of the definition; NARGS is the number of args
1224 in that list, or -1 for a macro name that wants no argument list.
1225 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1226 and NAMELEN is its length in characters.
1228 Note that comments, backslash-newlines, and leading white space
1229 have already been deleted from the argument. */
1234 U_CHAR *buf, U_CHAR *limit,
1236 struct arglist *arglist)
1239 register U_CHAR *p, *lastp, *exp_p;
1240 struct reflist *endpat = NULL;
1241 /* Pointer to first nonspace after last ## seen. */
1243 /* Pointer to first nonspace after last single-# seen. */
1244 U_CHAR *stringify = 0;
1246 int expected_delimiter = '\0';
1248 /* Scan thru the replacement list, ignoring comments and quoted
1249 strings, picking up on the macro calls. It does a linear search
1250 thru the arg list on every potential symbol. Profiling might say
1251 that something smarter should happen. */
1256 /* Find the beginning of the trailing whitespace. */
1258 while (p < limit && is_space[limit[-1]]) limit--;
1260 /* Allocate space for the text in the macro definition.
1261 Leading and trailing whitespace chars need 2 bytes each.
1262 Each other input char may or may not need 1 byte,
1263 so this is an upper bound. The extra 5 are for invented
1264 leading and trailing newline-marker and final null. */
1265 maxsize = (sizeof (DEFINITION)
1267 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1271 defn = (DEFINITION *) xcalloc (1, maxsize);
1273 defn->nargs = nargs;
1274 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1279 /* Add one initial space escape-marker to prevent accidental
1280 token-pasting (often removed by macroexpand). */
1284 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1285 cpp_error (pfile, "`##' at start of macro definition");
1289 /* Process the main body of the definition. */
1291 int skipped_arg = 0;
1292 register U_CHAR c = *p++;
1296 if (!CPP_TRADITIONAL (pfile)) {
1300 if (expected_delimiter != '\0') {
1301 if (c == expected_delimiter)
1302 expected_delimiter = '\0';
1304 expected_delimiter = c;
1308 if (p < limit && expected_delimiter) {
1309 /* In a string, backslash goes through
1310 and makes next char ordinary. */
1316 /* An '@' in a string or character constant stands for itself,
1317 and does not need to be escaped. */
1318 if (!expected_delimiter)
1323 /* # is ordinary inside a string. */
1324 if (expected_delimiter)
1326 if (p < limit && *p == '#') {
1327 /* ##: concatenate preceding and following tokens. */
1328 /* Take out the first #, discard preceding whitespace. */
1330 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1332 /* Skip the second #. */
1334 /* Discard following whitespace. */
1335 SKIP_WHITE_SPACE (p);
1338 cpp_error (pfile, "`##' at end of macro definition");
1339 } else if (nargs >= 0) {
1340 /* Single #: stringify following argument ref.
1341 Don't leave the # in the expansion. */
1343 SKIP_WHITE_SPACE (p);
1344 if (p == limit || ! is_idstart[*p])
1346 "`#' operator is not followed by a macro argument name");
1353 /* In -traditional mode, recognize arguments inside strings and
1354 and character constants, and ignore special properties of #.
1355 Arguments inside strings are considered "stringified", but no
1356 extra quote marks are supplied. */
1360 if (expected_delimiter != '\0') {
1361 if (c == expected_delimiter)
1362 expected_delimiter = '\0';
1364 expected_delimiter = c;
1368 /* Backslash quotes delimiters and itself, but not macro args. */
1369 if (expected_delimiter != 0 && p < limit
1370 && (*p == expected_delimiter || *p == '\\')) {
1377 if (expected_delimiter != '\0') /* No comments inside strings. */
1380 /* If we find a comment that wasn't removed by handle_directive,
1381 this must be -traditional. So replace the comment with
1385 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1388 /* Mark this as a concatenation-point, as if it had been ##. */
1396 /* Handle the start of a symbol. */
1397 if (is_idchar[c] && nargs > 0) {
1398 U_CHAR *id_beg = p - 1;
1402 while (p != limit && is_idchar[*p]) p++;
1403 id_len = p - id_beg;
1405 if (is_idstart[c]) {
1406 register struct arglist *arg;
1408 for (arg = arglist; arg != NULL; arg = arg->next) {
1409 struct reflist *tpat;
1411 if (arg->name[0] == c
1412 && arg->length == id_len
1413 && strncmp (arg->name, id_beg, id_len) == 0) {
1414 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1415 if (CPP_TRADITIONAL (pfile)) {
1416 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1420 "macro arg `%.*s' would be stringified with -traditional.",
1424 /* If ANSI, don't actually substitute inside a string. */
1425 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1427 /* make a pat node for this arg and append it to the end of
1429 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1431 tpat->raw_before = concat == id_beg;
1432 tpat->raw_after = 0;
1433 tpat->rest_args = arg->rest_args;
1434 tpat->stringify = (CPP_TRADITIONAL (pfile)
1435 ? expected_delimiter != '\0'
1436 : stringify == id_beg);
1439 defn->pattern = tpat;
1441 endpat->next = tpat;
1444 tpat->argno = arg->argno;
1445 tpat->nchars = exp_p - lastp;
1447 register U_CHAR *p1 = p;
1448 SKIP_WHITE_SPACE (p1);
1449 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1450 tpat->raw_after = 1;
1452 lastp = exp_p; /* place to start copying from next time */
1459 /* If this was not a macro arg, copy it into the expansion. */
1460 if (! skipped_arg) {
1461 register U_CHAR *lim1 = p;
1465 if (stringify == id_beg)
1467 "`#' operator should be followed by a macro argument name");
1472 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1474 /* If ANSI, put in a "@ " marker to prevent token pasting.
1475 But not if "inside a string" (which in ANSI mode
1476 happens only for -D option). */
1483 defn->length = exp_p - defn->expansion;
1485 /* Crash now if we overrun the allocated size. */
1486 if (defn->length + 1 > maxsize)
1490 /* This isn't worth the time it takes. */
1491 /* give back excess storage */
1492 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1499 * special extension string that can be added to the last macro argument to
1500 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1501 * #define wow(a, b...) process (b, a, b)
1502 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1503 * { wow (one, two); } -> { process (two, one, two); }
1504 * if this "rest_arg" is used with the concat token '##' and if it is not
1505 * supplied then the token attached to with ## will not be outputted. Ex:
1506 * #define wow (a, b...) process (b ## , a, ## b)
1507 * { wow (1, 2); } -> { process (2, 1, 2); }
1508 * { wow (one); } -> { process (one); {
1510 static char rest_extension[] = "...";
1511 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1513 /* Create a DEFINITION node from a #define directive. Arguments are
1514 as for do_define. */
1517 U_CHAR *buf, U_CHAR *limit,
1521 U_CHAR *bp; /* temp ptr into input buffer */
1522 U_CHAR *symname; /* remember where symbol name starts */
1523 int sym_length; /* and how long it is */
1526 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1528 int arglengths = 0; /* Accumulate lengths of arg names
1529 plus number of args. */
1531 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1535 while (is_hor_space[*bp])
1538 symname = bp; /* remember where it starts */
1540 sym_length = check_macro_name (pfile, bp, "macro");
1543 /* Lossage will occur if identifiers or control keywords are broken
1544 across lines using backslash. This is not the right place to take
1548 struct arglist *arg_ptrs = NULL;
1551 bp++; /* skip '(' */
1552 SKIP_WHITE_SPACE (bp);
1554 /* Loop over macro argument names. */
1555 while (*bp != ')') {
1556 struct arglist *temp;
1558 temp = (struct arglist *) alloca (sizeof (struct arglist));
1560 temp->next = arg_ptrs;
1561 temp->argno = argno++;
1562 temp->rest_args = 0;
1566 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1568 if (!is_idstart[*bp])
1569 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1571 /* Find the end of the arg name. */
1572 while (is_idchar[*bp]) {
1574 /* do we have a "special" rest-args extension here? */
1575 if (limit - bp > REST_EXTENSION_LENGTH &&
1576 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1578 temp->rest_args = 1;
1582 temp->length = bp - temp->name;
1584 bp += REST_EXTENSION_LENGTH;
1585 arglengths += temp->length + 2;
1586 SKIP_WHITE_SPACE (bp);
1587 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1588 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1593 SKIP_WHITE_SPACE (bp);
1596 cpp_error (pfile, "unterminated parameter list in `#define'");
1600 struct arglist *otemp;
1602 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1603 if (temp->length == otemp->length &&
1604 strncmp (temp->name, otemp->name, temp->length) == 0) {
1607 name = (U_CHAR *) alloca (temp->length + 1);
1608 (void) strncpy (name, temp->name, temp->length);
1609 name[temp->length] = '\0';
1611 "duplicate argument name `%s' in `#define'", name);
1617 ++bp; /* skip paren */
1618 SKIP_WHITE_SPACE (bp);
1619 /* now everything from bp before limit is the definition. */
1620 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1621 defn->rest_args = rest_args;
1623 /* Now set defn->args.argnames to the result of concatenating
1624 the argument names in reverse order
1625 with comma-space between them. */
1626 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1628 struct arglist *temp;
1630 for (temp = arg_ptrs; temp; temp = temp->next) {
1631 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1633 if (temp->next != 0) {
1634 defn->args.argnames[i++] = ',';
1635 defn->args.argnames[i++] = ' ';
1638 defn->args.argnames[i] = 0;
1641 /* Simple expansion or empty definition. */
1645 if (is_hor_space[*bp]) {
1647 SKIP_WHITE_SPACE (bp);
1650 case '!': case '"': case '#': case '%': case '&': case '\'':
1651 case ')': case '*': case '+': case ',': case '-': case '.':
1652 case '/': case ':': case ';': case '<': case '=': case '>':
1653 case '?': case '[': case '\\': case ']': case '^': case '{':
1654 case '|': case '}': case '~':
1655 cpp_warning (pfile, "missing white space after `#define %.*s'",
1656 sym_length, symname);
1660 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1661 sym_length, symname);
1666 /* now everything from bp before limit is the definition. */
1667 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1668 defn->args.argnames = (U_CHAR *) "";
1674 /* OP is null if this is a predefinition */
1675 defn->predefined = predefinition;
1677 mdef.symnam = symname;
1678 mdef.symlen = sym_length;
1687 /* Check a purported macro name SYMNAME, and yield its length.
1688 USAGE is the kind of name this is intended for. */
1699 for (p = symname; is_idchar[*p]; p++)
1701 sym_length = p - symname;
1702 if (sym_length == 0)
1703 cpp_error (pfile, "invalid %s name", usage);
1704 else if (!is_idstart[*symname]) {
1705 U_CHAR *msg; /* what pain... */
1706 msg = (U_CHAR *) alloca (sym_length + 1);
1707 bcopy (symname, msg, sym_length);
1708 msg[sym_length] = 0;
1709 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1711 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1712 cpp_error (pfile, "invalid %s name `defined'", usage);
1718 * return zero if two DEFINITIONs are isomorphic
1722 DEFINITION *d1, DEFINITION *d2)
1724 register struct reflist *a1, *a2;
1725 register U_CHAR *p1 = d1->expansion;
1726 register U_CHAR *p2 = d2->expansion;
1729 if (d1->nargs != d2->nargs)
1731 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1733 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1734 a1 = a1->next, a2 = a2->next) {
1735 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1736 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1737 || a1->argno != a2->argno
1738 || a1->stringify != a2->stringify
1739 || a1->raw_before != a2->raw_before
1740 || a1->raw_after != a2->raw_after)
1748 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1749 p2, d2->length - (p2 - d2->expansion), 1))
1754 /* Return 1 if two parts of two macro definitions are effectively different.
1755 One of the parts starts at BEG1 and has LEN1 chars;
1756 the other has LEN2 chars at BEG2.
1757 Any sequence of whitespace matches any other sequence of whitespace.
1758 FIRST means these parts are the first of a macro definition;
1759 so ignore leading whitespace entirely.
1760 LAST means these parts are the last of a macro definition;
1761 so ignore trailing whitespace entirely. */
1766 U_CHAR *beg1, int len1,
1767 U_CHAR *beg2, int len2 ,
1770 register U_CHAR *end1 = beg1 + len1;
1771 register U_CHAR *end2 = beg2 + len2;
1773 while (beg1 != end1 && is_space[*beg1]) beg1++;
1774 while (beg2 != end2 && is_space[*beg2]) beg2++;
1777 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1778 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1780 while (beg1 != end1 && beg2 != end2) {
1781 if (is_space[*beg1] && is_space[*beg2]) {
1782 while (beg1 != end1 && is_space[*beg1]) beg1++;
1783 while (beg2 != end2 && is_space[*beg2]) beg2++;
1784 } else if (*beg1 == *beg2) {
1788 return (beg1 != end1) || (beg2 != end2);
1791 /* Process a #define command.
1792 BUF points to the contents of the #define command, as a contiguous string.
1793 LIMIT points to the first character past the end of the definition.
1794 KEYWORD is the keyword-table entry for #define,
1795 or NULL for a "predefined" macro. */
1800 struct directive *keyword,
1801 U_CHAR *buf, U_CHAR *limit)
1808 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1809 if (pcp_outfile && keyword)
1810 pass_thru_directive (buf, limit, pfile, keyword);
1813 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1817 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1819 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1822 /* Redefining a precompiled key is ok. */
1823 if (hp->type == T_PCSTRING)
1825 /* Redefining a macro is ok if the definitions are the same. */
1826 else if (hp->type == T_MACRO)
1827 ok = ! compare_defs (mdef.defn, hp->value.defn);
1828 /* Redefining a constant is ok with -D. */
1829 else if (hp->type == T_CONST)
1830 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1831 /* Print the warning if it's not ok. */
1834 U_CHAR *msg; /* what pain... */
1836 /* If we are passing through #define and #undef directives, do
1837 that for this re-definition now. */
1838 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1839 pass_thru_directive (buf, limit, pfile, keyword);
1841 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1843 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1844 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1845 cpp_pedwarn (pfile, msg);
1846 if (hp->type == T_MACRO)
1847 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1848 "this is the location of the previous definition");
1850 /* Replace the old definition. */
1852 hp->value.defn = mdef.defn;
1856 /* If we are passing through #define and #undef directives, do
1857 that for this new definition now. */
1858 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1859 pass_thru_directive (buf, limit, pfile, keyword);
1860 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1861 (char *) mdef.defn, hashcode);
1871 /* This structure represents one parsed argument in a macro call.
1872 `raw' points to the argument text as written (`raw_length' is its length).
1873 `expanded' points to the argument's macro-expansion
1874 (its length is `expand_length').
1875 `stringified_length' is the length the argument would have
1877 `use_count' is the number of times this macro arg is substituted
1878 into the macro. If the actual use count exceeds 10,
1879 the value stored is 10. */
1881 /* raw and expanded are relative to ARG_BASE */
1882 #define ARG_BASE ((pfile)->token_buffer)
1885 /* Strings relative to pfile->token_buffer */
1886 long raw, expanded, stringified;
1887 int raw_length, expand_length;
1888 int stringified_length;
1900 #ifdef STATIC_BUFFERS
1901 register cpp_buffer *buf = CPP_BUFFER (pfile);
1902 if (buf == pfile->buffer_stack)
1903 fatal ("%s: macro or `#include' recursion too deep", buf->fname);
1905 bzero ((char *) buf, sizeof (cpp_buffer));
1906 CPP_BUFFER (pfile) = buf;
1908 register cpp_buffer *buf = (cpp_buffer*) xmalloc (sizeof(cpp_buffer));
1909 bzero ((char *) buf, sizeof (cpp_buffer));
1910 CPP_PREV_BUFFER (buf) = CPP_BUFFER (pfile);
1911 CPP_BUFFER (pfile) = buf;
1913 buf->if_stack = pfile->if_stack;
1914 buf->cleanup = null_cleanup;
1915 buf->underflow = null_underflow;
1916 buf->buf = buf->cur = buffer;
1917 buf->alimit = buf->rlimit = buffer + length;
1923 cpp_pop_buffer (pfile)
1926 cpp_buffer *buf = CPP_BUFFER (pfile);
1927 #ifdef STATIC_BUFFERS
1928 (*buf->cleanup) (buf, pfile);
1929 return ++CPP_BUFFER (pfile);
1931 cpp_buffer *next_buf = CPP_PREV_BUFFER (buf);
1932 (*buf->cleanup) (buf, pfile);
1933 CPP_BUFFER (pfile) = next_buf;
1939 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1940 Pop the buffer when done. */
1946 cpp_buffer *buffer = CPP_BUFFER (pfile);
1949 enum cpp_token token = cpp_get_token (pfile);
1950 if (token == CPP_EOF) /* Should not happen ... */
1952 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1954 cpp_pop_buffer (pfile);
1961 * Rescan a string (which may have escape marks) into pfile's buffer.
1962 * Place the result in pfile->token_buffer.
1964 * The input is copied before it is scanned, so it is safe to pass
1965 * it something from the token_buffer that will get overwritten
1966 * (because it follows CPP_WRITTEN). This is used by do_include.
1970 cpp_expand_to_buffer (
1975 register cpp_buffer *ip;
1977 U_CHAR *limit = buf + length;
1980 int odepth = indepth;
1986 /* Set up the input on the input stack. */
1988 buf1 = (U_CHAR *) alloca (length + 1);
1990 register U_CHAR *p1 = buf;
1991 register U_CHAR *p2 = buf1;
1998 ip = cpp_push_buffer (pfile, buf1, length);
1999 ip->has_escapes = 1;
2001 ip->lineno = obuf.lineno = 1;
2004 /* Scan the input, create the output. */
2005 cpp_scan_buffer (pfile);
2008 if (indepth != odepth)
2012 CPP_NUL_TERMINATE (pfile);
2027 (*linep)++, (*colp) = 1;
2033 /* Move line_base forward, updating lineno and colno. */
2037 register cpp_buffer *pbuf)
2039 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2040 unsigned char *new_pos = pbuf->cur;
2041 register struct parse_marker *mark;
2042 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2044 if (pbuf->buf + mark->position < new_pos)
2045 new_pos = pbuf->buf + mark->position;
2047 pbuf->line_base += new_pos - old_pos;
2048 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2052 cpp_buf_line_and_col (
2053 register cpp_buffer *pbuf,
2054 long *linep,long *colp)
2061 *linep = pbuf->lineno;
2062 *colp = pbuf->colno;
2063 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2072 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2078 cpp_buffer *ip = CPP_BUFFER (pfile);
2080 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2081 if (ip->fname != NULL)
2088 register U_CHAR *buf,
2089 register U_CHAR *limit)
2091 register long count = 0;
2102 * write out a #line command, for instance, after an #include file.
2103 * If CONDITIONAL is nonzero, we can omit the #line if it would
2104 * appear to be a no-op, and we can output a few newlines instead
2105 * if we want to increase the line number by a small amount.
2106 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2110 output_line_command (
2113 enum file_change_code file_change)
2116 char *line_cmd_buf, *line_end;
2118 cpp_buffer *ip = CPP_BUFFER (pfile);
2120 if (ip->fname == NULL || CPP_OPTIONS (pfile)->no_output) {
2124 update_position (ip);
2125 line = CPP_BUFFER (pfile)->lineno;
2126 col = CPP_BUFFER (pfile)->colno;
2127 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2129 if (CPP_OPTIONS (pfile)->no_line_commands)
2133 if (line == pfile->lineno)
2136 /* If the inherited line number is a little too small,
2137 output some newlines instead of a #line command. */
2138 if (line > pfile->lineno && line < pfile->lineno + 8) {
2139 CPP_RESERVE (pfile, 20);
2140 while (line > pfile->lineno) {
2141 CPP_PUTC_Q (pfile, '\n');
2149 /* Don't output a line number of 0 if we can help it. */
2150 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2151 && *ip->bufp == '\n') {
2157 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2159 static char sharp_line[] = "#line ";
2160 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2163 sprintf (CPP_PWRITTEN (pfile), "%d ", line+2);
2164 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2166 // modification for SDC51
2167 if (*ip->nominal_fname == '\0')
2168 quote_string (pfile,"standard input");
2170 quote_string (pfile, ip->nominal_fname);
2171 if (file_change != same_file) {
2172 CPP_PUTC_Q (pfile, ' ');
2173 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2175 /* Tell cc1 if following text comes from a system header file. */
2176 if (ip->system_header_p) {
2177 CPP_PUTC_Q (pfile, ' ');
2178 CPP_PUTC_Q (pfile, '3');
2180 #ifndef NO_IMPLICIT_EXTERN_C
2181 /* Tell cc1plus if following text should be treated as C. */
2182 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2183 CPP_PUTC_Q (pfile, ' ');
2184 CPP_PUTC_Q (pfile, '4');
2187 CPP_PUTC_Q (pfile, '\n');
2188 pfile->lineno = line;
2192 * Parse a macro argument and append the info on PFILE's token_buffer.
2193 * REST_ARGS means to absorb the rest of the args.
2194 * Return nonzero to indicate a syntax error.
2197 static enum cpp_token
2203 enum cpp_token token;
2204 long arg_start = CPP_WRITTEN (pfile);
2205 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2206 CPP_OPTIONS (pfile)->put_out_comments = 0;
2208 /* Try to parse as much of the argument as exists at this
2209 input stack level. */
2210 pfile->no_macro_expand++;
2213 token = cpp_get_token (pfile);
2219 /* If we've hit end of file, it's an error (reported by caller).
2220 Ditto if it's the end of cpp_expand_to_buffer text.
2221 If we've hit end of macro, just continue. */
2222 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2233 /* if we've returned to lowest level and
2234 we aren't absorbing all args */
2235 if (paren == 0 && rest_args == 0)
2239 /* Remove ',' or ')' from argument buffer. */
2240 CPP_ADJUST_WRITTEN (pfile, -1);
2247 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2248 pfile->no_macro_expand--;
2253 /* Turn newlines to spaces in the string of length LENGTH at START,
2254 except inside of string constants.
2255 The string is copied into itself with its beginning staying fixed. */
2262 register U_CHAR *ibp;
2263 register U_CHAR *obp;
2264 register U_CHAR *limit;
2268 limit = start + length;
2271 while (ibp < limit) {
2272 *obp++ = c = *ibp++;
2277 /* Notice and skip strings, so that we don't delete newlines in them. */
2280 while (ibp < limit) {
2281 *obp++ = c = *ibp++;
2284 if (c == '\n' && quotec == '\'')
2300 if (!pfile->timebuf) {
2301 time_t t = time ((time_t *)0);
2302 pfile->timebuf = localtime (&t);
2304 return pfile->timebuf;
2307 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2308 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2312 * expand things like __FILE__. Place the expansion into the output
2313 * buffer *without* rescanning.
2324 cpp_buffer *ip = NULL;
2327 int paren = 0; /* For special `defined' keyword */
2330 if (pcp_outfile && pcp_inside_if
2331 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2333 "Predefined macro `%s' used inside `#if' during precompilation",
2337 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2341 cpp_error (pfile, "cccp error: not in any file?!");
2342 return; /* the show must go on */
2344 if (ip->fname != NULL)
2354 if (hp->type == T_BASE_FILE)
2356 while (CPP_PREV_BUFFER (ip))
2357 ip = CPP_PREV_BUFFER (ip);
2359 string = ip->nominal_fname;
2363 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2364 quote_string (pfile, string);
2368 case T_INCLUDE_LEVEL:
2370 for (ip = CPP_BUFFER (pfile); ip != NULL; ip = CPP_PREV_BUFFER (ip))
2371 if (ip->fname != NULL)
2374 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2375 sprintf (buf, "%d", true_indepth - 1);
2379 buf = (char *) alloca (3 + strlen (version_string));
2380 sprintf (buf, "\"%s\"", version_string);
2383 #ifndef NO_BUILTIN_SIZE_TYPE
2389 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2390 case T_PTRDIFF_TYPE:
2396 buf = CPP_WCHAR_TYPE (pfile);
2399 case T_USER_LABEL_PREFIX_TYPE:
2400 buf = USER_LABEL_PREFIX;
2403 case T_REGISTER_PREFIX_TYPE:
2404 buf = REGISTER_PREFIX;
2408 buf = (char *) alloca (4 * sizeof (int));
2409 sprintf (buf, "%d", hp->value.ival);
2411 if (pcp_inside_if && pcp_outfile)
2412 /* Output a precondition for this macro use */
2413 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2419 long line = ip->lineno;
2420 long col = ip->colno;
2421 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2423 buf = (char *) alloca (10);
2424 sprintf (buf, "%d", line);
2430 buf = (char *) alloca (20);
2431 timebuf = timestamp (pfile);
2432 if (hp->type == T_DATE)
2433 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2434 timebuf->tm_mday, timebuf->tm_year + 1900);
2436 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2440 case T_SPEC_DEFINED:
2441 buf = " 0 "; /* Assume symbol is not defined */
2442 ip = CPP_BUFFER (pfile);
2443 SKIP_WHITE_SPACE (ip->cur);
2444 if (*ip->cur == '(')
2447 ip->cur++; /* Skip over the paren */
2448 SKIP_WHITE_SPACE (ip->cur);
2451 if (!is_idstart[*ip->cur])
2453 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2456 if (pcp_outfile && pcp_inside_if
2457 && (hp->type == T_CONST
2458 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2459 /* Output a precondition for this macro use. */
2460 fprintf (pcp_outfile, "#define %s\n", hp->name);
2466 if (pcp_outfile && pcp_inside_if)
2468 /* Output a precondition for this macro use */
2469 U_CHAR *cp = ip->bufp;
2470 fprintf (pcp_outfile, "#undef ");
2471 while (is_idchar[*cp]) /* Ick! */
2472 fputc (*cp++, pcp_outfile);
2473 putc ('\n', pcp_outfile);
2476 while (is_idchar[*ip->cur])
2478 SKIP_WHITE_SPACE (ip->cur);
2481 if (*ip->cur != ')')
2489 cpp_error (pfile, "`defined' without an identifier");
2493 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2497 CPP_RESERVE (pfile, len + 1);
2498 CPP_PUTS_Q (pfile, buf, len);
2499 CPP_NUL_TERMINATE_Q (pfile);
2504 /* Initialize the built-in macros. */
2507 initialize_builtins (
2510 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2511 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2512 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2513 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2514 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2515 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2516 #ifndef NO_BUILTIN_SIZE_TYPE
2517 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2519 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2520 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2522 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2523 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2524 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2525 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2526 if (!CPP_TRADITIONAL (pfile))
2527 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2528 if (CPP_OPTIONS (pfile)->objc)
2529 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2530 /* This is supplied using a -D by the compiler driver
2531 so that it is present only when truly compiling with GNU C. */
2532 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2534 if (CPP_OPTIONS (pfile)->debug_output)
2536 char directive[2048];
2537 register struct directive *dp = &directive_table[0];
2538 struct tm *timebuf = timestamp (pfile);
2539 cpp_buffer *pbuffer = CPP_BUFFER (pfile);
2541 while (CPP_PREV_BUFFER (pbuffer))
2542 pbuffer = CPP_PREV_BUFFER (pbuffer);
2543 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
2544 pbuffer->nominal_fname);
2545 output_line_command (pfile, 0, same_file);
2546 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2548 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
2549 output_line_command (pfile, 0, same_file);
2550 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2552 #ifndef NO_BUILTIN_SIZE_TYPE
2553 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2554 output_line_command (pfile, 0, same_file);
2555 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2558 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2559 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2560 output_line_command (pfile, 0, same_file);
2561 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2564 sprintf (directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile));
2565 output_line_command (pfile, 0, same_file);
2566 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2568 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
2569 monthnames[timebuf->tm_mon],
2570 timebuf->tm_mday, timebuf->tm_year + 1900);
2571 output_line_command (pfile, 0, same_file);
2572 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2574 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2575 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2576 output_line_command (pfile, 0, same_file);
2577 pass_thru_directive (directive, &directive[strlen (directive)], pfile, dp);
2579 if (!CPP_TRADITIONAL (pfile))
2581 sprintf (directive, " __STDC__ 1");
2582 output_line_command (pfile, 0, same_file);
2583 pass_thru_directive (directive, &directive[strlen (directive)],
2586 if (CPP_OPTIONS (pfile)->objc)
2588 sprintf (directive, " __OBJC__ 1");
2589 output_line_command (pfile, 0, same_file);
2590 pass_thru_directive (directive, &directive[strlen (directive)],
2596 /* Return 1 iff a token ending in C1 followed directly by a token C2
2597 could cause mis-tokenization. */
2606 if (c2 == c1 || c2 == '=')
2610 case '0': case '1': case '2': case '3': case '4':
2611 case '5': case '6': case '7': case '8': case '9':
2613 if (c2 == '-' || c2 == '+')
2614 return 1; /* could extend a pre-processing number */
2617 if (c2 == '\'' || c2 == '\"')
2618 return 1; /* Could turn into L"xxx" or L'xxx'. */
2622 case 'a': case 'b': case 'c': case 'd': case 'f':
2623 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2624 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2625 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2627 case 'A': case 'B': case 'C': case 'D': case 'F':
2628 case 'G': case 'H': case 'I': case 'J': case 'K':
2629 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2630 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2632 /* We're in the middle of either a name or a pre-processing number. */
2633 return (is_idchar[c2] || c2 == '.');
2634 case '<': case '>': case '!': case '%': case '#': case ':':
2635 case '^': case '&': case '|': case '*': case '/': case '=':
2636 return (c2 == c1 || c2 == '=');
2641 /* Expand a macro call.
2642 HP points to the symbol that is the macro being called.
2643 Put the result of expansion onto the input stack
2644 so that subsequent input by our caller will use it.
2646 If macro wants arguments, caller has already verified that
2647 an argument list follows; arguments come from the input stack. */
2655 DEFINITION *defn = hp->value.defn;
2656 register U_CHAR *xbuf;
2657 long start_line, start_column;
2659 struct argdata *args;
2660 long old_written = CPP_WRITTEN (pfile);
2662 int start_line = instack[indepth].lineno;
2664 int rest_args, rest_zero;
2668 CHECK_DEPTH (return;);
2672 /* This macro is being used inside a #if, which means it must be */
2673 /* recorded as a precondition. */
2674 if (pcp_inside_if && pcp_outfile && defn->predefined)
2675 dump_single_macro (hp, pcp_outfile);
2678 pfile->output_escapes++;
2679 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2681 nargs = defn->nargs;
2685 enum cpp_token token;
2687 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2689 for (i = 0; i < nargs; i++)
2691 args[i].raw = args[i].expanded = 0;
2692 args[i].raw_length = 0;
2693 args[i].expand_length = args[i].stringified_length = -1;
2694 args[i].use_count = 0;
2697 /* Parse all the macro args that are supplied. I counts them.
2698 The first NARGS args are stored in ARGS.
2699 The rest are discarded. If rest_args is set then we assume
2700 macarg absorbed the rest of the args. */
2704 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2709 if (i < nargs || (nargs == 0 && i == 0))
2711 /* if we are working on last arg which absorbs rest of args... */
2712 if (i == nargs - 1 && defn->rest_args)
2714 args[i].raw = CPP_WRITTEN (pfile);
2715 token = macarg (pfile, rest_args);
2716 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2717 args[i].newlines = 0; /* FIXME */
2720 token = macarg (pfile, 0);
2721 if (token == CPP_EOF || token == CPP_POP)
2723 cpp_error_with_line (pfile, start_line, start_column,
2724 "unterminated macro call");
2728 } while (token == CPP_COMMA);
2730 /* If we got one arg but it was just whitespace, call that 0 args. */
2733 register U_CHAR *bp = ARG_BASE + args[0].raw;
2734 register U_CHAR *lim = bp + args[0].raw_length;
2735 /* cpp.texi says for foo ( ) we provide one argument.
2736 However, if foo wants just 0 arguments, treat this as 0. */
2738 while (bp != lim && is_space[*bp]) bp++;
2743 /* Don't output an error message if we have already output one for
2744 a parse error above. */
2746 if (nargs == 0 && i > 0)
2748 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2752 /* traditional C allows foo() if foo wants one argument. */
2753 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2755 /* the rest args token is allowed to absorb 0 tokens */
2756 else if (i == nargs - 1 && defn->rest_args)
2759 cpp_error (pfile, "macro `%s' used without args", hp->name);
2761 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2763 cpp_error (pfile, "macro `%s' used with only %d args",
2769 "macro `%s' used with too many (%d) args", hp->name, i);
2773 /* If macro wants zero args, we parsed the arglist for checking only.
2774 Read directly from the macro definition. */
2777 xbuf = defn->expansion;
2778 xbuf_len = defn->length;
2782 register U_CHAR *exp = defn->expansion;
2783 register int offset; /* offset in expansion,
2784 copied a piece at a time */
2785 register int totlen; /* total amount of exp buffer filled so far */
2787 register struct reflist *ap, *last_ap;
2789 /* Macro really takes args. Compute the expansion of this call. */
2791 /* Compute length in characters of the macro's expansion.
2792 Also count number of times each arg is used. */
2793 xbuf_len = defn->length;
2794 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2798 register struct argdata *arg = &args[ap->argno];
2799 /* Stringify it it hasn't already been */
2800 if (arg->stringified_length < 0)
2802 int arglen = arg->raw_length;
2806 /* Initially need_space is -1. Otherwise, 1 means the
2807 previous character was a space, but we suppressed it;
2808 0 means the previous character was a non-space. */
2809 int need_space = -1;
2811 arg->stringified = CPP_WRITTEN (pfile);
2812 if (!CPP_TRADITIONAL (pfile))
2813 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2814 for (; i < arglen; i++)
2816 c = (ARG_BASE + arg->raw)[i];
2820 /* Internal sequences of whitespace are replaced by
2821 one space except within an string or char token.*/
2824 if (CPP_WRITTEN (pfile) > arg->stringified
2825 && (CPP_PWRITTEN (pfile))[-1] == '@')
2827 /* "@ " escape markers are removed */
2828 CPP_ADJUST_WRITTEN (pfile, -1);
2831 if (need_space == 0)
2835 else if (need_space > 0)
2836 CPP_PUTC (pfile, ' ');
2851 else if (c == '\"' || c == '\'')
2855 /* Escape these chars */
2856 if (c == '\"' || (in_string && c == '\\'))
2857 CPP_PUTC (pfile, '\\');
2859 CPP_PUTC (pfile, c);
2862 CPP_RESERVE (pfile, 4);
2863 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2865 CPP_ADJUST_WRITTEN (pfile, 4);
2868 if (!CPP_TRADITIONAL (pfile))
2869 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2870 arg->stringified_length
2871 = CPP_WRITTEN (pfile) - arg->stringified;
2873 xbuf_len += args[ap->argno].stringified_length;
2875 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2876 /* Add 4 for two newline-space markers to prevent
2877 token concatenation. */
2878 xbuf_len += args[ap->argno].raw_length + 4;
2881 /* We have an ordinary (expanded) occurrence of the arg.
2882 So compute its expansion, if we have not already. */
2883 if (args[ap->argno].expand_length < 0)
2885 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2886 cpp_expand_to_buffer (pfile,
2887 ARG_BASE + args[ap->argno].raw,
2888 args[ap->argno].raw_length);
2890 args[ap->argno].expand_length
2891 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2894 /* Add 4 for two newline-space markers to prevent
2895 token concatenation. */
2896 xbuf_len += args[ap->argno].expand_length + 4;
2898 if (args[ap->argno].use_count < 10)
2899 args[ap->argno].use_count++;
2902 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2904 /* Generate in XBUF the complete expansion
2905 with arguments substituted in.
2906 TOTLEN is the total size generated so far.
2907 OFFSET is the index in the definition
2908 of where we are copying from. */
2909 offset = totlen = 0;
2910 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2911 last_ap = ap, ap = ap->next)
2913 register struct argdata *arg = &args[ap->argno];
2914 int count_before = totlen;
2916 /* Add chars to XBUF. */
2917 for (i = 0; i < ap->nchars; i++, offset++)
2918 xbuf[totlen++] = exp[offset];
2920 /* If followed by an empty rest arg with concatenation,
2921 delete the last run of nonwhite chars. */
2922 if (rest_zero && totlen > count_before
2923 && ((ap->rest_args && ap->raw_before)
2924 || (last_ap != NULL && last_ap->rest_args
2925 && last_ap->raw_after)))
2927 /* Delete final whitespace. */
2928 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2931 /* Delete the nonwhites before them. */
2932 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2936 if (ap->stringify != 0)
2938 bcopy (ARG_BASE + arg->stringified,
2939 xbuf + totlen, arg->stringified_length);
2940 totlen += arg->stringified_length;
2942 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2944 U_CHAR *p1 = ARG_BASE + arg->raw;
2945 U_CHAR *l1 = p1 + arg->raw_length;
2948 while (p1 != l1 && is_space[*p1]) p1++;
2949 while (p1 != l1 && is_idchar[*p1])
2950 xbuf[totlen++] = *p1++;
2954 /* Arg is concatenated after: delete trailing whitespace,
2955 whitespace markers, and no-reexpansion markers. */
2958 if (is_space[l1[-1]]) l1--;
2959 else if (l1[-1] == '-')
2961 U_CHAR *p2 = l1 - 1;
2962 /* If a `-' is preceded by an odd number of newlines then it
2963 and the last newline are a no-reexpansion marker. */
2964 while (p2 != p1 && p2[-1] == '\n') p2--;
2965 if ((l1 - 1 - p2) & 1) {
2974 bcopy (p1, xbuf + totlen, l1 - p1);
2979 U_CHAR *expanded = ARG_BASE + arg->expanded;
2980 if (!ap->raw_before && totlen > 0 && arg->expand_length
2981 && !CPP_TRADITIONAL(pfile)
2982 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2984 xbuf[totlen++] = '@';
2985 xbuf[totlen++] = ' ';
2988 bcopy (expanded, xbuf + totlen, arg->expand_length);
2989 totlen += arg->expand_length;
2991 if (!ap->raw_after && totlen > 0 && offset < defn->length
2992 && !CPP_TRADITIONAL(pfile)
2993 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2995 xbuf[totlen++] = '@';
2996 xbuf[totlen++] = ' ';
2999 /* If a macro argument with newlines is used multiple times,
3000 then only expand the newlines once. This avoids creating
3001 output lines which don't correspond to any input line,
3002 which confuses gdb and gcov. */
3003 if (arg->use_count > 1 && arg->newlines > 0)
3005 /* Don't bother doing change_newlines for subsequent
3009 = change_newlines (expanded, arg->expand_length);
3013 if (totlen > xbuf_len)
3017 /* if there is anything left of the definition
3018 after handling the arg list, copy that in too. */
3020 for (i = offset; i < defn->length; i++)
3022 /* if we've reached the end of the macro */
3025 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3026 && last_ap->raw_after))
3027 xbuf[totlen++] = exp[i];
3035 pfile->output_escapes--;
3037 /* Now put the expansion on the input stack
3038 so our caller will commence reading from it. */
3039 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3040 CPP_BUFFER (pfile)->has_escapes = 1;
3042 /* Pop the space we've used in the token_buffer for argument expansion. */
3043 CPP_SET_WRITTEN (pfile, old_written);
3045 /* Recursive macro use sometimes works traditionally.
3046 #define foo(x,y) bar (x (y,0), y)
3049 if (!CPP_TRADITIONAL (pfile))
3050 hp->type = T_DISABLED;
3054 push_macro_expansion (
3056 register U_CHAR *xbuf,
3060 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3061 mbuf->cleanup = macro_cleanup;
3064 /* The first chars of the expansion should be a "@ " added by
3065 collect_expansion. This is to prevent accidental token-pasting
3066 between the text preceding the macro invocation, and the macro
3069 We would like to avoid adding unneeded spaces (for the sake of
3070 tools that use cpp, such as imake). In some common cases we can
3071 tell that it is safe to omit the space.
3073 The character before the macro invocation cannot have been an
3074 idchar (or else it would have been pasted with the idchars of
3075 the macro name). Therefore, if the first non-space character
3076 of the expansion is an idchar, we do not need the extra space
3077 to prevent token pasting.
3079 Also, we don't need the extra space if the first char is '(',
3080 or some other (less common) characters. */
3082 if (xbuf[0] == '@' && xbuf[1] == ' '
3083 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3084 || xbuf[2] == '\"'))
3088 /* Like cpp_get_token, except that it does not read past end-of-line.
3089 Also, horizontal space is skipped, and macros are popped. */
3091 static enum cpp_token
3092 get_directive_token (
3097 long old_written = CPP_WRITTEN (pfile);
3098 enum cpp_token token;
3099 cpp_skip_hspace (pfile);
3100 if (PEEKC () == '\n')
3102 token = cpp_get_token (pfile);
3106 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3108 /* ... else fall though ... */
3109 case CPP_HSPACE: case CPP_COMMENT:
3110 CPP_SET_WRITTEN (pfile, old_written);
3118 /* Handle #include and #import.
3119 This function expects to see "fname" or <fname> on the input.
3121 The input is normally in part of the output_buffer following
3122 CPP_WRITTEN, and will get overwritten by output_line_command.
3123 I.e. in input file specification has been popped by handle_directive.
3129 struct directive *keyword,
3130 U_CHAR *unused1, U_CHAR *unused2)
3132 int importing = (keyword->type == T_IMPORT);
3133 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3134 char *fname; /* Dynamically allocated fname buffer */
3137 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3138 enum cpp_token token;
3140 /* Chain of dirs to search */
3141 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3142 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3143 struct file_name_list *searchptr = 0;
3144 long old_written = CPP_WRITTEN (pfile);
3148 int f; /* file number */
3150 int retried = 0; /* Have already tried macro
3151 expanding the include line*/
3152 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3157 f= -1; /* JF we iz paranoid! */
3159 if (importing && CPP_OPTIONS (pfile)->warn_import
3160 && !CPP_OPTIONS (pfile)->inhibit_warnings
3161 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3163 pfile->import_warning = 1;
3164 cpp_warning (pfile, "using `#import' is not recommended");
3165 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3166 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3167 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3168 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3169 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3170 fprintf (stderr, " ... <real contents of file> ...\n");
3171 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3172 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3173 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3174 fprintf (stderr, "when it is equipped with such a conditional.\n");
3177 pfile->parsing_include_directive++;
3178 token = get_directive_token (pfile);
3179 pfile->parsing_include_directive--;
3181 if (token == CPP_STRING)
3183 /* FIXME - check no trailing garbage */
3184 fbeg = pfile->token_buffer + old_written + 1;
3185 fend = CPP_PWRITTEN (pfile) - 1;
3186 if (fbeg[-1] == '<')
3189 /* If -I-, start with the first -I dir after the -I-. */
3190 if (CPP_OPTIONS (pfile)->first_bracket_include)
3191 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3193 /* If -I- was specified, don't search current dir, only spec'd ones. */
3194 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3197 /* We have "filename". Figure out directory this source
3198 file is coming from and put it on the front of the list. */
3200 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3205 if ((nam = fp->nominal_fname) != NULL)
3207 /* Found a named file. Figure out dir of the file,
3208 and put it in front of the search list. */
3209 dsp[0].next = search_start;
3212 ep = rindex (nam, '/');
3214 ep = rindex (nam, ']');
3215 if (ep == NULL) ep = rindex (nam, '>');
3216 if (ep == NULL) ep = rindex (nam, ':');
3217 if (ep != NULL) ep++;
3222 dsp[0].fname = (char *) alloca (n + 1);
3223 strncpy (dsp[0].fname, nam, n);
3224 dsp[0].fname[n] = '\0';
3225 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3226 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3230 dsp[0].fname = 0; /* Current directory */
3232 dsp[0].got_name_map = 0;
3239 else if (token == CPP_NAME)
3242 * Support '#include xyz' like VAX-C to allow for easy use of all the
3243 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3244 * code from case '<' is repeated here) and generates a warning.
3247 "VAX-C-style include specification found, use '#include <filename.h>' !");
3249 /* If -I-, start with the first -I dir after the -I-. */
3250 if (CPP_OPTIONS (pfile)->first_bracket_include)
3251 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3252 fbeg = pfile->token_buffer + old_written;
3253 fend = CPP_PWRITTEN (pfile);
3259 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3260 CPP_SET_WRITTEN (pfile, old_written);
3261 skip_rest_of_line (pfile);
3267 token = get_directive_token (pfile);
3268 if (token != CPP_VSPACE)
3270 cpp_error (pfile, "junk at end of `#include'");
3271 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3272 token = get_directive_token (pfile);
3275 /* For #include_next, skip in the search path
3276 past the dir in which the containing file was found. */
3280 for (fp = CPP_BUFFER (pfile); fp != NULL; fp = CPP_PREV_BUFFER (fp))
3281 if (fp->fname != NULL)
3283 /* fp->dir is null if the containing file was specified with
3284 an absolute file name. In that case, don't skip anything. */
3285 if (fp->dir == SELF_DIR_DUMMY)
3286 search_start = CPP_OPTIONS (pfile)->include;
3288 search_start = fp->dir->next;
3293 CPP_SET_WRITTEN (pfile, old_written);
3299 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3303 /* Allocate this permanently, because it gets stored in the definitions
3305 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3306 /* + 2 above for slash and terminating null. */
3307 /* + 2 added for '.h' on VMS (to support '#include filename') */
3309 /* If specified file name is absolute, just open it. */
3312 strncpy (fname, fbeg, flen);
3314 if (redundant_include_p (pfile, fname))
3317 f = lookup_import (pfile, fname, NULL_PTR);
3319 f = open_include_file (pfile, fname, NULL_PTR);
3321 return 0; /* Already included this file */
3323 /* Search directory path, trying to open the file.
3324 Copy each filename tried into FNAME. */
3326 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3327 if (searchptr->fname) {
3328 /* The empty string in a search path is ignored.
3329 This makes it possible to turn off entirely
3330 a standard piece of the list. */
3331 if (searchptr->fname[0] == 0)
3333 strcpy (fname, searchptr->fname);
3334 strcat (fname, "/");
3335 fname[strlen (fname) + flen] = 0;
3339 strncat (fname, fbeg, flen);
3341 /* Change this 1/2 Unix 1/2 VMS file specification into a
3342 full VMS file specification */
3343 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3344 /* Fix up the filename */
3345 hack_vms_include_specification (fname);
3347 /* This is a normal VMS filespec, so use it unchanged. */
3348 strncpy (fname, fbeg, flen);
3350 /* if it's '#include filename', add the missing .h */
3351 if (index(fname,'.')==NULL) {
3352 strcat (fname, ".h");
3356 /* ??? There are currently 3 separate mechanisms for avoiding processing
3357 of redundant include files: #import, #pragma once, and
3358 redundant_include_p. It would be nice if they were unified. */
3359 if (redundant_include_p (pfile, fname))
3362 f = lookup_import (pfile, fname, searchptr);
3364 f = open_include_file (pfile, fname, searchptr);
3366 return 0; /* Already included this file */
3368 else if (f == -1 && errno == EACCES)
3369 cpp_warning (pfile, "Header file %s exists, but is not readable",
3379 /* A file that was not found. */
3380 strncpy (fname, fbeg, flen);
3382 /* If generating dependencies and -MG was specified, we assume missing
3383 files are leaf files, living in the same directory as the source file
3384 or other similar place; these missing files may be generated from
3385 other files and may not exist yet (eg: y.tab.h). */
3387 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3388 && CPP_PRINT_DEPS (pfile)
3389 > (angle_brackets || (pfile->system_include_depth > 0)))
3391 /* If it was requested as a system header file,
3392 then assume it belongs in the first place to look for such. */
3395 for (searchptr = search_start; searchptr;
3396 searchptr = searchptr->next)
3398 if (searchptr->fname)
3402 if (searchptr->fname[0] == 0)
3404 p = (char *) alloca (strlen (searchptr->fname)
3405 + strlen (fname) + 2);
3406 strcpy (p, searchptr->fname);
3409 deps_output (pfile, p, ' ');
3416 /* Otherwise, omit the directory, as if the file existed
3417 in the directory with the source. */
3418 deps_output (pfile, fname, ' ');
3421 /* If -M was specified, and this header file won't be added to the
3422 dependency list, then don't count this as an error, because we can
3423 still produce correct output. Otherwise, we can't produce correct
3424 output, because there may be dependencies we need inside the missing
3425 file, and we don't know what directory this missing file exists in.*/
3426 else if (CPP_PRINT_DEPS (pfile)
3427 && (CPP_PRINT_DEPS (pfile)
3428 <= (angle_brackets || (pfile->system_include_depth > 0))))
3429 cpp_warning (pfile, "No include path in which to find %s", fname);
3430 else if (search_start)
3431 cpp_error_from_errno (pfile, fname);
3433 cpp_error (pfile, "No include path in which to find %s", fname);
3436 /* Check to see if this include file is a once-only include file.
3439 struct file_name_list* ptr;
3441 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3442 if (!strcmp (ptr->fname, fname)) {
3444 return 0; /* This file was once'd. */
3448 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3449 if (!strcmp (ptr->fname, fname))
3450 break; /* This file was included before. */
3454 /* This is the first time for this file. */
3455 /* Add it to list of files included. */
3457 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3458 ptr->control_macro = 0;
3459 ptr->c_system_include_path = 0;
3460 ptr->next = pfile->all_include_files;
3461 pfile->all_include_files = ptr;
3462 ptr->fname = savestring (fname);
3463 ptr->got_name_map = 0;
3465 /* For -M, add this file to the dependencies. */
3466 if (CPP_PRINT_DEPS (pfile)
3467 > (angle_brackets || (pfile->system_include_depth > 0)))
3468 deps_output (pfile, fname, ' ');
3471 /* Handle -H option. */
3472 if (CPP_OPTIONS(pfile)->print_include_names)
3474 cpp_buffer *buf = CPP_BUFFER (pfile);
3475 while ((buf = CPP_PREV_BUFFER (buf)) != NULL)
3477 fprintf (stderr, "%s\n", fname);
3481 pfile->system_include_depth++;
3483 /* Actually process the file. */
3485 /* Record file on "seen" list for #import. */
3486 add_import (pfile, f, fname);
3488 pcftry = (char *) alloca (strlen (fname) + 30);
3500 sprintf (pcftry, "%s%d", fname, pcfnum++);
3502 pcf = open (pcftry, O_RDONLY, 0666);
3508 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3510 || stat_f.st_dev != s.st_dev)
3512 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3513 /* Don't need it any more. */
3518 /* Don't need it at all. */
3523 } while (pcf != -1 && !pcfbuf);
3527 /* Actually process the file */
3528 cpp_push_buffer (pfile, NULL, 0);
3529 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3530 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3532 output_line_command (pfile, 0, enter_file);
3533 pfile->only_seen_white = 2;
3537 pfile->system_include_depth--;
3542 /* Return nonzero if there is no need to include file NAME
3543 because it has already been included and it contains a conditional
3544 to make a repeated include do nothing. */
3547 redundant_include_p (
3551 struct file_name_list *l = pfile->all_include_files;
3552 for (; l; l = l->next)
3553 if (! strcmp (name, l->fname)
3555 && cpp_lookup (pfile, l->control_macro, -1, -1))
3560 /* Return nonzero if the given FILENAME is an absolute pathname which
3561 designates a file within one of the known "system" include file
3562 directories. We assume here that if the given FILENAME looks like
3563 it is the name of a file which resides either directly in a "system"
3564 include file directory, or within any subdirectory thereof, then the
3565 given file must be a "system" include file. This function tells us
3566 if we should suppress pedantic errors/warnings for the given FILENAME.
3568 The value is 2 if the file is a C-language system header file
3569 for which C++ should (on most systems) assume `extern "C"'. */
3574 register char *filename)
3576 struct file_name_list *searchptr;
3578 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3579 searchptr = searchptr->next)
3580 if (searchptr->fname) {
3581 register char *sys_dir = searchptr->fname;
3582 register unsigned length = strlen (sys_dir);
3584 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3586 if (searchptr->c_system_include_path)
3597 * Install a name in the assertion hash table.
3599 * If LEN is >= 0, it is the length of the name.
3600 * Otherwise, compute the length by scanning the entire name.
3602 * If HASH is >= 0, it is the precomputed hash code.
3603 * Otherwise, compute the hash code.
3605 static ASSERTION_HASHNODE *
3612 register ASSERTION_HASHNODE *hp;
3613 register int i, bucket;
3614 register U_CHAR *p, *q;
3616 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3617 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3619 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3620 hp->next = pfile->assertion_hashtab[bucket];
3621 pfile->assertion_hashtab[bucket] = hp;
3623 if (hp->next != NULL)
3624 hp->next->prev = hp;
3627 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3630 for (i = 0; i < len; i++)
3636 * find the most recent hash node for name name (ending with first
3637 * non-identifier char) installed by install
3639 * If LEN is >= 0, it is the length of the name.
3640 * Otherwise, compute the length by scanning the entire name.
3642 * If HASH is >= 0, it is the precomputed hash code.
3643 * Otherwise, compute the hash code.
3646 static ASSERTION_HASHNODE *
3653 register ASSERTION_HASHNODE *bucket;
3655 bucket = pfile->assertion_hashtab[hash];
3657 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3659 bucket = bucket->next;
3666 ASSERTION_HASHNODE *hp)
3668 struct tokenlist_list *tail;
3669 if (hp->prev != NULL)
3670 hp->prev->next = hp->next;
3671 if (hp->next != NULL)
3672 hp->next->prev = hp->prev;
3674 for (tail = hp->value; tail; )
3676 struct tokenlist_list *next = tail->next;
3677 free_token_list (tail->tokens);
3682 /* make sure that the bucket chain header that
3683 the deleted guy was on points to the right thing afterwards. */
3684 if (hp == *hp->bucket_hdr)
3685 *hp->bucket_hdr = hp->next;
3690 /* Convert a character string literal into a nul-terminated string.
3691 The input string is [IN ... LIMIT).
3692 The result is placed in RESULT. RESULT can be the same as IN.
3693 The value returned in the end of the string written to RESULT,
3694 or NULL on error. */
3699 register U_CHAR *result, U_CHAR *in,U_CHAR *limit,
3719 char *bpc = (char *) in;
3720 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3721 in = (U_CHAR *) bpc;
3723 *result++ = (U_CHAR)c;
3726 /* else fall through */
3736 * interpret #line command. Remembers previously seen fnames
3737 * in its very own hash table.
3739 #define FNAME_HASHSIZE 37
3744 struct directive *keyword)
3746 cpp_buffer *ip = CPP_BUFFER (pfile);
3748 long old_written = CPP_WRITTEN (pfile);
3749 enum file_change_code file_change = same_file;
3750 enum cpp_token token;
3753 token = get_directive_token (pfile);
3755 if (token != CPP_NUMBER
3756 || !isdigit(pfile->token_buffer[old_written]))
3758 cpp_error (pfile, "invalid format `#line' command");
3759 goto bad_line_directive;
3762 /* The Newline at the end of this line remains to be processed.
3763 To put the next line at the specified line number,
3764 we must store a line number now that is one less. */
3765 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3766 CPP_SET_WRITTEN (pfile, old_written);
3768 /* NEW_LINENO is one less than the actual line number here. */
3769 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3770 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3772 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3773 if (PEEKC() && !is_space[PEEKC()]) {
3774 cpp_error (pfile, "invalid format `#line' command");
3775 goto bad_line_directive;
3779 token = get_directive_token (pfile);
3781 if (token == CPP_STRING) {
3782 U_CHAR *fname = pfile->token_buffer + old_written;
3784 static HASHNODE *fname_table[FNAME_HASHSIZE];
3785 HASHNODE *hp, **hash_bucket;
3790 /* Turn the file name, which is a character string literal,
3791 into a null-terminated string. Do this in place. */
3792 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3793 if (end_name == NULL)
3795 cpp_error (pfile, "invalid format `#line' command");
3796 goto bad_line_directive;
3799 fname_length = end_name - fname;
3801 num_start = CPP_WRITTEN (pfile);
3802 token = get_directive_token (pfile);
3803 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3804 p = pfile->token_buffer + num_start;
3805 if (CPP_PEDANTIC (pfile))
3806 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3808 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3810 cpp_error (pfile, "invalid format `#line' command");
3811 goto bad_line_directive;
3814 file_change = enter_file;
3816 file_change = leave_file;
3818 ip->system_header_p = 1;
3819 else /* if (*p == 4) */
3820 ip->system_header_p = 2;
3822 CPP_SET_WRITTEN (pfile, num_start);
3823 token = get_directive_token (pfile);
3824 p = pfile->token_buffer + num_start;
3825 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3826 ip->system_header_p = *p == 3 ? 1 : 2;
3827 token = get_directive_token (pfile);
3829 if (token != CPP_VSPACE) {
3830 cpp_error (pfile, "invalid format `#line' command");
3831 goto bad_line_directive;
3836 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3837 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3838 if (hp->length == fname_length &&
3839 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3840 ip->nominal_fname = hp->value.cpval;
3844 /* Didn't find it; cons up a new one. */
3845 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3846 hp->next = *hash_bucket;
3849 hp->length = fname_length;
3850 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3851 bcopy (fname, hp->value.cpval, fname_length);
3854 else if (token != CPP_VSPACE && token != CPP_EOF) {
3855 cpp_error (pfile, "invalid format `#line' command");
3856 goto bad_line_directive;
3859 ip->lineno = new_lineno;
3861 skip_rest_of_line (pfile);
3862 CPP_SET_WRITTEN (pfile, old_written);
3863 output_line_command (pfile, 0, file_change);
3868 * remove the definition of a symbol from the symbol table.
3869 * according to un*x /lib/cpp, it is not an error to undef
3870 * something that has no definitions, so it isn't one here either.
3876 struct directive *keyword,
3877 U_CHAR *buf, U_CHAR *limit)
3881 U_CHAR *orig_buf = buf;
3884 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3885 if (pcp_outfile && keyword)
3886 pass_thru_directive (buf, limit, pfile, keyword);
3889 SKIP_WHITE_SPACE (buf);
3890 sym_length = check_macro_name (pfile, buf, "macro");
3892 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3894 /* If we are generating additional info for debugging (with -g) we
3895 need to pass through all effective #undef commands. */
3896 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3897 pass_thru_directive (orig_buf, limit, pfile, keyword);
3898 if (hp->type != T_MACRO)
3899 cpp_warning (pfile, "undefining `%s'", hp->name);
3903 if (CPP_PEDANTIC (pfile)) {
3905 SKIP_WHITE_SPACE (buf);
3907 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3913 * Report an error detected by the program we are processing.
3914 * Use the text of the line in the error message.
3915 * (We use error because it prints the filename & line#.)
3921 struct directive *keyword,
3922 U_CHAR *buf,U_CHAR *limit)
3924 int length = limit - buf;
3925 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3926 bcopy (buf, copy, length);
3928 SKIP_WHITE_SPACE (copy);
3929 cpp_error (pfile, "#error %s", copy);
3934 * Report a warning detected by the program we are processing.
3935 * Use the text of the line in the warning message, then continue.
3936 * (We use error because it prints the filename & line#.)
3942 struct directive *keyword,
3943 U_CHAR *buf,U_CHAR *limit)
3945 int length = limit - buf;
3946 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3947 bcopy (buf, copy, length);
3949 SKIP_WHITE_SPACE (copy);
3950 cpp_warning (pfile, "#warning %s", copy);
3954 /* Remember the name of the current file being read from so that we can
3955 avoid ever including it again. */
3961 cpp_buffer *ip = NULL;
3962 struct file_name_list *new;
3964 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3968 if (ip->fname != NULL)
3973 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3974 new->next = pfile->dont_repeat_files;
3975 pfile->dont_repeat_files = new;
3976 new->fname = savestring (ip->fname);
3977 new->control_macro = 0;
3978 new->got_name_map = 0;
3979 new->c_system_include_path = 0;
3984 /* #ident has already been copied to the output file, so just ignore it. */
3989 struct directive *keyword,
3990 U_CHAR *buf, U_CHAR *limit)
3992 /* long old_written = CPP_WRITTEN (pfile);*/
3995 /* Allow #ident in system headers, since that's not user's fault. */
3996 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3997 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3999 /* Leave rest of line to be read by later calls to cpp_get_token. */
4004 /* #pragma and its argument line have already been copied to the output file.
4005 Just check for some recognized pragmas that need validation here. */
4010 struct directive *keyword,
4011 U_CHAR *buf,U_CHAR *limit)
4013 while (*buf == ' ' || *buf == '\t')
4015 if (!strncmp (buf, "once", 4)) {
4016 /* Allow #pragma once in system headers, since that's not the user's
4018 if (!CPP_BUFFER (pfile)->system_header_p)
4019 cpp_warning (pfile, "`#pragma once' is obsolete");
4023 if (!strncmp (buf, "implementation", 14)) {
4024 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4025 been included yet. */
4026 struct file_name_list *ptr;
4027 U_CHAR *p = buf + 14, *fname, *inc_fname;
4029 SKIP_WHITE_SPACE (p);
4030 if (*p == '\n' || *p != '\"')
4034 p = (U_CHAR *) index (fname, '\"');
4035 fname_len = p != NULL ? p - fname : strlen (fname);
4037 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4038 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4039 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4040 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4042 "`#pragma implementation' for `%s' appears after file is included",
4051 /* This was a fun hack, but #pragma seems to start to be useful.
4052 By failing to recognize it, we pass it through unchanged to cc1. */
4055 * the behavior of the #pragma directive is implementation defined.
4056 * this implementation defines it as follows.
4063 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4066 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4068 execl ("/usr/games/hack", "#pragma", 0);
4069 execl ("/usr/games/rogue", "#pragma", 0);
4070 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4071 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4073 fatal ("You are in a maze of twisty compiler features, all different");
4077 /* Just ignore #sccs, on systems where we define it at all. */
4082 struct directive *keyword,
4083 U_CHAR *buf,U_CHAR *limit)
4085 if (CPP_PEDANTIC (pfile))
4086 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4091 * handle #if command by
4092 * 1) inserting special `defined' keyword into the hash table
4093 * that gets turned into 0 or 1 by special_symbol (thus,
4094 * if the luser has a symbol called `defined' already, it won't
4095 * work inside the #if command)
4096 * 2) rescan the input into a temporary output buffer
4097 * 3) pass the output buffer to the yacc parser and collect a value
4098 * 4) clean up the mess left from steps 1 and 2.
4099 * 5) call conditional_skip to skip til the next #endif (etc.),
4100 * or not, depending on the value from step 3.
4106 struct directive *keyword,
4107 U_CHAR *buf,U_CHAR *limit)
4109 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4110 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4115 * handle a #elif directive by not changing if_stack either.
4116 * see the comment above do_else.
4122 struct directive *keyword,
4123 U_CHAR *buf,U_CHAR *limit)
4125 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4126 cpp_error (pfile, "`#elif' not within a conditional");
4129 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4130 cpp_error (pfile, "`#elif' after `#else'");
4132 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4134 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4135 && strcmp (pfile->if_stack->fname,
4136 CPP_BUFFER (pfile)->nominal_fname) != 0)
4137 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4138 fprintf (stderr, ")\n");
4140 pfile->if_stack->type = T_ELIF;
4143 if (pfile->if_stack->if_succeeded)
4144 skip_if_group (pfile, 0);
4146 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4148 skip_if_group (pfile, 0);
4150 ++pfile->if_stack->if_succeeded; /* continue processing input */
4151 output_line_command (pfile, 1, same_file);
4158 * evaluate a #if expression in BUF, of length LENGTH,
4159 * then parse the result as a C expression and return the value as an int.
4161 static HOST_WIDE_INT
4162 eval_if_expression (
4167 HASHNODE *save_defined;
4168 HOST_WIDE_INT value;
4169 long old_written = CPP_WRITTEN (pfile);
4171 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4172 pfile->pcp_inside_if = 1;
4174 value = cpp_parse_expr (pfile);
4175 pfile->pcp_inside_if = 0;
4176 delete_macro (save_defined); /* clean up special symbol */
4178 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4184 * routine to handle ifdef/ifndef. Try to look up the symbol,
4185 * then do or don't skip to the #endif/#else/#elif depending
4186 * on what directive is actually being processed.
4192 struct directive *keyword,
4193 U_CHAR *unused1, U_CHAR *unused2)
4196 cpp_buffer *ip = CPP_BUFFER (pfile);
4199 enum cpp_token token;
4200 int start_of_file = 0;
4201 U_CHAR *control_macro = 0;
4202 int old_written = CPP_WRITTEN (pfile);
4204 /* Detect a #ifndef at start of file (not counting comments). */
4205 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4206 start_of_file = pfile->only_seen_white == 2;
4208 pfile->no_macro_expand++;
4209 token = get_directive_token (pfile);
4210 pfile->no_macro_expand--;
4212 ident = pfile->token_buffer + old_written;
4213 ident_length = CPP_WRITTEN (pfile) - old_written;
4214 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4216 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4218 skip = (keyword->type == T_IFDEF);
4219 if (! CPP_TRADITIONAL (pfile))
4220 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4222 else if (token == CPP_NAME)
4224 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4225 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4226 if (start_of_file && !skip)
4228 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4229 bcopy (ident, control_macro, ident_length + 1);
4234 skip = (keyword->type == T_IFDEF);
4235 if (! CPP_TRADITIONAL (pfile))
4236 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4239 if (!CPP_TRADITIONAL (pfile))
4241 cpp_skip_hspace (pfile);
4243 if (c != EOF && c != '\n')
4244 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4246 skip_rest_of_line (pfile);
4250 /* Output a precondition for this macro. */
4251 if (hp && hp->value.defn->predefined)
4252 fprintf (pcp_outfile, "#define %s\n", hp->name);
4255 fprintf (pcp_outfile, "#undef ");
4256 while (is_idchar[*cp]) /* Ick! */
4257 fputc (*cp++, pcp_outfile);
4258 putc ('\n', pcp_outfile);
4262 conditional_skip (pfile, skip, T_IF, control_macro);
4266 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4267 If this is a #ifndef starting at the beginning of a file,
4268 CONTROL_MACRO is the macro name tested by the #ifndef.
4269 Otherwise, CONTROL_MACRO is 0. */
4275 enum node_type type,
4276 U_CHAR *control_macro)
4278 IF_STACK_FRAME *temp;
4280 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4281 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4283 temp->lineno = CPP_BUFFER (pfile)->lineno;
4285 temp->next = pfile->if_stack;
4286 temp->control_macro = control_macro;
4287 pfile->if_stack = temp;
4289 pfile->if_stack->type = type;
4292 skip_if_group (pfile, 0);
4295 ++pfile->if_stack->if_succeeded;
4296 output_line_command (pfile, 1, same_file);
4301 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4302 * leaves input ptr at the sharp sign found.
4303 * If ANY is nonzero, return at next directive of any sort.
4311 int at_beg_of_line = 1;
4312 struct directive *kt;
4313 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4315 U_CHAR *beg_of_line = bp;
4317 register int ident_length;
4318 U_CHAR *ident, *after_ident;
4319 struct parse_marker line_start_mark;
4321 parse_set_mark (&line_start_mark, pfile);
4323 if (CPP_OPTIONS (pfile)->output_conditionals) {
4324 static char failed[] = "#failed\n";
4325 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4327 output_line_command (pfile, 1, same_file);
4331 if (CPP_OPTIONS (pfile)->output_conditionals)
4333 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4334 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4335 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4337 parse_move_mark (&line_start_mark, pfile);
4338 if (!CPP_TRADITIONAL (pfile))
4339 cpp_skip_hspace (pfile);
4343 int old_written = CPP_WRITTEN (pfile);
4344 cpp_skip_hspace (pfile);
4346 parse_name (pfile, GETC());
4347 ident_length = CPP_WRITTEN (pfile) - old_written;
4348 ident = pfile->token_buffer + old_written;
4349 pfile->limit = ident;
4351 if (ident_length == 0)
4352 goto not_a_directive;
4354 /* Handle # followed by a line number. */
4356 /* Avoid error for `###' and similar cases unless -pedantic. */
4359 for (kt = directive_table; kt->length >= 0; kt++)
4361 IF_STACK_FRAME *temp;
4362 if (ident_length == kt->length
4363 && strncmp (ident, kt->name, kt->length) == 0)
4365 /* If we are asked to return on next directive, do so now. */
4375 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4376 temp->next = pfile->if_stack;
4377 pfile->if_stack = temp;
4379 temp->lineno = CPP_BUFFER(pfile)->lineno;
4381 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4382 temp->type = kt->type;
4386 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4387 validate_else (pfile,
4388 kt->type == T_ELSE ? "#else" : "#endif");
4390 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4393 "`#%s' not within a conditional", kt->name);
4396 else if (pfile->if_stack == save_if_stack)
4397 goto done; /* found what we came for */
4399 if (kt->type != T_ENDIF)
4401 if (pfile->if_stack->type == T_ELSE)
4402 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4403 pfile->if_stack->type = kt->type;
4407 temp = pfile->if_stack;
4408 pfile->if_stack = temp->next;
4415 /* Don't let erroneous code go by. */
4416 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4417 && CPP_PEDANTIC (pfile))
4418 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4422 /* We're in the middle of a line. Skip the rest of it. */
4429 case '/': /* possible comment */
4430 c = skip_comment (pfile, NULL);
4437 old = CPP_WRITTEN (pfile);
4438 cpp_get_token (pfile);
4439 CPP_SET_WRITTEN (pfile, old);
4442 /* Char after backslash loses its special meaning. */
4443 if (PEEKC() == '\n')
4453 if (CPP_OPTIONS (pfile)->output_conditionals) {
4454 static char end_failed[] = "#endfailed\n";
4455 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4458 pfile->only_seen_white = 1;
4459 parse_goto_mark (&line_start_mark, pfile);
4460 parse_clear_mark (&line_start_mark);
4464 * handle a #else directive. Do this by just continuing processing
4465 * without changing if_stack ; this is so that the error message
4466 * for missing #endif's etc. will point to the original #if. It
4467 * is possible that something different would be better.
4473 struct directive *keyword,
4474 U_CHAR *buf, U_CHAR *limit)
4476 cpp_buffer *ip = CPP_BUFFER (pfile);
4478 if (CPP_PEDANTIC (pfile))
4479 validate_else (pfile, "#else");
4480 skip_rest_of_line (pfile);
4482 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4483 cpp_error (pfile, "`#else' not within a conditional");
4486 /* #ifndef can't have its special treatment for containing the whole file
4487 if it has a #else clause. */
4488 pfile->if_stack->control_macro = 0;
4490 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4491 cpp_error (pfile, "`#else' after `#else'");
4492 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4493 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4494 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4495 fprintf (stderr, ")\n");
4497 pfile->if_stack->type = T_ELSE;
4500 if (pfile->if_stack->if_succeeded)
4501 skip_if_group (pfile, 0);
4503 ++pfile->if_stack->if_succeeded; /* continue processing input */
4504 output_line_command (pfile, 1, same_file);
4510 * unstack after #endif command
4516 struct directive *keyword,
4517 U_CHAR *buf, U_CHAR *limit)
4519 if (CPP_PEDANTIC (pfile))
4520 validate_else (pfile, "#endif");
4521 skip_rest_of_line (pfile);
4523 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4524 cpp_error (pfile, "unbalanced `#endif'");
4527 IF_STACK_FRAME *temp = pfile->if_stack;
4528 pfile->if_stack = temp->next;
4529 if (temp->control_macro != 0)
4531 /* This #endif matched a #ifndef at the start of the file.
4532 See if it is at the end of the file. */
4533 struct parse_marker start_mark;
4536 parse_set_mark (&start_mark, pfile);
4540 cpp_skip_hspace (pfile);
4545 parse_goto_mark (&start_mark, pfile);
4546 parse_clear_mark (&start_mark);
4550 /* If we get here, this #endif ends a #ifndef
4551 that contains all of the file (aside from whitespace).
4552 Arrange not to include the file again
4553 if the macro that was tested is defined.
4555 Do not do this for the top-level file in a -include or any
4556 file in a -imacros. */
4560 && ! (indepth == 1 && pfile->no_record_file)
4561 && ! (pfile->no_record_file && no_output))
4564 struct file_name_list *ifile = pfile->all_include_files;
4566 for ( ; ifile != NULL; ifile = ifile->next)
4568 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4570 ifile->control_macro = temp->control_macro;
4578 output_line_command (pfile, 1, same_file);
4583 /* When an #else or #endif is found while skipping failed conditional,
4584 if -pedantic was specified, this is called to warn about text after
4585 the command name. P points to the first char after the command name. */
4593 cpp_skip_hspace (pfile);
4595 if (c != EOF && c != '\n')
4597 "text following `%s' violates ANSI standard", directive);
4600 /* Get the next token, and add it to the text in pfile->token_buffer.
4601 Return the kind of token we got. */
4608 register int c, c2, c3;
4610 long start_line, start_column;
4611 enum cpp_token token;
4612 struct cpp_options *opts = CPP_OPTIONS (pfile);
4613 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4619 if (CPP_BUFFER (pfile)->seen_eof)
4621 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4628 cpp_buffer *next_buf
4629 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4630 CPP_BUFFER (pfile)->seen_eof = 1;
4631 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != 0)
4633 /* We're about to return from an #include file.
4634 Emit #line information now (as part of the CPP_POP) result.
4635 But the #line refers to the file we will pop to. */
4636 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4637 CPP_BUFFER (pfile) = next_buf;
4638 pfile->input_stack_listing_current = 0;
4639 output_line_command (pfile, 0, leave_file);
4640 CPP_BUFFER (pfile) = cur_buffer;
4650 struct parse_marker start_mark;
4652 if (PEEKC () == '=')
4654 if (opts->put_out_comments)
4655 parse_set_mark (&start_mark, pfile);
4657 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4658 &start_line, &start_column);
4659 c = skip_comment (pfile, &newlines);
4660 if (opts->put_out_comments && (c == '/' || c == EOF))
4661 parse_clear_mark (&start_mark);
4666 cpp_error_with_line (pfile, start_line, start_column,
4667 "unterminated comment");
4670 c = '/'; /* Initial letter of comment. */
4672 /* Comments are equivalent to spaces.
4673 For -traditional, a comment is equivalent to nothing. */
4674 if (opts->put_out_comments)
4676 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4678 U_CHAR *start = pbuf->buf + start_mark.position;
4679 int len = pbuf->cur - start;
4680 CPP_RESERVE(pfile, 1 + len);
4681 CPP_PUTC_Q (pfile, c);
4682 CPP_PUTS_Q (pfile, start, len);
4683 pfile->lineno += newlines;
4684 parse_clear_mark (&start_mark);
4687 else if (CPP_TRADITIONAL (pfile))
4694 /* This may not work if cpp_get_token is called recursively,
4695 since many places look for horizontal space. */
4698 /* Copy the newlines into the output buffer, in order to
4699 avoid the pain of a #line every time a multiline comment
4701 CPP_RESERVE(pfile, newlines);
4702 while (--newlines >= 0)
4704 CPP_PUTC_Q (pfile, '\n');
4710 CPP_RESERVE(pfile, 1);
4711 CPP_PUTC_Q (pfile, ' ');
4715 if (opts->for_lint) {
4718 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4720 if (lintcmd != NULL) {
4721 /* I believe it is always safe to emit this newline: */
4723 bcopy ("#pragma lint ", (char *) obp, 13);
4725 bcopy (lintcmd, (char *) obp, cmdlen);
4730 bcopy (argbp, (char *) obp, arglen);
4734 /* OK, now bring us back to the state we were in before we entered
4735 this branch. We need #line b/c the newline for the pragma
4736 could fuck things up. */
4737 output_line_command (pfile, 0, same_file);
4738 *(obp++) = ' '; /* just in case, if comments are copied thru */
4746 /* If this is expanding a macro definition, don't recognize
4747 preprocessor directives. */
4750 /* If this is expand_into_temp_buffer, recognize them
4751 only after an actual newline at this level,
4752 not at the beginning of the input level. */
4753 if (ip->fname == 0 && beg_of_line == ip->buf)
4759 if (!pfile->only_seen_white)
4761 if (handle_directive (pfile))
4762 return CPP_DIRECTIVE;
4763 pfile->only_seen_white = 0;
4768 /* A single quoted string is treated like a double -- some
4769 programs (e.g., troff) are perverse this way */
4770 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4771 &start_line, &start_column);
4772 old_written = CPP_WRITTEN (pfile);
4774 CPP_PUTC (pfile, c);
4780 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4782 /* try harder: this string crosses a macro expansion
4783 boundary. This can happen naturally if -traditional.
4784 Otherwise, only -D can make a macro with an unmatched
4786 cpp_buffer *next_buf
4787 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4788 (*CPP_BUFFER (pfile)->cleanup)
4789 (CPP_BUFFER (pfile), pfile);
4790 CPP_BUFFER (pfile) = next_buf;
4793 if (!CPP_TRADITIONAL (pfile))
4795 cpp_error_with_line (pfile, start_line, start_column,
4796 "unterminated string or character constant");
4797 if (pfile->multiline_string_line != start_line
4798 && pfile->multiline_string_line != 0)
4799 cpp_error_with_line (pfile,
4800 pfile->multiline_string_line, -1,
4801 "possible real start of unterminated constant");
4802 pfile->multiline_string_line = 0;
4806 CPP_PUTC (pfile, cc);
4810 /* Traditionally, end of line ends a string constant with
4811 no error. So exit the loop and record the new line. */
4812 if (CPP_TRADITIONAL (pfile))
4816 cpp_error_with_line (pfile, start_line, start_column,
4817 "unterminated character constant");
4820 if (CPP_PEDANTIC (pfile)
4821 && pfile->multiline_string_line == 0)
4823 cpp_pedwarn_with_line (pfile, start_line, start_column,
4824 "string constant runs past end of line");
4826 if (pfile->multiline_string_line == 0)
4827 pfile->multiline_string_line = start_line;
4834 /* Backslash newline is replaced by nothing at all. */
4835 CPP_ADJUST_WRITTEN (pfile, -1);
4840 /* ANSI stupidly requires that in \\ the second \
4841 is *not* prevented from combining with a newline. */
4844 CPP_PUTC (pfile, cc);
4856 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4857 CPP_PWRITTEN (pfile));
4858 pfile->only_seen_white = 0;
4859 return c == '\'' ? CPP_CHAR : CPP_STRING;
4862 if (!opts->dollars_in_ident)
4867 if (opts->cplusplus && PEEKC () == ':')
4876 if (c2 == c || c2 == '=')
4886 if (PEEKC () == '=')
4893 if (c2 == '-' && opts->chill)
4895 /* Chill style comment */
4896 if (opts->put_out_comments)
4897 parse_set_mark (&start_mark, pfile);
4898 FORWARD(1); /* Skip second '-'. */
4906 /* Don't consider final '\n' to be part of comment. */
4912 goto return_comment;
4914 if (c2 == '-' || c2 == '=' || c2 == '>')
4919 if (pfile->parsing_include_directive)
4923 CPP_PUTC (pfile, c);
4928 if (c == '\n' || c == EOF)
4931 "missing '>' in `#include <FILENAME>'");
4937 /* else fall through */
4946 CPP_RESERVE (pfile, 4);
4947 CPP_PUTC (pfile, c);
4948 CPP_PUTC (pfile, c2);
4952 CPP_PUTC_Q (pfile, GETC ());
4953 CPP_NUL_TERMINATE_Q (pfile);
4954 pfile->only_seen_white = 0;
4958 if (CPP_BUFFER (pfile)->has_escapes)
4963 if (pfile->output_escapes)
4964 CPP_PUTS (pfile, "@-", 2);
4965 parse_name (pfile, GETC ());
4968 else if (is_space [c])
4970 CPP_RESERVE (pfile, 2);
4971 if (pfile->output_escapes)
4972 CPP_PUTC_Q (pfile, '@');
4973 CPP_PUTC_Q (pfile, c);
4977 if (pfile->output_escapes)
4979 CPP_PUTS (pfile, "@@", 2);
4989 CPP_RESERVE(pfile, 2);
4990 CPP_PUTC_Q (pfile, '.');
4994 /* FIXME - misses the case "..\\\n." */
4995 if (c2 == '.' && PEEKN(1) == '.')
4997 CPP_RESERVE(pfile, 4);
4998 CPP_PUTC_Q (pfile, '.');
4999 CPP_PUTC_Q (pfile, '.');
5000 CPP_PUTC_Q (pfile, '.');
5002 CPP_NUL_TERMINATE_Q (pfile);
5003 pfile->only_seen_white = 0;
5010 pfile->only_seen_white = 0;
5012 CPP_RESERVE(pfile, 3);
5013 CPP_PUTC_Q (pfile, c);
5014 CPP_PUTC_Q (pfile, GETC ());
5015 CPP_NUL_TERMINATE_Q (pfile);
5021 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5023 CPP_PUTC (pfile, c);
5029 case '0': case '1': case '2': case '3': case '4':
5030 case '5': case '6': case '7': case '8': case '9':
5035 CPP_RESERVE (pfile, 2);
5036 CPP_PUTC_Q (pfile, c);
5041 if (!is_idchar[c] && c != '.'
5042 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
5047 CPP_NUL_TERMINATE_Q (pfile);
5048 pfile->only_seen_white = 0;
5050 case 'b': case 'c': case 'd': case 'h': case 'o':
5051 case 'B': case 'C': case 'D': case 'H': case 'O':
5052 if (opts->chill && PEEKC () == '\'')
5054 pfile->only_seen_white = 0;
5055 CPP_RESERVE (pfile, 2);
5056 CPP_PUTC_Q (pfile, c);
5057 CPP_PUTC_Q (pfile, '\'');
5063 goto chill_number_eof;
5066 if (c == '\\' && PEEKC() == '\n')
5073 CPP_PUTC (pfile, c);
5077 CPP_RESERVE (pfile, 2);
5078 CPP_PUTC_Q (pfile, c);
5079 CPP_NUL_TERMINATE_Q (pfile);
5086 CPP_NUL_TERMINATE (pfile);
5093 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5094 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5095 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5096 case 'x': case 'y': case 'z':
5097 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5098 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5099 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5104 unsigned char *ident;
5105 int before_name_written = CPP_WRITTEN (pfile);
5107 parse_name (pfile, c);
5108 pfile->only_seen_white = 0;
5109 if (pfile->no_macro_expand)
5111 ident = pfile->token_buffer + before_name_written;
5112 ident_len = CPP_PWRITTEN (pfile) - ident;
5113 hp = cpp_lookup (pfile, ident, ident_len, -1);
5116 if (hp->type == T_DISABLED)
5118 if (pfile->output_escapes)
5119 { /* Return "@-IDENT", followed by '\0'. */
5121 CPP_RESERVE (pfile, 3);
5122 ident = pfile->token_buffer + before_name_written;
5123 CPP_ADJUST_WRITTEN (pfile, 2);
5124 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5131 /* If macro wants an arglist, verify that a '(' follows.
5132 first skip all whitespace, copying it to the output
5133 after the macro name. Then, if there is no '(',
5134 decide this is not a macro call and leave things that way. */
5135 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5137 struct parse_marker macro_mark;
5139 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5141 cpp_buffer *next_buf;
5142 cpp_skip_hspace (pfile);
5143 if (PEEKC () != EOF)
5145 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5146 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5147 CPP_BUFFER (pfile) = next_buf;
5149 parse_set_mark (¯o_mark, pfile);
5152 cpp_skip_hspace (pfile);
5154 is_macro_call = c == '(';
5160 parse_goto_mark (¯o_mark, pfile);
5161 parse_clear_mark (¯o_mark);
5165 /* This is now known to be a macro call. */
5167 /* it might not actually be a macro. */
5168 if (hp->type != T_MACRO) {
5169 int xbuf_len; U_CHAR *xbuf;
5170 CPP_SET_WRITTEN (pfile, before_name_written);
5171 special_symbol (hp, pfile);
5172 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5173 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5174 CPP_SET_WRITTEN (pfile, before_name_written);
5175 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5176 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5180 /* Expand the macro, reading arguments as needed,
5181 and push the expansion on the input stack. */
5182 macroexpand (pfile, hp);
5183 CPP_SET_WRITTEN (pfile, before_name_written);
5186 /* An extra "@ " is added to the end of a macro expansion
5187 to prevent accidental token pasting. We prefer to avoid
5188 unneeded extra spaces (for the sake of cpp-using tools like
5189 imake). Here we remove the space if it is safe to do so. */
5190 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5191 && pfile->buffer->rlimit[-2] == '@'
5192 && pfile->buffer->rlimit[-1] == ' ')
5194 int c1 = pfile->buffer->rlimit[-3];
5195 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5196 if (c2 == EOF || ! unsafe_chars (c1, c2))
5197 pfile->buffer->rlimit -= 2;
5202 case ' ': case '\t': case '\v': case '\r':
5205 CPP_PUTC (pfile, c);
5207 if (c == EOF || !is_hor_space[c])
5221 CPP_PUTC (pfile, c);
5222 if (pfile->only_seen_white == 0)
5223 pfile->only_seen_white = 1;
5225 output_line_command (pfile, 1, same_file);
5228 case '(': token = CPP_LPAREN; goto char1;
5229 case ')': token = CPP_RPAREN; goto char1;
5230 case '{': token = CPP_LBRACE; goto char1;
5231 case '}': token = CPP_RBRACE; goto char1;
5232 case ',': token = CPP_COMMA; goto char1;
5233 case ';': token = CPP_SEMICOLON; goto char1;
5239 pfile->only_seen_white = 0;
5240 CPP_PUTC (pfile, c);
5246 /* Like cpp_get_token, but skip spaces and comments. */
5248 cpp_get_non_space_token (
5251 int old_written = CPP_WRITTEN (pfile);
5254 enum cpp_token token = cpp_get_token (pfile);
5255 if (token != CPP_COMMENT && token != CPP_POP
5256 && token != CPP_HSPACE && token != CPP_VSPACE)
5258 CPP_SET_WRITTEN (pfile, old_written);
5262 /* Parse an identifier starting with C. */
5266 cpp_reader *pfile, int c)
5272 if (c == '\\' && PEEKC() == '\n')
5281 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5282 CPP_PUTC_Q (pfile, c);
5287 CPP_NUL_TERMINATE_Q (pfile);
5292 /* Maintain and search list of included files, for #import. */
5294 /* Hash a file name for import_hash_table. */
5302 while (*f) val += *f++;
5303 return (val%IMPORT_HASH_SIZE);
5306 /* Search for file FILENAME in import_hash_table.
5307 Return -2 if found, either a matching name or a matching inode.
5308 Otherwise, open the file and return a file descriptor if successful
5309 or -1 if unsuccessful. */
5315 struct file_name_list *searchptr)
5317 struct import_file *i;
5323 hashval = import_hash (filename);
5325 /* Attempt to find file in list of already included files */
5326 i = pfile->import_hash_table[hashval];
5329 if (!strcmp (filename, i->name))
5330 return -2; /* return found */
5333 /* Open it and try a match on inode/dev */
5334 fd = open_include_file (pfile, filename, searchptr);
5338 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5339 i = pfile->import_hash_table[h];
5341 /* Compare the inode and the device.
5342 Supposedly on some systems the inode is not a scalar. */
5343 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5344 && i->dev == sb.st_dev) {
5346 return -2; /* return found */
5351 return fd; /* Not found, return open file */
5354 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5362 struct import_file *i;
5366 hashval = import_hash (fname);
5368 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5369 i->name = (char *)xmalloc (strlen (fname)+1);
5370 strcpy (i->name, fname);
5371 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5373 i->next = pfile->import_hash_table[hashval];
5374 pfile->import_hash_table[hashval] = i;
5377 /* The file_name_map structure holds a mapping of file names for a
5378 particular directory. This mapping is read from the file named
5379 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5380 map filenames on a file system with severe filename restrictions,
5381 such as DOS. The format of the file name map file is just a series
5382 of lines with two tokens on each line. The first token is the name
5383 to map, and the second token is the actual name to use. */
5385 struct file_name_map
5387 struct file_name_map *map_next;
5392 #define FILE_NAME_MAP_FILE "header.gcc"
5394 /* Read a space delimited string of unlimited length from a stdio
5398 read_filename_string (
5406 set = alloc = xmalloc (len + 1);
5410 while ((ch = getc (f)) != EOF && ! is_space[ch])
5412 if (set - alloc == len)
5415 alloc = xrealloc (alloc, len + 1);
5416 set = alloc + len / 2;
5426 /* This structure holds a linked list of file name maps, one per directory. */
5427 struct file_name_map_list
5429 struct file_name_map_list *map_list_next;
5430 char *map_list_name;
5431 struct file_name_map *map_list_map;
5434 /* Read the file name map file for DIRNAME. */
5436 static struct file_name_map *
5441 register struct file_name_map_list *map_list_ptr;
5445 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5446 map_list_ptr = map_list_ptr->map_list_next)
5447 if (! strcmp (map_list_ptr->map_list_name, dirname))
5448 return map_list_ptr->map_list_map;
5450 map_list_ptr = ((struct file_name_map_list *)
5451 xmalloc (sizeof (struct file_name_map_list)));
5452 map_list_ptr->map_list_name = savestring (dirname);
5453 map_list_ptr->map_list_map = NULL;
5455 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5456 strcpy (name, dirname);
5459 strcat (name, FILE_NAME_MAP_FILE);
5460 f = fopen (name, "r");
5462 map_list_ptr->map_list_map = NULL;
5466 int dirlen = strlen (dirname);
5468 while ((ch = getc (f)) != EOF)
5471 struct file_name_map *ptr;
5475 from = read_filename_string (ch, f);
5476 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5478 to = read_filename_string (ch, f);
5480 ptr = ((struct file_name_map *)
5481 xmalloc (sizeof (struct file_name_map)));
5482 ptr->map_from = from;
5484 /* Make the real filename absolute. */
5489 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5490 strcpy (ptr->map_to, dirname);
5491 ptr->map_to[dirlen] = '/';
5492 strcpy (ptr->map_to + dirlen + 1, to);
5496 ptr->map_next = map_list_ptr->map_list_map;
5497 map_list_ptr->map_list_map = ptr;
5499 while ((ch = getc (f)) != '\n')
5506 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5507 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5509 return map_list_ptr->map_list_map;
5512 /* Try to open include file FILENAME. SEARCHPTR is the directory
5513 being tried from the include file search path. This function maps
5514 filenames on file systems based on information read by
5521 struct file_name_list *searchptr)
5523 register struct file_name_map *map;
5524 register char *from;
5527 if (searchptr && ! searchptr->got_name_map)
5529 searchptr->name_map = read_name_map (pfile,
5531 ? searchptr->fname : ".");
5532 searchptr->got_name_map = 1;
5535 /* First check the mapping for the directory we are using. */
5536 if (searchptr && searchptr->name_map)
5539 if (searchptr->fname)
5540 from += strlen (searchptr->fname) + 1;
5541 for (map = searchptr->name_map; map; map = map->map_next)
5543 if (! strcmp (map->map_from, from))
5545 /* Found a match. */
5546 return open (map->map_to, O_RDONLY, 0666);
5551 /* Try to find a mapping file for the particular directory we are
5552 looking in. Thus #include <sys/types.h> will look up sys/types.h
5553 in /usr/include/header.gcc and look up types.h in
5554 /usr/include/sys/header.gcc. */
5555 p = rindex (filename, '/');
5560 && strlen (searchptr->fname) == p - filename
5561 && ! strncmp (searchptr->fname, filename, p - filename))
5563 /* FILENAME is in SEARCHPTR, which we've already checked. */
5564 return open (filename, O_RDONLY, 0666);
5574 dir = (char *) alloca (p - filename + 1);
5575 bcopy (filename, dir, p - filename);
5576 dir[p - filename] = '\0';
5579 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5580 if (! strcmp (map->map_from, from))
5581 return open (map->map_to, O_RDONLY, 0666);
5583 return open (filename, O_RDONLY, 0666);
5586 /* Process the contents of include file FNAME, already open on descriptor F,
5588 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5589 "system" include directories (as decided by the `is_system_include'
5591 DIRPTR is the link in the dir path through which this file was found,
5592 or 0 if the file name was absolute or via the current directory.
5593 Return 1 on success, 0 on failure.
5595 The caller is responsible for the cpp_push_buffer. */
5602 int system_header_p,
5603 struct file_name_list *dirptr)
5609 cpp_buffer *fp; /* For input stack frame */
5610 int missing_newline = 0;
5612 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5614 cpp_perror_with_name (pfile, fname);
5616 cpp_pop_buffer (pfile);
5620 fp = CPP_BUFFER (pfile);
5621 fp->nominal_fname = fp->fname = fname;
5626 fp->system_header_p = system_header_p;
5629 fp->cleanup = file_cleanup;
5631 if (S_ISREG (st_mode)) {
5632 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5633 fp->alimit = fp->buf + st_size + 2;
5636 /* Read the file contents, knowing that st_size is an upper bound
5637 on the number of bytes we can read. */
5638 length = safe_read (f, fp->buf, st_size);
5639 fp->rlimit = fp->buf + length;
5640 if (length < 0) goto nope;
5642 else if (S_ISDIR (st_mode)) {
5643 cpp_error (pfile, "directory `%s' specified in #include", fname);
5647 /* Cannot count its file size before reading.
5648 First read the entire file into heap and
5649 copy them into buffer on stack. */
5654 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5657 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5659 goto nope; /* error! */
5661 if (st_size != bsize)
5662 break; /* End of file */
5664 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5670 if ((length > 0 && fp->buf[length - 1] != '\n')
5671 /* Backslash-newline at end is not good enough. */
5672 || (length > 1 && fp->buf[length - 2] == '\\')) {
5673 fp->buf[length++] = '\n';
5675 missing_newline = 1;
5678 fp->buf[length] = '\0';
5679 fp->rlimit = fp->buf + length;
5681 /* Close descriptor now, so nesting does not use lots of descriptors. */
5684 /* Must do this before calling trigraph_pcp, so that the correct file name
5685 will be printed in warning messages. */
5687 pfile->input_stack_listing_current = 0;
5697 if (missing_newline)
5700 if (CPP_PEDANTIC (pfile) && missing_newline)
5701 pedwarn ("file does not end in newline");
5704 input_file_stack_tick++;
5711 cpp_perror_with_name (pfile, fname);
5722 struct cpp_options *opts = CPP_OPTIONS (pfile);
5723 struct cpp_pending *pend;
5728 /* The code looks at the defaults through this pointer, rather than through
5729 the constant structure above. This pointer gets changed if an environment
5730 variable specifies other defaults. */
5731 struct default_include *include_defaults = include_defaults_array;
5733 /* Add dirs from CPATH after dirs from -I. */
5734 /* There seems to be confusion about what CPATH should do,
5735 so for the moment it is not documented. */
5736 /* Some people say that CPATH should replace the standard include dirs,
5737 but that seems pointless: it comes before them, so it overrides them
5739 p = (char *) getenv ("CPATH");
5740 if (p != 0 && ! opts->no_standard_includes)
5741 path_include (pfile, p);
5743 /* Now that dollars_in_ident is known, initialize is_idchar. */
5744 initialize_char_syntax (opts);
5746 /* Do partial setup of input buffer for the sake of generating
5747 early #line directives (when -g is in effect). */
5748 fp = cpp_push_buffer (pfile, NULL, 0);
5749 if (opts->in_fname == NULL)
5750 opts->in_fname = "";
5751 fp->nominal_fname = fp->fname = opts->in_fname;
5754 /* Install __LINE__, etc. Must follow initialize_char_syntax
5755 and option processing. */
5756 initialize_builtins (pfile);
5758 /* Do standard #defines and assertions
5759 that identify system and machine type. */
5761 if (!opts->inhibit_predefs) {
5762 char *p = (char *) alloca (strlen (predefs) + 1);
5763 strcpy (p, predefs);
5766 while (*p == ' ' || *p == '\t')
5768 /* Handle -D options. */
5769 if (p[0] == '-' && p[1] == 'D') {
5771 while (*p && *p != ' ' && *p != '\t')
5775 if (opts->debug_output)
5776 output_line_command (pfile, 0, same_file);
5777 cpp_define (pfile, q);
5778 while (*p == ' ' || *p == '\t')
5780 } else if (p[0] == '-' && p[1] == 'A') {
5781 /* Handle -A options (assertions). */
5790 past_name = assertion;
5791 /* Locate end of name. */
5792 while (*past_name && *past_name != ' '
5793 && *past_name != '\t' && *past_name != '(')
5795 /* Locate `(' at start of value. */
5797 while (*value && (*value == ' ' || *value == '\t'))
5799 if (*value++ != '(')
5801 while (*value && (*value == ' ' || *value == '\t'))
5804 /* Locate end of value. */
5805 while (*past_value && *past_value != ' '
5806 && *past_value != '\t' && *past_value != ')')
5808 termination = past_value;
5809 while (*termination && (*termination == ' ' || *termination == '\t'))
5811 if (*termination++ != ')')
5813 if (*termination && *termination != ' ' && *termination != '\t')
5815 /* Temporarily null-terminate the value. */
5816 save_char = *termination;
5817 *termination = '\0';
5818 /* Install the assertion. */
5819 make_assertion (pfile, "-A", assertion);
5820 *termination = (char) save_char;
5822 while (*p == ' ' || *p == '\t')
5830 /* Now handle the command line options. */
5832 /* Do -U's, -D's and -A's in the order they were seen. */
5833 /* First reverse the list. */
5834 opts->pending = nreverse_pending (opts->pending);
5836 for (pend = opts->pending; pend; pend = pend->next)
5838 if (pend->cmd != NULL && pend->cmd[0] == '-')
5840 switch (pend->cmd[1])
5843 if (opts->debug_output)
5844 output_line_command (pfile, 0, same_file);
5845 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5848 if (opts->debug_output)
5849 output_line_command (pfile, 0, same_file);
5850 cpp_define (pfile, pend->arg);
5853 make_assertion (pfile, "-A", pend->arg);
5859 opts->done_initializing = 1;
5861 { /* read the appropriate environment variable and if it exists
5862 replace include_defaults with the listed path. */
5864 switch ((opts->objc << 1) + opts->cplusplus)
5867 epath = getenv ("C_INCLUDE_PATH");
5870 epath = getenv ("CPLUS_INCLUDE_PATH");
5873 epath = getenv ("OBJC_INCLUDE_PATH");
5876 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5879 /* If the environment var for this language is set,
5880 add to the default list of include directories. */
5882 char *nstore = (char *) alloca (strlen (epath) + 2);
5884 char *startp, *endp;
5886 for (num_dirs = 1, startp = epath; *startp; startp++)
5887 if (*startp == PATH_SEPARATOR)
5890 = (struct default_include *) xmalloc ((num_dirs
5891 * sizeof (struct default_include))
5892 + sizeof (include_defaults_array));
5893 startp = endp = epath;
5896 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5897 if ((*endp == PATH_SEPARATOR)
5899 strncpy (nstore, startp, endp-startp);
5901 strcpy (nstore, ".");
5903 nstore[endp-startp] = '\0';
5905 include_defaults[num_dirs].fname = savestring (nstore);
5906 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5907 include_defaults[num_dirs].cxx_aware = 1;
5911 endp = startp = endp + 1;
5915 /* Put the usual defaults back in at the end. */
5916 bcopy ((char *) include_defaults_array,
5917 (char *) &include_defaults[num_dirs],
5918 sizeof (include_defaults_array));
5922 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5923 opts->first_system_include = opts->before_system;
5925 /* Unless -fnostdinc,
5926 tack on the standard include file dirs to the specified list */
5927 if (!opts->no_standard_includes) {
5928 struct default_include *p = include_defaults;
5929 char *specd_prefix = opts->include_prefix;
5930 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5931 int default_len = 0;
5932 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5933 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5934 default_len = strlen (default_prefix) - 7;
5935 default_prefix[default_len] = 0;
5937 /* Search "translated" versions of GNU directories.
5938 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5939 if (specd_prefix != 0 && default_len != 0)
5940 for (p = include_defaults; p->fname; p++) {
5941 /* Some standard dirs are only for C++. */
5943 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5944 /* Does this dir start with the prefix? */
5945 if (!strncmp (p->fname, default_prefix, default_len)) {
5946 /* Yes; change prefix and add to search list. */
5947 struct file_name_list *new
5948 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5949 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5950 char *str = (char *) xmalloc (this_len + 1);
5951 strcpy (str, specd_prefix);
5952 strcat (str, p->fname + default_len);
5954 new->control_macro = 0;
5955 new->c_system_include_path = !p->cxx_aware;
5956 new->got_name_map = 0;
5957 append_include_chain (pfile, new, new);
5958 if (opts->first_system_include == 0)
5959 opts->first_system_include = new;
5963 /* Search ordinary names for GNU include directories. */
5964 for (p = include_defaults; p->fname; p++) {
5965 /* Some standard dirs are only for C++. */
5967 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5968 struct file_name_list *new
5969 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5970 new->control_macro = 0;
5971 new->c_system_include_path = !p->cxx_aware;
5972 new->fname = p->fname;
5973 new->got_name_map = 0;
5974 append_include_chain (pfile, new, new);
5975 if (opts->first_system_include == 0)
5976 opts->first_system_include = new;
5981 /* Tack the after_include chain at the end of the include chain. */
5982 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5983 if (opts->first_system_include == 0)
5984 opts->first_system_include = opts->after_include;
5986 /* With -v, print the list of dirs to search. */
5987 if (opts->verbose) {
5988 struct file_name_list *p;
5989 fprintf (stderr, "#include \"...\" search starts here:\n");
5990 for (p = opts->include; p; p = p->next) {
5991 if (p == opts->first_bracket_include)
5992 fprintf (stderr, "#include <...> search starts here:\n");
5993 fprintf (stderr, " %s\n", p->fname);
5995 fprintf (stderr, "End of search list.\n");
5998 /* Scan the -imacros files before the main input.
5999 Much like #including them, but with no_output set
6000 so that only their macro definitions matter. */
6002 opts->no_output++; pfile->no_record_file++;
6003 for (pend = opts->pending; pend; pend = pend->next)
6005 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6007 int fd = open (pend->arg, O_RDONLY, 0666);
6010 cpp_perror_with_name (pfile, pend->arg);
6011 return FATAL_EXIT_CODE;
6013 cpp_push_buffer (pfile, NULL, 0);
6014 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6015 cpp_scan_buffer (pfile);
6018 opts->no_output--; pfile->no_record_file--;
6020 /* Copy the entire contents of the main input file into
6021 the stacked input buffer previously allocated for it. */
6022 if (fname == NULL || *fname == 0) {
6025 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6026 cpp_pfatal_with_name (pfile, fname);
6028 /* -MG doesn't select the form of output and must be specified with one of
6029 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6030 inhibit compilation. */
6031 if (opts->print_deps_missing_files
6032 && (opts->print_deps == 0 || !opts->no_output))
6033 fatal (pfile, "-MG must be specified with one of -M or -MM");
6035 /* Either of two environment variables can specify output of deps.
6036 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6037 where OUTPUT_FILE is the file to write deps info to
6038 and DEPS_TARGET is the target to mention in the deps. */
6040 if (opts->print_deps == 0
6041 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6042 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6043 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6049 spec = getenv ("SUNPRO_DEPENDENCIES");
6050 opts->print_deps = 2;
6053 opts->print_deps = 1;
6056 /* Find the space before the DEPS_TARGET, if there is one. */
6057 /* This should use index. (mrs) */
6058 while (*s != 0 && *s != ' ') s++;
6061 opts->deps_target = s + 1;
6062 output_file = (char *) xmalloc (s - spec + 1);
6063 bcopy (spec, output_file, s - spec);
6064 output_file[s - spec] = 0;
6068 opts->deps_target = 0;
6072 opts->deps_file = output_file;
6073 opts->print_deps_append = 1;
6076 /* For -M, print the expected object file name
6077 as the target of this Make-rule. */
6078 if (opts->print_deps)
6080 pfile->deps_allocated_size = 200;
6081 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6082 pfile->deps_buffer[0] = 0;
6083 pfile->deps_size = 0;
6084 pfile->deps_column = 0;
6086 if (opts->deps_target)
6087 deps_output (pfile, opts->deps_target, ':');
6088 else if (*opts->in_fname == 0)
6089 deps_output (pfile, "-", ':');
6095 /* Discard all directory prefixes from filename. */
6096 if ((q = rindex (opts->in_fname, '/')) != NULL
6097 #ifdef DIR_SEPARATOR
6098 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6105 /* Copy remainder to mungable area. */
6106 p = (char *) alloca (strlen(q) + 8);
6109 /* Output P, but remove known suffixes. */
6113 && p[len - 2] == '.'
6114 && index("cCsSm", p[len - 1]))
6117 && p[len - 3] == '.'
6118 && p[len - 2] == 'c'
6119 && p[len - 1] == 'c')
6122 && p[len - 4] == '.'
6123 && p[len - 3] == 'c'
6124 && p[len - 2] == 'x'
6125 && p[len - 1] == 'x')
6128 && p[len - 4] == '.'
6129 && p[len - 3] == 'c'
6130 && p[len - 2] == 'p'
6131 && p[len - 1] == 'p')
6134 /* Supply our own suffix. */
6145 deps_output (pfile, p, ':');
6146 deps_output (pfile, opts->in_fname, ' ');
6151 /* Make sure data ends with a newline. And put a null after it. */
6153 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6154 /* Backslash-newline at end is not good enough. */
6155 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6156 fp->buf[fp->length++] = '\n';
6157 missing_newline = 1;
6159 fp->buf[fp->length] = '\0';
6161 /* Unless inhibited, convert trigraphs in the input. */
6167 /* Scan the -include files before the main input.
6168 We push these in reverse order, so that the first one is handled first. */
6170 pfile->no_record_file++;
6171 opts->pending = nreverse_pending (opts->pending);
6172 for (pend = opts->pending; pend; pend = pend->next)
6174 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6176 int fd = open (pend->arg, O_RDONLY, 0666);
6179 cpp_perror_with_name (pfile, pend->arg);
6180 return FATAL_EXIT_CODE;
6182 cpp_push_buffer (pfile, NULL, 0);
6183 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6186 pfile->no_record_file--;
6188 /* Free the pending list. */
6189 for (pend = opts->pending; pend; )
6191 struct cpp_pending *next = pend->next;
6195 opts->pending = NULL;
6198 /* Scan the input, processing macros and directives. */
6200 rescan (&outbuf, 0);
6202 if (missing_newline)
6205 if (CPP_PEDANTIC (pfile) && missing_newline)
6206 pedwarn ("file does not end in newline");
6209 if (finclude (pfile, f, fname, 0, NULL_PTR))
6210 output_line_command (pfile, 0, same_file);
6211 return SUCCESS_EXIT_CODE;
6218 bzero ((char *) pfile, sizeof (cpp_reader));
6219 pfile->get_token = cpp_get_token;
6221 pfile->token_buffer_size = 200;
6222 pfile->token_buffer = (U_CHAR*)xmalloc (pfile->token_buffer_size);
6223 CPP_SET_WRITTEN (pfile, 0);
6225 pfile->system_include_depth = 0;
6226 pfile->dont_repeat_files = 0;
6227 pfile->all_include_files = 0;
6228 pfile->max_include_len = 0;
6229 pfile->timebuf = NULL;
6230 pfile->only_seen_white = 1;
6231 pfile->buffer = CPP_NULL_BUFFER(pfile);
6234 static struct cpp_pending *
6236 struct cpp_pending *list)
6239 register struct cpp_pending *prev = 0, *next, *pend;
6240 for (pend = list; pend; pend = next)
6255 struct cpp_pending *pend
6256 = (struct cpp_pending*)xmalloc (sizeof (struct cpp_pending));
6259 pend->next = CPP_OPTIONS (pfile)->pending;
6260 CPP_OPTIONS (pfile)->pending = pend;
6263 /* Handle command-line options in (argc, argv).
6264 Can be called multiple times, to handle multiple sets of options.
6265 Returns if an unrecognized option is seen.
6266 Returns number of handled arguments. */
6269 cpp_handle_options (
6275 struct cpp_options *opts = CPP_OPTIONS (pfile);
6276 for (i = 0; i < argc; i++) {
6277 if (argv[i][0] != '-') {
6278 if (opts->out_fname != NULL)
6279 fatal ("Usage: %s [switches] input output", argv[0]);
6280 else if (opts->in_fname != NULL)
6281 opts->out_fname = argv[i];
6283 opts->in_fname = argv[i];
6285 switch (argv[i][1]) {
6288 if (!strcmp (argv[i], "-include")
6289 || !strcmp (argv[i], "-imacros")) {
6291 fatal ("Filename missing after `%s' option", argv[i]);
6293 push_pending (pfile, argv[i], argv[i+1]), i++;
6295 if (!strcmp (argv[i], "-iprefix")) {
6297 fatal ("Filename missing after `-iprefix' option");
6299 opts->include_prefix = argv[++i];
6301 if (!strcmp (argv[i], "-ifoutput")) {
6302 opts->output_conditionals = 1;
6304 if (!strcmp (argv[i], "-isystem")) {
6305 struct file_name_list *dirtmp;
6308 fatal ("Filename missing after `-isystem' option");
6310 dirtmp = (struct file_name_list *)
6311 xmalloc (sizeof (struct file_name_list));
6313 dirtmp->control_macro = 0;
6314 dirtmp->c_system_include_path = 1;
6315 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6316 strcpy (dirtmp->fname, argv[++i]);
6317 dirtmp->got_name_map = 0;
6319 if (opts->before_system == 0)
6320 opts->before_system = dirtmp;
6322 opts->last_before_system->next = dirtmp;
6323 opts->last_before_system = dirtmp; /* Tail follows the last one */
6325 /* Add directory to end of path for includes,
6326 with the default prefix at the front of its name. */
6327 if (!strcmp (argv[i], "-iwithprefix")) {
6328 struct file_name_list *dirtmp;
6331 if (opts->include_prefix != 0)
6332 prefix = opts->include_prefix;
6334 prefix = savestring (GCC_INCLUDE_DIR);
6335 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6336 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6337 prefix[strlen (prefix) - 7] = 0;
6340 dirtmp = (struct file_name_list *)
6341 xmalloc (sizeof (struct file_name_list));
6342 dirtmp->next = 0; /* New one goes on the end */
6343 dirtmp->control_macro = 0;
6344 dirtmp->c_system_include_path = 0;
6346 fatal ("Directory name missing after `-iwithprefix' option");
6348 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6349 + strlen (prefix) + 1);
6350 strcpy (dirtmp->fname, prefix);
6351 strcat (dirtmp->fname, argv[++i]);
6352 dirtmp->got_name_map = 0;
6354 if (opts->after_include == 0)
6355 opts->after_include = dirtmp;
6357 opts->last_after_include->next = dirtmp;
6358 opts->last_after_include = dirtmp; /* Tail follows the last one */
6360 /* Add directory to main path for includes,
6361 with the default prefix at the front of its name. */
6362 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6363 struct file_name_list *dirtmp;
6366 if (opts->include_prefix != 0)
6367 prefix = opts->include_prefix;
6369 prefix = savestring (GCC_INCLUDE_DIR);
6370 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6371 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6372 prefix[strlen (prefix) - 7] = 0;
6375 dirtmp = (struct file_name_list *)
6376 xmalloc (sizeof (struct file_name_list));
6377 dirtmp->next = 0; /* New one goes on the end */
6378 dirtmp->control_macro = 0;
6379 dirtmp->c_system_include_path = 0;
6381 fatal ("Directory name missing after `-iwithprefixbefore' option");
6383 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6384 + strlen (prefix) + 1);
6385 strcpy (dirtmp->fname, prefix);
6386 strcat (dirtmp->fname, argv[++i]);
6387 dirtmp->got_name_map = 0;
6389 append_include_chain (pfile, dirtmp, dirtmp);
6391 /* Add directory to end of path for includes. */
6392 if (!strcmp (argv[i], "-idirafter")) {
6393 struct file_name_list *dirtmp;
6395 dirtmp = (struct file_name_list *)
6396 xmalloc (sizeof (struct file_name_list));
6397 dirtmp->next = 0; /* New one goes on the end */
6398 dirtmp->control_macro = 0;
6399 dirtmp->c_system_include_path = 0;
6401 fatal ("Directory name missing after `-idirafter' option");
6403 dirtmp->fname = argv[++i];
6404 dirtmp->got_name_map = 0;
6406 if (opts->after_include == 0)
6407 opts->after_include = dirtmp;
6409 opts->last_after_include->next = dirtmp;
6410 opts->last_after_include = dirtmp; /* Tail follows the last one */
6415 if (opts->out_fname != NULL)
6416 fatal ("Output filename specified twice");
6418 fatal ("Filename missing after -o option");
6419 opts->out_fname = argv[++i];
6420 if (!strcmp (opts->out_fname, "-"))
6421 opts->out_fname = "";
6425 if (!strcmp (argv[i], "-pedantic"))
6426 CPP_PEDANTIC (pfile) = 1;
6427 else if (!strcmp (argv[i], "-pedantic-errors")) {
6428 CPP_PEDANTIC (pfile) = 1;
6429 opts->pedantic_errors = 1;
6432 else if (!strcmp (argv[i], "-pcp")) {
6433 char *pcp_fname = argv[++i];
6435 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6436 ? fopen (pcp_fname, "w")
6437 : fdopen (dup (fileno (stdout)), "w"));
6438 if (pcp_outfile == 0)
6439 cpp_pfatal_with_name (pfile, pcp_fname);
6446 if (!strcmp (argv[i], "-traditional")) {
6447 opts->traditional = 1;
6448 if (opts->dollars_in_ident > 0)
6449 opts->dollars_in_ident = 1;
6450 } else if (!strcmp (argv[i], "-trigraphs")) {
6452 opts->no_trigraphs = 0;
6457 if (! strcmp (argv[i], "-lang-c"))
6458 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6459 if (! strcmp (argv[i], "-lang-c++"))
6460 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6461 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6462 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6463 if (! strcmp (argv[i], "-lang-objc"))
6464 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6465 if (! strcmp (argv[i], "-lang-objc++"))
6466 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6467 if (! strcmp (argv[i], "-lang-asm"))
6469 if (! strcmp (argv[i], "-lint"))
6471 if (! strcmp (argv[i], "-lang-chill"))
6472 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6473 opts->traditional = 1, opts->no_trigraphs = 1;
6477 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6481 opts->inhibit_warnings = 1;
6485 if (!strcmp (argv[i], "-Wtrigraphs"))
6486 opts->warn_trigraphs = 1;
6487 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6488 opts->warn_trigraphs = 0;
6489 else if (!strcmp (argv[i], "-Wcomment"))
6490 opts->warn_comments = 1;
6491 else if (!strcmp (argv[i], "-Wno-comment"))
6492 opts->warn_comments = 0;
6493 else if (!strcmp (argv[i], "-Wcomments"))
6494 opts->warn_comments = 1;
6495 else if (!strcmp (argv[i], "-Wno-comments"))
6496 opts->warn_comments = 0;
6497 else if (!strcmp (argv[i], "-Wtraditional"))
6498 opts->warn_stringify = 1;
6499 else if (!strcmp (argv[i], "-Wno-traditional"))
6500 opts->warn_stringify = 0;
6501 else if (!strcmp (argv[i], "-Wimport"))
6502 opts->warn_import = 1;
6503 else if (!strcmp (argv[i], "-Wno-import"))
6504 opts->warn_import = 0;
6505 else if (!strcmp (argv[i], "-Werror"))
6506 opts->warnings_are_errors = 1;
6507 else if (!strcmp (argv[i], "-Wno-error"))
6508 opts->warnings_are_errors = 0;
6509 else if (!strcmp (argv[i], "-Wall"))
6511 opts->warn_trigraphs = 1;
6512 opts->warn_comments = 1;
6517 /* The style of the choices here is a bit mixed.
6518 The chosen scheme is a hybrid of keeping all options in one string
6519 and specifying each option in a separate argument:
6520 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6521 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6522 -M[M][G][D file]. This is awkward to handle in specs, and is not
6524 /* ??? -MG must be specified in addition to one of -M or -MM.
6525 This can be relaxed in the future without breaking anything.
6526 The converse isn't true. */
6528 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6529 if (!strcmp (argv[i], "-MG"))
6531 opts->print_deps_missing_files = 1;
6534 if (!strcmp (argv[i], "-M"))
6535 opts->print_deps = 2;
6536 else if (!strcmp (argv[i], "-MM"))
6537 opts->print_deps = 1;
6538 else if (!strcmp (argv[i], "-MD"))
6539 opts->print_deps = 2;
6540 else if (!strcmp (argv[i], "-MMD"))
6541 opts->print_deps = 1;
6542 /* For -MD and -MMD options, write deps on file named by next arg. */
6543 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6546 fatal ("Filename missing after %s option", argv[i]);
6547 opts->deps_file = argv[++i];
6551 /* For -M and -MM, write deps on standard output
6552 and suppress the usual output. */
6553 opts->no_output = 1;
6559 char *p = argv[i] + 2;
6561 while ((c = *p++) != 0) {
6562 /* Arg to -d specifies what parts of macros to dump */
6565 opts->dump_macros = dump_only;
6566 opts->no_output = 1;
6569 opts->dump_macros = dump_names;
6572 opts->dump_macros = dump_definitions;
6580 if (argv[i][2] == '3')
6581 opts->debug_output = 1;
6585 fprintf (stderr, "GNU CPP version %s", version_string);
6586 #ifdef TARGET_VERSION
6589 fprintf (stderr, "\n");
6590 // opts->verbose = 1;
6594 opts->print_include_names = 1;
6598 if (argv[i][2] != 0)
6599 push_pending (pfile, "-D", argv[i] + 2);
6600 else if (i + 1 == argc)
6601 fatal ("Macro name missing after -D option");
6603 i++, push_pending (pfile, "-D", argv[i]);
6610 if (argv[i][2] != 0)
6612 else if (i + 1 == argc)
6613 fatal ("Assertion missing after -A option");
6617 if (!strcmp (p, "-")) {
6618 struct cpp_pending **ptr;
6619 /* -A- eliminates all predefined macros and assertions.
6620 Let's include also any that were specified earlier
6621 on the command line. That way we can get rid of any
6622 that were passed automatically in from GCC. */
6624 opts->inhibit_predefs = 1;
6625 for (ptr = &opts->pending; *ptr != NULL; )
6627 struct cpp_pending *pend = *ptr;
6628 if (pend->cmd && pend->cmd[0] == '-'
6629 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6638 push_pending (pfile, "-A", p);
6643 case 'U': /* JF #undef something */
6644 if (argv[i][2] != 0)
6645 push_pending (pfile, "-U", argv[i] + 2);
6646 else if (i + 1 == argc)
6647 fatal ("Macro name missing after -U option");
6649 push_pending (pfile, "-U", argv[i+1]), i++;
6653 opts->put_out_comments = 1;
6656 case 'E': /* -E comes from cc -E; ignore it. */
6660 opts->no_line_commands = 1;
6663 case '$': /* Don't include $ in identifiers. */
6664 opts->dollars_in_ident = 0;
6667 case 'I': /* Add directory to path for includes. */
6669 struct file_name_list *dirtmp;
6671 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6672 && !strcmp (argv[i] + 2, "-")) {
6673 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6674 /* Don't use any preceding -I directories for #include <...>. */
6675 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6678 dirtmp = (struct file_name_list *)
6679 xmalloc (sizeof (struct file_name_list));
6680 dirtmp->next = 0; /* New one goes on the end */
6681 dirtmp->control_macro = 0;
6682 dirtmp->c_system_include_path = 0;
6683 if (argv[i][2] != 0)
6684 dirtmp->fname = argv[i] + 2;
6685 else if (i + 1 == argc)
6686 fatal ("Directory name missing after -I option");
6688 dirtmp->fname = argv[++i];
6689 dirtmp->got_name_map = 0;
6690 append_include_chain (pfile, dirtmp, dirtmp);
6696 if (!strcmp (argv[i], "-nostdinc"))
6697 /* -nostdinc causes no default include directories.
6698 You must specify all include-file directories with -I. */
6699 opts->no_standard_includes = 1;
6700 else if (!strcmp (argv[i], "-nostdinc++"))
6701 /* -nostdinc++ causes no default C++-specific include directories. */
6702 opts->no_standard_cplusplus_includes = 1;
6704 else if (!strcmp (argv[i], "-noprecomp"))
6710 /* Sun compiler passes undocumented switch "-undef".
6711 Let's assume it means to inhibit the predefined symbols. */
6712 opts->inhibit_predefs = 1;
6715 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6716 if (opts->in_fname == NULL) {
6717 opts->in_fname = "";
6719 } else if (opts->out_fname == NULL) {
6720 opts->out_fname = "";
6722 } /* else fall through into error */
6736 struct cpp_options *opts = CPP_OPTIONS (pfile);
6738 if (opts->print_deps)
6740 /* Stream on which to print the dependency information. */
6743 /* Don't actually write the deps file if compilation has failed. */
6744 if (pfile->errors == 0)
6746 char *deps_mode = opts->print_deps_append ? "a" : "w";
6747 if (opts->deps_file == 0)
6748 deps_stream = stdout;
6749 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6750 cpp_pfatal_with_name (pfile, opts->deps_file);
6751 fputs (pfile->deps_buffer, deps_stream);
6752 putc ('\n', deps_stream);
6753 if (opts->deps_file)
6755 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6756 fatal ("I/O error on output");
6762 /* Free resources used by PFILE. */
6769 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6770 cpp_pop_buffer (pfile);
6772 if (pfile->token_buffer)
6774 free (pfile->token_buffer);
6775 pfile->token_buffer = NULL;
6778 if (pfile->deps_buffer)
6780 free (pfile->deps_buffer);
6781 pfile->deps_buffer = NULL;
6782 pfile->deps_allocated_size = 0;
6785 while (pfile->if_stack)
6787 IF_STACK_FRAME *temp = pfile->if_stack;
6788 pfile->if_stack = temp->next;
6792 while (pfile->dont_repeat_files)
6794 struct file_name_list *temp = pfile->dont_repeat_files;
6795 pfile->dont_repeat_files = temp->next;
6800 while (pfile->all_include_files)
6802 struct file_name_list *temp = pfile->all_include_files;
6803 pfile->all_include_files = temp->next;
6808 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6810 register struct import_file *imp = pfile->import_hash_table[i];
6813 struct import_file *next = imp->next;
6818 pfile->import_hash_table[i] = 0;
6821 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6823 while (pfile->assertion_hashtab[i])
6824 delete_assertion (pfile->assertion_hashtab[i]);
6827 cpp_hash_cleanup (pfile);
6833 struct directive *keyword,
6834 U_CHAR *buf, U_CHAR *limit)
6836 long symstart; /* remember where symbol name starts */
6838 int sym_length; /* and how long it is */
6839 struct arglist *tokens = NULL;
6841 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6842 && !CPP_BUFFER (pfile)->system_header_p)
6843 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6845 cpp_skip_hspace (pfile);
6846 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6847 parse_name (pfile, GETC());
6848 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6851 cpp_skip_hspace (pfile);
6852 if (PEEKC() != '(') {
6853 cpp_error (pfile, "missing token-sequence in `#assert'");
6859 tokens = read_token_list (pfile, &error_flag);
6863 cpp_error (pfile, "empty token-sequence in `#assert'");
6866 cpp_skip_hspace (pfile);
6868 if (c != EOF && c != '\n')
6869 cpp_pedwarn (pfile, "junk at end of `#assert'");
6870 skip_rest_of_line (pfile);
6873 /* If this name isn't already an assertion name, make it one.
6874 Error if it was already in use in some other way. */
6877 ASSERTION_HASHNODE *hp;
6878 U_CHAR *symname = pfile->token_buffer + symstart;
6879 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6880 struct tokenlist_list *value
6881 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6883 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6885 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6886 cpp_error (pfile, "`defined' redefined as assertion");
6887 hp = assertion_install (pfile, symname, sym_length, hashcode);
6890 /* Add the spec'd token-sequence to the list of such. */
6891 value->tokens = tokens;
6892 value->next = hp->value;
6895 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6898 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6899 skip_rest_of_line (pfile);
6906 struct directive *keyword,
6907 U_CHAR *buf, U_CHAR *limit)
6909 long symstart; /* remember where symbol name starts */
6910 int sym_length; /* and how long it is */
6913 struct arglist *tokens = NULL;
6914 int tokens_specified = 0;
6916 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6917 && !CPP_BUFFER (pfile)->system_header_p)
6918 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6920 cpp_skip_hspace (pfile);
6922 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6923 parse_name (pfile, GETC());
6924 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6927 cpp_skip_hspace (pfile);
6928 if (PEEKC() == '(') {
6931 tokens = read_token_list (pfile, &error_flag);
6935 cpp_error (pfile, "empty token list in `#unassert'");
6939 tokens_specified = 1;
6942 cpp_skip_hspace (pfile);
6944 if (c != EOF && c != '\n')
6945 cpp_error (pfile, "junk at end of `#unassert'");
6946 skip_rest_of_line (pfile);
6949 ASSERTION_HASHNODE *hp;
6950 U_CHAR *symname = pfile->token_buffer + symstart;
6951 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6952 struct tokenlist_list *tail, *prev;
6954 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6958 /* If no token list was specified, then eliminate this assertion
6960 if (! tokens_specified)
6961 delete_assertion (hp);
6963 /* If a list of tokens was given, then delete any matching list. */
6968 struct tokenlist_list *next = tail->next;
6969 if (compare_token_lists (tail->tokens, tokens)) {
6973 hp->value = tail->next;
6974 free_token_list (tail->tokens);
6984 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6987 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6988 skip_rest_of_line (pfile);
6992 /* Test whether there is an assertion named NAME
6993 and optionally whether it has an asserted token list TOKENS.
6994 NAME is not null terminated; its length is SYM_LENGTH.
6995 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7002 int tokens_specified,
7003 struct arglist *tokens)
7005 ASSERTION_HASHNODE *hp;
7006 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7008 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7009 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7011 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7013 /* It is not an assertion; just return false. */
7016 /* If no token list was specified, then value is 1. */
7017 if (! tokens_specified)
7021 struct tokenlist_list *tail;
7025 /* If a list of tokens was given,
7026 then succeed if the assertion records a matching list. */
7029 if (compare_token_lists (tail->tokens, tokens))
7034 /* Fail if the assertion has no matching list. */
7039 /* Compare two lists of tokens for equality including order of tokens. */
7042 compare_token_lists (
7043 struct arglist *l1, struct arglist *l2 )
7046 if (l1->length != l2->length)
7048 if (strncmp (l1->name, l2->name, l1->length))
7054 /* Succeed if both lists end at the same time. */
7059 reverse_token_list (
7060 struct arglist *tokens)
7062 register struct arglist *prev = 0, *this, *next;
7063 for (this = tokens; this; this = next)
7072 /* Read a space-separated list of tokens ending in a close parenthesis.
7073 Return a list of strings, in the order they were written.
7074 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7076 static struct arglist *
7081 struct arglist *token_ptrs = 0;
7086 FORWARD (1); /* Skip '(' */
7088 /* Loop over the assertion value tokens. */
7091 struct arglist *temp;
7092 long name_written = CPP_WRITTEN (pfile);
7093 int eofp = 0; int c;
7095 cpp_skip_hspace (pfile);
7099 /* Find the end of the token. */
7102 CPP_PUTC (pfile, c);
7110 CPP_PUTC (pfile, c);
7112 else if (c == '"' || c == '\'')
7115 cpp_get_token (pfile);
7121 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7122 && c != '"' && c != '\'')
7124 CPP_PUTC (pfile, c);
7127 if (c != EOF) FORWARD(-1);
7130 length = CPP_WRITTEN (pfile) - name_written;
7131 temp = (struct arglist *)
7132 xmalloc (sizeof (struct arglist) + length + 1);
7133 temp->name = (U_CHAR *) (temp + 1);
7134 bcopy ((char *) (pfile->token_buffer + name_written),
7135 (char *) temp->name, length);
7136 temp->name[length] = 0;
7137 temp->next = token_ptrs;
7139 temp->length = length;
7141 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7143 if (c == EOF || c == '\n')
7146 "unterminated token sequence following `#' operator");
7151 /* We accumulated the names in reverse order.
7152 Now reverse them to get the proper order. */
7153 return reverse_token_list (token_ptrs);
7158 struct arglist *tokens)
7161 struct arglist *next = tokens->next;
7162 free (tokens->name);
7168 /* Get the file-mode and data size of the file open on FD
7169 and store them in *MODE_POINTER and *SIZE_POINTER. */
7172 file_size_and_mode (
7175 long int *size_pointer)
7179 if (fstat (fd, &sbuf) < 0) return (-1);
7180 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7181 if (size_pointer) *size_pointer = sbuf.st_size;
7185 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7186 retrying if necessary. Return a negative value if an error occurs,
7187 otherwise return the actual number of bytes read,
7188 which must be LEN unless end-of-file was reached. */
7198 int nchars = read (desc, ptr, left);
7219 unsigned size = strlen (input);
7220 char *output = xmalloc (size + 1);
7221 strcpy (output, input);
7225 /* Initialize PMARK to remember the current position of PFILE. */
7228 struct parse_marker *pmark,
7231 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7232 pmark->next = pbuf->marks;
7233 pbuf->marks = pmark;
7235 pmark->position = pbuf->cur - pbuf->buf;
7238 /* Cleanup PMARK - we no longer need it. */
7241 struct parse_marker *pmark)
7243 struct parse_marker **pp = &pmark->buf->marks;
7244 for (; ; pp = &(*pp)->next) {
7245 if (*pp == NULL) fatal ("internal error", "in parse_set_mark");
7246 if (*pp == pmark) break;
7251 /* Backup the current position of PFILE to that saved in PMARK. */
7255 struct parse_marker *pmark,
7258 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7259 if (pbuf != pmark->buf)
7260 fatal ("internal error %s", "parse_goto_mark");
7261 pbuf->cur = pbuf->buf + pmark->position;
7264 /* Reset PMARK to point to the current position of PFILE. (Same
7265 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7269 struct parse_marker *pmark,
7272 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7273 if (pbuf != pmark->buf)
7274 fatal ("internal error %s", "parse_move_mark");
7275 pmark->position = pbuf->cur - pbuf->buf;
7279 cpp_read_check_assertion (
7282 int name_start = CPP_WRITTEN (pfile);
7283 int name_length, name_written;
7285 FORWARD (1); /* Skip '#' */
7286 cpp_skip_hspace (pfile);
7287 parse_name (pfile, GETC ());
7288 name_written = CPP_WRITTEN (pfile);
7289 name_length = name_written - name_start;
7290 cpp_skip_hspace (pfile);
7291 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7294 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7295 result = check_assertion (pfile,
7296 pfile->token_buffer + name_start, name_length,
7300 result = check_assertion (pfile,
7301 pfile->token_buffer + name_start, name_length,
7303 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7308 cpp_print_file_and_line (pfile)
7311 cpp_buffer *ip = cpp_file_buffer (pfile);
7316 cpp_buf_line_and_col (ip, &line, &col);
7317 cpp_file_line_for_message (pfile, ip->nominal_fname,
7318 line, pfile->show_column ? col : -1);
7323 cpp_error (pfile, msg, arg1, arg2, arg3)
7326 char *arg1, *arg2, *arg3;
7328 cpp_print_containing_files (pfile);
7329 cpp_print_file_and_line (pfile);
7330 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7333 /* Print error message but don't count it. */
7336 cpp_warning (pfile, msg, arg1, arg2, arg3)
7339 char *arg1, *arg2, *arg3;
7341 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7344 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7347 cpp_print_containing_files (pfile);
7348 cpp_print_file_and_line (pfile);
7349 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7352 /* Print an error message and maybe count it. */
7355 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7358 char *arg1, *arg2, *arg3;
7360 if (CPP_OPTIONS (pfile)->pedantic_errors)
7361 cpp_error (pfile, msg, arg1, arg2, arg3);
7363 cpp_warning (pfile, msg, arg1, arg2, arg3);
7367 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7371 char *arg1, *arg2, *arg3;
7374 cpp_buffer *ip = cpp_file_buffer (pfile);
7376 cpp_print_containing_files (pfile);
7379 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7381 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7385 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7389 char *arg1, *arg2, *arg3;
7394 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7397 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7400 cpp_print_containing_files (pfile);
7402 ip = cpp_file_buffer (pfile);
7405 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7407 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7411 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7415 char *arg1, *arg2, *arg3;
7417 if (CPP_OPTIONS (pfile)->pedantic_errors)
7418 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7420 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7423 /* Report a warning (or an error if pedantic_errors)
7424 giving specified file name and line number, not current. */
7427 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7432 char *arg1, *arg2, *arg3;
7434 if (!CPP_OPTIONS (pfile)->pedantic_errors
7435 && CPP_OPTIONS (pfile)->inhibit_warnings)
7438 cpp_file_line_for_message (pfile, file, line, -1);
7439 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7440 msg, arg1, arg2, arg3);
7443 /* This defines "errno" properly for VMS, and gives us EACCES. */
7450 #ifndef HAVE_STRERROR
7451 extern int sys_nerr;
7453 extern const char *const sys_errlist[];
7456 extern char *sys_errlist[];
7459 #else /* HAVE_STRERROR */
7463 char *strerror (int,...);
7467 * my_strerror - return the descriptive text associated with an `errno' code.
7471 my_strerror (errnum)
7477 #ifndef HAVE_STRERROR
7478 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7480 result = strerror (errnum);
7483 /* VAXCRTL's strerror() takes an optional second argument, which only
7484 matters when the first argument is EVMSERR. However, it's simplest
7485 just to pass it unconditionally. `vaxc$errno' is declared in
7486 <errno.h>, and maintained by the library in parallel with `errno'.
7487 We assume that caller's `errnum' either matches the last setting of
7488 `errno' by the library or else does not have the value `EVMSERR'. */
7490 result = strerror (errnum, vaxc$errno);
7494 result = "undocumented I/O error";
7499 /* Error including a message from `errno'. */
7502 cpp_error_from_errno (
7509 cpp_buffer *ip = cpp_file_buffer (pfile);
7511 cpp_print_containing_files (pfile);
7514 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7516 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7520 cpp_perror_with_name (
7524 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7528 * No pre-compiled header file support.
7530 * Possibly different enum token codes for each C/C++ token.
7532 * Should clean up remaining directives to that do_XXX functions
7533 * only take two arguments and all have command_reads_line.
7535 * Find and cleanup remaining uses of static variables,
7537 * Support for trigraphs.
7539 * Support -dM flag (dump_all_macros).
7541 * Support for_lint flag.