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
6 * Copyright (C) 2003-2011 Kim Woelders
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 * In other words, you are welcome to use, share and improve this program.
23 * You are forbidden to forbid anyone else to use, share and improve
24 * what you give them. Help stamp out software-hoarding! */
32 #elif defined __GNUC__
33 # define alloca __builtin_alloca
35 # define alloca __alloca
36 #elif defined _MSC_VER
38 # define alloca _alloca
41 void *alloca (size_t);
48 #ifndef STANDARD_INCLUDE_DIR
49 #define STANDARD_INCLUDE_DIR "/usr/include"
52 #ifndef LOCAL_INCLUDE_DIR
53 #define LOCAL_INCLUDE_DIR "/usr/local/include"
60 * On Windows, if the file is not opened in binary mode,
61 * read does not return the correct size, because of
62 * CR / LF translation.
68 const char *version_string = "0.0.0";
74 /* By default, colon separates directories in a path. */
75 #ifndef PATH_SEPARATOR
76 #define PATH_SEPARATOR ':'
85 #include <sys/types.h>
93 #include <sys/time.h> /* for __DATE__ and __TIME__ */
94 #ifdef HAVE_SYS_RESOURCE_H
95 # include <sys/resource.h>
98 #include <sys/param.h> /* CYGNUS LOCAL: shebs -noquiet */
99 #include <sys/times.h>
105 /* This defines "errno" properly for VMS, and gives us EACCES. */
114 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
115 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
118 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
122 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
125 /* Define a generic NULL if one hasn't already been defined. */
128 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
129 #define GENERIC_PTR void *
131 #define GENERIC_PTR char *
135 #ifndef INCLUDE_LEN_FUDGE
136 #define INCLUDE_LEN_FUDGE 0
139 #define USE_FILE_NAME_MAPS 0
141 /* Symbols to predefine. */
143 #ifdef CPP_PREDEFINES
144 static const char *predefs = CPP_PREDEFINES;
147 static const char *predefs = "";
151 /* We let tm.h override the types used here, to handle trivial differences
152 * such as the choice of unsigned int or long unsigned int for size_t.
153 * When machines start needing nontrivial differences in the size type,
154 * it would be best to do something here to figure out automatically
155 * from other information what type to use. */
157 /* The string value for __SIZE_TYPE__. */
160 #define SIZE_TYPE "long unsigned int"
163 /* The string value for __PTRDIFF_TYPE__. */
166 #define PTRDIFF_TYPE "long int"
169 /* The string value for __WCHAR_TYPE__. */
172 #define WCHAR_TYPE "int"
174 #define CPP_WCHAR_TYPE(PFILE) \
175 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
177 /* The string value for __USER_LABEL_PREFIX__ */
179 #ifndef USER_LABEL_PREFIX
180 #define USER_LABEL_PREFIX ""
183 /* The string value for __REGISTER_PREFIX__ */
185 #ifndef REGISTER_PREFIX
186 #define REGISTER_PREFIX ""
191 int (*func) (cpp_reader * pfile, struct directive * keyword,
192 unsigned char *buf, unsigned char *limit);
195 char command_reads_line;
196 char traditional_comments;
200 /* In the definition of a #assert name, this structure forms
201 * a list of the individual values asserted.
202 * Each value is itself a list of "tokens".
203 * These are strings that are compared by name. */
205 struct tokenlist_list {
206 struct tokenlist_list *next;
207 struct arglist *tokens;
210 struct assertion_hashnode {
211 struct assertion_hashnode *next; /* double links for easy deletion */
212 struct assertion_hashnode *prev;
213 /* also, a back pointer to this node's hash
214 * chain is kept, in case the node is the head
215 * of the chain and gets deleted. */
216 struct assertion_hashnode **bucket_hdr;
217 int length; /* length of token, for quick comparison */
218 char *name; /* the actual name */
219 /* List of token-sequences. */
220 struct tokenlist_list *value;
223 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[(unsigned char)(*p)]) p++; } while (0)
224 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[(unsigned char)(*p)]) p++; } while (0)
226 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
227 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
228 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
229 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
230 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
231 * (Note that it is false while we're expanding marco *arguments*.) */
232 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
234 /* Move all backslash-newline pairs out of embarrassing places.
235 * Exchange all such pairs following BP
236 * with any potentially-embarrassing characters that follow them.
237 * Potentially-embarrassing characters are / and *
238 * (because a backslash-newline inside a comment delimiter
239 * would cause it not to be recognized). */
241 #define NEWLINE_FIX \
242 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
244 /* Same, but assume we've already read the potential '\\' into C. */
245 #define NEWLINE_FIX1(C) do { \
246 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
249 /* Name under which this program was invoked. */
254 struct cpp_pending *next;
259 /* Structure returned by create_definition */
261 struct definition *defn;
266 /* Forward declarations. */
267 typedef struct file_name_list file_name_list;
269 static void add_import(cpp_reader * pfile, int fd, char *fname);
270 static int finclude(cpp_reader * pfile, int f, const char *fname,
271 int system_header_p, file_name_list * dirptr);
272 static void validate_else(cpp_reader * pfile, const char *directive);
273 static int comp_def_part(int first, unsigned char *beg1, int len1,
274 unsigned char *beg2, int len2, int last);
275 static int lookup_import(cpp_reader * pfile, char *filename,
276 file_name_list * searchptr);
277 static int redundant_include_p(cpp_reader * pfile, char *name);
279 static int is_system_include(cpp_reader * pfile, char *filename);
281 static int open_include_file(cpp_reader * pfile, char *filename,
282 file_name_list * searchptr);
283 static int check_macro_name(cpp_reader * pfile, unsigned char *symname,
286 static int compare_token_lists(struct arglist *l1, struct arglist *l2);
287 static HOST_WIDE_INT eval_if_expression(cpp_reader * pfile, unsigned char *buf,
290 static int file_size_and_mode(int fd, int *mode_pointer,
291 long int *size_pointer);
292 static struct arglist *read_token_list(cpp_reader * pfile, int *error_flag);
293 static void free_token_list(struct arglist *tokens);
294 static int safe_read(int desc, char *ptr, int len);
295 static void push_macro_expansion(cpp_reader * pfile,
297 int xbuf_len, HASHNODE * hp);
299 static struct cpp_pending *nreverse_pending(struct cpp_pending *list);
300 static char *savestring(const char *input);
302 static void conditional_skip(cpp_reader * pfile, int skip,
304 unsigned char *control_macro);
305 static void skip_if_group(cpp_reader * pfile, int any);
307 static void cpp_error_with_line(cpp_reader * pfile, int line,
308 int column, const char *msg);
309 static void cpp_pedwarn_with_line(cpp_reader * pfile, int line,
310 int column, const char *msg);
311 static void cpp_pedwarn_with_file_and_line(cpp_reader * pfile,
312 const char *file, int line,
317 static void cpp_error_from_errno(cpp_reader * pfile, const char *name);
319 static cpp_buffer *cpp_push_buffer(cpp_reader * pfile, unsigned char *buffer,
321 static cpp_buffer *cpp_pop_buffer(cpp_reader * pfile);
323 /* Last arg to output_line_command. */
324 enum file_change_code {
325 same_file, enter_file, leave_file
328 /* These functions are declared to return int instead of void since they
329 * are going to be placed in a table and some old compilers have trouble with
330 * pointers to functions returning void. */
332 static int do_define(cpp_reader * pfile, struct directive *keyword,
333 unsigned char *buf, unsigned char *limit);
335 static int do_line(cpp_reader * pfile, struct directive *keyword,
336 unsigned char *unused1, unsigned char *unused2);
338 static int do_include(cpp_reader * pfile, struct directive *keyword,
339 unsigned char *unused1, unsigned char *unused2);
341 static int do_undef(cpp_reader * pfile, struct directive *keyword,
342 unsigned char *buf, unsigned char *limit);
344 static int do_error(cpp_reader * pfile, struct directive *keyword,
345 unsigned char *buf, unsigned char *limit);
347 static int do_pragma(cpp_reader * pfile, struct directive *keyword,
348 unsigned char *buf, unsigned char *limit);
350 static int do_ident(cpp_reader * pfile, struct directive *keyword,
351 unsigned char *buf, unsigned char *limit);
353 static int do_if(cpp_reader * pfile, struct directive *keyword,
354 unsigned char *buf, unsigned char *limit);
356 static int do_xifdef(cpp_reader * pfile, struct directive *keyword,
357 unsigned char *buf, unsigned char *limit);
359 static int do_else(cpp_reader * pfile, struct directive *keyword,
360 unsigned char *buf, unsigned char *limit);
362 static int do_elif(cpp_reader * pfile, struct directive *keyword,
363 unsigned char *buf, unsigned char *limit);
365 static int do_endif(cpp_reader * pfile, struct directive *keyword,
366 unsigned char *buf, unsigned char *limit);
368 static int do_assert(cpp_reader * pfile, struct directive *keyword,
369 unsigned char *buf, unsigned char *limit);
371 static int do_unassert(cpp_reader * pfile, struct directive *keyword,
372 unsigned char *buf, unsigned char *limit);
374 static int do_warning(cpp_reader * pfile, struct directive *keyword,
375 unsigned char *buf, unsigned char *limit);
377 struct arglist *reverse_token_list(struct arglist *tokens);
379 static int parse_name(cpp_reader * pfile, int c);
381 static void parse_set_mark(struct parse_marker *pmark,
383 static void parse_clear_mark(struct parse_marker *pmark);
384 static void parse_goto_mark(struct parse_marker *pmark,
386 static void parse_move_mark(struct parse_marker *pmark,
389 struct file_name_list {
390 file_name_list *next;
392 /* If the following is nonzero, it is a macro name.
393 * Don't include the file again if that macro is defined. */
394 unsigned char *control_macro;
395 /* If the following is nonzero, it is a C-language system include
397 int c_system_include_path;
398 /* Mapping of file names for this directory. */
399 struct file_name_map *name_map;
400 /* Non-zero if name_map is valid. */
404 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
405 * via the same directory as the file that #included it. */
406 #define SELF_DIR_DUMMY ((file_name_list*)(~0))
408 /* #include "file" looks in source file dir, then stack. */
409 /* #include <file> just looks in the stack. */
410 /* -I directories are added to the end, then the defaults are added. */
412 static struct default_include {
413 const char *fname; /* The name of the directory. */
414 int cplusplus; /* Only look here if we're compiling C++. */
415 int cxx_aware; /* Includes in this directory don't need to
416 * be wrapped in extern "C" when compiling
418 } include_defaults_array[]
419 #ifdef INCLUDE_DEFAULTS
425 /* Pick up GNU C++ specific include files. */
427 GPLUSPLUS_INCLUDE_DIR, 1, 1}
430 /* This is the dir for fixincludes. Put it just before
431 * the files that we fix. */
433 GCC_INCLUDE_DIR, 0, 0}
435 /* For cross-compilation, this dir name is generated
436 * automatically in Makefile.in. */
438 CROSS_INCLUDE_DIR, 0, 0}
440 /* This is another place that the target system's headers might be. */
442 TOOL_INCLUDE_DIR, 0, 1}
444 #else /* not CROSS_COMPILE */
445 /* This should be /usr/local/include and should come before
446 * the fixincludes-fixed header files. */
448 LOCAL_INCLUDE_DIR, 0, 1}
450 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
451 * Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
453 TOOL_INCLUDE_DIR, 0, 1}
455 /* This is the dir for fixincludes. Put it just before
456 * the files that we fix. */
458 GCC_INCLUDE_DIR, 0, 0}
460 /* Some systems have an extra dir of include files. */
461 #ifdef SYSTEM_INCLUDE_DIR
463 SYSTEM_INCLUDE_DIR, 0, 0}
467 STANDARD_INCLUDE_DIR, 0, 0}
469 #endif /* not CROSS_COMPILE */
474 #endif /* no INCLUDE_DEFAULTS */
476 /* Here is the actual list of #-directives, most-often-used first.
477 * The initialize_builtins function assumes #define is the very first. */
479 static struct directive directive_table[] = {
480 {6, do_define, "define", T_DEFINE, 0, 1, 0},
481 {5, do_xifdef, "ifdef", T_IFDEF, 1, 0, 0},
482 {6, do_xifdef, "ifndef", T_IFNDEF, 1, 0, 0},
483 {7, do_include, "include", T_INCLUDE, 1, 0, 0},
484 {12, do_include, "include_next", T_INCLUDE_NEXT, 1, 0, 0},
485 {6, do_include, "import", T_IMPORT, 1, 0, 0},
486 {5, do_endif, "endif", T_ENDIF, 1, 0, 0},
487 {4, do_else, "else", T_ELSE, 1, 0, 0},
488 {2, do_if, "if", T_IF, 1, 0, 0},
489 {4, do_elif, "elif", T_ELIF, 1, 0, 0},
490 {5, do_undef, "undef", T_UNDEF, 0, 0, 0},
491 {5, do_error, "error", T_ERROR, 0, 0, 0},
492 {7, do_warning, "warning", T_WARNING, 0, 0, 0},
493 {6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
494 {4, do_line, "line", T_LINE, 1, 0, 0},
495 {5, do_ident, "ident", T_IDENT, 1, 0, 1},
496 #ifdef SCCS_DIRECTIVE
497 {4, do_sccs, "sccs", T_SCCS, 0, 0, 0},
499 {6, do_assert, "assert", T_ASSERT, 1, 0, 0},
500 {8, do_unassert, "unassert", T_UNASSERT, 1, 0, 0},
501 {-1, 0, "", T_UNUSED, 0, 0, 0},
504 /* table to tell if char can be part of a C identifier. */
505 unsigned char is_idchar[256];
507 /* table to tell if char can be first char of a c identifier. */
508 unsigned char is_idstart[256];
510 /* table to tell if c is horizontal space. */
511 unsigned char is_hor_space[256];
513 /* table to tell if c is horizontal or vertical space. */
514 static unsigned char is_space[256];
516 /* Initialize syntactic classifications of characters. */
519 initialize_char_syntax(struct cpp_options *opts)
524 * Set up is_idchar and is_idstart tables. These should be
525 * faster than saying (is_alpha (c) || c == '_'), etc.
526 * Set up these things before calling any routines tthat
529 for (i = 'a'; i <= 'z'; i++)
531 is_idchar[i - 'a' + 'A'] = 1;
533 is_idstart[i - 'a' + 'A'] = 1;
536 for (i = '0'; i <= '9'; i++)
538 is_idchar[(unsigned char)'_'] = 1;
539 is_idstart[(unsigned char)'_'] = 1;
540 is_idchar[(unsigned char)'$'] = opts->dollars_in_ident;
541 is_idstart[(unsigned char)'$'] = opts->dollars_in_ident;
543 /* horizontal space table */
544 is_hor_space[(unsigned char)' '] = 1;
545 is_hor_space[(unsigned char)'\t'] = 1;
546 is_hor_space[(unsigned char)'\v'] = 1;
547 is_hor_space[(unsigned char)'\f'] = 1;
548 is_hor_space[(unsigned char)'\r'] = 1;
550 is_space[(unsigned char)' '] = 1;
551 is_space[(unsigned char)'\t'] = 1;
552 is_space[(unsigned char)'\v'] = 1;
553 is_space[(unsigned char)'\f'] = 1;
554 is_space[(unsigned char)'\n'] = 1;
555 is_space[(unsigned char)'\r'] = 1;
558 /* Place into PFILE a quoted string representing the string SRC.
559 * Caller must reserve enough space in pfile->token_buffer. */
561 quote_string(cpp_reader * pfile, const char *src)
565 CPP_PUTC_Q(pfile, '\"');
567 switch ((c = *src++))
571 CPP_PUTC_Q(pfile, c);
574 sprintf((char *)CPP_PWRITTEN(pfile), "\\%03o", c);
575 CPP_ADJUST_WRITTEN(pfile, 4);
581 CPP_PUTC_Q(pfile, '\\');
582 CPP_PUTC_Q(pfile, c);
586 CPP_PUTC_Q(pfile, '\"');
587 CPP_NUL_TERMINATE_Q(pfile);
592 /* Make sure PFILE->token_buffer will hold at least N more chars. */
595 cpp_grow_buffer(cpp_reader * pfile, long n)
597 long old_written = CPP_WRITTEN(pfile);
599 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
600 pfile->token_buffer =
601 (unsigned char *)xrealloc(pfile->token_buffer, pfile->token_buffer_size);
602 CPP_SET_WRITTEN(pfile, old_written);
606 * process a given definition string, for initialization
607 * If STR is just an identifier, define it with value 1.
608 * If STR has anything after the identifier, then it should
609 * be identifier=definition.
613 cpp_define(cpp_reader * pfile, unsigned char *str)
615 unsigned char *buf, *p;
621 cpp_error(pfile, "malformed option `-D %s'", str);
624 while (is_idchar[*++p])
628 buf = (unsigned char *)alloca(p - buf + 4);
629 strcpy((char *)buf, (const char *)str);
630 strcat((char *)buf, " 1");
634 cpp_error(pfile, "malformed option `-D %s'", str);
641 /* Copy the entire option so we can modify it. */
642 buf = (unsigned char *)alloca(2 * strlen((char *)str) + 1);
643 strncpy((char *)buf, (const char *)str, p - str);
644 /* Change the = to a space. */
646 /* Scan for any backslash-newline and remove it. */
651 if (*p == '\\' && p[1] == '\n')
659 do_define(pfile, NULL, buf, buf + strlen((char *)buf));
662 /* Process the string STR as if it appeared as the body of a #assert.
663 * OPTION is the option name for which STR was the argument. */
666 make_assertion(cpp_reader * pfile, const char *option, const char *str)
668 unsigned char *buf, *p, *q;
670 /* Copy the entire option so we can modify it. */
671 buf = (unsigned char *)alloca(strlen((char *)str) + 1);
672 strcpy((char *)buf, (const char *)str);
673 /* Scan for any backslash-newline and remove it. */
684 cpp_error(pfile, "malformed option `%s %s'", option, str);
687 while (is_idchar[*++p])
689 while (*p == ' ' || *p == '\t')
691 if (!(*p == 0 || *p == '('))
693 cpp_error(pfile, "malformed option `%s %s'", option, str);
696 cpp_push_buffer(pfile, buf, strlen((char *)buf));
697 do_assert(pfile, NULL, NULL, NULL);
698 cpp_pop_buffer(pfile);
701 /* Append a chain of `file_name_list's
702 * to the end of the main include chain.
703 * FIRST is the beginning of the chain to append, and LAST is the end. */
706 append_include_chain(cpp_reader * pfile, file_name_list * first,
707 file_name_list * last)
709 struct cpp_options *opts = CPP_OPTIONS(pfile);
716 opts->include = first;
718 opts->last_include->next = first;
720 if (!opts->first_bracket_include)
721 opts->first_bracket_include = first;
723 for (dir = first;; dir = dir->next)
725 int len = strlen(dir->fname) + INCLUDE_LEN_FUDGE;
727 if (len > pfile->max_include_len)
728 pfile->max_include_len = len;
734 opts->last_include = last;
737 /* Add output to `deps_buffer' for the -M switch.
738 * STRING points to the text to be output.
739 * SPACER is ':' for targets, ' ' for dependencies, zero for text
740 * to be inserted literally. */
743 deps_output(cpp_reader * pfile, const char *string, int spacer)
745 int size = strlen(string);
750 #ifndef MAX_OUTPUT_COLUMNS
751 #define MAX_OUTPUT_COLUMNS 72
754 && pfile->deps_column > 0
755 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
757 deps_output(pfile, " \\\n ", 0);
758 pfile->deps_column = 0;
760 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
762 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
763 pfile->deps_buffer = (char *)xrealloc(pfile->deps_buffer,
764 pfile->deps_allocated_size);
766 if (spacer == ' ' && pfile->deps_column > 0)
767 pfile->deps_buffer[pfile->deps_size++] = ' ';
768 memcpy(&pfile->deps_buffer[pfile->deps_size], string, size);
769 pfile->deps_size += size;
770 pfile->deps_column += size;
772 pfile->deps_buffer[pfile->deps_size++] = ':';
773 pfile->deps_buffer[pfile->deps_size] = 0;
776 /* Given a colon-separated list of file names PATH,
777 * add all the names to the search path for include files. */
780 path_include(cpp_reader * pfile, char *path)
791 file_name_list *dirtmp;
793 /* Find the end of this name. */
794 while (*q != 0 && *q != PATH_SEPARATOR)
798 /* An empty name in the path stands for the current directory. */
799 name = (char *)xmalloc(2);
805 /* Otherwise use the directory that is named. */
806 name = (char *)xmalloc(q - p + 1);
807 memcpy(name, p, q - p);
811 dirtmp = (file_name_list *) xmalloc(sizeof(file_name_list));
813 dirtmp->next = 0; /* New one goes on the end */
814 dirtmp->control_macro = 0;
815 dirtmp->c_system_include_path = 0;
816 dirtmp->fname = name;
817 dirtmp->got_name_map = 0;
818 append_include_chain(pfile, dirtmp, dirtmp);
820 /* Advance past this name. */
824 /* Skip the colon. */
830 init_parse_options(struct cpp_options *opts)
832 memset((char *)opts, 0, sizeof *opts);
833 opts->in_fname = NULL;
834 opts->out_fname = NULL;
836 /* Initialize is_idchar to allow $. */
837 opts->dollars_in_ident = 1;
838 initialize_char_syntax(opts);
839 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
841 opts->no_line_commands = 0;
842 opts->no_trigraphs = 1;
843 opts->put_out_comments = 0;
844 opts->print_include_names = 0;
845 opts->dump_macros = dump_none;
848 opts->cplusplus_comments = 1;
855 opts->pedantic_errors = 0;
856 opts->inhibit_warnings = 0;
857 opts->warn_comments = 0;
858 opts->warn_import = 1;
859 opts->warnings_are_errors = 0;
862 static enum cpp_token
863 null_underflow(cpp_reader * pfile __UNUSED__)
869 null_cleanup(cpp_buffer * pbuf __UNUSED__, cpp_reader * pfile __UNUSED__)
875 macro_cleanup(cpp_buffer * pbuf, cpp_reader * pfile __UNUSED__)
877 HASHNODE *macro = (HASHNODE *) pbuf->data;
879 if (macro->type == T_DISABLED)
880 macro->type = T_MACRO;
881 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
887 file_cleanup(cpp_buffer * pbuf, cpp_reader * pfile __UNUSED__)
897 /* Assuming we have read '/'.
898 * If this is the start of a comment (followed by '*' or '/'),
899 * skip to the end of the comment, and return ' '.
900 * Return EOF if we reached the end of file before the end of the comment.
901 * If not the start of a comment, return '/'. */
904 skip_comment(cpp_reader * pfile, long *linep)
908 while (PEEKC() == '\\' && PEEKN(1) == '\n')
924 while (c == '\\' && PEEKC() == '\n')
928 FORWARD(1), c = GETC();
930 if (prev_c == '*' && c == '/')
932 if (c == '\n' && linep)
936 else if (PEEKC() == '/' && CPP_OPTIONS(pfile)->cplusplus_comments)
943 return ' '; /* Allow // to be terminated by EOF. */
944 while (c == '\\' && PEEKC() == '\n')
953 /* Don't consider final '\n' to be part of comment. */
963 /* Skip whitespace \-newline and comments. Does not macro-expand. */
965 cpp_skip_hspace(cpp_reader * pfile)
975 if ((c == '\f' || c == '\v') && CPP_PEDANTIC(pfile))
976 cpp_pedwarn(pfile, "%s in preprocessing directive",
977 c == '\f' ? "formfeed" : "vertical tab");
983 c = skip_comment(pfile, NULL);
986 if (c == EOF || c == '/')
989 else if (c == '\\' && PEEKN(1) == '\n')
993 else if (c == '@' && CPP_BUFFER(pfile)->has_escapes
994 && is_hor_space[PEEKN(1)])
1003 /* Read the rest of the current line.
1004 * The line is appended to PFILE's output buffer. */
1007 copy_rest_of_line(cpp_reader * pfile)
1009 struct cpp_options *opts = CPP_OPTIONS(pfile);
1021 if (PEEKC() == '\n')
1028 goto scan_directive_token;
1032 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
1033 goto scan_directive_token;
1037 if (CPP_PEDANTIC(pfile))
1038 cpp_pedwarn(pfile, "%s in preprocessing directive",
1039 c == '\f' ? "formfeed" : "vertical tab");
1045 scan_directive_token:
1047 cpp_get_token(pfile);
1053 CPP_NUL_TERMINATE(pfile);
1057 skip_rest_of_line(cpp_reader * pfile)
1059 long old = CPP_WRITTEN(pfile);
1061 copy_rest_of_line(pfile);
1062 CPP_SET_WRITTEN(pfile, old);
1065 /* Handle a possible # directive.
1066 * '#' has already been read. */
1069 handle_directive(cpp_reader * pfile)
1072 struct directive *kt;
1074 long after_ident = 0;
1075 unsigned char *ident, *line_end;
1076 long old_written = CPP_WRITTEN(pfile);
1078 cpp_skip_hspace(pfile);
1081 if (c >= '0' && c <= '9')
1083 /* Handle # followed by a line number. */
1084 if (CPP_PEDANTIC(pfile))
1085 cpp_pedwarn(pfile, "`#' followed by integer");
1086 do_line(pfile, NULL, NULL, NULL);
1087 goto done_a_directive;
1089 /* Now find the directive name. */
1090 CPP_PUTC(pfile, '#');
1091 parse_name(pfile, GETC());
1092 ident = pfile->token_buffer + old_written + 1;
1093 ident_length = CPP_PWRITTEN(pfile) - ident;
1094 if (ident_length == 0 && PEEKC() == '\n')
1096 /* A line of just `#' becomes blank. */
1097 goto done_a_directive;
1100 * Decode the keyword and call the appropriate expansion
1101 * routine, after moving the input pointer up to the next line.
1103 for (kt = directive_table;; kt++)
1105 if (kt->length <= 0)
1106 goto not_a_directive;
1107 if (kt->length == ident_length
1108 && !strncmp(kt->name, (const char *)ident, ident_length))
1112 if (!kt->command_reads_line)
1114 /* Nonzero means do not delete comments within the directive.
1115 * #define needs this when -traditional. */
1117 int save_put_out_comments =
1118 CPP_OPTIONS(pfile)->put_out_comments;
1120 CPP_OPTIONS(pfile)->put_out_comments = comments;
1121 after_ident = CPP_WRITTEN(pfile);
1122 copy_rest_of_line(pfile);
1123 CPP_OPTIONS(pfile)->put_out_comments = save_put_out_comments;
1125 /* For #pragma and #define, we may want to pass through the directive.
1126 * Other directives may create output, but we don't want the directive
1127 * itself out, so we pop it now. For example #include may write a #line
1128 * command (see comment in do_include), and conditionals may emit
1129 * #failed ... #endfailed stuff. But note that popping the buffer
1130 * means the parameters to kt->func may point after pfile->limit
1131 * so these parameters are invalid as soon as something gets appended
1132 * to the token_buffer. */
1134 line_end = CPP_PWRITTEN(pfile);
1135 if (!kt->pass_thru && kt->type != T_DEFINE)
1136 CPP_SET_WRITTEN(pfile, old_written);
1138 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1140 || (kt->type == T_DEFINE
1141 && CPP_OPTIONS(pfile)->dump_macros == dump_definitions))
1143 /* Just leave the entire #define in the output stack. */
1145 else if (kt->type == T_DEFINE
1146 && CPP_OPTIONS(pfile)->dump_macros == dump_names)
1148 unsigned char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1150 SKIP_WHITE_SPACE(p);
1151 while (is_idchar[*p])
1154 CPP_PUTC(pfile, '\n');
1156 else if (kt->type == T_DEFINE)
1157 CPP_SET_WRITTEN(pfile, old_written);
1165 /* Pass a directive through to the output file.
1166 * BUF points to the contents of the directive, as a contiguous string.
1167 * LIMIT points to the first character past the end of the directive.
1168 * KEYWORD is the keyword-table entry for the directive. */
1171 pass_thru_directive(char *buf, char *limit, cpp_reader * pfile,
1172 struct directive *keyword)
1174 unsigned keyword_length = keyword->length;
1176 CPP_RESERVE(pfile, 1 + keyword_length + (limit - buf));
1177 CPP_PUTC_Q(pfile, '#');
1178 CPP_PUTS_Q(pfile, keyword->name, keyword_length);
1179 if (limit != buf && buf[0] != ' ')
1180 CPP_PUTC_Q(pfile, ' ');
1181 CPP_PUTS_Q(pfile, buf, limit - buf);
1184 /* The arglist structure is built by do_define to tell
1185 * collect_definition where the argument names begin. That
1186 * is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1187 * would contain pointers to the strings x, y, and z.
1188 * Collect_definition would then build a DEFINITION node,
1189 * with reflist nodes pointing to the places x, y, and z had
1190 * appeared. So the arglist is just convenience data passed
1191 * between these two routines. It is not kept around after
1192 * the current #define has been processed and entered into the
1196 struct arglist *next;
1203 /* Read a replacement list for a macro with parameters.
1204 * Build the DEFINITION structure.
1205 * Reads characters of text starting at BUF until END.
1206 * ARGLIST specifies the formal parameters to look for
1207 * in the text of the definition; NARGS is the number of args
1208 * in that list, or -1 for a macro name that wants no argument list.
1209 * MACRONAME is the macro name itself (so we can avoid recursive expansion)
1210 * and NAMELEN is its length in characters.
1212 * Note that comments, backslash-newlines, and leading white space
1213 * have already been deleted from the argument. */
1216 collect_expansion(cpp_reader * pfile, unsigned char *buf, unsigned char *limit,
1217 int nargs, struct arglist *arglist)
1220 unsigned char *p, *lastp, *exp_p;
1221 reflist *endpat = NULL;
1223 /* Pointer to first nonspace after last ## seen. */
1224 unsigned char *concat = 0;
1226 /* Pointer to first nonspace after last single-# seen. */
1227 unsigned char *stringify = 0;
1229 int expected_delimiter = '\0';
1231 /* Scan thru the replacement list, ignoring comments and quoted
1232 * strings, picking up on the macro calls. It does a linear search
1233 * thru the arg list on every potential symbol. Profiling might say
1234 * that something smarter should happen. */
1239 /* Find the beginning of the trailing whitespace. */
1241 while (p < limit && is_space[limit[-1]])
1244 /* Allocate space for the text in the macro definition.
1245 * Leading and trailing whitespace chars need 2 bytes each.
1246 * Each other input char may or may not need 1 byte,
1247 * so this is an upper bound. The extra 5 are for invented
1248 * leading and trailing newline-marker and final null. */
1249 maxsize = (sizeof(DEFINITION) + (limit - p) + 5);
1250 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1254 defn = (DEFINITION *) xcalloc(1, maxsize);
1256 defn->nargs = nargs;
1257 exp_p = defn->expansion = (unsigned char *)defn + sizeof(DEFINITION);
1262 /* Add one initial space escape-marker to prevent accidental
1263 * token-pasting (often removed by macroexpand). */
1267 if (limit - p >= 2 && p[0] == '#' && p[1] == '#')
1269 cpp_error(pfile, "`##' at start of macro definition");
1272 /* Process the main body of the definition. */
1275 int skipped_arg = 0;
1276 unsigned char c = *p++;
1284 if (expected_delimiter != '\0')
1286 if (c == expected_delimiter)
1287 expected_delimiter = '\0';
1290 expected_delimiter = c;
1294 if (p < limit && expected_delimiter)
1296 /* In a string, backslash goes through
1297 * and makes next char ordinary. */
1303 /* An '@' in a string or character constant stands for itself,
1304 * and does not need to be escaped. */
1305 if (!expected_delimiter)
1310 /* # is ordinary inside a string. */
1311 if (expected_delimiter)
1313 if (p < limit && *p == '#')
1315 /* ##: concatenate preceding and following tokens. */
1316 /* Take out the first #, discard preceding whitespace. */
1318 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1320 /* Skip the second #. */
1322 /* Discard following whitespace. */
1323 SKIP_WHITE_SPACE(p);
1326 cpp_error(pfile, "`##' at end of macro definition");
1328 else if (nargs >= 0)
1330 /* Single #: stringify following argument ref.
1331 * Don't leave the # in the expansion. */
1333 SKIP_WHITE_SPACE(p);
1334 if (p == limit || !is_idstart[*p])
1336 "`#' operator is not followed by a macro argument name");
1343 /* Handle the start of a symbol. */
1344 if (is_idchar[c] && nargs > 0)
1346 unsigned char *id_beg = p - 1;
1350 while (p != limit && is_idchar[*p])
1352 id_len = p - id_beg;
1356 struct arglist *arg;
1358 for (arg = arglist; arg; arg = arg->next)
1362 if (arg->name[0] == c
1363 && arg->length == id_len
1364 && strncmp((const char *)arg->name,
1365 (const char *)id_beg, id_len) == 0)
1367 if (expected_delimiter
1368 && CPP_OPTIONS(pfile)->warn_stringify)
1371 "macro arg `%.*s' would be stringified with -traditional.",
1374 /* If ANSI, don't actually substitute inside a string. */
1375 if (expected_delimiter)
1377 /* make a pat node for this arg and append it to the end of
1379 tpat = (reflist *) xmalloc(sizeof(reflist));
1382 tpat->raw_before = concat == id_beg;
1383 tpat->raw_after = 0;
1384 tpat->rest_args = arg->rest_args;
1385 tpat->stringify = (stringify == id_beg);
1388 defn->pattern = tpat;
1390 endpat->next = tpat;
1393 tpat->argno = arg->argno;
1394 tpat->nchars = exp_p - lastp;
1396 unsigned char *p1 = p;
1398 SKIP_WHITE_SPACE(p1);
1399 if (p1 + 2 <= limit && p1[0] == '#'
1401 tpat->raw_after = 1;
1403 lastp = exp_p; /* place to start copying from next time */
1409 /* If this was not a macro arg, copy it into the expansion. */
1412 unsigned char *lim1 = p;
1417 if (stringify == id_beg)
1419 "`#' operator should be followed by a macro argument name");
1424 if (expected_delimiter == 0)
1426 /* If ANSI, put in a "@ " marker to prevent token pasting.
1427 * But not if "inside a string" (which in ANSI mode
1428 * happens only for -D option). */
1434 defn->length = exp_p - defn->expansion;
1436 /* Crash now if we overrun the allocated size. */
1437 if (defn->length + 1 > maxsize)
1444 * special extension string that can be added to the last macro argument to
1445 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1446 * #define wow(a, b...) process (b, a, b)
1447 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1448 * { wow (one, two); } -> { process (two, one, two); }
1449 * if this "rest_arg" is used with the concat token '##' and if it is not
1450 * supplied then the token attached to with ## will not be outputted. Ex:
1451 * #define wow (a, b...) process (b ## , a, ## b)
1452 * { wow (1, 2); } -> { process (2, 1, 2); }
1453 * { wow (one); } -> { process (one); {
1455 static char rest_extension[] = "...";
1457 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1459 /* Create a DEFINITION node from a #define directive. Arguments are
1460 * as for do_define. */
1462 create_definition(MACRODEF * mdef, unsigned char *buf, unsigned char *limit,
1463 cpp_reader * pfile, int predefinition)
1465 unsigned char *bp; /* temp ptr into input buffer */
1466 unsigned char *symname; /* remember where symbol name starts */
1467 int sym_length; /* and how long it is */
1471 CPP_BUFFER(pfile) ? CPP_BUFFER(pfile)->nominal_fname : "";
1473 int arglengths = 0; /* Accumulate lengths of arg names
1474 * plus number of args. */
1476 cpp_buf_line_and_col(CPP_BUFFER(pfile), &line, &col);
1480 while (is_hor_space[*bp])
1483 symname = bp; /* remember where it starts */
1485 sym_length = check_macro_name(pfile, bp, "macro");
1488 /* Lossage will occur if identifiers or control keywords are broken
1489 * across lines using backslash. This is not the right place to take
1494 struct arglist *arg_ptrs = NULL;
1497 bp++; /* skip '(' */
1498 SKIP_WHITE_SPACE(bp);
1500 /* Loop over macro argument names. */
1503 struct arglist *temp;
1505 temp = (struct arglist *)alloca(sizeof(struct arglist));
1507 temp->name = (char *)bp;
1508 temp->next = arg_ptrs;
1509 temp->argno = argno++;
1510 temp->rest_args = 0;
1514 cpp_pedwarn(pfile, "another parameter follows `%s'",
1517 if (!is_idstart[*bp])
1518 cpp_pedwarn(pfile, "invalid character in macro parameter name");
1520 /* Find the end of the arg name. */
1521 while (is_idchar[*bp])
1524 /* do we have a "special" rest-args extension here? */
1525 if ((unsigned)(limit - bp) > REST_EXTENSION_LENGTH &&
1526 strncmp(rest_extension, (const char *)bp,
1527 REST_EXTENSION_LENGTH) == 0)
1530 temp->rest_args = 1;
1534 temp->length = (char *)bp - temp->name;
1536 bp += REST_EXTENSION_LENGTH;
1537 arglengths += temp->length + 2;
1538 SKIP_WHITE_SPACE(bp);
1539 if (temp->length == 0 || (*bp != ',' && *bp != ')'))
1542 "badly punctuated parameter list in `#define'");
1548 SKIP_WHITE_SPACE(bp);
1552 cpp_error(pfile, "unterminated parameter list in `#define'");
1556 struct arglist *otemp;
1558 for (otemp = temp->next; otemp; otemp = otemp->next)
1559 if (temp->length == otemp->length &&
1560 strncmp((const char *)temp->name,
1561 (const char *)otemp->name, temp->length) == 0)
1565 name = (char *)alloca(temp->length + 1);
1566 strncpy(name, (const char *)temp->name, temp->length);
1567 name[temp->length] = '\0';
1569 "duplicate argument name `%s' in `#define'",
1576 ++bp; /* skip paren */
1577 SKIP_WHITE_SPACE(bp);
1578 /* now everything from bp before limit is the definition. */
1579 defn = collect_expansion(pfile, bp, limit, argno, arg_ptrs);
1580 defn->rest_args = rest_args;
1582 /* Now set defn->args.argnames to the result of concatenating
1583 * the argument names in reverse order
1584 * with comma-space between them. */
1585 defn->args.argnames = (unsigned char *)xmalloc(arglengths + 1);
1587 struct arglist *temp;
1590 for (temp = arg_ptrs; temp; temp = temp->next)
1592 memcpy(&defn->args.argnames[i], temp->name, temp->length);
1596 defn->args.argnames[i++] = ',';
1597 defn->args.argnames[i++] = ' ';
1600 defn->args.argnames[i] = 0;
1605 /* Simple expansion or empty definition. */
1609 if (is_hor_space[*bp])
1612 SKIP_WHITE_SPACE(bp);
1646 "missing white space after `#define %.*s'",
1647 sym_length, symname);
1652 "missing white space after `#define %.*s'",
1653 sym_length, symname);
1658 /* now everything from bp before limit is the definition. */
1659 defn = collect_expansion(pfile, bp, limit, -1, NULL);
1660 defn->args.argnames = (unsigned char *)"";
1666 /* OP is null if this is a predefinition */
1667 defn->predefined = predefinition;
1669 mdef->symnam = (char *)symname;
1670 mdef->symlen = sym_length;
1678 /* Check a purported macro name SYMNAME, and yield its length.
1679 * USAGE is the kind of name this is intended for. */
1682 check_macro_name(cpp_reader * pfile, unsigned char *symname, const char *usage)
1687 for (p = symname; is_idchar[*p]; p++)
1689 sym_length = p - symname;
1690 if (sym_length == 0)
1692 cpp_error(pfile, "invalid %s name", usage);
1694 else if (!is_idstart[*symname])
1696 unsigned char *msg; /* what pain... */
1698 msg = (unsigned char *)alloca(sym_length + 1);
1699 memcpy(msg, symname, sym_length);
1700 msg[sym_length] = 0;
1701 cpp_error(pfile, "invalid %s name `%s'", usage, msg);
1705 if (!strncmp((const char *)symname, "defined", 7) && sym_length == 7)
1706 cpp_error(pfile, "invalid %s name `defined'", usage);
1712 * return zero if two DEFINITIONs are isomorphic
1715 compare_defs(DEFINITION * d1, DEFINITION * d2)
1718 unsigned char *p1 = d1->expansion;
1719 unsigned char *p2 = d2->expansion;
1722 if (d1->nargs != d2->nargs)
1724 if (strcmp((char *)d1->args.argnames, (char *)d2->args.argnames))
1726 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1727 a1 = a1->next, a2 = a2->next)
1730 ((a1->nchars == a2->nchars
1731 && !strncmp((const char *)p1, (const char *)p2, a1->nchars))
1732 || !comp_def_part(first, p1, a1->nchars, p2, a2->nchars, 0))
1733 || a1->argno != a2->argno || a1->stringify != a2->stringify
1734 || a1->raw_before != a2->raw_before
1735 || a1->raw_after != a2->raw_after)
1743 if (comp_def_part(first, p1, d1->length - (p1 - d1->expansion),
1744 p2, d2->length - (p2 - d2->expansion), 1))
1749 /* Return 1 if two parts of two macro definitions are effectively different.
1750 * One of the parts starts at BEG1 and has LEN1 chars;
1751 * the other has LEN2 chars at BEG2.
1752 * Any sequence of whitespace matches any other sequence of whitespace.
1753 * FIRST means these parts are the first of a macro definition;
1754 * so ignore leading whitespace entirely.
1755 * LAST means these parts are the last of a macro definition;
1756 * so ignore trailing whitespace entirely. */
1759 comp_def_part(int first, unsigned char *beg1, int len1,
1760 unsigned char *beg2, int len2, int last)
1762 unsigned char *end1 = beg1 + len1;
1763 unsigned char *end2 = beg2 + len2;
1767 while (beg1 != end1 && is_space[*beg1])
1769 while (beg2 != end2 && is_space[*beg2])
1774 while (beg1 != end1 && is_space[end1[-1]])
1776 while (beg2 != end2 && is_space[end2[-1]])
1779 while (beg1 != end1 && beg2 != end2)
1781 if (is_space[*beg1] && is_space[*beg2])
1783 while (beg1 != end1 && is_space[*beg1])
1785 while (beg2 != end2 && is_space[*beg2])
1788 else if (*beg1 == *beg2)
1796 return (beg1 != end1) || (beg2 != end2);
1799 /* Process a #define command.
1800 * BUF points to the contents of the #define command, as a contiguous string.
1801 * LIMIT points to the first character past the end of the definition.
1802 * KEYWORD is the keyword-table entry for #define,
1803 * or NULL for a "predefined" macro. */
1806 do_define(cpp_reader * pfile, struct directive *keyword,
1807 unsigned char *buf, unsigned char *limit)
1813 create_definition(&mdef, buf, limit, pfile, !keyword);
1817 hashcode = hashf(mdef.symnam, mdef.symlen, HASHSIZE);
1819 if ((hp = cpp_lookup(mdef.symnam, mdef.symlen, hashcode)))
1823 /* Redefining a precompiled key is ok. */
1824 if (hp->type == T_PCSTRING)
1826 /* Redefining a macro is ok if the definitions are the same. */
1827 else if (hp->type == T_MACRO)
1828 ok = !compare_defs(mdef.defn, hp->value.defn);
1829 /* Redefining a constant is ok with -D. */
1830 else if (hp->type == T_CONST)
1831 ok = !CPP_OPTIONS(pfile)->done_initializing;
1832 /* Print the warning if it's not ok. */
1835 char *msg; /* what pain... */
1837 /* If we are passing through #define and #undef directives, do
1838 * that for this re-definition now. */
1839 if (CPP_OPTIONS(pfile)->debug_output && keyword)
1840 pass_thru_directive((char *)buf, (char *)limit, pfile, keyword);
1842 msg = (char *)alloca(mdef.symlen + 22);
1844 memcpy(msg + 1, mdef.symnam, mdef.symlen);
1845 strcpy((msg + mdef.symlen + 1), "' redefined");
1846 cpp_pedwarn(pfile, msg);
1847 if (hp->type == T_MACRO)
1848 cpp_pedwarn_with_file_and_line(pfile, hp->value.defn->file,
1849 hp->value.defn->line,
1850 "this is the location of the previous definition",
1853 /* Replace the old definition. */
1855 hp->value.defn = mdef.defn;
1859 /* If we are passing through #define and #undef directives, do
1860 * that for this new definition now. */
1861 if (CPP_OPTIONS(pfile)->debug_output && keyword)
1862 pass_thru_directive((char *)buf, (char *)limit, pfile, keyword);
1863 install(mdef.symnam, mdef.symlen, T_MACRO, 0,
1864 (char *)mdef.defn, hashcode);
1870 /* This structure represents one parsed argument in a macro call.
1871 * `raw' points to the argument text as written (`raw_length' is its length).
1872 * `expanded' points to the argument's macro-expansion
1873 * (its length is `expand_length').
1874 * `stringified_length' is the length the argument would have
1876 * `use_count' is the number of times this macro arg is substituted
1877 * into the macro. If the actual use count exceeds 10,
1878 * the value stored is 10. */
1880 /* raw and expanded are relative to ARG_BASE */
1881 #define ARG_BASE ((pfile)->token_buffer)
1884 /* Strings relative to pfile->token_buffer */
1885 long raw, expanded, stringified;
1886 int raw_length, expand_length;
1887 int stringified_length;
1893 cpp_push_buffer(cpp_reader * pfile, unsigned char *buffer, long length)
1895 #ifdef STATIC_BUFFERS
1896 cpp_buffer *buf = CPP_BUFFER(pfile);
1898 if (buf == pfile->buffer_stack)
1899 cpp_fatal("macro or `#include' recursion too deep");
1901 memset((char *)buf, 0, sizeof(cpp_buffer));
1902 CPP_BUFFER(pfile) = buf;
1904 cpp_buffer *buf = (cpp_buffer *) xmalloc(sizeof(cpp_buffer));
1906 memset((char *)buf, 0, sizeof(cpp_buffer));
1907 CPP_PREV_BUFFER(buf) = CPP_BUFFER(pfile);
1908 CPP_BUFFER(pfile) = buf;
1910 buf->if_stack = pfile->if_stack;
1911 buf->cleanup = null_cleanup;
1912 buf->underflow = null_underflow;
1913 buf->buf = buf->cur = buffer;
1914 buf->alimit = buf->rlimit = buffer + length;
1920 cpp_pop_buffer(cpp_reader * pfile)
1922 cpp_buffer *buf = CPP_BUFFER(pfile);
1924 #ifdef STATIC_BUFFERS
1925 (*buf->cleanup) (buf, pfile);
1926 return ++CPP_BUFFER(pfile);
1928 cpp_buffer *next_buf = CPP_PREV_BUFFER(buf);
1930 (*buf->cleanup) (buf, pfile);
1931 CPP_BUFFER(pfile) = next_buf;
1937 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1938 * Pop the buffer when done. */
1941 cpp_scan_buffer(cpp_reader * pfile)
1943 cpp_buffer *buffer = CPP_BUFFER(pfile);
1947 enum cpp_token token = cpp_get_token(pfile);
1949 if (token == CPP_EOF) /* Should not happen ... */
1951 if (token == CPP_POP && CPP_BUFFER(pfile) == buffer)
1953 cpp_pop_buffer(pfile);
1960 * Rescan a string (which may have escape marks) into pfile's buffer.
1961 * Place the result in pfile->token_buffer.
1963 * The input is copied before it is scanned, so it is safe to pass
1964 * it something from the token_buffer that will get overwritten
1965 * (because it follows CPP_WRITTEN). This is used by do_include.
1969 cpp_expand_to_buffer(cpp_reader * pfile, unsigned char *buf, int length)
1972 unsigned char *limit = buf + length;
1973 unsigned char *buf1;
1978 /* Set up the input on the input stack. */
1980 buf1 = (unsigned char *)alloca(length + 1);
1982 unsigned char *p1 = buf;
1983 unsigned char *p2 = buf1;
1986 #if 0 /* old behavior */
1987 while (p1 != limit) *p2++ = *p1++;
1988 #else /* new one - handle \ escapes if not in string */
1993 if (*p1 == '"') in_string = 1;
1997 if (p1 != limit) *p2++ = *p1++;
2004 if ((*p1 == '"') && (p1 > buf) && (p1[-1] != '\\'))
2014 ip = cpp_push_buffer(pfile, buf1, length);
2015 ip->has_escapes = 1;
2017 /* Scan the input, create the output. */
2018 cpp_scan_buffer(pfile);
2020 CPP_NUL_TERMINATE(pfile);
2024 adjust_position(unsigned char *buf, unsigned char *limit, long *linep,
2029 unsigned char ch = *buf++;
2032 (*linep)++, (*colp) = 1;
2038 /* Move line_base forward, updating lineno and colno. */
2041 update_position(cpp_buffer * pbuf)
2043 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2044 unsigned char *new_pos = pbuf->cur;
2045 struct parse_marker *mark;
2047 for (mark = pbuf->marks; mark; mark = mark->next)
2049 if (pbuf->buf + mark->position < new_pos)
2050 new_pos = pbuf->buf + mark->position;
2052 pbuf->line_base += new_pos - old_pos;
2053 adjust_position(old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2057 cpp_buf_line_and_col(cpp_buffer * pbuf, long *linep, long *colp)
2065 *linep = pbuf->lineno;
2066 *colp = pbuf->colno;
2067 adjust_position(pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2076 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2079 cpp_file_buffer(cpp_reader * pfile)
2081 cpp_buffer *ip = CPP_BUFFER(pfile);
2083 for (; ip != CPP_NULL_BUFFER(pfile); ip = CPP_PREV_BUFFER(ip))
2090 count_newlines(unsigned char *buf, unsigned char *limit)
2096 unsigned char ch = *buf++;
2105 * write out a #line command, for instance, after an #include file.
2106 * If CONDITIONAL is nonzero, we can omit the #line if it would
2107 * appear to be a no-op, and we can output a few newlines instead
2108 * if we want to increase the line number by a small amount.
2109 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2113 output_line_command(cpp_reader * pfile, int conditional,
2114 enum file_change_code file_change)
2117 cpp_buffer *ip = CPP_BUFFER(pfile);
2119 if (CPP_OPTIONS(pfile)->no_line_commands
2120 || !ip->fname || 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);
2131 if (line == pfile->lineno)
2134 /* If the inherited line number is a little too small,
2135 * output some newlines instead of a #line command. */
2136 if (line > pfile->lineno && line < pfile->lineno + 8)
2138 CPP_RESERVE(pfile, 20);
2139 while (line > pfile->lineno)
2141 CPP_PUTC_Q(pfile, '\n');
2148 CPP_RESERVE(pfile, 4 * strlen(ip->nominal_fname) + 50);
2150 static char sharp_line[] = "#line ";
2152 CPP_PUTS_Q(pfile, sharp_line, sizeof(sharp_line) - 1);
2155 sprintf((char *)CPP_PWRITTEN(pfile), "%d ", (int)line);
2156 CPP_ADJUST_WRITTEN(pfile, strlen((char *)CPP_PWRITTEN(pfile)));
2158 quote_string(pfile, ip->nominal_fname);
2159 if (file_change != same_file)
2161 CPP_PUTC_Q(pfile, ' ');
2162 CPP_PUTC_Q(pfile, file_change == enter_file ? '1' : '2');
2164 /* Tell cc1 if following text comes from a system header file. */
2165 if (ip->system_header_p)
2167 CPP_PUTC_Q(pfile, ' ');
2168 CPP_PUTC_Q(pfile, '3');
2170 #ifndef NO_IMPLICIT_EXTERN_C
2171 /* Tell cc1plus if following text should be treated as C. */
2172 if (ip->system_header_p == 2 && CPP_OPTIONS(pfile)->cplusplus)
2174 CPP_PUTC_Q(pfile, ' ');
2175 CPP_PUTC_Q(pfile, '4');
2178 CPP_PUTC_Q(pfile, '\n');
2179 pfile->lineno = line;
2183 * Parse a macro argument and append the info on PFILE's token_buffer.
2184 * REST_ARGS means to absorb the rest of the args.
2185 * Return nonzero to indicate a syntax error.
2188 static enum cpp_token
2189 macarg(cpp_reader * pfile, int rest_args)
2192 enum cpp_token token;
2193 char save_put_out_comments =
2194 CPP_OPTIONS(pfile)->put_out_comments;
2196 CPP_OPTIONS(pfile)->put_out_comments = 0;
2199 /* Try to parse as much of the argument as exists at this
2200 * input stack level. */
2201 pfile->no_macro_expand++;
2204 token = cpp_get_token(pfile);
2210 /* If we've hit end of file, it's an error (reported by caller).
2211 * Ditto if it's the end of cpp_expand_to_buffer text.
2212 * If we've hit end of macro, just continue. */
2213 if (!CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
2224 /* if we've returned to lowest level and
2225 * we aren't absorbing all args */
2226 if (paren == 0 && rest_args == 0)
2230 /* Remove ',' or ')' from argument buffer. */
2231 CPP_ADJUST_WRITTEN(pfile, -1);
2238 CPP_OPTIONS(pfile)->put_out_comments = save_put_out_comments;
2239 pfile->no_macro_expand--;
2244 /* Turn newlines to spaces in the string of length LENGTH at START,
2245 * except inside of string constants.
2246 * The string is copied into itself with its beginning staying fixed. */
2249 change_newlines(unsigned char *start, int length)
2253 unsigned char *limit;
2257 limit = start + length;
2262 *obp++ = c = *ibp++;
2268 /* Notice and skip strings, so that we don't delete newlines in them. */
2274 *obp++ = c = *ibp++;
2277 if (c == '\n' && quotec == '\'')
2289 timestamp(cpp_reader * pfile)
2291 if (!pfile->timebuf)
2293 time_t t = time((time_t *) 0);
2295 pfile->timebuf = localtime(&t);
2297 return pfile->timebuf;
2300 static const char *monthnames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2301 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2305 * expand things like __FILE__. Place the expansion into the output
2306 * buffer *without* rescanning.
2310 special_symbol(HASHNODE * hp, cpp_reader * pfile)
2316 cpp_buffer *ip = NULL;
2319 int paren = 0; /* For special `defined' keyword */
2321 for (ip = CPP_BUFFER(pfile);; ip = CPP_PREV_BUFFER(ip))
2325 cpp_error(pfile, "cccp error: not in any file?!");
2326 return; /* the show must go on */
2339 if (hp->type == T_BASE_FILE)
2341 while (CPP_PREV_BUFFER(ip))
2342 ip = CPP_PREV_BUFFER(ip);
2344 string = ip->nominal_fname;
2348 CPP_RESERVE(pfile, 3 + 4 * strlen(string));
2349 quote_string(pfile, string);
2353 case T_INCLUDE_LEVEL:
2355 for (ip = CPP_BUFFER(pfile); ip; ip = CPP_PREV_BUFFER(ip))
2359 bufx = (char *)alloca(8); /* Eight bytes ought to be more than enough */
2360 sprintf(bufx, "%d", true_indepth - 1);
2365 bufx = (char *)alloca(3 + strlen(version_string));
2366 sprintf(bufx, "\"%s\"", version_string);
2370 #ifndef NO_BUILTIN_SIZE_TYPE
2376 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2377 case T_PTRDIFF_TYPE:
2383 buf = CPP_WCHAR_TYPE(pfile);
2386 case T_USER_LABEL_PREFIX_TYPE:
2387 buf = USER_LABEL_PREFIX;
2390 case T_REGISTER_PREFIX_TYPE:
2391 buf = REGISTER_PREFIX;
2395 bufx = (char *)alloca(4 * sizeof(int));
2396 sprintf(bufx, "%d", hp->value.ival);
2402 long line = ip->lineno;
2403 long col = ip->colno;
2405 adjust_position(CPP_LINE_BASE(ip), ip->cur, &line, &col);
2407 bufx = (char *)alloca(10);
2408 sprintf(bufx, "%d", (int)line);
2415 bufx = (char *)alloca(20);
2416 timebuf = timestamp(pfile);
2417 if (hp->type == T_DATE)
2418 sprintf(bufx, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2419 timebuf->tm_mday, timebuf->tm_year + 1900);
2421 sprintf(bufx, "\"%02d:%02d:%02d\"", timebuf->tm_hour,
2422 timebuf->tm_min, timebuf->tm_sec);
2426 case T_SPEC_DEFINED:
2427 buf = " 0 "; /* Assume symbol is not defined */
2428 ip = CPP_BUFFER(pfile);
2429 SKIP_WHITE_SPACE(ip->cur);
2430 if (*ip->cur == '(')
2433 ip->cur++; /* Skip over the paren */
2434 SKIP_WHITE_SPACE(ip->cur);
2436 if (!is_idstart[*ip->cur])
2438 if ((hp = cpp_lookup((const char *)ip->cur, -1, -1)))
2442 while (is_idchar[*ip->cur])
2444 SKIP_WHITE_SPACE(ip->cur);
2447 if (*ip->cur != ')')
2455 cpp_error(pfile, "`defined' without an identifier");
2459 cpp_error(pfile, "cccp error: invalid special hash type"); /* time for gdb */
2463 CPP_RESERVE(pfile, len + 1);
2464 CPP_PUTS_Q(pfile, buf, len);
2465 CPP_NUL_TERMINATE_Q(pfile);
2470 /* Initialize the built-in macros. */
2473 initialize_builtins(cpp_reader * pfile)
2475 install("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2476 install("__DATE__", -1, T_DATE, 0, 0, -1);
2477 install("__FILE__", -1, T_FILE, 0, 0, -1);
2478 install("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2479 install("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2480 install("__VERSION__", -1, T_VERSION, 0, 0, -1);
2481 #ifndef NO_BUILTIN_SIZE_TYPE
2482 install("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2484 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2485 install("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2487 install("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2488 install("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2489 install("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2490 install("__TIME__", -1, T_TIME, 0, 0, -1);
2491 install("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2492 if (CPP_OPTIONS(pfile)->objc)
2493 install("__OBJC__", -1, T_CONST, 1, 0, -1);
2494 /* This is supplied using a -D by the compiler driver
2495 * so that it is present only when truly compiling with GNU C. */
2496 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2498 if (CPP_OPTIONS(pfile)->debug_output)
2500 char directive[2048];
2501 struct directive *dp = &directive_table[0];
2502 struct tm *timebuf = timestamp(pfile);
2503 cpp_buffer *pbuffer = CPP_BUFFER(pfile);
2505 while (CPP_PREV_BUFFER(pbuffer))
2506 pbuffer = CPP_PREV_BUFFER(pbuffer);
2507 sprintf(directive, " __BASE_FILE__ \"%s\"\n", pbuffer->nominal_fname);
2508 output_line_command(pfile, 0, same_file);
2509 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2512 sprintf(directive, " __VERSION__ \"%s\"\n", version_string);
2513 output_line_command(pfile, 0, same_file);
2514 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2517 #ifndef NO_BUILTIN_SIZE_TYPE
2518 sprintf(directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
2519 output_line_command(pfile, 0, same_file);
2520 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2524 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2525 sprintf(directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
2526 output_line_command(pfile, 0, same_file);
2527 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2531 sprintf(directive, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE(pfile));
2532 output_line_command(pfile, 0, same_file);
2533 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2536 sprintf(directive, " __DATE__ \"%s %2d %4d\"\n",
2537 monthnames[timebuf->tm_mon],
2538 timebuf->tm_mday, timebuf->tm_year + 1900);
2539 output_line_command(pfile, 0, same_file);
2540 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2543 sprintf(directive, " __TIME__ \"%02d:%02d:%02d\"\n",
2544 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
2545 output_line_command(pfile, 0, same_file);
2546 pass_thru_directive(directive, &directive[strlen(directive)], pfile,
2549 sprintf(directive, " __STDC__ 1");
2550 output_line_command(pfile, 0, same_file);
2551 pass_thru_directive(directive, &directive[strlen(directive)],
2556 /* Return 1 iff a token ending in C1 followed directly by a token C2
2557 * could cause mis-tokenization. */
2560 unsafe_chars(int c1, int c2)
2562 // printf("unsafe %c %c ...", c1, c2);
2568 // printf(" no0\n");
2582 if (c2 == '-' || c2 == '+')
2584 // printf(" yes2\n");
2585 return 1; /* could extend a pre-processing number */
2589 if (c2 == '\'' || c2 == '\"')
2591 // printf(" yes3\n");
2592 return 1; /* Could turn into L"xxx" or L'xxx'. */
2646 /* We're in the middle of either a name or a pre-processing number. */
2647 if (is_idchar[c2] || c2 == '.')
2649 // printf(" yes4 %i %i\n", is_idchar[c2], c2 == '.');
2654 // printf(" no5\n");
2669 if (c2 == c1 || c2 == '=')
2671 // printf(" yes6\n");
2676 // printf(" no7\n");
2680 // printf(" no8\n");
2684 /* Expand a macro call.
2685 * HP points to the symbol that is the macro being called.
2686 * Put the result of expansion onto the input stack
2687 * so that subsequent input by our caller will use it.
2689 * If macro wants arguments, caller has already verified that
2690 * an argument list follows; arguments come from the input stack. */
2693 macroexpand(cpp_reader * pfile, HASHNODE * hp)
2696 DEFINITION *defn = hp->value.defn;
2697 unsigned char *xbuf;
2698 long start_line, start_column;
2700 struct argdata *args;
2701 long old_written = CPP_WRITTEN(pfile);
2703 int rest_args, rest_zero;
2706 pfile->output_escapes++;
2707 cpp_buf_line_and_col(cpp_file_buffer(pfile), &start_line, &start_column);
2711 nargs = defn->nargs;
2715 enum cpp_token token;
2719 args = (struct argdata *)alloca((nargs + 1) * sizeof(struct argdata));
2721 for (i = 0; i < nargs; i++)
2723 args[i].raw = args[i].expanded = 0;
2724 args[i].raw_length = 0;
2725 args[i].expand_length = args[i].stringified_length = -1;
2726 args[i].use_count = 0;
2729 /* Parse all the macro args that are supplied. I counts them.
2730 * The first NARGS args are stored in ARGS.
2731 * The rest are discarded. If rest_args is set then we assume
2732 * macarg absorbed the rest of the args. */
2736 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2741 if (i < nargs || (nargs == 0 && i == 0))
2745 /* if we are working on last arg which absorbs rest of args... */
2746 if (i == nargs - 1 && defn->rest_args)
2748 args[i].raw = CPP_WRITTEN(pfile);
2749 token = macarg(pfile, rest_args);
2750 args[i].raw_length = CPP_WRITTEN(pfile) - args[i].raw;
2751 args[i].newlines = 0; /* FIXME */
2752 bp = ARG_BASE + args[i].raw;
2753 while (is_space[(unsigned char)(*bp)]) { bp++; }
2754 args[i].raw_length -= bp - (ARG_BASE + args[i].raw);
2755 args[i].raw = bp - ARG_BASE;
2756 if (args[i].raw_length > 0)
2758 bp = ARG_BASE + args[i].raw + args[i].raw_length - 1;
2759 while (is_space[(unsigned char)(*bp)])
2762 args[i].raw_length--;
2763 if (args[i].raw_length < 1) break;
2768 token = macarg(pfile, 0);
2769 if (token == CPP_EOF || token == CPP_POP)
2771 cpp_error_with_line(pfile, start_line, start_column,
2772 "unterminated macro call");
2777 while (token == CPP_COMMA);
2779 /* If we got one arg but it was just whitespace, call that 0 args. */
2782 unsigned char *bp = ARG_BASE + args[0].raw;
2783 unsigned char *lim = bp + args[0].raw_length;
2785 /* cpp.texi says for foo ( ) we provide one argument.
2786 * However, if foo wants just 0 arguments, treat this as 0. */
2788 while (bp != lim && is_space[*bp])
2793 /* Don't output an error message if we have already output one for
2794 * a parse error above. */
2795 if (nargs == 0 && i > 0)
2797 cpp_error(pfile, "arguments given to macro `%s'", hp->name);
2801 if (i == nargs - 1 && defn->rest_args)
2804 cpp_error(pfile, "macro `%s' used without args", hp->name);
2806 cpp_error(pfile, "macro `%s' used with just one arg", hp->name);
2808 cpp_error(pfile, "macro `%s' used with only %d args",
2814 "macro `%s' used with too many (%d) args", hp->name, i);
2817 /* If macro wants zero args, we parsed the arglist for checking only.
2818 * Read directly from the macro definition. */
2821 xbuf = defn->expansion;
2822 xbuf_len = defn->length;
2826 unsigned char *exp = defn->expansion;
2827 int offset; /* offset in expansion,
2828 * copied a piece at a time */
2829 int totlen; /* total amount of exp buffer filled so far */
2830 reflist *ap, *last_ap;
2832 /* Macro really takes args. Compute the expansion of this call. */
2834 /* Compute length in characters of the macro's expansion.
2835 * Also count number of times each arg is used. */
2836 xbuf_len = defn->length;
2837 for (ap = defn->pattern; ap; ap = ap->next)
2841 struct argdata *arg = &args[ap->argno];
2843 /* Stringify it it hasn't already been */
2844 if (arg->stringified_length < 0)
2846 int arglen = arg->raw_length;
2851 /* Initially need_space is -1. Otherwise, 1 means the
2852 * previous character was a space, but we suppressed it;
2853 * 0 means the previous character was a non-space. */
2854 int need_space = -1;
2857 arg->stringified = CPP_WRITTEN(pfile);
2858 CPP_PUTC(pfile, '\"'); /* insert beginning quote */
2859 for (; i < arglen; i++)
2861 c = (ARG_BASE + arg->raw)[i];
2865 /* Internal sequences of whitespace are replaced by
2866 * one space except within an string or char token. */
2869 if (CPP_WRITTEN(pfile) > arg->stringified
2870 && (CPP_PWRITTEN(pfile))[-1] == '@')
2872 /* "@ " escape markers are removed */
2873 CPP_ADJUST_WRITTEN(pfile, -1);
2876 if (need_space == 0)
2880 else if (need_space > 0)
2881 CPP_PUTC(pfile, ' ');
2895 else if (c == '\"' || c == '\'')
2899 /* Escape these chars */
2900 if (c == '\"' || (in_string && c == '\\'))
2901 CPP_PUTC(pfile, '\\');
2906 CPP_RESERVE(pfile, 4);
2907 sprintf((char *)CPP_PWRITTEN(pfile), "\\%03o",
2909 CPP_ADJUST_WRITTEN(pfile, 4);
2912 CPP_PUTC(pfile, '\"'); /* insert ending quote */
2913 arg->stringified_length
2914 = CPP_WRITTEN(pfile) - arg->stringified;
2916 xbuf_len += args[ap->argno].stringified_length;
2918 else if (ap->raw_before || ap->raw_after)
2920 /* Add 4 for two newline-space markers to prevent
2921 * token concatenation. */
2922 xbuf_len += args[ap->argno].raw_length + 4;
2926 /* We have an ordinary (expanded) occurrence of the arg.
2927 * So compute its expansion, if we have not already. */
2928 if (args[ap->argno].expand_length < 0)
2930 args[ap->argno].expanded = CPP_WRITTEN(pfile);
2931 cpp_expand_to_buffer(pfile,
2932 ARG_BASE + args[ap->argno].raw,
2933 args[ap->argno].raw_length);
2935 args[ap->argno].expand_length
2936 = CPP_WRITTEN(pfile) - args[ap->argno].expanded;
2938 /* Add 4 for two newline-space markers to prevent
2939 * token concatenation. */
2940 xbuf_len += args[ap->argno].expand_length + 4;
2942 if (args[ap->argno].use_count < 10)
2943 args[ap->argno].use_count++;
2946 xbuf = (unsigned char *)xmalloc(xbuf_len + 1);
2948 /* Generate in XBUF the complete expansion
2949 * with arguments substituted in.
2950 * TOTLEN is the total size generated so far.
2951 * OFFSET is the index in the definition
2952 * of where we are copying from. */
2953 offset = totlen = 0;
2954 for (last_ap = NULL, ap = defn->pattern; ap;
2955 last_ap = ap, ap = ap->next)
2957 struct argdata *arg = &args[ap->argno];
2958 int count_before = totlen;
2960 /* Add chars to XBUF. */
2961 for (i = 0; i < ap->nchars; i++, offset++)
2962 xbuf[totlen++] = exp[offset];
2964 /* If followed by an empty rest arg with concatenation,
2965 * delete the last run of nonwhite chars. */
2966 if (rest_zero && totlen > count_before
2967 && ((ap->rest_args && ap->raw_before)
2968 || (last_ap && last_ap->rest_args && last_ap->raw_after)))
2970 /* Delete final whitespace. */
2971 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2974 /* Delete the nonwhites before them. */
2975 while (totlen > count_before && !is_space[xbuf[totlen - 1]])
2978 if (ap->stringify != 0)
2980 memcpy(xbuf + totlen, ARG_BASE + arg->stringified,
2981 arg->stringified_length);
2982 totlen += arg->stringified_length;
2984 else if (ap->raw_before || ap->raw_after)
2986 unsigned char *p1 = ARG_BASE + arg->raw;
2987 unsigned char *l1 = p1 + arg->raw_length;
2991 while (p1 != l1 && is_space[*p1])
2993 while (p1 != l1 && is_idchar[*p1])
2994 xbuf[totlen++] = *p1++;
2998 /* Arg is concatenated after: delete trailing whitespace,
2999 * whitespace markers, and no-reexpansion markers. */
3002 if (is_space[l1[-1]])
3004 else if (l1[-1] == '-')
3006 unsigned char *p2 = l1 - 1;
3008 /* If a `-' is preceded by an odd number of newlines then it
3009 * and the last newline are a no-reexpansion marker. */
3010 while (p2 != p1 && p2[-1] == '\n')
3012 if ((l1 - 1 - p2) & 1)
3023 memcpy(xbuf + totlen, p1, l1 - p1);
3028 unsigned char *expanded = ARG_BASE + arg->expanded;
3030 if (!ap->raw_before && totlen > 0 && arg->expand_length
3031 && unsafe_chars(xbuf[totlen - 1], expanded[0]))
3033 xbuf[totlen++] = '@';
3034 xbuf[totlen++] = ' ';
3036 memcpy(xbuf + totlen, expanded, arg->expand_length);
3037 totlen += arg->expand_length;
3039 if (!ap->raw_after && totlen > 0 && offset < defn->length
3040 && unsafe_chars(xbuf[totlen - 1], exp[offset]))
3042 xbuf[totlen++] = '@';
3043 xbuf[totlen++] = ' ';
3045 /* If a macro argument with newlines is used multiple times,
3046 * then only expand the newlines once. This avoids creating
3047 * output lines which don't correspond to any input line,
3048 * which confuses gdb and gcov. */
3049 if (arg->use_count > 1 && arg->newlines > 0)
3051 /* Don't bother doing change_newlines for subsequent
3055 = change_newlines(expanded, arg->expand_length);
3059 if (totlen > xbuf_len)
3062 /* if there is anything left of the definition
3063 * after handling the arg list, copy that in too. */
3065 for (i = offset; i < defn->length; i++)
3067 /* if we've reached the end of the macro */
3070 if (!(rest_zero && last_ap && last_ap->rest_args
3071 && last_ap->raw_after))
3072 xbuf[totlen++] = exp[i];
3079 pfile->output_escapes--;
3081 /* Now put the expansion on the input stack
3082 * so our caller will commence reading from it. */
3083 push_macro_expansion(pfile, xbuf, xbuf_len, hp);
3084 CPP_BUFFER(pfile)->has_escapes = 1;
3086 /* Pop the space we've used in the token_buffer for argument expansion. */
3087 CPP_SET_WRITTEN(pfile, old_written);
3089 /* Recursive macro use sometimes works traditionally.
3090 * #define foo(x,y) bar (x (y,0), y)
3093 hp->type = T_DISABLED;
3097 push_macro_expansion(cpp_reader * pfile, unsigned char *xbuf, int xbuf_len,
3100 cpp_buffer *mbuf = cpp_push_buffer(pfile, xbuf, xbuf_len);
3102 mbuf->cleanup = macro_cleanup;
3105 /* The first chars of the expansion should be a "@ " added by
3106 * collect_expansion. This is to prevent accidental token-pasting
3107 * between the text preceding the macro invocation, and the macro
3110 * We would like to avoid adding unneeded spaces (for the sake of
3111 * tools that use cpp, such as imake). In some common cases we can
3112 * tell that it is safe to omit the space.
3114 * The character before the macro invocation cannot have been an
3115 * idchar (or else it would have been pasted with the idchars of
3116 * the macro name). Therefore, if the first non-space character
3117 * of the expansion is an idchar, we do not need the extra space
3118 * to prevent token pasting.
3120 * Also, we don't need the extra space if the first char is '(',
3121 * or some other (less common) characters. */
3123 if (xbuf[0] == '@' && xbuf[1] == ' '
3124 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3125 || xbuf[2] == '\"'))
3129 /* Like cpp_get_token, except that it does not read past end-of-line.
3130 * Also, horizontal space is skipped, and macros are popped. */
3132 static enum cpp_token
3133 get_directive_token(cpp_reader * pfile)
3137 long old_written = CPP_WRITTEN(pfile);
3138 enum cpp_token token;
3140 cpp_skip_hspace(pfile);
3141 if (PEEKC() == '\n')
3143 token = cpp_get_token(pfile);
3147 if (!CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
3149 /* ... else fall though ... */
3152 CPP_SET_WRITTEN(pfile, old_written);
3160 /* Handle #include and #import.
3161 * This function expects to see "fname" or <fname> on the input.
3163 * The input is normally in part of the output_buffer following
3164 * CPP_WRITTEN, and will get overwritten by output_line_command.
3165 * I.e. in input file specification has been popped by handle_directive.
3169 do_include(cpp_reader * pfile, struct directive *keyword,
3170 unsigned char *unused1 __UNUSED__, unsigned char *unused2 __UNUSED__)
3172 int importing = (keyword->type == T_IMPORT);
3173 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3174 char *fname; /* Dynamically allocated fname buffer */
3175 unsigned char *fbeg, *fend; /* Beginning and end of fname */
3176 enum cpp_token token;
3178 /* Chain of dirs to search */
3179 file_name_list *search_start = CPP_OPTIONS(pfile)->include;
3180 file_name_list dsp[1]; /* First in chain, if #include "..." */
3181 file_name_list *searchptr = 0;
3182 long old_written = CPP_WRITTEN(pfile);
3184 int f; /* file number */
3185 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3187 f = -1; /* JF we iz paranoid! */
3189 if (importing && CPP_OPTIONS(pfile)->warn_import
3190 && !CPP_OPTIONS(pfile)->inhibit_warnings
3191 && !CPP_BUFFER(pfile)->system_header_p && !pfile->import_warning)
3193 pfile->import_warning = 1;
3194 cpp_warning(pfile, "using `#import' is not recommended");
3196 "The fact that a certain header file need not be processed more than once\n");
3198 "should be indicated in the header file, not where it is used.\n");
3200 "The best way to do this is with a conditional of this form:\n\n");
3201 fprintf(stderr, " #ifndef _FOO_H_INCLUDED\n");
3202 fprintf(stderr, " #define _FOO_H_INCLUDED\n");
3203 fprintf(stderr, " ... <real contents of file> ...\n");
3204 fprintf(stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3205 fprintf(stderr, "Then users can use `#include' any number of times.\n");
3207 "GNU C automatically avoids processing the file more than once\n");
3208 fprintf(stderr, "when it is equipped with such a conditional.\n");
3210 pfile->parsing_include_directive++;
3211 token = get_directive_token(pfile);
3212 pfile->parsing_include_directive--;
3214 if (token == CPP_STRING)
3216 /* FIXME - check no trailing garbage */
3217 fbeg = pfile->token_buffer + old_written + 1;
3218 fend = CPP_PWRITTEN(pfile) - 1;
3219 if (fbeg[-1] == '<')
3222 /* If -I-, start with the first -I dir after the -I-. */
3223 if (CPP_OPTIONS(pfile)->first_bracket_include)
3224 search_start = CPP_OPTIONS(pfile)->first_bracket_include;
3226 /* If -I- was specified, don't search current dir, only spec'd ones. */
3227 else if (!CPP_OPTIONS(pfile)->ignore_srcdir)
3231 /* We have "filename". Figure out directory this source
3232 * file is coming from and put it on the front of the list. */
3234 for (fp = CPP_BUFFER(pfile); fp; fp = CPP_PREV_BUFFER(fp))
3237 const char *ep, *nam;
3239 if ((nam = fp->nominal_fname))
3241 /* Found a named file. Figure out dir of the file,
3242 * and put it in front of the search list. */
3243 dsp[0].next = search_start;
3246 ep = strrchr(nam, '/');
3248 ep = strrchr(nam, ']');
3250 ep = strrchr(nam, '>');
3252 ep = strrchr(nam, ':');
3259 dsp[0].fname = (char *)alloca(n + 1);
3260 strncpy(dsp[0].fname, nam, n);
3261 dsp[0].fname[n] = '\0';
3262 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3263 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3267 dsp[0].fname = 0; /* Current directory */
3269 dsp[0].got_name_map = 0;
3278 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3279 CPP_SET_WRITTEN(pfile, old_written);
3280 skip_rest_of_line(pfile);
3286 token = get_directive_token(pfile);
3287 if (token != CPP_VSPACE)
3289 cpp_error(pfile, "junk at end of `#include'");
3290 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3291 token = get_directive_token(pfile);
3293 /* For #include_next, skip in the search path
3294 * past the dir in which the containing file was found. */
3299 for (fp = CPP_BUFFER(pfile); fp; fp = CPP_PREV_BUFFER(fp))
3302 /* fp->dir is null if the containing file was specified with
3303 * an absolute file name. In that case, don't skip anything. */
3304 if (fp->dir == SELF_DIR_DUMMY)
3305 search_start = CPP_OPTIONS(pfile)->include;
3307 search_start = fp->dir->next;
3311 CPP_SET_WRITTEN(pfile, old_written);
3317 cpp_error(pfile, "empty file name in `#%s'", keyword->name);
3320 /* Allocate this permanently, because it gets stored in the definitions
3322 fname = (char *)xmalloc(pfile->max_include_len + flen + 4);
3323 /* + 2 above for slash and terminating null. */
3324 /* + 2 added for '.h' on VMS (to support '#include filename') */
3326 /* If specified file name is absolute, just open it. */
3334 strncpy(fname, (const char *)fbeg, flen);
3336 if (redundant_include_p(pfile, fname))
3339 f = lookup_import(pfile, fname, NULL);
3341 f = open_include_file(pfile, fname, NULL);
3343 return 0; /* Already included this file */
3347 /* Search directory path, trying to open the file.
3348 * Copy each filename tried into FNAME. */
3350 for (searchptr = search_start; searchptr; searchptr = searchptr->next)
3352 if (searchptr->fname)
3354 /* The empty string in a search path is ignored.
3355 * This makes it possible to turn off entirely
3356 * a standard piece of the list. */
3357 if (searchptr->fname[0] == 0)
3359 strcpy(fname, searchptr->fname);
3361 fname[strlen(fname) + flen] = 0;
3367 strncat(fname, (const char *)fbeg, flen);
3369 /* Change this 1/2 Unix 1/2 VMS file specification into a
3370 * full VMS file specification */
3371 if (searchptr->fname && (searchptr->fname[0] != 0))
3373 /* Fix up the filename */
3374 hack_vms_include_specification(fname);
3378 /* This is a normal VMS filespec, so use it unchanged. */
3379 strncpy(fname, fbeg, flen);
3381 /* if it's '#include filename', add the missing .h */
3382 if (!strchr(fname, '.'))
3384 strcat(fname, ".h");
3388 /* ??? There are currently 3 separate mechanisms for avoiding processing
3389 * of redundant include files: #import, #pragma once, and
3390 * redundant_include_p. It would be nice if they were unified. */
3391 if (redundant_include_p(pfile, fname))
3394 f = lookup_import(pfile, fname, searchptr);
3396 f = open_include_file(pfile, fname, searchptr);
3398 return 0; /* Already included this file */
3400 else if (f == -1 && errno == EACCES)
3401 cpp_warning(pfile, "Header file %s exists, but is not readable",
3411 /* A file that was not found. */
3412 strncpy(fname, (const char *)fbeg, flen);
3414 /* If generating dependencies and -MG was specified, we assume missing
3415 * files are leaf files, living in the same directory as the source file
3416 * or other similar place; these missing files may be generated from
3417 * other files and may not exist yet (eg: y.tab.h). */
3419 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3420 && CPP_PRINT_DEPS(pfile)
3421 > (angle_brackets || (pfile->system_include_depth > 0)))
3423 /* If it was requested as a system header file,
3424 * then assume it belongs in the first place to look for such. */
3427 for (searchptr = search_start; searchptr;
3428 searchptr = searchptr->next)
3430 if (searchptr->fname)
3434 if (searchptr->fname[0] == 0)
3436 p = (char *)alloca(strlen(searchptr->fname)
3437 + strlen(fname) + 2);
3438 strcpy(p, searchptr->fname);
3441 deps_output(pfile, p, ' ');
3448 /* Otherwise, omit the directory, as if the file existed
3449 * in the directory with the source. */
3450 deps_output(pfile, fname, ' ');
3453 /* If -M was specified, and this header file won't be added to the
3454 * dependency list, then don't count this as an error, because we can
3455 * still produce correct output. Otherwise, we can't produce correct
3456 * output, because there may be dependencies we need inside the missing
3457 * file, and we don't know what directory this missing file exists in. */
3458 else if (CPP_PRINT_DEPS(pfile)
3459 && (CPP_PRINT_DEPS(pfile)
3460 <= (angle_brackets || (pfile->system_include_depth > 0))))
3461 cpp_warning(pfile, "No include path in which to find %s", fname);
3462 else if (search_start)
3463 cpp_error_from_errno(pfile, fname);
3465 cpp_error(pfile, "No include path in which to find %s", fname);
3469 /* Check to see if this include file is a once-only include file.
3470 * If so, give up. */
3472 file_name_list *ptr;
3474 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next)
3476 if (!strcmp(ptr->fname, fname))
3479 return 0; /* This file was once'd. */
3483 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next)
3485 if (!strcmp(ptr->fname, fname))
3486 break; /* This file was included before. */
3491 /* This is the first time for this file. */
3492 /* Add it to list of files included. */
3494 ptr = (file_name_list *) xmalloc(sizeof(file_name_list));
3496 ptr->control_macro = 0;
3497 ptr->c_system_include_path = 0;
3498 ptr->next = pfile->all_include_files;
3499 pfile->all_include_files = ptr;
3500 ptr->fname = savestring(fname);
3501 ptr->got_name_map = 0;
3503 /* For -M, add this file to the dependencies. */
3504 if (CPP_PRINT_DEPS(pfile)
3505 > (angle_brackets || (pfile->system_include_depth > 0)))
3506 deps_output(pfile, fname, ' ');
3508 /* Handle -H option. */
3509 if (CPP_OPTIONS(pfile)->print_include_names)
3511 cpp_buffer *buf = CPP_BUFFER(pfile);
3513 while ((buf = CPP_PREV_BUFFER(buf)))
3515 fprintf(stderr, "%s\n", fname);
3518 pfile->system_include_depth++;
3520 /* Actually process the file. */
3522 /* Record file on "seen" list for #import. */
3523 add_import(pfile, f, fname);
3525 /* Actually process the file */
3526 cpp_push_buffer(pfile, NULL, 0);
3527 if (finclude(pfile, f, fname, is_system_include(pfile, fname),
3528 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3530 output_line_command(pfile, 0, enter_file);
3531 pfile->only_seen_white = 2;
3534 pfile->system_include_depth--;
3539 /* Return nonzero if there is no need to include file NAME
3540 * because it has already been included and it contains a conditional
3541 * to make a repeated include do nothing. */
3544 redundant_include_p(cpp_reader * pfile, char *name)
3546 file_name_list *l = pfile->all_include_files;
3548 for (; l; l = l->next)
3549 if (!strcmp(name, l->fname)
3551 && cpp_lookup((const char *)l->control_macro, -1, -1))
3556 /* Return nonzero if the given FILENAME is an absolute pathname which
3557 * designates a file within one of the known "system" include file
3558 * directories. We assume here that if the given FILENAME looks like
3559 * it is the name of a file which resides either directly in a "system"
3560 * include file directory, or within any subdirectory thereof, then the
3561 * given file must be a "system" include file. This function tells us
3562 * if we should suppress pedantic errors/warnings for the given FILENAME.
3564 * The value is 2 if the file is a C-language system header file
3565 * for which C++ should (on most systems) assume `extern "C"'. */
3568 is_system_include(cpp_reader * pfile, char *filename)
3570 file_name_list *searchptr;
3572 for (searchptr = CPP_OPTIONS(pfile)->first_system_include; searchptr;
3573 searchptr = searchptr->next)
3574 if (searchptr->fname)
3576 char *sys_dir = searchptr->fname;
3577 unsigned length = strlen(sys_dir);
3579 if (!strncmp(sys_dir, filename, length) && filename[length] == '/')
3581 if (searchptr->c_system_include_path)
3591 * Install a name in the assertion hash table.
3593 * If LEN is >= 0, it is the length of the name.
3594 * Otherwise, compute the length by scanning the entire name.
3596 * If HASH is >= 0, it is the precomputed hash code.
3597 * Otherwise, compute the hash code.
3599 static ASSERTION_HASHNODE *
3600 assertion_install(cpp_reader * pfile, const char *name, int len, int hash)
3602 ASSERTION_HASHNODE *hp;
3605 i = sizeof(ASSERTION_HASHNODE) + len + 1;
3606 hp = (ASSERTION_HASHNODE *) xmalloc(i);
3608 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3609 hp->next = pfile->assertion_hashtab[bucket];
3610 pfile->assertion_hashtab[bucket] = hp;
3613 hp->next->prev = hp;
3616 hp->name = ((char *)hp) + sizeof(ASSERTION_HASHNODE);
3617 memcpy(hp->name, name, len);
3622 * find the most recent hash node for name name (ending with first
3623 * non-identifier char) installed by install
3625 * If LEN is >= 0, it is the length of the name.
3626 * Otherwise, compute the length by scanning the entire name.
3628 * If HASH is >= 0, it is the precomputed hash code.
3629 * Otherwise, compute the hash code.
3632 static ASSERTION_HASHNODE *
3633 assertion_lookup(cpp_reader * pfile, const char *name, int len, int hash)
3635 ASSERTION_HASHNODE *bucket;
3637 bucket = pfile->assertion_hashtab[hash];
3640 if (bucket->length == len && strncmp(bucket->name, name, len) == 0)
3642 bucket = bucket->next;
3648 delete_assertion(ASSERTION_HASHNODE * hp)
3650 struct tokenlist_list *tail;
3653 hp->prev->next = hp->next;
3655 hp->next->prev = hp->prev;
3657 for (tail = hp->value; tail;)
3659 struct tokenlist_list *next = tail->next;
3661 free_token_list(tail->tokens);
3666 /* make sure that the bucket chain header that
3667 * the deleted guy was on points to the right thing afterwards. */
3668 if (hp == *hp->bucket_hdr)
3669 *hp->bucket_hdr = hp->next;
3674 /* Convert a character string literal into a nul-terminated string.
3675 * The input string is [IN ... LIMIT).
3676 * The result is placed in RESULT. RESULT can be the same as IN.
3677 * The value returned in the end of the string written to RESULT,
3678 * or NULL on error. */
3681 convert_string(cpp_reader * pfile, char *result, char *in, char *limit,
3705 (unsigned char)cpp_parse_escape(pfile, &bpc);
3709 *result++ = (unsigned char)c;
3712 /* else fall through */
3722 * interpret #line command. Remembers previously seen fnames
3723 * in its very own hash table.
3725 #define FNAME_HASHSIZE 37
3728 do_line(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3729 unsigned char *unused1 __UNUSED__, unsigned char *unused2 __UNUSED__)
3731 cpp_buffer *ip = CPP_BUFFER(pfile);
3733 long old_written = CPP_WRITTEN(pfile);
3734 enum file_change_code file_change = same_file;
3735 enum cpp_token token;
3737 token = get_directive_token(pfile);
3739 if (token != CPP_NUMBER || !isdigit(pfile->token_buffer[old_written]))
3741 cpp_error(pfile, "invalid format `#line' command");
3742 goto bad_line_directive;
3744 /* The Newline at the end of this line remains to be processed.
3745 * To put the next line at the specified line number,
3746 * we must store a line number now that is one less. */
3747 new_lineno = atoi((char *)(pfile->token_buffer + old_written)) - 1;
3748 CPP_SET_WRITTEN(pfile, old_written);
3750 /* NEW_LINENO is one less than the actual line number here. */
3751 if (CPP_PEDANTIC(pfile) && new_lineno < 0)
3752 cpp_pedwarn(pfile, "line number out of range in `#line' command");
3754 token = get_directive_token(pfile);
3756 if (token == CPP_STRING)
3758 char *fname = (char *)pfile->token_buffer + old_written;
3760 static HASHNODE *fname_table[FNAME_HASHSIZE];
3761 HASHNODE *hp, **hash_bucket;
3766 /* Turn the file name, which is a character string literal,
3767 * into a null-terminated string. Do this in place. */
3769 convert_string(pfile, fname, fname, (char *)CPP_PWRITTEN(pfile), 1);
3772 cpp_error(pfile, "invalid format `#line' command");
3773 goto bad_line_directive;
3775 fname_length = end_name - fname;
3777 num_start = CPP_WRITTEN(pfile);
3778 token = get_directive_token(pfile);
3779 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3781 p = pfile->token_buffer + num_start;
3782 if (CPP_PEDANTIC(pfile))
3783 cpp_pedwarn(pfile, "garbage at end of `#line' command");
3785 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3787 cpp_error(pfile, "invalid format `#line' command");
3788 goto bad_line_directive;
3791 file_change = enter_file;
3793 file_change = leave_file;
3795 ip->system_header_p = 1;
3796 else /* if (*p == 4) */
3797 ip->system_header_p = 2;
3799 CPP_SET_WRITTEN(pfile, num_start);
3800 token = get_directive_token(pfile);
3801 p = pfile->token_buffer + num_start;
3802 if (token == CPP_NUMBER && p[1] == '\0'
3803 && (*p == '3' || *p == '4'))
3805 ip->system_header_p = *p == 3 ? 1 : 2;
3806 token = get_directive_token(pfile);
3808 if (token != CPP_VSPACE)
3810 cpp_error(pfile, "invalid format `#line' command");
3811 goto bad_line_directive;
3814 hash_bucket = &fname_table[hashf(fname, fname_length, FNAME_HASHSIZE)];
3815 for (hp = *hash_bucket; hp; hp = hp->next)
3816 if (hp->length == fname_length &&
3817 strncmp(hp->value.cpval, fname, fname_length) == 0)
3819 ip->nominal_fname = hp->value.cpval;
3824 /* Didn't find it; cons up a new one. */
3825 hp = (HASHNODE *) xcalloc(1, sizeof(HASHNODE) + fname_length + 1);
3826 hp->next = *hash_bucket;
3829 hp->length = fname_length;
3830 ip->nominal_fname = hp->value.cpval =
3831 ((char *)hp) + sizeof(HASHNODE);
3832 memcpy(hp->value.cpval, fname, fname_length);
3835 else if (token != CPP_VSPACE && token != CPP_EOF)
3837 cpp_error(pfile, "invalid format `#line' command");
3838 goto bad_line_directive;
3840 ip->lineno = new_lineno;
3842 skip_rest_of_line(pfile);
3843 CPP_SET_WRITTEN(pfile, old_written);
3844 output_line_command(pfile, 0, file_change);
3849 * remove the definition of a symbol from the symbol table.
3850 * according to un*x /lib/cpp, it is not an error to undef
3851 * something that has no definitions, so it isn't one here either.
3855 do_undef(cpp_reader * pfile, struct directive *keyword, unsigned char *buf,
3856 unsigned char *limit)
3860 unsigned char *orig_buf = buf;
3862 SKIP_WHITE_SPACE(buf);
3863 sym_length = check_macro_name(pfile, buf, "macro");
3865 while ((hp = cpp_lookup((const char *)buf, sym_length, -1)))
3867 /* If we are generating additional info for debugging (with -g) we
3868 * need to pass through all effective #undef commands. */
3869 if (CPP_OPTIONS(pfile)->debug_output && keyword)
3870 pass_thru_directive((char *)orig_buf, (char *)limit, pfile, keyword);
3871 if (hp->type != T_MACRO)
3872 cpp_warning(pfile, "undefining `%s'", hp->name);
3876 if (CPP_PEDANTIC(pfile))
3879 SKIP_WHITE_SPACE(buf);
3881 cpp_pedwarn(pfile, "garbage after `#undef' directive");
3887 * Report an error detected by the program we are processing.
3888 * Use the text of the line in the error message.
3889 * (We use error because it prints the filename & line#.)
3893 do_error(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3894 unsigned char *buf, unsigned char *limit)
3896 int length = limit - buf;
3897 unsigned char *copy = (unsigned char *)xmalloc(length + 1);
3899 memcpy(copy, buf, length);
3901 SKIP_WHITE_SPACE(copy);
3902 cpp_error(pfile, "#error %s", copy);
3907 * Report a warning detected by the program we are processing.
3908 * Use the text of the line in the warning message, then continue.
3909 * (We use error because it prints the filename & line#.)
3913 do_warning(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3914 unsigned char *buf, unsigned char *limit)
3916 int length = limit - buf;
3917 unsigned char *copy = (unsigned char *)xmalloc(length + 1);
3919 memcpy(copy, buf, length);
3921 SKIP_WHITE_SPACE(copy);
3922 cpp_warning(pfile, "#warning %s", copy);
3926 /* Remember the name of the current file being read from so that we can
3927 * avoid ever including it again. */
3930 do_once(cpp_reader * pfile)
3932 cpp_buffer *ip = NULL;
3933 file_name_list *new_;
3935 for (ip = CPP_BUFFER(pfile);; ip = CPP_PREV_BUFFER(ip))
3943 new_ = (file_name_list *) xmalloc(sizeof(file_name_list));
3945 new_->next = pfile->dont_repeat_files;
3946 pfile->dont_repeat_files = new_;
3947 new_->fname = savestring(ip->fname);
3948 new_->control_macro = 0;
3949 new_->got_name_map = 0;
3950 new_->c_system_include_path = 0;
3955 /* #ident has already been copied to the output file, so just ignore it. */
3958 do_ident(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3959 unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
3961 /* long old_written = CPP_WRITTEN (pfile); */
3963 /* Allow #ident in system headers, since that's not user's fault. */
3964 if (CPP_PEDANTIC(pfile) && !CPP_BUFFER(pfile)->system_header_p)
3965 cpp_pedwarn(pfile, "ANSI C does not allow `#ident'");
3967 /* Leave rest of line to be read by later calls to cpp_get_token. */
3972 /* #pragma and its argument line have already been copied to the output file.
3973 * Just check for some recognized pragmas that need validation here. */
3976 do_pragma(cpp_reader * pfile, struct directive *keyword __UNUSED__,
3977 unsigned char *buf, unsigned char *limit __UNUSED__)
3979 while (*buf == ' ' || *buf == '\t')
3982 if (!strncmp((const char *)buf, "once", 4))
3984 /* Allow #pragma once in system headers, since that's not the user's
3986 if (!CPP_BUFFER(pfile)->system_header_p)
3987 cpp_warning(pfile, "`#pragma once' is obsolete");
3990 if (!strncmp((const char *)buf, "implementation", 14))
3992 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3993 * been included yet. */
3994 file_name_list *ptr;
3995 char *p = (char *)buf + 14, *fname, *inc_fname;
3998 SKIP_WHITE_SPACE(p);
3999 if (*p == '\n' || *p != '\"')
4003 p = strchr(fname, '\"');
4004 fname_len = (p) ? (int)(p - fname) : (int)strlen(fname);
4006 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next)
4008 inc_fname = strrchr(ptr->fname, '/');
4009 inc_fname = inc_fname ? inc_fname + 1 : (char *)ptr->fname;
4010 if (inc_fname && !strncmp(inc_fname, fname, fname_len))
4012 "`#pragma implementation' for `%s' appears after file is included",
4019 /* Just ignore #sccs, on systems where we define it at all. */
4022 * handle #if command by
4023 * 1) inserting special `defined' keyword into the hash table
4024 * that gets turned into 0 or 1 by special_symbol (thus,
4025 * if the luser has a symbol called `defined' already, it won't
4026 * work inside the #if command)
4027 * 2) rescan the input into a temporary output buffer
4028 * 3) pass the output buffer to the yacc parser and collect a value
4029 * 4) clean up the mess left from steps 1 and 2.
4030 * 5) call conditional_skip to skip til the next #endif (etc.),
4031 * or not, depending on the value from step 3.
4035 do_if(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4036 unsigned char *buf, unsigned char *limit)
4038 HOST_WIDE_INT value = eval_if_expression(pfile, buf, limit - buf);
4040 conditional_skip(pfile, value == 0, T_IF, NULL);
4045 * handle a #elif directive by not changing if_stack either.
4046 * see the comment above do_else.
4050 do_elif(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4051 unsigned char *buf, unsigned char *limit)
4053 if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4055 cpp_error(pfile, "`#elif' not within a conditional");
4060 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4062 cpp_error(pfile, "`#elif' after `#else'");
4063 if (pfile->if_stack->fname
4064 && CPP_BUFFER(pfile)->fname
4065 && strcmp(pfile->if_stack->fname,
4066 CPP_BUFFER(pfile)->nominal_fname) != 0)
4067 fprintf(stderr, ", file %s", pfile->if_stack->fname);
4068 fprintf(stderr, ")\n");
4070 pfile->if_stack->type = T_ELIF;
4073 if (pfile->if_stack->if_succeeded)
4074 skip_if_group(pfile, 0);
4077 HOST_WIDE_INT value = eval_if_expression(pfile, buf, limit - buf);
4080 skip_if_group(pfile, 0);
4083 ++pfile->if_stack->if_succeeded; /* continue processing input */
4084 output_line_command(pfile, 1, same_file);
4091 * evaluate a #if expression in BUF, of length LENGTH,
4092 * then parse the result as a C expression and return the value as an int.
4094 static HOST_WIDE_INT
4095 eval_if_expression(cpp_reader * pfile, unsigned char *buf __UNUSED__,
4096 int length __UNUSED__)
4098 HASHNODE *save_defined;
4099 HOST_WIDE_INT value;
4100 long old_written = CPP_WRITTEN(pfile);
4102 save_defined = install("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4103 pfile->pcp_inside_if = 1;
4105 value = cpp_parse_expr(pfile);
4106 pfile->pcp_inside_if = 0;
4107 delete_macro(save_defined); /* clean up special symbol */
4109 CPP_SET_WRITTEN(pfile, old_written); /* Pop */
4115 * routine to handle ifdef/ifndef. Try to look up the symbol,
4116 * then do or don't skip to the #endif/#else/#elif depending
4117 * on what directive is actually being processed.
4121 do_xifdef(cpp_reader * pfile, struct directive *keyword,
4122 unsigned char *unused1 __UNUSED__, unsigned char *unused2 __UNUSED__)
4125 cpp_buffer *ip = CPP_BUFFER(pfile);
4128 enum cpp_token token;
4129 int start_of_file = 0;
4130 unsigned char *control_macro = 0;
4131 int old_written = CPP_WRITTEN(pfile);
4134 /* Detect a #ifndef at start of file (not counting comments). */
4135 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4136 start_of_file = pfile->only_seen_white == 2;
4138 pfile->no_macro_expand++;
4139 token = get_directive_token(pfile);
4140 pfile->no_macro_expand--;
4142 ident = (char *)pfile->token_buffer + old_written;
4143 ident_length = CPP_WRITTEN(pfile) - old_written;
4144 CPP_SET_WRITTEN(pfile, old_written); /* Pop */
4146 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4148 skip = (keyword->type == T_IFDEF);
4149 cpp_pedwarn(pfile, "`#%s' with no argument", keyword->name);
4151 else if (token == CPP_NAME)
4153 HASHNODE *hp = cpp_lookup(ident, ident_length, -1);
4155 skip = (!hp) ^ (keyword->type == T_IFNDEF);
4156 if (start_of_file && !skip)
4158 control_macro = (unsigned char *)xmalloc(ident_length + 1);
4159 memcpy(control_macro, ident, ident_length + 1);
4164 skip = (keyword->type == T_IFDEF);
4165 cpp_error(pfile, "`#%s' with invalid argument", keyword->name);
4168 cpp_skip_hspace(pfile);
4170 if (c != EOF && c != '\n')
4171 cpp_pedwarn(pfile, "garbage at end of `#%s' argument",
4173 skip_rest_of_line(pfile);
4175 conditional_skip(pfile, skip, T_IF, control_macro);
4179 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4180 * If this is a #ifndef starting at the beginning of a file,
4181 * CONTROL_MACRO is the macro name tested by the #ifndef.
4182 * Otherwise, CONTROL_MACRO is 0. */
4185 conditional_skip(cpp_reader * pfile, int skip, enum node_type type,
4186 unsigned char *control_macro)
4188 IF_STACK_FRAME *temp;
4190 temp = (IF_STACK_FRAME *) xcalloc(1, sizeof(IF_STACK_FRAME));
4191 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4192 temp->next = pfile->if_stack;
4193 temp->control_macro = control_macro;
4194 pfile->if_stack = temp;
4196 pfile->if_stack->type = type;
4200 skip_if_group(pfile, 0);
4205 ++pfile->if_stack->if_succeeded;
4206 output_line_command(pfile, 1, same_file);
4211 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4212 * leaves input ptr at the sharp sign found.
4213 * If ANY is nonzero, return at next directive of any sort.
4217 skip_if_group(cpp_reader * pfile, int any)
4220 struct directive *kt;
4221 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4225 struct parse_marker line_start_mark;
4227 parse_set_mark(&line_start_mark, pfile);
4229 if (CPP_OPTIONS(pfile)->output_conditionals)
4231 static char failed[] = "#failed\n";
4233 CPP_PUTS(pfile, failed, sizeof(failed) - 1);
4235 output_line_command(pfile, 1, same_file);
4238 if (CPP_OPTIONS(pfile)->output_conditionals)
4240 cpp_buffer *pbuf = CPP_BUFFER(pfile);
4241 unsigned char *start_line = pbuf->buf + line_start_mark.position;
4243 CPP_PUTS(pfile, start_line, pbuf->cur - start_line);
4245 parse_move_mark(&line_start_mark, pfile);
4246 cpp_skip_hspace(pfile);
4250 int old_written = CPP_WRITTEN(pfile);
4252 cpp_skip_hspace(pfile);
4254 parse_name(pfile, GETC());
4255 ident_length = CPP_WRITTEN(pfile) - old_written;
4256 ident = (char *)pfile->token_buffer + old_written;
4257 pfile->limit = (unsigned char *)ident;
4259 for (kt = directive_table; kt->length >= 0; kt++)
4261 IF_STACK_FRAME *temp;
4263 if (ident_length == kt->length
4264 && strncmp(ident, kt->name, kt->length) == 0)
4266 /* If we are asked to return on next directive, do so now. */
4277 (IF_STACK_FRAME *) xcalloc(1, sizeof(IF_STACK_FRAME));
4278 temp->next = pfile->if_stack;
4279 pfile->if_stack = temp;
4280 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4281 temp->type = kt->type;
4285 if (CPP_PEDANTIC(pfile)
4286 && pfile->if_stack != save_if_stack)
4287 validate_else(pfile,
4289 T_ELSE ? "#else" : "#endif");
4291 if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4294 "`#%s' not within a conditional",
4298 else if (pfile->if_stack == save_if_stack)
4299 goto done; /* found what we came for */
4301 if (kt->type != T_ENDIF)
4303 if (pfile->if_stack->type == T_ELSE)
4305 "`#else' or `#elif' after `#else'");
4306 pfile->if_stack->type = kt->type;
4309 temp = pfile->if_stack;
4310 pfile->if_stack = temp->next;
4317 /* Don't let erroneous code go by. */
4318 if (kt->length < 0 && !CPP_OPTIONS(pfile)->lang_asm
4319 && CPP_PEDANTIC(pfile))
4320 cpp_pedwarn(pfile, "invalid preprocessor directive name");
4324 /* We're in the middle of a line. Skip the rest of it. */
4333 case '/': /* possible comment */
4334 c = skip_comment(pfile, NULL);
4341 old = CPP_WRITTEN(pfile);
4342 cpp_get_token(pfile);
4343 CPP_SET_WRITTEN(pfile, old);
4346 /* Char after backslash loses its special meaning. */
4347 if (PEEKC() == '\n')
4357 if (CPP_OPTIONS(pfile)->output_conditionals)
4359 static char end_failed[] = "#endfailed\n";
4361 CPP_PUTS(pfile, end_failed, sizeof(end_failed) - 1);
4364 pfile->only_seen_white = 1;
4365 parse_goto_mark(&line_start_mark, pfile);
4366 parse_clear_mark(&line_start_mark);
4370 * handle a #else directive. Do this by just continuing processing
4371 * without changing if_stack ; this is so that the error message
4372 * for missing #endif's etc. will point to the original #if. It
4373 * is possible that something different would be better.
4377 do_else(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4378 unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
4380 cpp_buffer *ip = CPP_BUFFER(pfile);
4382 if (CPP_PEDANTIC(pfile))
4383 validate_else(pfile, "#else");
4384 skip_rest_of_line(pfile);
4386 if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4388 cpp_error(pfile, "`#else' not within a conditional");
4393 /* #ifndef can't have its special treatment for containing the whole file
4394 * if it has a #else clause. */
4395 pfile->if_stack->control_macro = 0;
4397 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4399 cpp_error(pfile, "`#else' after `#else'");
4400 fprintf(stderr, " (matches line %d", pfile->if_stack->lineno);
4401 if (strcmp(pfile->if_stack->fname, ip->nominal_fname) != 0)
4402 fprintf(stderr, ", file %s", pfile->if_stack->fname);
4403 fprintf(stderr, ")\n");
4405 pfile->if_stack->type = T_ELSE;
4408 if (pfile->if_stack->if_succeeded)
4409 skip_if_group(pfile, 0);
4412 ++pfile->if_stack->if_succeeded; /* continue processing input */
4413 output_line_command(pfile, 1, same_file);
4419 * unstack after #endif command
4423 do_endif(cpp_reader * pfile, struct directive *keyword __UNUSED__,
4424 unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
4426 if (CPP_PEDANTIC(pfile))
4427 validate_else(pfile, "#endif");
4428 skip_rest_of_line(pfile);
4430 if (pfile->if_stack == CPP_BUFFER(pfile)->if_stack)
4432 cpp_error(pfile, "unbalanced `#endif'");
4436 IF_STACK_FRAME *temp = pfile->if_stack;
4438 pfile->if_stack = temp->next;
4439 if (temp->control_macro)
4441 /* This #endif matched a #ifndef at the start of the file.
4442 * See if it is at the end of the file. */
4443 struct parse_marker start_mark;
4446 parse_set_mark(&start_mark, pfile);
4450 cpp_skip_hspace(pfile);
4455 parse_goto_mark(&start_mark, pfile);
4456 parse_clear_mark(&start_mark);
4460 /* If we get here, this #endif ends a #ifndef
4461 * that contains all of the file (aside from whitespace).
4462 * Arrange not to include the file again
4463 * if the macro that was tested is defined.
4465 * Do not do this for the top-level file in a -include or any
4466 * file in a -imacros. */
4468 file_name_list *ifile = pfile->all_include_files;
4470 for (; ifile; ifile = ifile->next)
4472 if (!strcmp(ifile->fname, CPP_BUFFER(pfile)->fname))
4474 ifile->control_macro = temp->control_macro;
4482 output_line_command(pfile, 1, same_file);
4487 /* When an #else or #endif is found while skipping failed conditional,
4488 * if -pedantic was specified, this is called to warn about text after
4489 * the command name. P points to the first char after the command name. */
4492 validate_else(cpp_reader * pfile, const char *directive)
4496 cpp_skip_hspace(pfile);
4498 if (c != EOF && c != '\n')
4500 "text following `%s' violates ANSI standard", directive);
4503 /* Get the next token, and add it to the text in pfile->token_buffer.
4504 * Return the kind of token we got. */
4507 cpp_get_token(cpp_reader * pfile)
4510 long old_written = 0;
4511 long start_line = 0, start_column = 0;
4512 enum cpp_token token;
4513 struct cpp_options *opts = CPP_OPTIONS(pfile);
4515 CPP_BUFFER(pfile)->prev = CPP_BUFFER(pfile)->cur;
4521 if (CPP_BUFFER(pfile)->seen_eof)
4523 if (cpp_pop_buffer(pfile) != CPP_NULL_BUFFER(pfile))
4530 cpp_buffer *next_buf = CPP_PREV_BUFFER(CPP_BUFFER(pfile));
4532 CPP_BUFFER(pfile)->seen_eof = 1;
4533 if (CPP_BUFFER(pfile)->nominal_fname && next_buf)
4535 /* We're about to return from an #include file.
4536 * Emit #line information now (as part of the CPP_POP) result.
4537 * But the #line refers to the file we will pop to. */
4538 cpp_buffer *cur_buffer = CPP_BUFFER(pfile);
4540 CPP_BUFFER(pfile) = next_buf;
4541 pfile->input_stack_listing_current = 0;
4542 output_line_command(pfile, 0, leave_file);
4543 CPP_BUFFER(pfile) = cur_buffer;
4553 struct parse_marker start_mark;
4558 if (opts->put_out_comments)
4559 parse_set_mark(&start_mark, pfile);
4561 cpp_buf_line_and_col(cpp_file_buffer(pfile),
4562 &start_line, &start_column);
4563 c = skip_comment(pfile, &newlines);
4564 if (opts->put_out_comments && (c == '/' || c == EOF))
4565 parse_clear_mark(&start_mark);
4570 cpp_error_with_line(pfile, start_line, start_column,
4571 "unterminated comment");
4574 c = '/'; /* Initial letter of comment. */
4576 /* Comments are equivalent to spaces.
4577 * For -traditional, a comment is equivalent to nothing. */
4578 if (opts->put_out_comments)
4580 cpp_buffer *pbuf = CPP_BUFFER(pfile);
4581 unsigned char *start = pbuf->buf + start_mark.position;
4582 int len = pbuf->cur - start;
4584 CPP_RESERVE(pfile, 1 + len);
4585 CPP_PUTC_Q(pfile, c);
4586 CPP_PUTS_Q(pfile, start, len);
4587 pfile->lineno += newlines;
4588 parse_clear_mark(&start_mark);
4591 else if (newlines > 0)
4593 output_line_command(pfile, 0, same_file);
4594 CPP_RESERVE(pfile, 1);
4595 CPP_PUTC_Q(pfile, ' ');
4600 CPP_RESERVE(pfile, 1);
4601 CPP_PUTC_Q(pfile, ' ');
4606 if (!pfile->only_seen_white)
4608 if (handle_directive(pfile))
4609 return CPP_DIRECTIVE;
4610 pfile->only_seen_white = 0;
4615 /* A single quoted string is treated like a double -- some
4616 * programs (e.g., troff) are perverse this way */
4617 cpp_buf_line_and_col(cpp_file_buffer(pfile),
4618 &start_line, &start_column);
4619 old_written = CPP_WRITTEN(pfile);
4628 if (CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
4630 /* try harder: this string crosses a macro expansion
4631 * boundary. This can happen naturally if -traditional.
4632 * Otherwise, only -D can make a macro with an unmatched
4634 cpp_buffer *next_buf
4635 = CPP_PREV_BUFFER(CPP_BUFFER(pfile));
4637 (*CPP_BUFFER(pfile)->cleanup)
4638 (CPP_BUFFER(pfile), pfile);
4639 CPP_BUFFER(pfile) = next_buf;
4642 cpp_error_with_line(pfile, start_line, start_column,
4643 "unterminated string or character constant");
4644 if (pfile->multiline_string_line != start_line
4645 && pfile->multiline_string_line != 0)
4646 cpp_error_with_line(pfile,
4647 pfile->multiline_string_line,
4649 "possible real start of unterminated constant");
4650 pfile->multiline_string_line = 0;
4653 CPP_PUTC(pfile, cc);
4657 /* Traditionally, end of line ends a string constant with
4658 * no error. So exit the loop and record the new line. */
4661 cpp_error_with_line(pfile, start_line, start_column,
4662 "unterminated character constant");
4665 if (CPP_PEDANTIC(pfile)
4666 && pfile->multiline_string_line == 0)
4668 cpp_pedwarn_with_line(pfile, start_line,
4670 "string constant runs past end of line");
4672 if (pfile->multiline_string_line == 0)
4673 pfile->multiline_string_line = start_line;
4680 /* Backslash newline is replaced by nothing at all. */
4681 CPP_ADJUST_WRITTEN(pfile, -1);
4686 /* ANSI stupidly requires that in \\ the second \
4687 * is *not* prevented from combining with a newline. */
4690 CPP_PUTC(pfile, cc);
4702 pfile->lineno += count_newlines(pfile->token_buffer + old_written,
4703 CPP_PWRITTEN(pfile));
4704 pfile->only_seen_white = 0;
4705 return c == '\'' ? CPP_CHAR : CPP_STRING;
4708 if (!opts->dollars_in_ident)
4713 if (opts->cplusplus && PEEKC() == ':')
4722 if (c2 == c || c2 == '=')
4739 if (c2 == '-' && opts->chill)
4741 /* Chill style comment */
4742 if (opts->put_out_comments)
4743 parse_set_mark(&start_mark, pfile);
4744 FORWARD(1); /* Skip second '-'. */
4752 /* Don't consider final '\n' to be part of comment. */
4758 goto return_comment;
4760 if (c2 == '-' || c2 == '=' || c2 == '>')
4765 if (pfile->parsing_include_directive)
4774 if (c == '\n' || c == EOF)
4777 "missing '>' in `#include <FILENAME>'");
4783 /* else fall through */
4792 CPP_RESERVE(pfile, 4);
4794 CPP_PUTC(pfile, c2);
4798 CPP_PUTC_Q(pfile, GETC());
4799 CPP_NUL_TERMINATE_Q(pfile);
4800 pfile->only_seen_white = 0;
4804 if (CPP_BUFFER(pfile)->has_escapes)
4807 // fix macro expansions starting with - losing the -
4810 CPP_PUTS(pfile, "-", 1);
4813 // fix macro expansions starting with - losing the +
4816 CPP_PUTS(pfile, "+", 1);
4819 // fix macro expansions starting with - losing the .
4822 CPP_PUTS(pfile, ".", 1);
4825 else if (is_space[c])
4827 CPP_RESERVE(pfile, 1);
4828 if (pfile->output_escapes)
4829 CPP_PUTC_Q(pfile, '@');
4833 if (pfile->output_escapes)
4835 CPP_PUTS(pfile, "@@", 2);
4845 CPP_RESERVE(pfile, 2);
4846 CPP_PUTC_Q(pfile, '.');
4850 /* FIXME - misses the case "..\\\n." */
4851 if (c2 == '.' && PEEKN(1) == '.')
4853 CPP_RESERVE(pfile, 4);
4854 CPP_PUTC_Q(pfile, '.');
4855 CPP_PUTC_Q(pfile, '.');
4856 CPP_PUTC_Q(pfile, '.');
4858 CPP_NUL_TERMINATE_Q(pfile);
4859 pfile->only_seen_white = 0;
4866 pfile->only_seen_white = 0;
4868 CPP_RESERVE(pfile, 3);
4869 CPP_PUTC_Q(pfile, c);
4870 CPP_PUTC_Q(pfile, GETC());
4871 CPP_NUL_TERMINATE_Q(pfile);
4877 if ((c2 == '\'' || c2 == '\"'))
4899 CPP_RESERVE(pfile, 2);
4900 CPP_PUTC_Q(pfile, c);
4905 if (!is_idchar[c] && c != '.'
4906 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
4911 CPP_NUL_TERMINATE_Q(pfile);
4912 pfile->only_seen_white = 0;
4924 if (opts->chill && PEEKC() == '\'')
4926 pfile->only_seen_white = 0;
4927 CPP_RESERVE(pfile, 2);
4928 CPP_PUTC_Q(pfile, c);
4929 CPP_PUTC_Q(pfile, '\'');
4935 goto chill_number_eof;
4938 if (c == '\\' && PEEKC() == '\n')
4949 CPP_RESERVE(pfile, 2);
4950 CPP_PUTC_Q(pfile, c);
4951 CPP_NUL_TERMINATE_Q(pfile);
4958 CPP_NUL_TERMINATE(pfile);
5010 int before_name_written = CPP_WRITTEN(pfile);
5013 parse_name(pfile, c);
5014 pfile->only_seen_white = 0;
5015 if (pfile->no_macro_expand)
5017 ident = (char *)pfile->token_buffer + before_name_written;
5018 ident_len = CPP_PWRITTEN(pfile) - (unsigned char *)ident;
5019 hp = cpp_lookup(ident, ident_len, -1);
5022 if (hp->type == T_DISABLED)
5024 if (pfile->output_escapes)
5025 { /* Return "@-IDENT", followed by '\0'. */
5028 CPP_RESERVE(pfile, 3);
5030 (char *)pfile->token_buffer + before_name_written;
5031 CPP_ADJUST_WRITTEN(pfile, 2);
5032 for (i = ident_len; i >= 0; i--)
5033 ident[i + 2] = ident[i];
5039 /* If macro wants an arglist, verify that a '(' follows.
5040 * first skip all whitespace, copying it to the output
5041 * after the macro name. Then, if there is no '(',
5042 * decide this is not a macro call and leave things that way. */
5043 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5045 struct parse_marker macro_mark;
5048 while (CPP_IS_MACRO_BUFFER(CPP_BUFFER(pfile)))
5050 cpp_buffer *next_buf;
5052 cpp_skip_hspace(pfile);
5055 next_buf = CPP_PREV_BUFFER(CPP_BUFFER(pfile));
5056 (*CPP_BUFFER(pfile)->cleanup) (CPP_BUFFER(pfile),
5058 CPP_BUFFER(pfile) = next_buf;
5060 parse_set_mark(¯o_mark, pfile);
5063 cpp_skip_hspace(pfile);
5065 is_macro_call = c == '(';
5071 parse_goto_mark(¯o_mark, pfile);
5072 parse_clear_mark(¯o_mark);
5076 /* This is now known to be a macro call. */
5078 /* it might not actually be a macro. */
5079 if (hp->type != T_MACRO)
5082 unsigned char *xbuf;
5084 CPP_SET_WRITTEN(pfile, before_name_written);
5085 special_symbol(hp, pfile);
5086 xbuf_len = CPP_WRITTEN(pfile) - before_name_written;
5087 xbuf = (unsigned char *)xmalloc(xbuf_len + 1);
5088 CPP_SET_WRITTEN(pfile, before_name_written);
5089 memcpy(xbuf, CPP_PWRITTEN(pfile), xbuf_len + 1);
5090 push_macro_expansion(pfile, xbuf, xbuf_len, hp);
5094 /* Expand the macro, reading arguments as needed,
5095 * and push the expansion on the input stack. */
5096 macroexpand(pfile, hp);
5097 CPP_SET_WRITTEN(pfile, before_name_written);
5100 /* An extra "@ " is added to the end of a macro expansion
5101 * to prevent accidental token pasting. We prefer to avoid
5102 * unneeded extra spaces (for the sake of cpp-using tools like
5103 * imake). Here we remove the space if it is safe to do so. */
5104 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5105 && pfile->buffer->rlimit[-2] == '@'
5106 && pfile->buffer->rlimit[-1] == ' ')
5108 int c1 = pfile->buffer->rlimit[-3];
5110 c2 = CPP_BUF_PEEK(CPP_PREV_BUFFER(CPP_BUFFER(pfile)));
5112 if (c2 == EOF || !unsafe_chars(c1, c2))
5113 pfile->buffer->rlimit -= 2;
5126 if (c == EOF || !is_hor_space[c])
5141 if (pfile->only_seen_white == 0)
5142 pfile->only_seen_white = 1;
5144 output_line_command(pfile, 1, same_file);
5163 token = CPP_SEMICOLON;
5170 pfile->only_seen_white = 0;
5178 /* Like cpp_get_token, but skip spaces and comments. */
5180 cpp_get_non_space_token(cpp_reader * pfile)
5182 int old_written = CPP_WRITTEN(pfile);
5186 enum cpp_token token = cpp_get_token(pfile);
5188 if (token != CPP_COMMENT && token != CPP_POP
5189 && token != CPP_HSPACE && token != CPP_VSPACE)
5191 CPP_SET_WRITTEN(pfile, old_written);
5196 /* Parse an identifier starting with C. */
5199 parse_name(cpp_reader * pfile, int c)
5205 if (c == '\\' && PEEKC() == '\n')
5213 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5214 CPP_PUTC_Q(pfile, c);
5219 CPP_NUL_TERMINATE_Q(pfile);
5223 /* Maintain and search list of included files, for #import. */
5225 /* Hash a file name for import_hash_table. */
5228 import_hash(char *f)
5234 return (val % IMPORT_HASH_SIZE);
5237 /* Search for file FILENAME in import_hash_table.
5238 * Return -2 if found, either a matching name or a matching inode.
5239 * Otherwise, open the file and return a file descriptor if successful
5240 * or -1 if unsuccessful. */
5243 lookup_import(cpp_reader * pfile, char *filename, file_name_list * searchptr)
5245 struct import_file *i;
5251 hashval = import_hash(filename);
5253 /* Attempt to find file in list of already included files */
5254 i = pfile->import_hash_table[hashval];
5258 if (!strcmp(filename, i->name))
5259 return -2; /* return found */
5262 /* Open it and try a match on inode/dev */
5263 fd = open_include_file(pfile, filename, searchptr);
5267 for (h = 0; h < IMPORT_HASH_SIZE; h++)
5269 i = pfile->import_hash_table[h];
5272 /* Compare the inode and the device.
5273 * Supposedly on some systems the inode is not a scalar. */
5275 ((char *)&i->inode, (char *)&sb.st_ino, sizeof(sb.st_ino))
5276 && i->dev == sb.st_dev)
5279 return -2; /* return found */
5284 return fd; /* Not found, return open file */
5287 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5290 add_import(cpp_reader * pfile, int fd, char *fname)
5292 struct import_file *i;
5296 hashval = import_hash(fname);
5298 i = (struct import_file *)xmalloc(sizeof(struct import_file));
5300 i->name = (char *)xmalloc(strlen(fname) + 1);
5301 strcpy(i->name, fname);
5302 memcpy((char *)&i->inode, (char *)&sb.st_ino, sizeof(sb.st_ino));
5304 i->next = pfile->import_hash_table[hashval];
5305 pfile->import_hash_table[hashval] = i;
5308 /* The file_name_map structure holds a mapping of file names for a
5309 * particular directory. This mapping is read from the file named
5310 * FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5311 * map filenames on a file system with severe filename restrictions,
5312 * such as DOS. The format of the file name map file is just a series
5313 * of lines with two tokens on each line. The first token is the name
5314 * to map, and the second token is the actual name to use. */
5316 struct file_name_map {
5317 struct file_name_map *map_next;
5322 #if USE_FILE_NAME_MAPS
5324 #define FILE_NAME_MAP_FILE "header.gcc"
5326 /* Read a space delimited string of unlimited length from a stdio
5330 read_filename_string(int ch, FILE * f)
5336 set = alloc = (char *)xmalloc(len + 1);
5340 while ((ch = getc(f)) != EOF && !is_space[ch])
5342 if (set - alloc == len)
5345 alloc = (char *)xrealloc(alloc, len + 1);
5346 set = alloc + len / 2;
5356 /* This structure holds a linked list of file name maps, one per directory. */
5357 struct file_name_map_list {
5358 struct file_name_map_list *map_list_next;
5359 char *map_list_name;
5360 struct file_name_map *map_list_map;
5363 /* Read the file name map file for DIRNAME. */
5365 static struct file_name_map *
5366 read_name_map(cpp_reader * pfile, const char *dirname)
5368 struct file_name_map_list *map_list_ptr;
5372 for (map_list_ptr = CPP_OPTIONS(pfile)->map_list; map_list_ptr;
5373 map_list_ptr = map_list_ptr->map_list_next)
5374 if (!strcmp(map_list_ptr->map_list_name, dirname))
5375 return map_list_ptr->map_list_map;
5378 ((struct file_name_map_list *)xmalloc(sizeof(struct file_name_map_list)));
5380 map_list_ptr->map_list_name = savestring(dirname);
5381 map_list_ptr->map_list_map = NULL;
5383 name = (char *)alloca(strlen(dirname) + strlen(FILE_NAME_MAP_FILE) + 2);
5384 strcpy(name, dirname);
5387 strcat(name, FILE_NAME_MAP_FILE);
5389 f = fopen(name, "rb");
5391 f = fopen(name, "rtb");
5394 map_list_ptr->map_list_map = NULL;
5398 int dirlen = strlen(dirname);
5400 while ((ch = getc(f)) != EOF)
5403 struct file_name_map *ptr;
5407 from = read_filename_string(ch, f);
5408 while ((ch = getc(f)) != EOF && is_hor_space[ch]);
5409 to = read_filename_string(ch, f);
5412 ((struct file_name_map *)xmalloc(sizeof(struct file_name_map)));
5414 ptr->map_from = from;
5416 /* Make the real filename absolute. */
5421 ptr->map_to = (char *)xmalloc(dirlen + strlen(to) + 2);
5422 strcpy(ptr->map_to, dirname);
5423 ptr->map_to[dirlen] = '/';
5424 strcpy(ptr->map_to + dirlen + 1, to);
5428 ptr->map_next = map_list_ptr->map_list_map;
5429 map_list_ptr->map_list_map = ptr;
5431 while ((ch = getc(f)) != '\n')
5438 map_list_ptr->map_list_next = CPP_OPTIONS(pfile)->map_list;
5439 CPP_OPTIONS(pfile)->map_list = map_list_ptr;
5441 return map_list_ptr->map_list_map;
5444 /* Try to open include file FILENAME. SEARCHPTR is the directory
5445 * being tried from the include file search path. This function maps
5446 * filenames on file systems based on information read by
5450 open_include_file(cpp_reader * pfile, char *filename,
5451 file_name_list * searchptr)
5453 struct file_name_map *map;
5455 const char *p, *dir;
5457 if (searchptr && !searchptr->got_name_map)
5459 searchptr->name_map = read_name_map(pfile,
5461 ? searchptr->fname : ".");
5462 searchptr->got_name_map = 1;
5464 /* First check the mapping for the directory we are using. */
5465 if (searchptr && searchptr->name_map)
5468 if (searchptr->fname)
5469 from += strlen(searchptr->fname) + 1;
5470 for (map = searchptr->name_map; map; map = map->map_next)
5472 if (!strcmp(map->map_from, from))
5474 /* Found a match. */
5475 return open(map->map_to, O_RDONLY | O_BINARY, 0666);
5479 /* Try to find a mapping file for the particular directory we are
5480 * looking in. Thus #include <sys/types.h> will look up sys/types.h
5481 * in /usr/include/header.gcc and look up types.h in
5482 * /usr/include/sys/header.gcc. */
5483 p = strrchr(filename, '/');
5488 && strlen(searchptr->fname) == (unsigned)(p - filename)
5489 && !strncmp(searchptr->fname, filename, p - filename))
5491 /* FILENAME is in SEARCHPTR, which we've already checked. */
5492 using_file(filename);
5493 return open(filename, O_RDONLY | O_BINARY, 0666);
5504 s = (char *)alloca(p - filename + 1);
5505 memcpy(s, filename, p - filename);
5506 s[p - filename] = '\0';
5510 for (map = read_name_map(pfile, dir); map; map = map->map_next)
5511 if (!strcmp(map->map_from, from))
5513 using_file(map->map_to);
5514 return open(map->map_to, O_RDONLY | O_BINARY, 0666);
5517 using_file(filename);
5518 return open(filename, O_RDONLY | O_BINARY, 0666);
5524 open_include_file(cpp_reader * pfile __UNUSED__, char *filename,
5525 file_name_list * searchptr __UNUSED__)
5527 using_file(filename);
5528 return open(filename, O_RDONLY | O_BINARY, 0666);
5531 #endif /* USE_FILE_NAME_MAPS */
5534 dos2unix(cpp_buffer *fp, int length)
5536 unsigned char *tbuf;
5539 tbuf = xmalloc(length + 4);
5540 if (!tbuf) return length;
5541 for (i = 0; i < length; i++)
5543 if ((fp->buf[i] == '\r') &&
5544 (fp->buf[i + 1] == '\n'))
5549 tbuf[nlen] = fp->buf[i];
5559 /* Process the contents of include file FNAME, already open on descriptor F,
5560 * with output to OP.
5561 * SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5562 * "system" include directories (as decided by the `is_system_include'
5564 * DIRPTR is the link in the dir path through which this file was found,
5565 * or 0 if the file name was absolute or via the current directory.
5566 * Return 1 on success, 0 on failure.
5568 * The caller is responsible for the cpp_push_buffer. */
5571 finclude(cpp_reader * pfile, int f, const char *fname, int system_header_p,
5572 file_name_list * dirptr)
5578 cpp_buffer *fp; /* For input stack frame */
5580 if (file_size_and_mode(f, &st_mode, &st_size) < 0)
5582 cpp_perror_with_name(pfile, fname);
5584 cpp_pop_buffer(pfile);
5587 fp = CPP_BUFFER(pfile);
5588 fp->nominal_fname = fp->fname = fname;
5590 fp->system_header_p = system_header_p;
5593 fp->cleanup = file_cleanup;
5595 if (S_ISREG(st_mode))
5597 fp->buf = (unsigned char *)xmalloc(st_size + 2);
5598 /* Read the file contents, knowing that st_size is an upper bound
5599 * on the number of bytes we can read. */
5600 length = safe_read(f, (char *)fp->buf, st_size);
5601 length = dos2unix(fp, length);
5603 fp->alimit = fp->buf + st_size + 2;
5605 fp->rlimit = fp->buf + length;
5609 else if (S_ISDIR(st_mode))
5611 cpp_error(pfile, "directory `%s' specified in #include", fname);
5617 /* Cannot count its file size before reading.
5618 * First read the entire file into heap and
5619 * copy them into buffer on stack. */
5624 fp->buf = (unsigned char *)xmalloc(bsize + 2);
5628 i = safe_read(f, (char *)(fp->buf + st_size), bsize - st_size);
5630 goto nope; /* error! */
5632 if (st_size != bsize)
5633 break; /* End of file */
5635 fp->buf = (unsigned char *)xrealloc(fp->buf, bsize + 2);
5638 length = dos2unix(fp, length);
5641 if ((length > 0 && fp->buf[length - 1] != '\n')
5642 /* Backslash-newline at end is not good enough. */
5643 || (length > 1 && fp->buf[length - 2] == '\\'))
5645 fp->buf[length++] = '\n';
5647 fp->buf[length] = '\0';
5649 fp->rlimit = fp->buf + length;
5651 /* Close descriptor now, so nesting does not use lots of descriptors. */
5654 /* Must do this before calling trigraph_pcp, so that the correct file name
5655 * will be printed in warning messages. */
5657 pfile->input_stack_listing_current = 0;
5663 cpp_perror_with_name(pfile, fname);
5670 push_parse_file(cpp_reader * pfile, const char *fname)
5672 struct cpp_options *opts = CPP_OPTIONS(pfile);
5673 struct cpp_pending *pend;
5678 /* The code looks at the defaults through this pointer, rather than through
5679 * the constant structure above. This pointer gets changed if an environment
5680 * variable specifies other defaults. */
5681 struct default_include *include_defaults = include_defaults_array;
5683 /* Add dirs from CPATH after dirs from -I. */
5684 /* There seems to be confusion about what CPATH should do,
5685 * so for the moment it is not documented. */
5686 /* Some people say that CPATH should replace the standard include dirs,
5687 * but that seems pointless: it comes before them, so it overrides them
5689 p = (char *)getenv("CPATH");
5690 if (p && !opts->no_standard_includes)
5691 path_include(pfile, p);
5693 /* Now that dollars_in_ident is known, initialize is_idchar. */
5694 initialize_char_syntax(opts);
5696 /* Do partial setup of input buffer for the sake of generating
5697 * early #line directives (when -g is in effect). */
5698 fp = cpp_push_buffer(pfile, NULL, 0);
5699 if (!opts->in_fname)
5700 opts->in_fname = "";
5701 fp->nominal_fname = fp->fname = opts->in_fname;
5704 /* Install __LINE__, etc. Must follow initialize_char_syntax
5705 * and option processing. */
5706 initialize_builtins(pfile);
5708 /* Do standard #defines and assertions
5709 * that identify system and machine type. */
5711 if (!opts->inhibit_predefs)
5713 p = (char *)alloca(strlen(predefs) + 1);
5720 while (*p == ' ' || *p == '\t')
5722 /* Handle -D options. */
5723 if (p[0] == '-' && p[1] == 'D')
5726 while (*p && *p != ' ' && *p != '\t')
5730 if (opts->debug_output)
5731 output_line_command(pfile, 0, same_file);
5732 cpp_define(pfile, (unsigned char *)q);
5733 while (*p == ' ' || *p == '\t')
5736 else if (p[0] == '-' && p[1] == 'A')
5738 /* Handle -A options (assertions). */
5747 past_name = assertion;
5748 /* Locate end of name. */
5749 while (*past_name && *past_name != ' '
5750 && *past_name != '\t' && *past_name != '(')
5752 /* Locate `(' at start of value. */
5754 while (*value && (*value == ' ' || *value == '\t'))
5756 if (*value++ != '(')
5758 while (*value && (*value == ' ' || *value == '\t'))
5761 /* Locate end of value. */
5762 while (*past_value && *past_value != ' '
5763 && *past_value != '\t' && *past_value != ')')
5765 termination = past_value;
5767 && (*termination == ' ' || *termination == '\t'))
5769 if (*termination++ != ')')
5771 if (*termination && *termination != ' '
5772 && *termination != '\t')
5774 /* Temporarily null-terminate the value. */
5775 save_char = *termination;
5776 *termination = '\0';
5777 /* Install the assertion. */
5778 make_assertion(pfile, "-A", assertion);
5779 *termination = (char)save_char;
5781 while (*p == ' ' || *p == '\t')
5790 /* Now handle the command line options. */
5792 /* Do -U's, -D's and -A's in the order they were seen. */
5793 /* First reverse the list. */
5794 opts->pending = nreverse_pending(opts->pending);
5796 for (pend = opts->pending; pend; pend = pend->next)
5798 if (pend->cmd && pend->cmd[0] == '-')
5800 switch (pend->cmd[1])
5803 if (opts->debug_output)
5804 output_line_command(pfile, 0, same_file);
5805 do_undef(pfile, NULL, (unsigned char *)pend->arg,
5806 (unsigned char *)pend->arg + strlen(pend->arg));
5809 if (opts->debug_output)
5810 output_line_command(pfile, 0, same_file);
5811 cpp_define(pfile, (unsigned char *)pend->arg);
5814 make_assertion(pfile, "-A", pend->arg);
5820 opts->done_initializing = 1;
5822 { /* read the appropriate environment variable and if it exists
5823 * replace include_defaults with the listed path. */
5826 switch ((opts->objc << 1) + opts->cplusplus)
5829 epath = getenv("C_INCLUDE_PATH");
5832 epath = getenv("CPLUS_INCLUDE_PATH");
5835 epath = getenv("OBJC_INCLUDE_PATH");
5838 epath = getenv("OBJCPLUS_INCLUDE_PATH");
5841 /* If the environment var for this language is set,
5842 * add to the default list of include directories. */
5845 char *nstore = (char *)alloca(strlen(epath) + 2);
5847 char *startp, *endp;
5849 for (num_dirs = 1, startp = epath; *startp; startp++)
5850 if (*startp == PATH_SEPARATOR)
5853 = (struct default_include *)xmalloc((num_dirs
5858 (include_defaults_array));
5860 startp = endp = epath;
5864 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5865 if ((*endp == PATH_SEPARATOR) || *endp == 0)
5867 strncpy(nstore, startp, endp - startp);
5869 strcpy(nstore, ".");
5871 nstore[endp - startp] = '\0';
5873 include_defaults[num_dirs].fname = savestring(nstore);
5874 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5875 include_defaults[num_dirs].cxx_aware = 1;
5879 endp = startp = endp + 1;
5884 /* Put the usual defaults back in at the end. */
5885 memcpy((char *)&include_defaults[num_dirs],
5886 (char *)include_defaults_array,
5887 sizeof(include_defaults_array));
5891 append_include_chain(pfile, opts->before_system, opts->last_before_system);
5892 opts->first_system_include = opts->before_system;
5894 /* Unless -fnostdinc,
5895 * tack on the standard include file dirs to the specified list */
5896 if (!opts->no_standard_includes)
5898 struct default_include *di = include_defaults;
5899 char *specd_prefix = opts->include_prefix;
5900 char *default_prefix = savestring(GCC_INCLUDE_DIR);
5901 int default_len = 0;
5903 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5904 if (!strcmp(default_prefix + strlen(default_prefix) - 8, "/include"))
5906 default_len = strlen(default_prefix) - 7;
5907 default_prefix[default_len] = 0;
5909 /* Search "translated" versions of GNU directories.
5910 * These have /usr/local/lib/gcc... replaced by specd_prefix. */
5911 if (specd_prefix && default_len != 0)
5912 for (di = include_defaults; di->fname; di++)
5914 /* Some standard dirs are only for C++. */
5917 && !opts->no_standard_cplusplus_includes))
5919 /* Does this dir start with the prefix? */
5920 if (!strncmp(di->fname, default_prefix, default_len))
5922 /* Yes; change prefix and add to search list. */
5923 file_name_list *new_
5925 (file_name_list *) xmalloc(sizeof(file_name_list));
5927 strlen(specd_prefix) + strlen(di->fname) -
5930 (char *)xmalloc(this_len + 1);
5932 strcpy(str, specd_prefix);
5933 strcat(str, di->fname + default_len);
5935 new_->control_macro = 0;
5936 new_->c_system_include_path = !di->cxx_aware;
5937 new_->got_name_map = 0;
5938 append_include_chain(pfile, new_, new_);
5939 if (!opts->first_system_include)
5940 opts->first_system_include = new_;
5944 /* Search ordinary names for GNU include directories. */
5945 for (di = include_defaults; di->fname; di++)
5947 /* Some standard dirs are only for C++. */
5949 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
5951 file_name_list *new_
5952 = (file_name_list *) xmalloc(sizeof(file_name_list));
5954 new_->control_macro = 0;
5955 new_->c_system_include_path = !di->cxx_aware;
5956 new_->fname = (char *)di->fname;
5957 new_->got_name_map = 0;
5958 append_include_chain(pfile, new_, new_);
5959 if (!opts->first_system_include)
5960 opts->first_system_include = new_;
5964 /* Tack the after_include chain at the end of the include chain. */
5965 append_include_chain(pfile, opts->after_include, opts->last_after_include);
5966 if (!opts->first_system_include)
5967 opts->first_system_include = opts->after_include;
5969 /* With -v, print the list of dirs to search. */
5974 fprintf(stderr, "#include \"...\" search starts here:\n");
5975 for (fl = opts->include; fl; fl = fl->next)
5977 if (fl == opts->first_bracket_include)
5978 fprintf(stderr, "#include <...> search starts here:\n");
5979 fprintf(stderr, " %s\n", fl->fname);
5981 fprintf(stderr, "End of search list.\n");
5983 /* Scan the -imacros files before the main input.
5984 * Much like #including them, but with no_output set
5985 * so that only their macro definitions matter. */
5988 pfile->no_record_file++;
5989 for (pend = opts->pending; pend; pend = pend->next)
5991 if (pend->cmd && strcmp(pend->cmd, "-imacros") == 0)
5993 int fd = open(pend->arg, O_RDONLY | O_BINARY, 0666);
5997 cpp_perror_with_name(pfile, pend->arg);
5998 return FATAL_EXIT_CODE;
6000 cpp_push_buffer(pfile, NULL, 0);
6001 finclude(pfile, fd, pend->arg, 0, NULL);
6002 cpp_scan_buffer(pfile);
6006 pfile->no_record_file--;
6008 /* Copy the entire contents of the main input file into
6009 * the stacked input buffer previously allocated for it. */
6010 if (!fname || *fname == 0)
6015 else if ((f = open(fname, O_RDONLY | O_BINARY, 0666)) < 0)
6016 cpp_pfatal_with_name(pfile, fname);
6018 /* -MG doesn't select the form of output and must be specified with one of
6019 * -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6020 * inhibit compilation. */
6021 if (opts->print_deps_missing_files
6022 && (opts->print_deps == 0 || !opts->no_output))
6023 cpp_fatal("-MG must be specified with one of -M or -MM");
6025 /* Either of two environment variables can specify output of deps.
6026 * Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6027 * where OUTPUT_FILE is the file to write deps info to
6028 * and DEPS_TARGET is the target to mention in the deps. */
6030 if (opts->print_deps == 0
6031 && (getenv("SUNPRO_DEPENDENCIES") != 0
6032 || getenv("DEPENDENCIES_OUTPUT") != 0))
6034 char *spec = getenv("DEPENDENCIES_OUTPUT");
6040 spec = getenv("SUNPRO_DEPENDENCIES");
6041 opts->print_deps = 2;
6044 opts->print_deps = 1;
6047 /* Find the space before the DEPS_TARGET, if there is one. */
6048 /* This should use index. (mrs) */
6049 while (*s != 0 && *s != ' ')
6053 opts->deps_target = s + 1;
6054 output_file = (char *)xmalloc(s - spec + 1);
6055 memcpy(output_file, spec, s - spec);
6056 output_file[s - spec] = 0;
6060 opts->deps_target = 0;
6064 opts->deps_file = output_file;
6065 opts->print_deps_append = 1;
6067 /* For -M, print the expected object file name
6068 * as the target of this Make-rule. */
6069 if (opts->print_deps)
6071 pfile->deps_allocated_size = 200;
6072 pfile->deps_buffer = (char *)xmalloc(pfile->deps_allocated_size);
6073 pfile->deps_buffer[0] = 0;
6074 pfile->deps_size = 0;
6075 pfile->deps_column = 0;
6077 if (opts->deps_target)
6078 deps_output(pfile, opts->deps_target, ':');
6079 else if (*opts->in_fname == 0)
6080 deps_output(pfile, "-", ':');
6086 /* Discard all directory prefixes from filename. */
6087 if ((q = (char *)strrchr(opts->in_fname, '/'))
6088 #ifdef DIR_SEPARATOR
6089 && (q = strrchr(opts->in_fname, DIR_SEPARATOR))
6094 q = (char *)opts->in_fname;
6096 /* Copy remainder to mungable area. */
6097 p = (char *)alloca(strlen(q) + 8);
6100 /* Output P, but remove known suffixes. */
6103 if (len >= 2 && p[len - 2] == '.' && strchr("cCsSm", p[len - 1]))
6106 && p[len - 3] == '.'
6107 && p[len - 2] == 'c' && p[len - 1] == 'c')
6110 && p[len - 4] == '.'
6111 && p[len - 3] == 'c'
6112 && p[len - 2] == 'x' && p[len - 1] == 'x')
6115 && p[len - 4] == '.'
6116 && p[len - 3] == 'c'
6117 && p[len - 2] == 'p' && p[len - 1] == 'p')
6120 /* Supply our own suffix. */
6123 deps_output(pfile, p, ':');
6124 deps_output(pfile, opts->in_fname, ' ');
6128 /* Scan the -include files before the main input.
6129 * We push these in reverse order, so that the first one is handled first. */
6131 pfile->no_record_file++;
6132 opts->pending = nreverse_pending(opts->pending);
6133 for (pend = opts->pending; pend; pend = pend->next)
6135 if (pend->cmd && strcmp(pend->cmd, "-include") == 0)
6137 int fd = open(pend->arg, O_RDONLY | O_BINARY, 0666);
6141 cpp_perror_with_name(pfile, pend->arg);
6142 return FATAL_EXIT_CODE;
6144 cpp_push_buffer(pfile, NULL, 0);
6145 finclude(pfile, fd, pend->arg, 0, NULL);
6148 pfile->no_record_file--;
6150 /* Free the pending list. */
6151 for (pend = opts->pending; pend;)
6153 struct cpp_pending *next = pend->next;
6158 opts->pending = NULL;
6160 if (finclude(pfile, f, fname, 0, NULL))
6161 output_line_command(pfile, 0, same_file);
6162 return SUCCESS_EXIT_CODE;
6166 init_parse_file(cpp_reader * pfile)
6168 memset((char *)pfile, 0, sizeof(cpp_reader));
6169 pfile->get_token = cpp_get_token;
6171 pfile->token_buffer_size = 200;
6172 pfile->token_buffer = (unsigned char *)xmalloc(pfile->token_buffer_size);
6173 CPP_SET_WRITTEN(pfile, 0);
6175 pfile->system_include_depth = 0;
6176 pfile->dont_repeat_files = 0;
6177 pfile->all_include_files = 0;
6178 pfile->max_include_len = 0;
6179 pfile->timebuf = NULL;
6180 pfile->only_seen_white = 1;
6181 pfile->buffer = CPP_NULL_BUFFER(pfile);
6184 static struct cpp_pending *
6185 nreverse_pending(struct cpp_pending *list)
6187 struct cpp_pending *prev = 0, *next, *pend;
6189 for (pend = list; pend; pend = next)
6199 push_pending(cpp_reader * pfile, const char *cmd, const char *arg)
6201 struct cpp_pending *pend
6202 = (struct cpp_pending *)xmalloc(sizeof(struct cpp_pending));
6206 pend->next = CPP_OPTIONS(pfile)->pending;
6207 CPP_OPTIONS(pfile)->pending = pend;
6210 /* Handle command-line options in (argc, argv).
6211 * Can be called multiple times, to handle multiple sets of options.
6212 * Returns if an unrecognized option is seen.
6213 * Returns number of handled arguments. */
6216 cpp_handle_options(cpp_reader * pfile, int argc, char **argv)
6219 struct cpp_options *opts = CPP_OPTIONS(pfile);
6221 for (i = 0; i < argc; i++)
6223 if (argv[i][0] != '-')
6225 if (opts->out_fname)
6226 cpp_fatal("Usage: %s [switches] input output", argv[0]);
6227 else if (opts->in_fname)
6228 opts->out_fname = argv[i];
6230 opts->in_fname = argv[i];
6238 if (!strcmp(argv[i], "-include")
6239 || !strcmp(argv[i], "-imacros"))
6242 cpp_fatal("Filename missing after `%s' option",
6245 push_pending(pfile, argv[i], argv[i + 1]), i++;
6247 if (!strcmp(argv[i], "-iprefix"))
6250 cpp_fatal("Filename missing after `-iprefix' option");
6252 opts->include_prefix = argv[++i];
6254 if (!strcmp(argv[i], "-ifoutput"))
6256 opts->output_conditionals = 1;
6258 if (!strcmp(argv[i], "-isystem"))
6260 file_name_list *dirtmp;
6263 cpp_fatal("Filename missing after `-isystem' option");
6266 (file_name_list *) xmalloc(sizeof(file_name_list));
6269 dirtmp->control_macro = 0;
6270 dirtmp->c_system_include_path = 1;
6271 dirtmp->fname = (char *)xmalloc(strlen(argv[i + 1]) + 1);
6272 strcpy(dirtmp->fname, argv[++i]);
6273 dirtmp->got_name_map = 0;
6275 if (!opts->before_system)
6276 opts->before_system = dirtmp;
6278 opts->last_before_system->next = dirtmp;
6279 opts->last_before_system = dirtmp; /* Tail follows the last one */
6281 /* Add directory to end of path for includes,
6282 * with the default prefix at the front of its name. */
6283 if (!strcmp(argv[i], "-iwithprefix"))
6285 file_name_list *dirtmp;
6288 if (opts->include_prefix)
6289 prefix = opts->include_prefix;
6292 prefix = savestring(GCC_INCLUDE_DIR);
6293 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6295 (prefix + strlen(prefix) - 8, "/include"))
6296 prefix[strlen(prefix) - 7] = 0;
6300 (file_name_list *) xmalloc(sizeof(file_name_list));
6302 dirtmp->next = 0; /* New one goes on the end */
6303 dirtmp->control_macro = 0;
6304 dirtmp->c_system_include_path = 0;
6307 ("Directory name missing after `-iwithprefix' option");
6309 dirtmp->fname = (char *)xmalloc(strlen(argv[i + 1])
6310 + strlen(prefix) + 1);
6311 strcpy(dirtmp->fname, prefix);
6312 strcat(dirtmp->fname, argv[++i]);
6313 dirtmp->got_name_map = 0;
6315 if (!opts->after_include)
6316 opts->after_include = dirtmp;
6318 opts->last_after_include->next = dirtmp;
6319 opts->last_after_include = dirtmp; /* Tail follows the last one */
6321 /* Add directory to main path for includes,
6322 * with the default prefix at the front of its name. */
6323 if (!strcmp(argv[i], "-iwithprefixbefore"))
6325 file_name_list *dirtmp;
6328 if (opts->include_prefix)
6329 prefix = opts->include_prefix;
6332 prefix = savestring(GCC_INCLUDE_DIR);
6333 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6335 (prefix + strlen(prefix) - 8, "/include"))
6336 prefix[strlen(prefix) - 7] = 0;
6340 (file_name_list *) xmalloc(sizeof(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;
6347 ("Directory name missing after `-iwithprefixbefore' option");
6349 dirtmp->fname = (char *)xmalloc(strlen(argv[i + 1])
6350 + strlen(prefix) + 1);
6351 strcpy(dirtmp->fname, prefix);
6352 strcat(dirtmp->fname, argv[++i]);
6353 dirtmp->got_name_map = 0;
6355 append_include_chain(pfile, dirtmp, dirtmp);
6357 /* Add directory to end of path for includes. */
6358 if (!strcmp(argv[i], "-idirafter"))
6360 file_name_list *dirtmp;
6363 (file_name_list *) xmalloc(sizeof(file_name_list));
6365 dirtmp->next = 0; /* New one goes on the end */
6366 dirtmp->control_macro = 0;
6367 dirtmp->c_system_include_path = 0;
6370 ("Directory name missing after `-idirafter' option");
6372 dirtmp->fname = argv[++i];
6373 dirtmp->got_name_map = 0;
6375 if (!opts->after_include)
6376 opts->after_include = dirtmp;
6378 opts->last_after_include->next = dirtmp;
6379 opts->last_after_include = dirtmp; /* Tail follows the last one */
6384 if (opts->out_fname)
6385 cpp_fatal("Output filename specified twice");
6387 cpp_fatal("Filename missing after -o option");
6388 opts->out_fname = argv[++i];
6389 if (!strcmp(opts->out_fname, "-"))
6390 opts->out_fname = "";
6394 if (!strcmp(argv[i], "-pedantic"))
6395 CPP_PEDANTIC(pfile) = 1;
6396 else if (!strcmp(argv[i], "-pedantic-errors"))
6398 CPP_PEDANTIC(pfile) = 1;
6399 opts->pedantic_errors = 1;
6404 if (!strcmp(argv[i], "-trigraphs"))
6407 opts->no_trigraphs = 0;
6412 if (!strcmp(argv[i], "-lang-c"))
6413 opts->cplusplus = 0, opts->cplusplus_comments =
6415 if (!strcmp(argv[i], "-lang-c++"))
6416 opts->cplusplus = 1, opts->cplusplus_comments =
6418 if (!strcmp(argv[i], "-lang-c-c++-comments"))
6419 opts->cplusplus = 0, opts->cplusplus_comments =
6421 if (!strcmp(argv[i], "-lang-objc"))
6422 opts->objc = 1, opts->cplusplus =
6423 0, opts->cplusplus_comments = 1;
6424 if (!strcmp(argv[i], "-lang-objc++"))
6425 opts->objc = 1, opts->cplusplus =
6426 1, opts->cplusplus_comments = 1;
6427 if (!strcmp(argv[i], "-lang-asm"))
6429 if (!strcmp(argv[i], "-lint"))
6431 if (!strcmp(argv[i], "-lang-chill"))
6432 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6433 opts->traditional = 1, opts->no_trigraphs = 1;
6437 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6441 opts->inhibit_warnings = 1;
6445 if (!strcmp(argv[i], "-Wtrigraphs"))
6446 opts->warn_trigraphs = 1;
6447 else if (!strcmp(argv[i], "-Wno-trigraphs"))
6448 opts->warn_trigraphs = 0;
6449 else if (!strcmp(argv[i], "-Wcomment"))
6450 opts->warn_comments = 1;
6451 else if (!strcmp(argv[i], "-Wno-comment"))
6452 opts->warn_comments = 0;
6453 else if (!strcmp(argv[i], "-Wcomments"))
6454 opts->warn_comments = 1;
6455 else if (!strcmp(argv[i], "-Wno-comments"))
6456 opts->warn_comments = 0;
6457 else if (!strcmp(argv[i], "-Wtraditional"))
6458 opts->warn_stringify = 1;
6459 else if (!strcmp(argv[i], "-Wno-traditional"))
6460 opts->warn_stringify = 0;
6461 else if (!strcmp(argv[i], "-Wimport"))
6462 opts->warn_import = 1;
6463 else if (!strcmp(argv[i], "-Wno-import"))
6464 opts->warn_import = 0;
6465 else if (!strcmp(argv[i], "-Werror"))
6466 opts->warnings_are_errors = 1;
6467 else if (!strcmp(argv[i], "-Wno-error"))
6468 opts->warnings_are_errors = 0;
6469 else if (!strcmp(argv[i], "-Wall"))
6471 opts->warn_trigraphs = 1;
6472 opts->warn_comments = 1;
6477 /* The style of the choices here is a bit mixed.
6478 * The chosen scheme is a hybrid of keeping all options in one string
6479 * and specifying each option in a separate argument:
6480 * -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6481 * -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6482 * -M[M][G][D file]. This is awkward to handle in specs, and is not
6484 /* ??? -MG must be specified in addition to one of -M or -MM.
6485 * This can be relaxed in the future without breaking anything.
6486 * The converse isn't true. */
6488 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6489 if (!strcmp(argv[i], "-MG"))
6491 opts->print_deps_missing_files = 1;
6494 if (!strcmp(argv[i], "-M"))
6495 opts->print_deps = 2;
6496 else if (!strcmp(argv[i], "-MM"))
6497 opts->print_deps = 1;
6498 else if (!strcmp(argv[i], "-MD"))
6499 opts->print_deps = 2;
6500 else if (!strcmp(argv[i], "-MMD"))
6501 opts->print_deps = 1;
6502 /* For -MD and -MMD options, write deps on file named by next arg. */
6503 if (!strcmp(argv[i], "-MD") || !strcmp(argv[i], "-MMD"))
6506 cpp_fatal("Filename missing after %s option",
6508 opts->deps_file = argv[++i];
6512 /* For -M and -MM, write deps on standard output
6513 * and suppress the usual output. */
6514 opts->no_output = 1;
6520 char *p = argv[i] + 2;
6523 while ((c = *p++) != 0)
6525 /* Arg to -d specifies what parts of macros to dump */
6529 opts->dump_macros = dump_only;
6530 opts->no_output = 1;
6533 opts->dump_macros = dump_names;
6536 opts->dump_macros = dump_definitions;
6544 if (argv[i][2] == '3')
6545 opts->debug_output = 1;
6549 fprintf(stderr, "GNU CPP version %s", version_string);
6550 #ifdef TARGET_VERSION
6553 fprintf(stderr, "\n");
6558 opts->print_include_names = 1;
6562 if (argv[i][2] != 0)
6563 push_pending(pfile, "-D", argv[i] + 2);
6564 else if (i + 1 == argc)
6565 cpp_fatal("Macro name missing after -D option");
6567 i++, push_pending(pfile, "-D", argv[i]);
6572 if (!strcmp(argv[i], "-a"))
6575 cpp_fatal("Filename missing after `-a` option");
6576 else if (strcmp(argv[++i], "/dev/null"))
6578 opts->watchfile = argv[i];
6588 if (argv[i][2] != 0)
6590 else if (i + 1 == argc)
6591 cpp_fatal("Assertion missing after -A option");
6595 if (!strcmp(p, "-"))
6597 struct cpp_pending **ptr;
6599 /* -A- eliminates all predefined macros and assertions.
6600 * Let's include also any that were specified earlier
6601 * on the command line. That way we can get rid of any
6602 * that were passed automatically in from GCC. */
6604 opts->inhibit_predefs = 1;
6605 for (ptr = &opts->pending; *ptr;)
6607 struct cpp_pending *pend = *ptr;
6609 if (pend->cmd && pend->cmd[0] == '-'
6610 && (pend->cmd[1] == 'D'
6611 || pend->cmd[1] == 'A'))
6622 push_pending(pfile, "-A", p);
6627 case 'U': /* JF #undef something */
6628 if (argv[i][2] != 0)
6629 push_pending(pfile, "-U", argv[i] + 2);
6630 else if (i + 1 == argc)
6631 cpp_fatal("Macro name missing after -U option");
6633 push_pending(pfile, "-U", argv[i + 1]), i++;
6637 opts->put_out_comments = 1;
6640 case 'E': /* -E comes from cc -E; ignore it. */
6644 opts->no_line_commands = 1;
6647 case '$': /* Don't include $ in identifiers. */
6648 opts->dollars_in_ident = 0;
6651 case 'I': /* Add directory to path for includes. */
6653 file_name_list *dirtmp;
6655 if (!CPP_OPTIONS(pfile)->ignore_srcdir
6656 && !strcmp(argv[i] + 2, "-"))
6658 CPP_OPTIONS(pfile)->ignore_srcdir = 1;
6659 /* Don't use any preceding -I directories for #include <...>. */
6660 CPP_OPTIONS(pfile)->first_bracket_include = 0;
6665 (file_name_list *) xmalloc(sizeof(file_name_list));
6667 dirtmp->next = 0; /* New one goes on the end */
6668 dirtmp->control_macro = 0;
6669 dirtmp->c_system_include_path = 0;
6670 if (argv[i][2] != 0)
6671 dirtmp->fname = argv[i] + 2;
6672 else if (i + 1 == argc)
6674 ("Directory name missing after -I option");
6676 dirtmp->fname = argv[++i];
6677 dirtmp->got_name_map = 0;
6678 append_include_chain(pfile, dirtmp, dirtmp);
6684 if (!strcmp(argv[i], "-nostdinc"))
6685 /* -nostdinc causes no default include directories.
6686 * You must specify all include-file directories with -I. */
6687 opts->no_standard_includes = 1;
6688 else if (!strcmp(argv[i], "-nostdinc++"))
6689 /* -nostdinc++ causes no default C++-specific include directories. */
6690 opts->no_standard_cplusplus_includes = 1;
6694 /* Sun compiler passes undocumented switch "-undef".
6695 * Let's assume it means to inhibit the predefined symbols. */
6696 opts->inhibit_predefs = 1;
6699 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6700 if (!opts->in_fname)
6702 opts->in_fname = "";
6705 else if (!opts->out_fname)
6707 opts->out_fname = "";
6709 } /* else fall through into error */
6719 cpp_finish(cpp_reader * pfile)
6721 struct cpp_options *opts = CPP_OPTIONS(pfile);
6723 if (opts->print_deps)
6725 /* Stream on which to print the dependency information. */
6728 /* Don't actually write the deps file if compilation has failed. */
6729 if (pfile->errors == 0)
6731 const char *deps_mode =
6732 opts->print_deps_append ? "ab" : "wb";
6734 if (!opts->deps_file)
6735 deps_stream = stdout;
6736 else if (!(deps_stream = fopen(opts->deps_file, deps_mode)))
6737 cpp_pfatal_with_name(pfile, opts->deps_file);
6738 fputs(pfile->deps_buffer, deps_stream);
6739 putc('\n', deps_stream);
6740 if (opts->deps_file)
6742 if (ferror(deps_stream) || fclose(deps_stream) != 0)
6743 cpp_fatal("I/O error on output");
6750 do_assert(cpp_reader * pfile, struct directive *keyword __UNUSED__,
6751 unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
6753 long symstart; /* remember where symbol name starts */
6755 int sym_length; /* and how long it is */
6756 struct arglist *tokens = NULL;
6758 if (CPP_PEDANTIC(pfile) && CPP_OPTIONS(pfile)->done_initializing
6759 && !CPP_BUFFER(pfile)->system_header_p)
6760 cpp_pedwarn(pfile, "ANSI C does not allow `#assert'");
6762 cpp_skip_hspace(pfile);
6763 symstart = CPP_WRITTEN(pfile); /* remember where it starts */
6764 parse_name(pfile, GETC());
6765 sym_length = check_macro_name(pfile, pfile->token_buffer + symstart,
6768 cpp_skip_hspace(pfile);
6771 cpp_error(pfile, "missing token-sequence in `#assert'");
6777 tokens = read_token_list(pfile, &error_flag);
6782 cpp_error(pfile, "empty token-sequence in `#assert'");
6785 cpp_skip_hspace(pfile);
6787 if (c != EOF && c != '\n')
6788 cpp_pedwarn(pfile, "junk at end of `#assert'");
6789 skip_rest_of_line(pfile);
6792 /* If this name isn't already an assertion name, make it one.
6793 * Error if it was already in use in some other way. */
6796 ASSERTION_HASHNODE *hp;
6797 const char *symname = (char *)pfile->token_buffer + symstart;
6799 hashf(symname, sym_length, ASSERTION_HASHSIZE);
6800 struct tokenlist_list *value =
6801 (struct tokenlist_list *)xmalloc(sizeof(struct tokenlist_list));
6803 hp = assertion_lookup(pfile, symname, sym_length, hashcode);
6806 if (sym_length == 7 && !strncmp(symname, "defined", sym_length))
6807 cpp_error(pfile, "`defined' redefined as assertion");
6808 hp = assertion_install(pfile, symname, sym_length, hashcode);
6810 /* Add the spec'd token-sequence to the list of such. */
6811 value->tokens = tokens;
6812 value->next = hp->value;
6815 CPP_SET_WRITTEN(pfile, symstart); /* Pop */
6818 CPP_SET_WRITTEN(pfile, symstart); /* Pop */
6819 skip_rest_of_line(pfile);
6824 do_unassert(cpp_reader * pfile, struct directive *keyword __UNUSED__,
6825 unsigned char *buf __UNUSED__, unsigned char *limit __UNUSED__)
6827 long symstart; /* remember where symbol name starts */
6828 int sym_length; /* and how long it is */
6831 struct arglist *tokens = NULL;
6832 int tokens_specified = 0;
6834 if (CPP_PEDANTIC(pfile) && CPP_OPTIONS(pfile)->done_initializing
6835 && !CPP_BUFFER(pfile)->system_header_p)
6836 cpp_pedwarn(pfile, "ANSI C does not allow `#unassert'");
6838 cpp_skip_hspace(pfile);
6840 symstart = CPP_WRITTEN(pfile); /* remember where it starts */
6841 parse_name(pfile, GETC());
6842 sym_length = check_macro_name(pfile, pfile->token_buffer + symstart,
6845 cpp_skip_hspace(pfile);
6850 tokens = read_token_list(pfile, &error_flag);
6855 cpp_error(pfile, "empty token list in `#unassert'");
6858 tokens_specified = 1;
6860 cpp_skip_hspace(pfile);
6862 if (c != EOF && c != '\n')
6863 cpp_error(pfile, "junk at end of `#unassert'");
6864 skip_rest_of_line(pfile);
6867 ASSERTION_HASHNODE *hp;
6868 const char *symname = (char *)pfile->token_buffer + symstart;
6870 hashf(symname, sym_length, ASSERTION_HASHSIZE);
6871 struct tokenlist_list *tail, *prev;
6873 hp = assertion_lookup(pfile, symname, sym_length, hashcode);
6877 /* If no token list was specified, then eliminate this assertion
6879 if (!tokens_specified)
6880 delete_assertion(hp);
6883 /* If a list of tokens was given, then delete any matching list. */
6889 struct tokenlist_list *next = tail->next;
6891 if (compare_token_lists(tail->tokens, tokens))
6896 hp->value = tail->next;
6897 free_token_list(tail->tokens);
6909 CPP_SET_WRITTEN(pfile, symstart); /* Pop */
6912 CPP_SET_WRITTEN(pfile, symstart); /* Pop */
6913 skip_rest_of_line(pfile);
6917 /* Test whether there is an assertion named NAME
6918 * and optionally whether it has an asserted token list TOKENS.
6919 * NAME is not null terminated; its length is SYM_LENGTH.
6920 * If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6923 check_assertion(cpp_reader * pfile, const char *name, int sym_length,
6924 int tokens_specified, struct arglist *tokens)
6926 ASSERTION_HASHNODE *hp;
6927 int hashcode = hashf(name, sym_length, ASSERTION_HASHSIZE);
6929 if (CPP_PEDANTIC(pfile) && !CPP_BUFFER(pfile)->system_header_p)
6930 cpp_pedwarn(pfile, "ANSI C does not allow testing assertions");
6932 hp = assertion_lookup(pfile, name, sym_length, hashcode);
6934 /* It is not an assertion; just return false. */
6937 /* If no token list was specified, then value is 1. */
6938 if (!tokens_specified)
6942 struct tokenlist_list *tail;
6946 /* If a list of tokens was given,
6947 * then succeed if the assertion records a matching list. */
6951 if (compare_token_lists(tail->tokens, tokens))
6956 /* Fail if the assertion has no matching list. */
6961 /* Compare two lists of tokens for equality including order of tokens. */
6964 compare_token_lists(struct arglist *l1, struct arglist *l2)
6968 if (l1->length != l2->length)
6970 if (strncmp(l1->name, l2->name, l1->length))
6976 /* Succeed if both lists end at the same time. */
6981 reverse_token_list(struct arglist *tokens)
6983 struct arglist *prev = 0, *cur, *next;
6985 for (cur = tokens; cur; cur = next)
6994 /* Read a space-separated list of tokens ending in a close parenthesis.
6995 * Return a list of strings, in the order they were written.
6996 * (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
6998 static struct arglist *
6999 read_token_list(cpp_reader * pfile, int *error_flag)
7001 struct arglist *token_ptrs = 0;
7006 FORWARD(1); /* Skip '(' */
7008 /* Loop over the assertion value tokens. */
7011 struct arglist *temp;
7012 long name_written = CPP_WRITTEN(pfile);
7015 cpp_skip_hspace(pfile);
7019 /* Find the end of the token. */
7032 else if (c == '"' || c == '\'')
7035 cpp_get_token(pfile);
7041 while (c != EOF && !is_space[c] && c != '(' && c != ')'
7042 && c != '"' && c != '\'')
7051 length = CPP_WRITTEN(pfile) - name_written;
7052 temp = (struct arglist *)xmalloc(sizeof(struct arglist) + length + 1);
7054 temp->name = (char *)(temp + 1);
7055 memcpy(temp->name, (char *)(pfile->token_buffer + name_written),
7057 temp->name[length] = 0;
7058 temp->next = token_ptrs;
7060 temp->length = length;
7062 CPP_ADJUST_WRITTEN(pfile, -length); /* pop */
7064 if (c == EOF || c == '\n')
7067 "unterminated token sequence following `#' operator");
7072 /* We accumulated the names in reverse order.
7073 * Now reverse them to get the proper order. */
7074 return reverse_token_list(token_ptrs);
7078 free_token_list(struct arglist *tokens)
7082 struct arglist *next = tokens->next;
7090 /* Get the file-mode and data size of the file open on FD
7091 * and store them in *MODE_POINTER and *SIZE_POINTER. */
7094 file_size_and_mode(int fd, int *mode_pointer, long int *size_pointer)
7098 if (fstat(fd, &sbuf) < 0)
7101 *mode_pointer = sbuf.st_mode;
7103 *size_pointer = sbuf.st_size;
7107 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7108 * retrying if necessary. Return a negative value if an error occurs,
7109 * otherwise return the actual number of bytes read,
7110 * which must be LEN unless end-of-file was reached. */
7113 safe_read(int desc, char *ptr, int len)
7119 int nchars = read(desc, ptr, left);
7138 savestring(const char *input)
7140 unsigned size = strlen(input);
7141 char *output = (char *)xmalloc(size + 1);
7143 strcpy(output, input);
7147 /* Initialize PMARK to remember the current position of PFILE. */
7149 parse_set_mark(struct parse_marker *pmark, cpp_reader * pfile)
7151 cpp_buffer *pbuf = CPP_BUFFER(pfile);
7153 pmark->next = pbuf->marks;
7154 pbuf->marks = pmark;
7156 pmark->position = pbuf->cur - pbuf->buf;
7159 /* Cleanup PMARK - we no longer need it. */
7161 parse_clear_mark(struct parse_marker *pmark)
7163 struct parse_marker **pp = &pmark->buf->marks;
7165 for (;; pp = &(*pp)->next)
7168 cpp_fatal("internal error", "in parse_set_mark");
7175 /* Backup the current position of PFILE to that saved in PMARK. */
7178 parse_goto_mark(struct parse_marker *pmark, cpp_reader * pfile)
7180 cpp_buffer *pbuf = CPP_BUFFER(pfile);
7182 if (pbuf != pmark->buf)
7183 cpp_fatal("internal error %s", "parse_goto_mark");
7184 pbuf->cur = pbuf->buf + pmark->position;
7187 /* Reset PMARK to point to the current position of PFILE. (Same
7188 * as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7191 parse_move_mark(struct parse_marker *pmark, cpp_reader * pfile)
7193 cpp_buffer *pbuf = CPP_BUFFER(pfile);
7195 if (pbuf != pmark->buf)
7196 cpp_fatal("internal error %s", "parse_move_mark");
7197 pmark->position = pbuf->cur - pbuf->buf;
7201 cpp_read_check_assertion(cpp_reader * pfile)
7203 int name_start = CPP_WRITTEN(pfile);
7204 int name_length, name_written;
7207 FORWARD(1); /* Skip '#' */
7208 cpp_skip_hspace(pfile);
7209 parse_name(pfile, GETC());
7210 name_written = CPP_WRITTEN(pfile);
7211 name_length = name_written - name_start;
7212 cpp_skip_hspace(pfile);
7213 if (CPP_BUF_PEEK(CPP_BUFFER(pfile)) == '(')
7216 struct arglist *token_ptrs = read_token_list(pfile, &error_flag);
7218 result = check_assertion(pfile,
7219 (char *)pfile->token_buffer + name_start,
7220 name_length, 1, token_ptrs);
7223 result = check_assertion(pfile,
7224 (char *)pfile->token_buffer + name_start,
7225 name_length, 0, NULL);
7226 CPP_ADJUST_WRITTEN(pfile, -name_length); /* pop */
7231 cpp_print_file_and_line(cpp_reader * pfile)
7233 cpp_buffer *ip = cpp_file_buffer(pfile);
7239 cpp_buf_line_and_col(ip, &line, &col);
7240 cpp_file_line_for_message(pfile, ip->nominal_fname,
7241 line, pfile->show_column ? col : -1);
7246 cpp_error_v(cpp_reader * pfile, const char *msg, va_list args)
7248 cpp_print_containing_files(pfile);
7249 cpp_print_file_and_line(pfile);
7250 cpp_message_v(pfile, 1, msg, args);
7254 cpp_error(cpp_reader * pfile, const char *msg, ...)
7258 va_start(args, msg);
7260 cpp_error_v(pfile, msg, args);
7265 /* Print error message but don't count it. */
7268 cpp_warning_v(cpp_reader * pfile, const char *msg, va_list args)
7270 if (CPP_OPTIONS(pfile)->inhibit_warnings)
7273 if (CPP_OPTIONS(pfile)->warnings_are_errors)
7276 cpp_print_containing_files(pfile);
7277 cpp_print_file_and_line(pfile);
7278 cpp_message_v(pfile, 0, msg, args);
7282 cpp_warning(cpp_reader * pfile, const char *msg, ...)
7286 va_start(args, msg);
7288 cpp_warning_v(pfile, msg, args);
7293 /* Print an error message and maybe count it. */
7296 cpp_pedwarn(cpp_reader * pfile, const char *msg, ...)
7300 va_start(args, msg);
7302 if (CPP_OPTIONS(pfile)->pedantic_errors)
7303 cpp_error_v(pfile, msg, args);
7305 cpp_warning_v(pfile, msg, args);
7311 cpp_error_with_line(cpp_reader * pfile, int line, int column, const char *msg)
7313 cpp_buffer *ip = cpp_file_buffer(pfile);
7315 cpp_print_containing_files(pfile);
7318 cpp_file_line_for_message(pfile, ip->nominal_fname, line, column);
7320 cpp_message(pfile, 1, msg, NULL, NULL, NULL);
7324 cpp_warning_with_line(cpp_reader * pfile, int line, int column, const char *msg)
7328 if (CPP_OPTIONS(pfile)->inhibit_warnings)
7331 if (CPP_OPTIONS(pfile)->warnings_are_errors)
7334 cpp_print_containing_files(pfile);
7336 ip = cpp_file_buffer(pfile);
7339 cpp_file_line_for_message(pfile, ip->nominal_fname, line, column);
7341 cpp_message(pfile, 0, msg, NULL, NULL, NULL);
7345 cpp_pedwarn_with_line(cpp_reader * pfile, int line, int column, const char *msg)
7347 if (CPP_OPTIONS(pfile)->pedantic_errors)
7348 cpp_error_with_line(pfile, column, line, msg);
7350 cpp_warning_with_line(pfile, line, column, msg);
7353 /* Report a warning (or an error if pedantic_errors)
7354 * giving specified file name and line number, not current. */
7357 cpp_pedwarn_with_file_and_line(cpp_reader * pfile,
7358 const char *file, int line,
7359 const char *msg, const char *arg1,
7360 const char *arg2, const char *arg3)
7362 if (!CPP_OPTIONS(pfile)->pedantic_errors
7363 && CPP_OPTIONS(pfile)->inhibit_warnings)
7366 cpp_file_line_for_message(pfile, file, line, -1);
7367 cpp_message(pfile, CPP_OPTIONS(pfile)->pedantic_errors,
7368 msg, arg1, arg2, arg3);
7371 /* This defines "errno" properly for VMS, and gives us EACCES. */
7378 #ifndef HAVE_STRERROR
7379 extern int sys_nerr;
7382 extern const char *const sys_errlist[];
7385 extern char *sys_errlist[];
7388 #endif /* HAVE_STRERROR */
7391 * my_strerror - return the descriptive text associated with an `errno' code.
7395 my_strerror(int errnum)
7399 #ifndef HAVE_STRERROR
7400 result = ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7402 result = strerror(errnum);
7406 result = "undocumented I/O error";
7411 /* Error including a message from `errno'. */
7414 cpp_error_from_errno(cpp_reader * pfile, const char *name)
7416 cpp_buffer *ip = cpp_file_buffer(pfile);
7418 cpp_print_containing_files(pfile);
7421 cpp_file_line_for_message(pfile, ip->nominal_fname, ip->lineno, -1);
7423 cpp_message(pfile, 1, "%s: %s", name, my_strerror(errno), NULL);
7427 cpp_perror_with_name(cpp_reader * pfile, const char *name)
7429 cpp_message(pfile, 1, "%s: %s: %s", progname, name, my_strerror(errno));
7433 * No pre-compiled header file support.
7435 * Possibly different enum token codes for each C/C++ token.
7437 * Should clean up remaining directives to that do_XXX functions
7438 * only take two arguments and all have command_reads_line.
7440 * Find and cleanup remaining uses of static variables,
7442 * Support for trigraphs.
7444 * Support -dM flag (dump_all_macros).
7446 * Support for_lint flag.
7449 extern cpp_options options;
7452 using_file(const char *filename)
7456 f = fopen(options.watchfile, "a");