2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
42 #ifndef GET_ENV_PATH_LIST
43 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
46 extern char *update_path PARAMS ((char *, char *));
50 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
51 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
54 /* By default, colon separates directories in a path. */
55 #ifndef PATH_SEPARATOR
56 #define PATH_SEPARATOR ':'
59 #ifndef STANDARD_INCLUDE_DIR
60 #define STANDARD_INCLUDE_DIR "/usr/include"
63 /* Symbols to predefine. */
66 static char *predefs = CPP_PREDEFINES;
68 static char *predefs = "";
71 /* We let tm.h override the types used here, to handle trivial differences
72 such as the choice of unsigned int or long unsigned int for size_t.
73 When machines start needing nontrivial differences in the size type,
74 it would be best to do something here to figure out automatically
75 from other information what type to use. */
77 /* The string value for __SIZE_TYPE__. */
80 #define SIZE_TYPE "long unsigned int"
83 /* The string value for __PTRDIFF_TYPE__. */
86 #define PTRDIFF_TYPE "long int"
89 /* The string value for __WCHAR_TYPE__. */
92 #define WCHAR_TYPE "int"
94 #define CPP_WCHAR_TYPE(PFILE) \
95 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
97 /* The string value for __USER_LABEL_PREFIX__ */
99 #ifndef USER_LABEL_PREFIX
100 #define USER_LABEL_PREFIX ""
103 /* The string value for __REGISTER_PREFIX__ */
105 #ifndef REGISTER_PREFIX
106 #define REGISTER_PREFIX ""
109 /* In the definition of a #assert name, this structure forms
110 a list of the individual values asserted.
111 Each value is itself a list of "tokens".
112 These are strings that are compared by name. */
114 struct tokenlist_list {
115 struct tokenlist_list *next;
116 struct arglist *tokens;
119 struct assertion_hashnode {
120 struct assertion_hashnode *next; /* double links for easy deletion */
121 struct assertion_hashnode *prev;
122 /* also, a back pointer to this node's hash
123 chain is kept, in case the node is the head
124 of the chain and gets deleted. */
125 struct assertion_hashnode **bucket_hdr;
126 int length; /* length of token, for quick comparison */
127 U_CHAR *name; /* the actual name */
128 /* List of token-sequences. */
129 struct tokenlist_list *value;
132 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
133 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
135 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
136 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
137 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
138 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
139 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
140 (Note that it is false while we're expanding marco *arguments*.) */
141 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
143 /* Move all backslash-newline pairs out of embarrassing places.
144 Exchange all such pairs following BP
145 with any potentially-embarrassing characters that follow them.
146 Potentially-embarrassing characters are / and *
147 (because a backslash-newline inside a comment delimiter
148 would cause it not to be recognized). */
150 #define NEWLINE_FIX \
151 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
153 /* Same, but assume we've already read the potential '\\' into C. */
154 #define NEWLINE_FIX1(C) do { \
155 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
159 struct cpp_pending *next;
164 /* Forward declarations. */
166 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
168 static char *my_strerror PROTO ((int));
169 static void make_assertion PROTO ((cpp_reader *, char *, U_CHAR *));
170 static void path_include PROTO ((cpp_reader *, char *));
171 static void initialize_builtins PROTO ((cpp_reader *));
172 static void initialize_char_syntax PROTO ((void));
174 static void trigraph_pcp ();
176 static void validate_else PROTO ((cpp_reader *, char *));
177 static int comp_def_part PROTO ((int, U_CHAR *, int, U_CHAR *,
180 extern void fancy_abort ();
182 static int check_macro_name PROTO ((cpp_reader *, U_CHAR *, char *));
183 static int compare_defs PROTO ((cpp_reader *,
184 DEFINITION *, DEFINITION *));
185 static int compare_token_lists PROTO ((struct arglist *,
187 static HOST_WIDE_INT eval_if_expression PROTO ((cpp_reader *, U_CHAR *, int));
188 static int change_newlines PROTO ((U_CHAR *, int));
189 static struct arglist *read_token_list PROTO ((cpp_reader *, int *));
190 static void free_token_list PROTO ((struct arglist *));
191 static void push_macro_expansion PARAMS ((cpp_reader *,
192 U_CHAR *, int, HASHNODE *));
193 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
195 static void conditional_skip PROTO ((cpp_reader *, int,
196 enum node_type, U_CHAR *));
197 static void skip_if_group PROTO ((cpp_reader *, int));
198 static int parse_name PARAMS ((cpp_reader *, int));
199 static void print_help PROTO ((void));
201 /* Last arg to output_line_command. */
202 enum file_change_code {same_file, enter_file, leave_file};
204 /* External declarations. */
206 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
208 extern char *version_string;
209 extern struct tm *localtime ();
212 /* #include "file" looks in source file dir, then stack. */
213 /* #include <file> just looks in the stack. */
214 /* -I directories are added to the end, then the defaults are added. */
216 static struct default_include {
217 char *fname; /* The name of the directory. */
218 char *component; /* The component containing the directory */
219 int cplusplus; /* Only look here if we're compiling C++. */
220 int cxx_aware; /* Includes in this directory don't need to
221 be wrapped in extern "C" when compiling
223 } include_defaults_array[]
224 #ifdef INCLUDE_DEFAULTS
228 /* Pick up GNU C++ specific include files. */
229 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
231 /* This is the dir for fixincludes. Put it just before
232 the files that we fix. */
233 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
234 /* For cross-compilation, this dir name is generated
235 automatically in Makefile.in. */
236 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
237 #ifdef TOOL_INCLUDE_DIR
238 /* This is another place that the target system's headers might be. */
239 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
241 #else /* not CROSS_COMPILE */
242 #ifdef LOCAL_INCLUDE_DIR
243 /* This should be /usr/local/include and should come before
244 the fixincludes-fixed header files. */
245 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
247 #ifdef TOOL_INCLUDE_DIR
248 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
249 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
250 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
252 /* This is the dir for fixincludes. Put it just before
253 the files that we fix. */
254 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
255 /* Some systems have an extra dir of include files. */
256 #ifdef SYSTEM_INCLUDE_DIR
257 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
259 #ifndef STANDARD_INCLUDE_COMPONENT
260 #define STANDARD_INCLUDE_COMPONENT 0
262 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
263 #endif /* not CROSS_COMPILE */
266 #endif /* no INCLUDE_DEFAULTS */
268 /* `struct directive' defines one #-directive, including how to handle it. */
271 int length; /* Length of name */
272 int (*func) /* Function to handle directive */
273 PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
274 char *name; /* Name of directive */
275 enum node_type type; /* Code which describes which directive. */
276 char command_reads_line; /* One if rest of line is read by func. */
279 /* These functions are declared to return int instead of void since they
280 are going to be placed in a table and some old compilers have trouble with
281 pointers to functions returning void. */
283 static int do_define PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
284 static int do_line PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
285 static int do_include PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
286 static int do_undef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
287 static int do_error PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
288 static int do_pragma PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
289 static int do_ident PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
290 static int do_if PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
291 static int do_xifdef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
292 static int do_else PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
293 static int do_elif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
294 static int do_endif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
295 #ifdef SCCS_DIRECTIVE
296 static int do_sccs PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
298 static int do_assert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
299 static int do_unassert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
300 static int do_warning PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
302 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
303 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
305 /* Here is the actual list of #-directives, most-often-used first.
306 The initialize_builtins function assumes #define is the very first. */
308 static struct directive directive_table[] = {
309 { 6, do_define, "define", T_DEFINE, 0},
310 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
311 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
312 { 7, do_include, "include", T_INCLUDE, 1},
313 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
314 { 6, do_include, "import", T_IMPORT, 1},
315 { 5, do_endif, "endif", T_ENDIF, 1},
316 { 4, do_else, "else", T_ELSE, 1},
317 { 2, do_if, "if", T_IF, 1},
318 { 4, do_elif, "elif", T_ELIF, 1},
319 { 5, do_undef, "undef", T_UNDEF, 0},
320 { 5, do_error, "error", T_ERROR, 0},
321 { 7, do_warning, "warning", T_WARNING, 0},
322 { 6, do_pragma, "pragma", T_PRAGMA, 0},
323 { 4, do_line, "line", T_LINE, 1},
324 { 5, do_ident, "ident", T_IDENT, 1},
325 #ifdef SCCS_DIRECTIVE
326 { 4, do_sccs, "sccs", T_SCCS, 0},
328 { 6, do_assert, "assert", T_ASSERT, 1},
329 { 8, do_unassert, "unassert", T_UNASSERT, 1},
330 { -1, 0, "", T_UNUSED, 0},
333 /* table to tell if char can be part of a C identifier. */
334 U_CHAR is_idchar[256] = { 0 };
335 /* table to tell if char can be first char of a c identifier. */
336 U_CHAR is_idstart[256] = { 0 };
337 /* table to tell if c is horizontal space. */
338 U_CHAR is_hor_space[256] = { 0 };
339 /* table to tell if c is horizontal or vertical space. */
340 U_CHAR is_space[256] = { 0 };
342 /* Initialize syntactic classifications of characters. */
344 initialize_char_syntax ()
349 * Set up is_idchar and is_idstart tables. These should be
350 * faster than saying (is_alpha (c) || c == '_'), etc.
351 * Set up these things before calling any routines tthat
353 * XXX We should setlocale(LC_CTYPE, "C") here for safety.
355 for (i = 0; i < 256; i++)
357 is_idchar[i] = ISALNUM (i);
358 is_idstart[i] = ISALPHA (i);
364 /* These will be reset later if -$ is in effect. */
368 /* horizontal space table */
369 is_hor_space[' '] = 1;
370 is_hor_space['\t'] = 1;
371 is_hor_space['\v'] = 1;
372 is_hor_space['\f'] = 1;
373 is_hor_space['\r'] = 1;
384 /* Place into PFILE a quoted string representing the string SRC.
385 Caller must reserve enough space in pfile->token_buffer. */
388 quote_string (pfile, src)
394 CPP_PUTC_Q (pfile, '\"');
396 switch ((c = *src++))
400 CPP_PUTC_Q (pfile, c);
403 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
404 CPP_ADJUST_WRITTEN (pfile, 4);
410 CPP_PUTC_Q (pfile, '\\');
411 CPP_PUTC_Q (pfile, c);
415 CPP_PUTC_Q (pfile, '\"');
416 CPP_NUL_TERMINATE_Q (pfile);
421 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
424 cpp_grow_buffer (pfile, n)
428 long old_written = CPP_WRITTEN (pfile);
429 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
430 pfile->token_buffer = (U_CHAR *)
431 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
432 CPP_SET_WRITTEN (pfile, old_written);
437 * process a given definition string, for initialization
438 * If STR is just an identifier, define it with value 1.
439 * If STR has anything after the identifier, then it should
440 * be identifier=definition.
444 cpp_define (pfile, str)
454 cpp_error (pfile, "malformed option `-D %s'", str);
457 while (is_idchar[*++p])
460 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
463 p = (U_CHAR *) str; /* Error */
467 buf = (U_CHAR *) alloca (p - buf + 4);
468 strcpy ((char *)buf, str);
469 strcat ((char *)buf, " 1");
473 cpp_error (pfile, "malformed option `-D %s'", str);
479 /* Copy the entire option so we can modify it. */
480 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
481 strncpy (buf, str, p - str);
482 /* Change the = to a space. */
484 /* Scan for any backslash-newline and remove it. */
489 if (*p == '\\' && p[1] == '\n')
497 do_define (pfile, NULL, buf, buf + strlen (buf));
500 /* Process the string STR as if it appeared as the body of a #assert.
501 OPTION is the option name for which STR was the argument. */
504 make_assertion (pfile, option, str)
511 /* Copy the entire option so we can modify it. */
512 buf = (U_CHAR *) alloca (strlen (str) + 1);
513 strcpy ((char *) buf, str);
514 /* Scan for any backslash-newline and remove it. */
518 if (*p == '\\' && p[1] == '\n')
527 if (!is_idstart[*p]) {
528 cpp_error (pfile, "malformed option `%s %s'", option, str);
531 while (is_idchar[*++p])
533 while (*p == ' ' || *p == '\t') p++;
534 if (! (*p == 0 || *p == '(')) {
535 cpp_error (pfile, "malformed option `%s %s'", option, str);
539 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
541 do_assert (pfile, NULL, NULL, NULL);
542 cpp_pop_buffer (pfile);
546 /* Given a colon-separated list of file names PATH,
547 add all the names to the search path for include files. */
550 path_include (pfile, path)
563 /* Find the end of this name. */
564 while (*q != 0 && *q != PATH_SEPARATOR) q++;
566 /* An empty name in the path stands for the current directory. */
567 name = (char *) xmalloc (2);
571 /* Otherwise use the directory that is named. */
572 name = (char *) xmalloc (q - p + 1);
573 bcopy (p, name, q - p);
577 append_include_chain (pfile,
578 &(CPP_OPTIONS (pfile)->bracket_include), name, 0);
580 /* Advance past this name. */
584 /* Skip the colon. */
590 cpp_options_init (opts)
593 bzero ((char *) opts, sizeof *opts);
594 opts->in_fname = NULL;
595 opts->out_fname = NULL;
597 opts->dollars_in_ident = 1;
598 initialize_char_syntax ();
600 opts->no_line_commands = 0;
601 opts->no_trigraphs = 1;
602 opts->put_out_comments = 0;
603 opts->print_include_names = 0;
604 opts->dump_macros = dump_none;
608 opts->cplusplus_comments = 1;
615 opts->pedantic_errors = 0;
616 opts->inhibit_warnings = 0;
617 opts->warn_comments = 0;
618 opts->warn_import = 1;
619 opts->warnings_are_errors = 0;
623 null_underflow (pfile)
624 cpp_reader *pfile ATTRIBUTE_UNUSED;
630 null_cleanup (pbuf, pfile)
631 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
632 cpp_reader *pfile ATTRIBUTE_UNUSED;
638 macro_cleanup (pbuf, pfile)
640 cpp_reader *pfile ATTRIBUTE_UNUSED;
642 HASHNODE *macro = (HASHNODE *) pbuf->data;
643 if (macro->type == T_DISABLED)
644 macro->type = T_MACRO;
645 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
650 /* Assuming we have read '/'.
651 If this is the start of a comment (followed by '*' or '/'),
652 skip to the end of the comment, and return ' '.
653 Return EOF if we reached the end of file before the end of the comment.
654 If not the start of a comment, return '/'. */
657 skip_comment (pfile, linep)
662 while (PEEKC() == '\\' && PEEKN(1) == '\n')
677 while (c == '\\' && PEEKC() == '\n')
681 FORWARD(1), c = GETC();
683 if (prev_c == '*' && c == '/')
685 if (c == '\n' && linep)
689 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
696 return ' '; /* Allow // to be terminated by EOF. */
697 while (c == '\\' && PEEKC() == '\n')
706 /* Don't consider final '\n' to be part of comment. */
716 /* Skip whitespace \-newline and comments. Does not macro-expand. */
719 cpp_skip_hspace (pfile)
729 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
730 cpp_pedwarn (pfile, "%s in preprocessing directive",
731 c == '\f' ? "formfeed" : "vertical tab");
737 c = skip_comment (pfile, NULL);
740 if (c == EOF || c == '/')
743 else if (c == '\\' && PEEKN(1) == '\n') {
746 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
747 && is_hor_space[PEEKN(1)])
753 /* Read the rest of the current line.
754 The line is appended to PFILE's output buffer. */
757 copy_rest_of_line (pfile)
760 struct cpp_options *opts = CPP_OPTIONS (pfile);
777 goto scan_directive_token;
781 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
782 goto scan_directive_token;
786 if (CPP_PEDANTIC (pfile))
787 cpp_pedwarn (pfile, "%s in preprocessing directive",
788 c == '\f' ? "formfeed" : "vertical tab");
794 scan_directive_token:
796 cpp_get_token (pfile);
802 CPP_NUL_TERMINATE (pfile);
806 skip_rest_of_line (pfile)
809 long old = CPP_WRITTEN (pfile);
810 copy_rest_of_line (pfile);
811 CPP_SET_WRITTEN (pfile, old);
814 /* Handle a possible # directive.
815 '#' has already been read. */
818 handle_directive (pfile)
821 register struct directive *kt;
824 U_CHAR *ident, *line_end;
825 long old_written = CPP_WRITTEN (pfile);
827 cpp_skip_hspace (pfile);
830 if (c >= '0' && c <= '9')
832 /* Handle # followed by a line number. */
833 if (CPP_PEDANTIC (pfile))
834 cpp_pedwarn (pfile, "`#' followed by integer");
835 do_line (pfile, NULL, NULL, NULL);
836 goto done_a_directive;
839 /* Now find the directive name. */
840 CPP_PUTC (pfile, '#');
841 parse_name (pfile, GETC());
842 ident = pfile->token_buffer + old_written + 1;
843 ident_length = CPP_PWRITTEN (pfile) - ident;
844 if (ident_length == 0 && PEEKC() == '\n')
846 /* A line of just `#' becomes blank. */
847 goto done_a_directive;
851 if (ident_length == 0 || !is_idstart[*ident]) {
853 while (is_idchar[*p]) {
854 if (*p < '0' || *p > '9')
858 /* Avoid error for `###' and similar cases unless -pedantic. */
860 while (*p == '#' || is_hor_space[*p]) p++;
862 if (pedantic && !lang_asm)
863 cpp_warning (pfile, "invalid preprocessor directive");
869 cpp_error (pfile, "invalid preprocessor directive name");
875 * Decode the keyword and call the appropriate expansion
876 * routine, after moving the input pointer up to the next line.
878 for (kt = directive_table; ; kt++) {
880 goto not_a_directive;
881 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
885 if (kt->command_reads_line)
889 /* Nonzero means do not delete comments within the directive.
890 #define needs this when -traditional. */
891 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
892 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
893 CPP_OPTIONS (pfile)->put_out_comments = comments;
894 after_ident = CPP_WRITTEN (pfile);
895 copy_rest_of_line (pfile);
896 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
899 /* We may want to pass through #define, #pragma, and #include.
900 Other directives may create output, but we don't want the directive
901 itself out, so we pop it now. For example conditionals may emit
902 #failed ... #endfailed stuff. But note that popping the buffer
903 means the parameters to kt->func may point after pfile->limit
904 so these parameters are invalid as soon as something gets appended
905 to the token_buffer. */
907 line_end = CPP_PWRITTEN (pfile);
908 if (! (kt->type == T_DEFINE
909 || kt->type == T_PRAGMA
910 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
911 && CPP_OPTIONS (pfile)->dump_includes)))
912 CPP_SET_WRITTEN (pfile, old_written);
914 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
916 if (kt->type == T_DEFINE)
918 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
920 /* Skip "#define". */
921 U_CHAR *p = pfile->token_buffer + old_written + 7;
923 SKIP_WHITE_SPACE (p);
924 while (is_idchar[*p]) p++;
926 CPP_PUTC (pfile, '\n');
928 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
929 CPP_SET_WRITTEN (pfile, old_written);
939 /* Pass a directive through to the output file.
940 BUF points to the contents of the directive, as a contiguous string.
941 LIMIT points to the first character past the end of the directive.
942 KEYWORD is the keyword-table entry for the directive. */
945 pass_thru_directive (buf, limit, pfile, keyword)
948 struct directive *keyword;
950 register unsigned keyword_length = keyword->length;
952 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
953 CPP_PUTC_Q (pfile, '#');
954 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
955 if (limit != buf && buf[0] != ' ')
956 CPP_PUTC_Q (pfile, ' ');
957 CPP_PUTS_Q (pfile, buf, limit - buf);
959 CPP_PUTS_Q (pfile, '\n');
960 /* Count the line we have just made in the output,
961 to get in sync properly. */
966 /* The arglist structure is built by do_define to tell
967 collect_definition where the argument names begin. That
968 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
969 would contain pointers to the strings x, y, and z.
970 Collect_definition would then build a DEFINITION node,
971 with reflist nodes pointing to the places x, y, and z had
972 appeared. So the arglist is just convenience data passed
973 between these two routines. It is not kept around after
974 the current #define has been processed and entered into the
978 struct arglist *next;
985 /* Read a replacement list for a macro with parameters.
986 Build the DEFINITION structure.
987 Reads characters of text starting at BUF until END.
988 ARGLIST specifies the formal parameters to look for
989 in the text of the definition; NARGS is the number of args
990 in that list, or -1 for a macro name that wants no argument list.
991 MACRONAME is the macro name itself (so we can avoid recursive expansion)
992 and NAMELEN is its length in characters.
994 Note that comments, backslash-newlines, and leading white space
995 have already been deleted from the argument. */
998 collect_expansion (pfile, buf, limit, nargs, arglist)
1000 U_CHAR *buf, *limit;
1002 struct arglist *arglist;
1005 register U_CHAR *p, *lastp, *exp_p;
1006 struct reflist *endpat = NULL;
1007 /* Pointer to first nonspace after last ## seen. */
1009 /* Pointer to first nonspace after last single-# seen. */
1010 U_CHAR *stringify = 0;
1012 int expected_delimiter = '\0';
1014 /* Scan thru the replacement list, ignoring comments and quoted
1015 strings, picking up on the macro calls. It does a linear search
1016 thru the arg list on every potential symbol. Profiling might say
1017 that something smarter should happen. */
1022 /* Find the beginning of the trailing whitespace. */
1024 while (p < limit && is_space[limit[-1]]) limit--;
1026 /* Allocate space for the text in the macro definition.
1027 Leading and trailing whitespace chars need 2 bytes each.
1028 Each other input char may or may not need 1 byte,
1029 so this is an upper bound. The extra 5 are for invented
1030 leading and trailing newline-marker and final null. */
1031 maxsize = (sizeof (DEFINITION)
1033 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1037 defn = (DEFINITION *) xcalloc (1, maxsize);
1039 defn->nargs = nargs;
1040 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1045 /* Add one initial space escape-marker to prevent accidental
1046 token-pasting (often removed by macroexpand). */
1050 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1051 cpp_error (pfile, "`##' at start of macro definition");
1055 /* Process the main body of the definition. */
1057 int skipped_arg = 0;
1058 register U_CHAR c = *p++;
1062 if (!CPP_TRADITIONAL (pfile)) {
1066 if (expected_delimiter != '\0') {
1067 if (c == expected_delimiter)
1068 expected_delimiter = '\0';
1070 expected_delimiter = c;
1074 if (p < limit && expected_delimiter) {
1075 /* In a string, backslash goes through
1076 and makes next char ordinary. */
1082 /* An '@' in a string or character constant stands for itself,
1083 and does not need to be escaped. */
1084 if (!expected_delimiter)
1089 /* # is ordinary inside a string. */
1090 if (expected_delimiter)
1092 if (p < limit && *p == '#') {
1093 /* ##: concatenate preceding and following tokens. */
1094 /* Take out the first #, discard preceding whitespace. */
1096 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1098 /* Skip the second #. */
1100 /* Discard following whitespace. */
1101 SKIP_WHITE_SPACE (p);
1104 cpp_error (pfile, "`##' at end of macro definition");
1105 } else if (nargs >= 0) {
1106 /* Single #: stringify following argument ref.
1107 Don't leave the # in the expansion. */
1109 SKIP_WHITE_SPACE (p);
1110 if (p == limit || ! is_idstart[*p]
1111 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1113 "`#' operator is not followed by a macro argument name");
1120 /* In -traditional mode, recognize arguments inside strings and
1121 character constants, and ignore special properties of #.
1122 Arguments inside strings are considered "stringified", but no
1123 extra quote marks are supplied. */
1127 if (expected_delimiter != '\0') {
1128 if (c == expected_delimiter)
1129 expected_delimiter = '\0';
1131 expected_delimiter = c;
1135 /* Backslash quotes delimiters and itself, but not macro args. */
1136 if (expected_delimiter != 0 && p < limit
1137 && (*p == expected_delimiter || *p == '\\')) {
1144 if (expected_delimiter != '\0') /* No comments inside strings. */
1147 /* If we find a comment that wasn't removed by handle_directive,
1148 this must be -traditional. So replace the comment with
1152 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1155 /* Mark this as a concatenation-point, as if it had been ##. */
1163 /* Handle the start of a symbol. */
1164 if (is_idchar[c] && nargs > 0) {
1165 U_CHAR *id_beg = p - 1;
1169 while (p != limit && is_idchar[*p]) p++;
1170 id_len = p - id_beg;
1173 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1174 register struct arglist *arg;
1176 for (arg = arglist; arg != NULL; arg = arg->next) {
1177 struct reflist *tpat;
1179 if (arg->name[0] == c
1180 && arg->length == id_len
1181 && strncmp (arg->name, id_beg, id_len) == 0) {
1182 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1183 if (CPP_TRADITIONAL (pfile)) {
1184 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1188 "macro arg `%.*s' would be stringified with -traditional.",
1192 /* If ANSI, don't actually substitute inside a string. */
1193 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1195 /* make a pat node for this arg and append it to the end of
1197 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1199 tpat->raw_before = concat == id_beg;
1200 tpat->raw_after = 0;
1201 tpat->rest_args = arg->rest_args;
1202 tpat->stringify = (CPP_TRADITIONAL (pfile)
1203 ? expected_delimiter != '\0'
1204 : stringify == id_beg);
1207 defn->pattern = tpat;
1209 endpat->next = tpat;
1212 tpat->argno = arg->argno;
1213 tpat->nchars = exp_p - lastp;
1215 register U_CHAR *p1 = p;
1216 SKIP_WHITE_SPACE (p1);
1217 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1218 tpat->raw_after = 1;
1220 lastp = exp_p; /* place to start copying from next time */
1227 /* If this was not a macro arg, copy it into the expansion. */
1228 if (! skipped_arg) {
1229 register U_CHAR *lim1 = p;
1233 if (stringify == id_beg)
1235 "`#' operator should be followed by a macro argument name");
1240 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1242 /* If ANSI, put in a "@ " marker to prevent token pasting.
1243 But not if "inside a string" (which in ANSI mode
1244 happens only for -D option). */
1251 defn->length = exp_p - defn->expansion;
1253 /* Crash now if we overrun the allocated size. */
1254 if (defn->length + 1 > maxsize)
1258 /* This isn't worth the time it takes. */
1259 /* give back excess storage */
1260 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1267 * special extension string that can be added to the last macro argument to
1268 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1269 * #define wow(a, b...) process (b, a, b)
1270 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1271 * { wow (one, two); } -> { process (two, one, two); }
1272 * if this "rest_arg" is used with the concat token '##' and if it is not
1273 * supplied then the token attached to with ## will not be outputted. Ex:
1274 * #define wow (a, b...) process (b ## , a, ## b)
1275 * { wow (1, 2); } -> { process (2, 1, 2); }
1276 * { wow (one); } -> { process (one); {
1278 static char rest_extension[] = "...";
1279 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1281 /* Create a DEFINITION node from a #define directive. Arguments are
1282 as for do_define. */
1285 create_definition (buf, limit, pfile, predefinition)
1286 U_CHAR *buf, *limit;
1290 U_CHAR *bp; /* temp ptr into input buffer */
1291 U_CHAR *symname; /* remember where symbol name starts */
1292 int sym_length; /* and how long it is */
1295 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1297 int arglengths = 0; /* Accumulate lengths of arg names
1298 plus number of args. */
1300 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1304 while (is_hor_space[*bp])
1307 symname = bp; /* remember where it starts */
1309 sym_length = check_macro_name (pfile, bp, "macro");
1312 /* Lossage will occur if identifiers or control keywords are broken
1313 across lines using backslash. This is not the right place to take
1317 struct arglist *arg_ptrs = NULL;
1320 bp++; /* skip '(' */
1321 SKIP_WHITE_SPACE (bp);
1323 /* Loop over macro argument names. */
1324 while (*bp != ')') {
1325 struct arglist *temp;
1327 temp = (struct arglist *) alloca (sizeof (struct arglist));
1329 temp->next = arg_ptrs;
1330 temp->argno = argno++;
1331 temp->rest_args = 0;
1335 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1337 if (!is_idstart[*bp])
1338 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1340 /* Find the end of the arg name. */
1341 while (is_idchar[*bp]) {
1343 /* do we have a "special" rest-args extension here? */
1344 if ((size_t)(limit - bp) > REST_EXTENSION_LENGTH
1345 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1347 temp->rest_args = 1;
1351 temp->length = bp - temp->name;
1353 bp += REST_EXTENSION_LENGTH;
1354 arglengths += temp->length + 2;
1355 SKIP_WHITE_SPACE (bp);
1356 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1357 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1362 SKIP_WHITE_SPACE (bp);
1365 cpp_error (pfile, "unterminated parameter list in `#define'");
1369 struct arglist *otemp;
1371 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1372 if (temp->length == otemp->length
1373 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1376 name = (U_CHAR *) alloca (temp->length + 1);
1377 (void) strncpy (name, temp->name, temp->length);
1378 name[temp->length] = '\0';
1380 "duplicate argument name `%s' in `#define'", name);
1386 ++bp; /* skip paren */
1387 SKIP_WHITE_SPACE (bp);
1388 /* now everything from bp before limit is the definition. */
1389 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1390 defn->rest_args = rest_args;
1392 /* Now set defn->args.argnames to the result of concatenating
1393 the argument names in reverse order
1394 with comma-space between them. */
1395 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1397 struct arglist *temp;
1399 for (temp = arg_ptrs; temp; temp = temp->next) {
1400 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1402 if (temp->next != 0) {
1403 defn->args.argnames[i++] = ',';
1404 defn->args.argnames[i++] = ' ';
1407 defn->args.argnames[i] = 0;
1410 /* Simple expansion or empty definition. */
1414 if (is_hor_space[*bp]) {
1416 SKIP_WHITE_SPACE (bp);
1419 case '!': case '"': case '#': case '%': case '&': case '\'':
1420 case ')': case '*': case '+': case ',': case '-': case '.':
1421 case '/': case ':': case ';': case '<': case '=': case '>':
1422 case '?': case '[': case '\\': case ']': case '^': case '{':
1423 case '|': case '}': case '~':
1424 cpp_warning (pfile, "missing white space after `#define %.*s'",
1425 sym_length, symname);
1429 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1430 sym_length, symname);
1435 /* now everything from bp before limit is the definition. */
1436 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1437 defn->args.argnames = (U_CHAR *) "";
1443 /* OP is null if this is a predefinition */
1444 defn->predefined = predefinition;
1446 mdef.symnam = symname;
1447 mdef.symlen = sym_length;
1456 /* Check a purported macro name SYMNAME, and yield its length.
1457 USAGE is the kind of name this is intended for. */
1460 check_macro_name (pfile, symname, usage)
1468 for (p = symname; is_idchar[*p]; p++)
1470 sym_length = p - symname;
1472 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1473 cpp_error (pfile, "invalid %s name", usage);
1474 else if (!is_idstart[*symname]) {
1475 U_CHAR *msg; /* what pain... */
1476 msg = (U_CHAR *) alloca (sym_length + 1);
1477 bcopy (symname, msg, sym_length);
1478 msg[sym_length] = 0;
1479 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1481 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1482 cpp_error (pfile, "invalid %s name `defined'", usage);
1487 /* Return zero if two DEFINITIONs are isomorphic. */
1490 compare_defs (pfile, d1, d2)
1492 DEFINITION *d1, *d2;
1494 register struct reflist *a1, *a2;
1495 register U_CHAR *p1 = d1->expansion;
1496 register U_CHAR *p2 = d2->expansion;
1499 if (d1->nargs != d2->nargs)
1501 if (CPP_PEDANTIC (pfile)
1502 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1504 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1505 a1 = a1->next, a2 = a2->next) {
1506 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1507 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1508 || a1->argno != a2->argno
1509 || a1->stringify != a2->stringify
1510 || a1->raw_before != a2->raw_before
1511 || a1->raw_after != a2->raw_after)
1519 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1520 p2, d2->length - (p2 - d2->expansion), 1))
1525 /* Return 1 if two parts of two macro definitions are effectively different.
1526 One of the parts starts at BEG1 and has LEN1 chars;
1527 the other has LEN2 chars at BEG2.
1528 Any sequence of whitespace matches any other sequence of whitespace.
1529 FIRST means these parts are the first of a macro definition;
1530 so ignore leading whitespace entirely.
1531 LAST means these parts are the last of a macro definition;
1532 so ignore trailing whitespace entirely. */
1535 comp_def_part (first, beg1, len1, beg2, len2, last)
1537 U_CHAR *beg1, *beg2;
1541 register U_CHAR *end1 = beg1 + len1;
1542 register U_CHAR *end2 = beg2 + len2;
1544 while (beg1 != end1 && is_space[*beg1]) beg1++;
1545 while (beg2 != end2 && is_space[*beg2]) beg2++;
1548 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1549 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1551 while (beg1 != end1 && beg2 != end2) {
1552 if (is_space[*beg1] && is_space[*beg2]) {
1553 while (beg1 != end1 && is_space[*beg1]) beg1++;
1554 while (beg2 != end2 && is_space[*beg2]) beg2++;
1555 } else if (*beg1 == *beg2) {
1559 return (beg1 != end1) || (beg2 != end2);
1562 /* Process a #define command.
1563 BUF points to the contents of the #define command, as a contiguous string.
1564 LIMIT points to the first character past the end of the definition.
1565 KEYWORD is the keyword-table entry for #define,
1566 or NULL for a "predefined" macro. */
1569 do_define (pfile, keyword, buf, limit)
1571 struct directive *keyword;
1572 U_CHAR *buf, *limit;
1579 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1580 if (pcp_outfile && keyword)
1581 pass_thru_directive (buf, limit, pfile, keyword);
1584 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1588 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1590 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1593 /* Redefining a precompiled key is ok. */
1594 if (hp->type == T_PCSTRING)
1596 /* Redefining a macro is ok if the definitions are the same. */
1597 else if (hp->type == T_MACRO)
1598 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1599 /* Redefining a constant is ok with -D. */
1600 else if (hp->type == T_CONST)
1601 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1602 /* Print the warning if it's not ok. */
1605 U_CHAR *msg; /* what pain... */
1607 /* If we are passing through #define and #undef directives, do
1608 that for this re-definition now. */
1609 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1610 pass_thru_directive (buf, limit, pfile, keyword);
1612 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1614 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1615 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1616 cpp_pedwarn (pfile, msg);
1617 if (hp->type == T_MACRO)
1618 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1619 "this is the location of the previous definition");
1621 /* Replace the old definition. */
1623 hp->value.defn = mdef.defn;
1627 /* If we are passing through #define and #undef directives, do
1628 that for this new definition now. */
1629 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1630 pass_thru_directive (buf, limit, pfile, keyword);
1631 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1632 (char *) mdef.defn, hashcode);
1642 /* This structure represents one parsed argument in a macro call.
1643 `raw' points to the argument text as written (`raw_length' is its length).
1644 `expanded' points to the argument's macro-expansion
1645 (its length is `expand_length').
1646 `stringified_length' is the length the argument would have
1648 `use_count' is the number of times this macro arg is substituted
1649 into the macro. If the actual use count exceeds 10,
1650 the value stored is 10. */
1652 /* raw and expanded are relative to ARG_BASE */
1653 #define ARG_BASE ((pfile)->token_buffer)
1656 /* Strings relative to pfile->token_buffer */
1657 long raw, expanded, stringified;
1658 int raw_length, expand_length;
1659 int stringified_length;
1664 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1665 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1666 as the new input buffer.
1667 Return the new buffer, or NULL on failure. */
1670 cpp_push_buffer (pfile, buffer, length)
1675 register cpp_buffer *buf = CPP_BUFFER (pfile);
1676 if (buf == pfile->buffer_stack)
1678 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1683 bzero ((char *) buf, sizeof (cpp_buffer));
1684 CPP_BUFFER (pfile) = buf;
1685 buf->if_stack = pfile->if_stack;
1686 buf->cleanup = null_cleanup;
1687 buf->underflow = null_underflow;
1688 buf->buf = buf->cur = buffer;
1689 buf->alimit = buf->rlimit = buffer + length;
1695 cpp_pop_buffer (pfile)
1698 cpp_buffer *buf = CPP_BUFFER (pfile);
1699 (*buf->cleanup) (buf, pfile);
1700 return ++CPP_BUFFER (pfile);
1703 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1704 Pop the buffer when done. */
1707 cpp_scan_buffer (pfile)
1710 cpp_buffer *buffer = CPP_BUFFER (pfile);
1713 enum cpp_token token = cpp_get_token (pfile);
1714 if (token == CPP_EOF) /* Should not happen ... */
1716 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1718 cpp_pop_buffer (pfile);
1725 * Rescan a string (which may have escape marks) into pfile's buffer.
1726 * Place the result in pfile->token_buffer.
1728 * The input is copied before it is scanned, so it is safe to pass
1729 * it something from the token_buffer that will get overwritten
1730 * (because it follows CPP_WRITTEN). This is used by do_include.
1734 cpp_expand_to_buffer (pfile, buf, length)
1739 register cpp_buffer *ip;
1743 U_CHAR *limit = buf + length;
1746 int odepth = indepth;
1752 /* Set up the input on the input stack. */
1754 buf1 = (U_CHAR *) alloca (length + 1);
1756 register U_CHAR *p1 = buf;
1757 register U_CHAR *p2 = buf1;
1764 ip = cpp_push_buffer (pfile, buf1, length);
1767 ip->has_escapes = 1;
1769 ip->lineno = obuf.lineno = 1;
1772 /* Scan the input, create the output. */
1773 cpp_scan_buffer (pfile);
1776 if (indepth != odepth)
1780 CPP_NUL_TERMINATE (pfile);
1785 adjust_position (buf, limit, linep, colp)
1795 (*linep)++, (*colp) = 1;
1801 /* Move line_base forward, updating lineno and colno. */
1804 update_position (pbuf)
1805 register cpp_buffer *pbuf;
1807 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1808 unsigned char *new_pos = pbuf->cur;
1809 register struct parse_marker *mark;
1810 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
1812 if (pbuf->buf + mark->position < new_pos)
1813 new_pos = pbuf->buf + mark->position;
1815 pbuf->line_base += new_pos - old_pos;
1816 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1820 cpp_buf_line_and_col (pbuf, linep, colp)
1821 register cpp_buffer *pbuf;
1829 *linep = pbuf->lineno;
1830 *colp = pbuf->colno;
1831 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
1840 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1843 cpp_file_buffer (pfile)
1846 cpp_buffer *ip = CPP_BUFFER (pfile);
1848 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
1849 if (ip->fname != NULL)
1855 count_newlines (buf, limit)
1856 register U_CHAR *buf;
1857 register U_CHAR *limit;
1859 register long count = 0;
1870 * write out a #line command, for instance, after an #include file.
1871 * If CONDITIONAL is nonzero, we can omit the #line if it would
1872 * appear to be a no-op, and we can output a few newlines instead
1873 * if we want to increase the line number by a small amount.
1874 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
1878 output_line_command (pfile, conditional, file_change)
1881 enum file_change_code file_change;
1884 cpp_buffer *ip = CPP_BUFFER (pfile);
1886 if (ip->fname == NULL)
1889 update_position (ip);
1891 if (CPP_OPTIONS (pfile)->no_line_commands
1892 || CPP_OPTIONS (pfile)->no_output)
1895 line = CPP_BUFFER (pfile)->lineno;
1896 col = CPP_BUFFER (pfile)->colno;
1897 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
1899 if (CPP_OPTIONS (pfile)->no_line_commands)
1903 if (line == pfile->lineno)
1906 /* If the inherited line number is a little too small,
1907 output some newlines instead of a #line command. */
1908 if (line > pfile->lineno && line < pfile->lineno + 8) {
1909 CPP_RESERVE (pfile, 20);
1910 while (line > pfile->lineno) {
1911 CPP_PUTC_Q (pfile, '\n');
1919 /* Don't output a line number of 0 if we can help it. */
1920 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
1921 && *ip->bufp == '\n') {
1927 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
1929 #ifdef OUTPUT_LINE_COMMANDS
1930 static char sharp_line[] = "#line ";
1932 static char sharp_line[] = "# ";
1934 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
1937 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
1938 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
1940 quote_string (pfile, ip->nominal_fname);
1941 if (file_change != same_file) {
1942 CPP_PUTC_Q (pfile, ' ');
1943 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
1945 /* Tell cc1 if following text comes from a system header file. */
1946 if (ip->system_header_p) {
1947 CPP_PUTC_Q (pfile, ' ');
1948 CPP_PUTC_Q (pfile, '3');
1950 #ifndef NO_IMPLICIT_EXTERN_C
1951 /* Tell cc1plus if following text should be treated as C. */
1952 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
1953 CPP_PUTC_Q (pfile, ' ');
1954 CPP_PUTC_Q (pfile, '4');
1957 CPP_PUTC_Q (pfile, '\n');
1958 pfile->lineno = line;
1962 * Parse a macro argument and append the info on PFILE's token_buffer.
1963 * REST_ARGS means to absorb the rest of the args.
1964 * Return nonzero to indicate a syntax error.
1967 static enum cpp_token
1968 macarg (pfile, rest_args)
1973 enum cpp_token token;
1974 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1975 CPP_OPTIONS (pfile)->put_out_comments = 0;
1977 /* Try to parse as much of the argument as exists at this
1978 input stack level. */
1979 pfile->no_macro_expand++;
1982 token = cpp_get_token (pfile);
1988 /* If we've hit end of file, it's an error (reported by caller).
1989 Ditto if it's the end of cpp_expand_to_buffer text.
1990 If we've hit end of macro, just continue. */
1991 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2002 /* if we've returned to lowest level and
2003 we aren't absorbing all args */
2004 if (paren == 0 && rest_args == 0)
2008 /* Remove ',' or ')' from argument buffer. */
2009 CPP_ADJUST_WRITTEN (pfile, -1);
2016 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2017 pfile->no_macro_expand--;
2022 /* Turn newlines to spaces in the string of length LENGTH at START,
2023 except inside of string constants.
2024 The string is copied into itself with its beginning staying fixed. */
2027 change_newlines (start, length)
2031 register U_CHAR *ibp;
2032 register U_CHAR *obp;
2033 register U_CHAR *limit;
2037 limit = start + length;
2040 while (ibp < limit) {
2041 *obp++ = c = *ibp++;
2046 /* Notice and skip strings, so that we don't delete newlines in them. */
2049 while (ibp < limit) {
2050 *obp++ = c = *ibp++;
2053 if (c == '\n' && quotec == '\'')
2069 if (!pfile->timebuf) {
2070 time_t t = time ((time_t *) 0);
2071 pfile->timebuf = localtime (&t);
2073 return pfile->timebuf;
2076 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2077 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2081 * expand things like __FILE__. Place the expansion into the output
2082 * buffer *without* rescanning.
2086 special_symbol (hp, pfile)
2093 cpp_buffer *ip = NULL;
2096 int paren = 0; /* For special `defined' keyword */
2099 if (pcp_outfile && pcp_inside_if
2100 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2102 "Predefined macro `%s' used inside `#if' during precompilation",
2106 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2108 if (ip == CPP_NULL_BUFFER (pfile))
2110 cpp_error (pfile, "cccp error: not in any file?!");
2111 return; /* the show must go on */
2113 if (ip->fname != NULL)
2123 if (hp->type == T_BASE_FILE)
2125 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2126 ip = CPP_PREV_BUFFER (ip);
2128 string = ip->nominal_fname;
2132 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2133 quote_string (pfile, string);
2137 case T_INCLUDE_LEVEL:
2139 ip = CPP_BUFFER (pfile);
2140 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2141 if (ip->fname != NULL)
2144 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2145 sprintf (buf, "%d", true_indepth - 1);
2149 buf = (char *) alloca (3 + strlen (version_string));
2150 sprintf (buf, "\"%s\"", version_string);
2153 #ifndef NO_BUILTIN_SIZE_TYPE
2159 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2160 case T_PTRDIFF_TYPE:
2166 buf = CPP_WCHAR_TYPE (pfile);
2169 case T_USER_LABEL_PREFIX_TYPE:
2170 buf = user_label_prefix;
2173 case T_REGISTER_PREFIX_TYPE:
2174 buf = REGISTER_PREFIX;
2178 buf = (char *) alloca (4 * sizeof (int));
2179 sprintf (buf, "%d", hp->value.ival);
2180 #ifdef STDC_0_IN_SYSTEM_HEADERS
2181 if (ip->system_header_p
2182 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2183 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2187 if (pcp_inside_if && pcp_outfile)
2188 /* Output a precondition for this macro use */
2189 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2195 long line = ip->lineno;
2196 long col = ip->colno;
2197 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2199 buf = (char *) alloca (10);
2200 sprintf (buf, "%ld", line);
2206 buf = (char *) alloca (20);
2207 timebuf = timestamp (pfile);
2208 if (hp->type == T_DATE)
2209 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2210 timebuf->tm_mday, timebuf->tm_year + 1900);
2212 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2216 case T_SPEC_DEFINED:
2217 buf = " 0 "; /* Assume symbol is not defined */
2218 ip = CPP_BUFFER (pfile);
2219 SKIP_WHITE_SPACE (ip->cur);
2220 if (*ip->cur == '(')
2223 ip->cur++; /* Skip over the paren */
2224 SKIP_WHITE_SPACE (ip->cur);
2227 if (!is_idstart[*ip->cur])
2229 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2231 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2234 if (pcp_outfile && pcp_inside_if
2235 && (hp->type == T_CONST
2236 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2237 /* Output a precondition for this macro use. */
2238 fprintf (pcp_outfile, "#define %s\n", hp->name);
2244 if (pcp_outfile && pcp_inside_if)
2246 /* Output a precondition for this macro use */
2247 U_CHAR *cp = ip->bufp;
2248 fprintf (pcp_outfile, "#undef ");
2249 while (is_idchar[*cp]) /* Ick! */
2250 fputc (*cp++, pcp_outfile);
2251 putc ('\n', pcp_outfile);
2254 while (is_idchar[*ip->cur])
2256 SKIP_WHITE_SPACE (ip->cur);
2259 if (*ip->cur != ')')
2267 cpp_error (pfile, "`defined' without an identifier");
2271 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2275 CPP_RESERVE (pfile, len + 1);
2276 CPP_PUTS_Q (pfile, buf, len);
2277 CPP_NUL_TERMINATE_Q (pfile);
2282 /* Write out a #define command for the special named MACRO_NAME
2283 to PFILE's token_buffer. */
2286 dump_special_to_buffer (pfile, macro_name)
2290 static char define_directive[] = "#define ";
2291 int macro_name_length = strlen (macro_name);
2292 output_line_command (pfile, 0, same_file);
2293 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2294 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2295 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2296 CPP_PUTC_Q (pfile, ' ');
2297 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2298 CPP_PUTC (pfile, '\n');
2301 /* Initialize the built-in macros. */
2304 initialize_builtins (pfile)
2307 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2308 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2309 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2310 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2311 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2312 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2313 #ifndef NO_BUILTIN_SIZE_TYPE
2314 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2316 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2317 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2319 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2320 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2321 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2322 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2323 if (!CPP_TRADITIONAL (pfile))
2324 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2325 if (CPP_OPTIONS (pfile)->objc)
2326 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2327 /* This is supplied using a -D by the compiler driver
2328 so that it is present only when truly compiling with GNU C. */
2329 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2331 if (CPP_OPTIONS (pfile)->debug_output)
2333 dump_special_to_buffer (pfile, "__BASE_FILE__");
2334 dump_special_to_buffer (pfile, "__VERSION__");
2335 #ifndef NO_BUILTIN_SIZE_TYPE
2336 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2338 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2339 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2341 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2342 dump_special_to_buffer (pfile, "__DATE__");
2343 dump_special_to_buffer (pfile, "__TIME__");
2344 if (!CPP_TRADITIONAL (pfile))
2345 dump_special_to_buffer (pfile, "__STDC__");
2346 if (CPP_OPTIONS (pfile)->objc)
2347 dump_special_to_buffer (pfile, "__OBJC__");
2351 /* Return 1 iff a token ending in C1 followed directly by a token C2
2352 could cause mis-tokenization. */
2355 unsafe_chars (c1, c2)
2361 if (c2 == c1 || c2 == '=')
2365 case '0': case '1': case '2': case '3': case '4':
2366 case '5': case '6': case '7': case '8': case '9':
2367 case 'e': case 'E': case 'p': case 'P':
2368 if (c2 == '-' || c2 == '+')
2369 return 1; /* could extend a pre-processing number */
2372 if (c2 == '\'' || c2 == '\"')
2373 return 1; /* Could turn into L"xxx" or L'xxx'. */
2377 case 'a': case 'b': case 'c': case 'd': case 'f':
2378 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2379 case 'm': case 'n': case 'o': case 'q': case 'r':
2380 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2382 case 'A': case 'B': case 'C': case 'D': case 'F':
2383 case 'G': case 'H': case 'I': case 'J': case 'K':
2384 case 'M': case 'N': case 'O': case 'Q': case 'R':
2385 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2387 /* We're in the middle of either a name or a pre-processing number. */
2388 return (is_idchar[c2] || c2 == '.');
2389 case '<': case '>': case '!': case '%': case '#': case ':':
2390 case '^': case '&': case '|': case '*': case '/': case '=':
2391 return (c2 == c1 || c2 == '=');
2396 /* Expand a macro call.
2397 HP points to the symbol that is the macro being called.
2398 Put the result of expansion onto the input stack
2399 so that subsequent input by our caller will use it.
2401 If macro wants arguments, caller has already verified that
2402 an argument list follows; arguments come from the input stack. */
2405 macroexpand (pfile, hp)
2410 DEFINITION *defn = hp->value.defn;
2411 register U_CHAR *xbuf;
2412 long start_line, start_column;
2414 struct argdata *args;
2415 long old_written = CPP_WRITTEN (pfile);
2417 int start_line = instack[indepth].lineno;
2419 int rest_args, rest_zero;
2423 CHECK_DEPTH (return;);
2427 /* This macro is being used inside a #if, which means it must be */
2428 /* recorded as a precondition. */
2429 if (pcp_inside_if && pcp_outfile && defn->predefined)
2430 dump_single_macro (hp, pcp_outfile);
2433 pfile->output_escapes++;
2434 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2436 nargs = defn->nargs;
2440 enum cpp_token token;
2442 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2444 for (i = 0; i < nargs; i++)
2446 args[i].raw = args[i].expanded = 0;
2447 args[i].raw_length = 0;
2448 args[i].expand_length = args[i].stringified_length = -1;
2449 args[i].use_count = 0;
2452 /* Parse all the macro args that are supplied. I counts them.
2453 The first NARGS args are stored in ARGS.
2454 The rest are discarded. If rest_args is set then we assume
2455 macarg absorbed the rest of the args. */
2459 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2464 if (i < nargs || (nargs == 0 && i == 0))
2466 /* if we are working on last arg which absorbs rest of args... */
2467 if (i == nargs - 1 && defn->rest_args)
2469 args[i].raw = CPP_WRITTEN (pfile);
2470 token = macarg (pfile, rest_args);
2471 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2472 args[i].newlines = 0; /* FIXME */
2475 token = macarg (pfile, 0);
2476 if (token == CPP_EOF || token == CPP_POP)
2478 cpp_error_with_line (pfile, start_line, start_column,
2479 "unterminated macro call");
2483 } while (token == CPP_COMMA);
2485 /* If we got one arg but it was just whitespace, call that 0 args. */
2488 register U_CHAR *bp = ARG_BASE + args[0].raw;
2489 register U_CHAR *lim = bp + args[0].raw_length;
2490 /* cpp.texi says for foo ( ) we provide one argument.
2491 However, if foo wants just 0 arguments, treat this as 0. */
2493 while (bp != lim && is_space[*bp]) bp++;
2498 /* Don't output an error message if we have already output one for
2499 a parse error above. */
2501 if (nargs == 0 && i > 0)
2503 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2507 /* traditional C allows foo() if foo wants one argument. */
2508 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2510 /* the rest args token is allowed to absorb 0 tokens */
2511 else if (i == nargs - 1 && defn->rest_args)
2514 cpp_error (pfile, "macro `%s' used without args", hp->name);
2516 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2518 cpp_error (pfile, "macro `%s' used with only %d args",
2524 "macro `%s' used with too many (%d) args", hp->name, i);
2528 /* If macro wants zero args, we parsed the arglist for checking only.
2529 Read directly from the macro definition. */
2532 xbuf = defn->expansion;
2533 xbuf_len = defn->length;
2537 register U_CHAR *exp = defn->expansion;
2538 register int offset; /* offset in expansion,
2539 copied a piece at a time */
2540 register int totlen; /* total amount of exp buffer filled so far */
2542 register struct reflist *ap, *last_ap;
2544 /* Macro really takes args. Compute the expansion of this call. */
2546 /* Compute length in characters of the macro's expansion.
2547 Also count number of times each arg is used. */
2548 xbuf_len = defn->length;
2549 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2553 register struct argdata *arg = &args[ap->argno];
2554 /* Stringify if it hasn't already been */
2555 if (arg->stringified_length < 0)
2557 int arglen = arg->raw_length;
2561 /* Initially need_space is -1. Otherwise, 1 means the
2562 previous character was a space, but we suppressed it;
2563 0 means the previous character was a non-space. */
2564 int need_space = -1;
2566 arg->stringified = CPP_WRITTEN (pfile);
2567 if (!CPP_TRADITIONAL (pfile))
2568 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2569 for (; i < arglen; i++)
2571 c = (ARG_BASE + arg->raw)[i];
2575 /* Internal sequences of whitespace are replaced by
2576 one space except within an string or char token.*/
2579 if (CPP_WRITTEN (pfile) > (unsigned)arg->stringified
2580 && (CPP_PWRITTEN (pfile))[-1] == '@')
2582 /* "@ " escape markers are removed */
2583 CPP_ADJUST_WRITTEN (pfile, -1);
2586 if (need_space == 0)
2590 else if (need_space > 0)
2591 CPP_PUTC (pfile, ' ');
2606 else if (c == '\"' || c == '\'')
2610 /* Escape these chars */
2611 if (c == '\"' || (in_string && c == '\\'))
2612 CPP_PUTC (pfile, '\\');
2614 CPP_PUTC (pfile, c);
2617 CPP_RESERVE (pfile, 4);
2618 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2620 CPP_ADJUST_WRITTEN (pfile, 4);
2623 if (!CPP_TRADITIONAL (pfile))
2624 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2625 arg->stringified_length
2626 = CPP_WRITTEN (pfile) - arg->stringified;
2628 xbuf_len += args[ap->argno].stringified_length;
2630 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2631 /* Add 4 for two newline-space markers to prevent
2632 token concatenation. */
2633 xbuf_len += args[ap->argno].raw_length + 4;
2636 /* We have an ordinary (expanded) occurrence of the arg.
2637 So compute its expansion, if we have not already. */
2638 if (args[ap->argno].expand_length < 0)
2640 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2641 cpp_expand_to_buffer (pfile,
2642 ARG_BASE + args[ap->argno].raw,
2643 args[ap->argno].raw_length);
2645 args[ap->argno].expand_length
2646 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2649 /* Add 4 for two newline-space markers to prevent
2650 token concatenation. */
2651 xbuf_len += args[ap->argno].expand_length + 4;
2653 if (args[ap->argno].use_count < 10)
2654 args[ap->argno].use_count++;
2657 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2659 /* Generate in XBUF the complete expansion
2660 with arguments substituted in.
2661 TOTLEN is the total size generated so far.
2662 OFFSET is the index in the definition
2663 of where we are copying from. */
2664 offset = totlen = 0;
2665 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2666 last_ap = ap, ap = ap->next)
2668 register struct argdata *arg = &args[ap->argno];
2669 int count_before = totlen;
2671 /* Add chars to XBUF. */
2672 for (i = 0; i < ap->nchars; i++, offset++)
2673 xbuf[totlen++] = exp[offset];
2675 /* If followed by an empty rest arg with concatenation,
2676 delete the last run of nonwhite chars. */
2677 if (rest_zero && totlen > count_before
2678 && ((ap->rest_args && ap->raw_before)
2679 || (last_ap != NULL && last_ap->rest_args
2680 && last_ap->raw_after)))
2682 /* Delete final whitespace. */
2683 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2686 /* Delete the nonwhites before them. */
2687 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2691 if (ap->stringify != 0)
2693 bcopy (ARG_BASE + arg->stringified,
2694 xbuf + totlen, arg->stringified_length);
2695 totlen += arg->stringified_length;
2697 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2699 U_CHAR *p1 = ARG_BASE + arg->raw;
2700 U_CHAR *l1 = p1 + arg->raw_length;
2703 while (p1 != l1 && is_space[*p1]) p1++;
2704 while (p1 != l1 && is_idchar[*p1])
2705 xbuf[totlen++] = *p1++;
2709 /* Arg is concatenated after: delete trailing whitespace,
2710 whitespace markers, and no-reexpansion markers. */
2713 if (is_space[l1[-1]]) l1--;
2714 else if (l1[-1] == '@')
2716 U_CHAR *p2 = l1 - 1;
2717 /* If whitespace is preceded by an odd number
2718 of `@' signs, the last `@' was a whitespace
2719 marker; drop it too. */
2720 while (p2 != p1 && p2[0] == '@') p2--;
2725 else if (l1[-1] == '-')
2727 U_CHAR *p2 = l1 - 1;
2728 /* If a `-' is preceded by an odd number of
2729 `@' signs then it and the last `@' are
2730 a no-reexpansion marker. */
2731 while (p2 != p1 && p2[0] == '@') p2--;
2741 /* Delete any no-reexpansion marker that precedes
2742 an identifier at the beginning of the argument. */
2743 if (p1[0] == '@' && p1[1] == '-')
2746 bcopy (p1, xbuf + totlen, l1 - p1);
2751 U_CHAR *expanded = ARG_BASE + arg->expanded;
2752 if (!ap->raw_before && totlen > 0 && arg->expand_length
2753 && !CPP_TRADITIONAL(pfile)
2754 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2756 xbuf[totlen++] = '@';
2757 xbuf[totlen++] = ' ';
2760 bcopy (expanded, xbuf + totlen, arg->expand_length);
2761 totlen += arg->expand_length;
2763 if (!ap->raw_after && totlen > 0 && offset < defn->length
2764 && !CPP_TRADITIONAL(pfile)
2765 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2767 xbuf[totlen++] = '@';
2768 xbuf[totlen++] = ' ';
2771 /* If a macro argument with newlines is used multiple times,
2772 then only expand the newlines once. This avoids creating
2773 output lines which don't correspond to any input line,
2774 which confuses gdb and gcov. */
2775 if (arg->use_count > 1 && arg->newlines > 0)
2777 /* Don't bother doing change_newlines for subsequent
2781 = change_newlines (expanded, arg->expand_length);
2785 if (totlen > xbuf_len)
2789 /* if there is anything left of the definition
2790 after handling the arg list, copy that in too. */
2792 for (i = offset; i < defn->length; i++)
2794 /* if we've reached the end of the macro */
2797 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2798 && last_ap->raw_after))
2799 xbuf[totlen++] = exp[i];
2807 pfile->output_escapes--;
2809 /* Now put the expansion on the input stack
2810 so our caller will commence reading from it. */
2811 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2812 CPP_BUFFER (pfile)->has_escapes = 1;
2814 /* Pop the space we've used in the token_buffer for argument expansion. */
2815 CPP_SET_WRITTEN (pfile, old_written);
2817 /* Recursive macro use sometimes works traditionally.
2818 #define foo(x,y) bar (x (y,0), y)
2821 if (!CPP_TRADITIONAL (pfile))
2822 hp->type = T_DISABLED;
2826 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2828 register U_CHAR *xbuf;
2832 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2835 mbuf->cleanup = macro_cleanup;
2838 /* The first chars of the expansion should be a "@ " added by
2839 collect_expansion. This is to prevent accidental token-pasting
2840 between the text preceding the macro invocation, and the macro
2843 We would like to avoid adding unneeded spaces (for the sake of
2844 tools that use cpp, such as imake). In some common cases we can
2845 tell that it is safe to omit the space.
2847 The character before the macro invocation cannot have been an
2848 idchar (or else it would have been pasted with the idchars of
2849 the macro name). Therefore, if the first non-space character
2850 of the expansion is an idchar, we do not need the extra space
2851 to prevent token pasting.
2853 Also, we don't need the extra space if the first char is '(',
2854 or some other (less common) characters. */
2856 if (xbuf[0] == '@' && xbuf[1] == ' '
2857 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
2858 || xbuf[2] == '\"'))
2862 /* Like cpp_get_token, except that it does not read past end-of-line.
2863 Also, horizontal space is skipped, and macros are popped. */
2865 static enum cpp_token
2866 get_directive_token (pfile)
2871 long old_written = CPP_WRITTEN (pfile);
2872 enum cpp_token token;
2873 cpp_skip_hspace (pfile);
2874 if (PEEKC () == '\n')
2876 token = cpp_get_token (pfile);
2880 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2882 /* ... else fall though ... */
2883 case CPP_HSPACE: case CPP_COMMENT:
2884 CPP_SET_WRITTEN (pfile, old_written);
2892 /* Handle #include and #import.
2893 This function expects to see "fname" or <fname> on the input.
2895 The input is normally in part of the output_buffer following
2896 CPP_WRITTEN, and will get overwritten by output_line_command.
2897 I.e. in input file specification has been popped by handle_directive.
2901 do_include (pfile, keyword, unused1, unused2)
2903 struct directive *keyword;
2904 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
2906 int importing = (keyword->type == T_IMPORT);
2907 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
2908 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
2909 int before; /* included before? */
2914 enum cpp_token token;
2916 /* Chain of dirs to search */
2917 struct include_hash *ihash;
2918 struct file_name_list *search_start;
2920 long old_written = CPP_WRITTEN (pfile);
2924 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
2927 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
2929 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
2932 if (importing && CPP_OPTIONS (pfile)->warn_import
2933 && !CPP_OPTIONS (pfile)->inhibit_warnings
2934 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
2936 pfile->import_warning = 1;
2937 cpp_warning (pfile, "`#import' is obsolete, use an #ifdef wrapper in the header file");
2940 pfile->parsing_include_directive++;
2941 token = get_directive_token (pfile);
2942 pfile->parsing_include_directive--;
2944 if (token == CPP_STRING)
2946 fbeg = pfile->token_buffer + old_written + 1;
2947 fend = CPP_PWRITTEN (pfile) - 1;
2949 if (fbeg[-1] == '<')
2953 else if (token == CPP_NAME)
2955 /* Support '#include xyz' like VAX-C to allow for easy use of
2956 * all the decwindow include files. It defaults to '#include
2957 * <xyz.h>' and generates a warning. */
2959 "VAX-C-style include specification found, use '#include <filename.h>' !");
2962 /* Append the missing `.h' to the name. */
2963 CPP_PUTS (pfile, ".h", 3)
2964 CPP_NUL_TERMINATE_Q (pfile);
2966 fbeg = pfile->token_buffer + old_written;
2967 fend = CPP_PWRITTEN (pfile);
2973 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
2974 CPP_SET_WRITTEN (pfile, old_written);
2975 skip_rest_of_line (pfile);
2979 token = get_directive_token (pfile);
2980 if (token != CPP_VSPACE)
2982 cpp_error (pfile, "junk at end of `#include'");
2983 skip_rest_of_line (pfile);
2986 CPP_SET_WRITTEN (pfile, old_written);
2992 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
2998 for (fp = CPP_BUFFER (pfile);
2999 fp != CPP_NULL_BUFFER (pfile);
3000 fp = CPP_PREV_BUFFER (fp))
3001 if (fp->fname != NULL)
3004 if (fp == CPP_NULL_BUFFER (pfile))
3007 /* For #include_next, skip in the search path
3008 past the dir in which the containing file was found. */
3012 search_start = fp->ihash->foundhere->next;
3017 search_start = CPP_OPTIONS (pfile)->bracket_include;
3020 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
3023 search_start = fp->actual_dir;
3026 search_start = CPP_OPTIONS (pfile)->quote_include;
3032 cpp_error (pfile, "No include path in which to find %s", fbeg);
3036 fd = find_include_file (pfile, fbeg, search_start, &ihash, &before);
3043 if (CPP_OPTIONS (pfile)->print_deps_missing_files
3044 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
3045 (pfile->system_include_depth > 0)))
3047 if (!angle_brackets)
3048 deps_output (pfile, fbeg, ' ');
3052 struct file_name_list *ptr;
3053 /* If requested as a system header, assume it belongs in
3054 the first system header directory. */
3055 if (CPP_OPTIONS (pfile)->bracket_include)
3056 ptr = CPP_OPTIONS (pfile)->bracket_include;
3058 ptr = CPP_OPTIONS (pfile)->quote_include;
3060 p = (char *) alloca (strlen (ptr->name)
3061 + strlen (fbeg) + 2);
3062 if (*ptr->name != '\0')
3064 strcpy (p, ptr->name);
3068 deps_output (pfile, p, ' ');
3071 /* If -M was specified, and this header file won't be added to
3072 the dependency list, then don't count this as an error,
3073 because we can still produce correct output. Otherwise, we
3074 can't produce correct output, because there may be
3075 dependencies we need inside the missing file, and we don't
3076 know what directory this missing file exists in. */
3077 else if (CPP_PRINT_DEPS (pfile)
3078 && (CPP_PRINT_DEPS (pfile)
3079 <= (angle_brackets || (pfile->system_include_depth > 0))))
3080 cpp_warning (pfile, "No include path in which to find %s", fbeg);
3082 cpp_error_from_errno (pfile, fbeg);
3087 /* For -M, add the file to the dependencies on its first inclusion. */
3088 if (!before && (CPP_PRINT_DEPS (pfile)
3089 > (angle_brackets || (pfile->system_include_depth > 0))))
3090 deps_output (pfile, ihash->name, ' ');
3092 /* Handle -H option. */
3093 if (CPP_OPTIONS(pfile)->print_include_names)
3095 fp = CPP_BUFFER (pfile);
3096 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
3098 fprintf (stderr, " %s\n", ihash->name);
3101 /* Actually process the file */
3104 ihash->control_macro = "";
3106 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3113 pfile->system_include_depth++; /* Decremented in file_cleanup. */
3115 if (finclude (pfile, fd, ihash))
3117 output_line_command (pfile, 0, enter_file);
3118 pfile->only_seen_white = 2;
3125 * Install a name in the assertion hash table.
3127 * If LEN is >= 0, it is the length of the name.
3128 * Otherwise, compute the length by scanning the entire name.
3130 * If HASH is >= 0, it is the precomputed hash code.
3131 * Otherwise, compute the hash code.
3134 static ASSERTION_HASHNODE *
3135 assertion_install (pfile, name, len, hash)
3141 register ASSERTION_HASHNODE *hp;
3142 register int i, bucket;
3143 register U_CHAR *p, *q;
3145 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3146 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3148 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3149 hp->next = pfile->assertion_hashtab[bucket];
3150 pfile->assertion_hashtab[bucket] = hp;
3152 if (hp->next != NULL)
3153 hp->next->prev = hp;
3156 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3159 for (i = 0; i < len; i++)
3165 * find the most recent hash node for name "name" (ending with first
3166 * non-identifier char) installed by install
3168 * If LEN is >= 0, it is the length of the name.
3169 * Otherwise, compute the length by scanning the entire name.
3171 * If HASH is >= 0, it is the precomputed hash code.
3172 * Otherwise, compute the hash code.
3175 static ASSERTION_HASHNODE *
3176 assertion_lookup (pfile, name, len, hash)
3182 register ASSERTION_HASHNODE *bucket;
3184 bucket = pfile->assertion_hashtab[hash];
3186 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3188 bucket = bucket->next;
3194 delete_assertion (hp)
3195 ASSERTION_HASHNODE *hp;
3197 struct tokenlist_list *tail;
3198 if (hp->prev != NULL)
3199 hp->prev->next = hp->next;
3200 if (hp->next != NULL)
3201 hp->next->prev = hp->prev;
3203 for (tail = hp->value; tail; )
3205 struct tokenlist_list *next = tail->next;
3206 free_token_list (tail->tokens);
3211 /* Make sure that the bucket chain header that
3212 the deleted guy was on points to the right thing afterwards. */
3213 if (hp == *hp->bucket_hdr)
3214 *hp->bucket_hdr = hp->next;
3219 /* Convert a character string literal into a nul-terminated string.
3220 The input string is [IN ... LIMIT).
3221 The result is placed in RESULT. RESULT can be the same as IN.
3222 The value returned in the end of the string written to RESULT,
3223 or NULL on error. */
3226 convert_string (pfile, result, in, limit, handle_escapes)
3228 register U_CHAR *result, *in, *limit;
3248 char *bpc = (char *) in;
3249 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc, 0x00ffU);
3250 in = (U_CHAR *) bpc;
3252 *result++ = (U_CHAR)c;
3255 /* else fall through */
3265 * interpret #line command. Remembers previously seen fnames
3266 * in its very own hash table.
3268 #define FNAME_HASHSIZE 37
3271 do_line (pfile, keyword, unused1, unused2)
3273 struct directive *keyword ATTRIBUTE_UNUSED;
3274 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3276 cpp_buffer *ip = CPP_BUFFER (pfile);
3278 long old_written = CPP_WRITTEN (pfile);
3279 enum file_change_code file_change = same_file;
3280 enum cpp_token token;
3282 token = get_directive_token (pfile);
3284 if (token != CPP_NUMBER
3285 || !ISDIGIT(pfile->token_buffer[old_written]))
3287 cpp_error (pfile, "invalid format `#line' command");
3288 goto bad_line_directive;
3291 /* The Newline at the end of this line remains to be processed.
3292 To put the next line at the specified line number,
3293 we must store a line number now that is one less. */
3294 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3295 CPP_SET_WRITTEN (pfile, old_written);
3297 /* NEW_LINENO is one less than the actual line number here. */
3298 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3299 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3301 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3302 if (PEEKC() && !is_space[PEEKC()]) {
3303 cpp_error (pfile, "invalid format `#line' command");
3304 goto bad_line_directive;
3308 token = get_directive_token (pfile);
3310 if (token == CPP_STRING) {
3311 U_CHAR *fname = pfile->token_buffer + old_written;
3313 static HASHNODE *fname_table[FNAME_HASHSIZE];
3314 HASHNODE *hp, **hash_bucket;
3319 /* Turn the file name, which is a character string literal,
3320 into a null-terminated string. Do this in place. */
3321 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3322 if (end_name == NULL)
3324 cpp_error (pfile, "invalid format `#line' command");
3325 goto bad_line_directive;
3328 fname_length = end_name - fname;
3330 num_start = CPP_WRITTEN (pfile);
3331 token = get_directive_token (pfile);
3332 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3333 p = pfile->token_buffer + num_start;
3334 if (CPP_PEDANTIC (pfile))
3335 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3337 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3339 cpp_error (pfile, "invalid format `#line' command");
3340 goto bad_line_directive;
3343 file_change = enter_file;
3345 file_change = leave_file;
3347 ip->system_header_p = 1;
3348 else /* if (*p == '4') */
3349 ip->system_header_p = 2;
3351 CPP_SET_WRITTEN (pfile, num_start);
3352 token = get_directive_token (pfile);
3353 p = pfile->token_buffer + num_start;
3354 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3355 ip->system_header_p = *p == '3' ? 1 : 2;
3356 token = get_directive_token (pfile);
3358 if (token != CPP_VSPACE) {
3359 cpp_error (pfile, "invalid format `#line' command");
3360 goto bad_line_directive;
3364 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3365 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3366 if (hp->length == fname_length
3367 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3368 ip->nominal_fname = hp->value.cpval;
3372 /* Didn't find it; cons up a new one. */
3373 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3374 hp->next = *hash_bucket;
3377 hp->length = fname_length;
3378 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3379 bcopy (fname, hp->value.cpval, fname_length);
3382 else if (token != CPP_VSPACE && token != CPP_EOF) {
3383 cpp_error (pfile, "invalid format `#line' command");
3384 goto bad_line_directive;
3387 ip->lineno = new_lineno;
3389 skip_rest_of_line (pfile);
3390 CPP_SET_WRITTEN (pfile, old_written);
3391 output_line_command (pfile, 0, file_change);
3396 * remove the definition of a symbol from the symbol table.
3397 * according to un*x /lib/cpp, it is not an error to undef
3398 * something that has no definitions, so it isn't one here either.
3402 do_undef (pfile, keyword, buf, limit)
3404 struct directive *keyword;
3405 U_CHAR *buf, *limit;
3409 U_CHAR *orig_buf = buf;
3412 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3413 if (pcp_outfile && keyword)
3414 pass_thru_directive (buf, limit, pfile, keyword);
3417 SKIP_WHITE_SPACE (buf);
3418 sym_length = check_macro_name (pfile, buf, "macro");
3420 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3422 /* If we are generating additional info for debugging (with -g) we
3423 need to pass through all effective #undef commands. */
3424 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3425 pass_thru_directive (orig_buf, limit, pfile, keyword);
3426 if (hp->type != T_MACRO)
3427 cpp_warning (pfile, "undefining `%s'", hp->name);
3431 if (CPP_PEDANTIC (pfile)) {
3433 SKIP_WHITE_SPACE (buf);
3435 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3441 * Report an error detected by the program we are processing.
3442 * Use the text of the line in the error message.
3443 * (We use error because it prints the filename & line#.)
3447 do_error (pfile, keyword, buf, limit)
3449 struct directive *keyword ATTRIBUTE_UNUSED;
3450 U_CHAR *buf, *limit;
3452 int length = limit - buf;
3453 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3454 bcopy (buf, copy, length);
3456 SKIP_WHITE_SPACE (copy);
3457 cpp_error (pfile, "#error %s", copy);
3462 * Report a warning detected by the program we are processing.
3463 * Use the text of the line in the warning message, then continue.
3464 * (We use error because it prints the filename & line#.)
3468 do_warning (pfile, keyword, buf, limit)
3470 struct directive *keyword ATTRIBUTE_UNUSED;
3471 U_CHAR *buf, *limit;
3473 int length = limit - buf;
3474 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3475 bcopy (buf, copy, length);
3477 SKIP_WHITE_SPACE (copy);
3479 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3480 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3482 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3483 if -pedantic-errors is given, #warning should cause an error. */
3484 cpp_pedwarn (pfile, "#warning %s", copy);
3488 /* Report program identification. */
3491 do_ident (pfile, keyword, buf, limit)
3493 struct directive *keyword ATTRIBUTE_UNUSED;
3494 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3496 /* long old_written = CPP_WRITTEN (pfile);*/
3498 /* Allow #ident in system headers, since that's not user's fault. */
3499 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3500 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3502 /* Leave rest of line to be read by later calls to cpp_get_token. */
3507 /* #pragma and its argument line have already been copied to the output file.
3508 Just check for some recognized pragmas that need validation here. */
3511 do_pragma (pfile, keyword, buf, limit)
3513 struct directive *keyword ATTRIBUTE_UNUSED;
3514 U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
3516 while (*buf == ' ' || *buf == '\t')
3518 if (!strncmp (buf, "once", 4))
3520 cpp_buffer *ip = NULL;
3522 /* Allow #pragma once in system headers, since that's not the user's
3524 if (!CPP_BUFFER (pfile)->system_header_p)
3525 cpp_warning (pfile, "`#pragma once' is obsolete");
3527 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3529 if (ip == CPP_NULL_BUFFER (pfile))
3531 if (ip->fname != NULL)
3535 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
3536 cpp_warning (pfile, "`#pragma once' outside include file");
3538 ip->ihash->control_macro = ""; /* never repeat */
3541 if (!strncmp (buf, "implementation", 14))
3543 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3544 been included yet. */
3545 struct include_hash *ptr;
3546 U_CHAR *p = buf + 14, *fname, *fcopy;
3547 SKIP_WHITE_SPACE (p);
3548 if (*p == '\n' || *p != '\"')
3552 p = (U_CHAR *) index (fname, '\"');
3554 fcopy = alloca (p - fname + 1);
3555 bcopy (fname, fcopy, p - fname);
3556 fcopy[p-fname] = '\0';
3558 ptr = include_hash (pfile, fcopy, 0);
3561 "`#pragma implementation' for `%s' appears after file is included",
3569 /* This was a fun hack, but #pragma seems to start to be useful.
3570 By failing to recognize it, we pass it through unchanged to cc1. */
3573 * the behavior of the #pragma directive is implementation defined.
3574 * this implementation defines it as follows.
3581 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
3584 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
3586 execl ("/usr/games/hack", "#pragma", 0);
3587 execl ("/usr/games/rogue", "#pragma", 0);
3588 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
3589 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
3591 fatal ("You are in a maze of twisty compiler features, all different");
3595 #ifdef SCCS_DIRECTIVE
3596 /* Just ignore #sccs, on systems where we define it at all. */
3599 do_sccs (pfile, keyword, buf, limit)
3601 struct directive *keyword ATTRIBUTE_UNUSED;
3602 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3604 if (CPP_PEDANTIC (pfile))
3605 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
3611 * handle #if command by
3612 * 1) inserting special `defined' keyword into the hash table
3613 * that gets turned into 0 or 1 by special_symbol (thus,
3614 * if the luser has a symbol called `defined' already, it won't
3615 * work inside the #if command)
3616 * 2) rescan the input into a temporary output buffer
3617 * 3) pass the output buffer to the yacc parser and collect a value
3618 * 4) clean up the mess left from steps 1 and 2.
3619 * 5) call conditional_skip to skip til the next #endif (etc.),
3620 * or not, depending on the value from step 3.
3624 do_if (pfile, keyword, buf, limit)
3626 struct directive *keyword ATTRIBUTE_UNUSED;
3627 U_CHAR *buf, *limit;
3629 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
3630 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
3635 * handle a #elif directive by not changing if_stack either.
3636 * see the comment above do_else.
3640 do_elif (pfile, keyword, buf, limit)
3642 struct directive *keyword ATTRIBUTE_UNUSED;
3643 U_CHAR *buf, *limit;
3645 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3646 cpp_error (pfile, "`#elif' not within a conditional");
3649 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3650 cpp_error (pfile, "`#elif' after `#else'");
3652 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3654 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
3655 && strcmp (pfile->if_stack->fname,
3656 CPP_BUFFER (pfile)->nominal_fname) != 0)
3657 fprintf (stderr, ", file %s", pfile->if_stack->fname);
3658 fprintf (stderr, ")\n");
3660 pfile->if_stack->type = T_ELIF;
3663 if (pfile->if_stack->if_succeeded)
3664 skip_if_group (pfile, 0);
3666 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
3668 skip_if_group (pfile, 0);
3670 ++pfile->if_stack->if_succeeded; /* continue processing input */
3671 output_line_command (pfile, 1, same_file);
3678 * evaluate a #if expression in BUF, of length LENGTH,
3679 * then parse the result as a C expression and return the value as an int.
3682 static HOST_WIDE_INT
3683 eval_if_expression (pfile, buf, length)
3685 U_CHAR *buf ATTRIBUTE_UNUSED;
3686 int length ATTRIBUTE_UNUSED;
3688 HASHNODE *save_defined;
3689 HOST_WIDE_INT value;
3690 long old_written = CPP_WRITTEN (pfile);
3692 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
3693 pfile->pcp_inside_if = 1;
3695 value = cpp_parse_expr (pfile);
3696 pfile->pcp_inside_if = 0;
3697 delete_macro (save_defined); /* clean up special symbol */
3699 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3705 * routine to handle ifdef/ifndef. Try to look up the symbol,
3706 * then do or don't skip to the #endif/#else/#elif depending
3707 * on what directive is actually being processed.
3711 do_xifdef (pfile, keyword, unused1, unused2)
3713 struct directive *keyword;
3714 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3717 cpp_buffer *ip = CPP_BUFFER (pfile);
3720 enum cpp_token token;
3721 int start_of_file = 0;
3722 U_CHAR *control_macro = 0;
3723 int old_written = CPP_WRITTEN (pfile);
3725 /* Detect a #ifndef at start of file (not counting comments). */
3726 if (ip->fname != 0 && keyword->type == T_IFNDEF)
3727 start_of_file = pfile->only_seen_white == 2;
3729 pfile->no_macro_expand++;
3730 token = get_directive_token (pfile);
3731 pfile->no_macro_expand--;
3733 ident = pfile->token_buffer + old_written;
3734 ident_length = CPP_WRITTEN (pfile) - old_written;
3735 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3737 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
3739 skip = (keyword->type == T_IFDEF);
3740 if (! CPP_TRADITIONAL (pfile))
3741 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
3743 else if (token == CPP_NAME)
3745 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
3746 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
3747 if (start_of_file && !skip)
3749 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
3750 bcopy (ident, control_macro, ident_length + 1);
3755 skip = (keyword->type == T_IFDEF);
3756 if (! CPP_TRADITIONAL (pfile))
3757 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
3760 if (!CPP_TRADITIONAL (pfile))
3762 cpp_skip_hspace (pfile);
3764 if (c != EOF && c != '\n')
3765 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
3767 skip_rest_of_line (pfile);
3771 /* Output a precondition for this macro. */
3772 if (hp && hp->value.defn->predefined)
3773 fprintf (pcp_outfile, "#define %s\n", hp->name);
3776 fprintf (pcp_outfile, "#undef ");
3777 while (is_idchar[*cp]) /* Ick! */
3778 fputc (*cp++, pcp_outfile);
3779 putc ('\n', pcp_outfile);
3783 conditional_skip (pfile, skip, T_IF, control_macro);
3787 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3788 If this is a #ifndef starting at the beginning of a file,
3789 CONTROL_MACRO is the macro name tested by the #ifndef.
3790 Otherwise, CONTROL_MACRO is 0. */
3793 conditional_skip (pfile, skip, type, control_macro)
3796 enum node_type type;
3797 U_CHAR *control_macro;
3799 IF_STACK_FRAME *temp;
3801 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3802 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
3804 temp->lineno = CPP_BUFFER (pfile)->lineno;
3806 temp->next = pfile->if_stack;
3807 temp->control_macro = control_macro;
3808 pfile->if_stack = temp;
3810 pfile->if_stack->type = type;
3813 skip_if_group (pfile, 0);
3816 ++pfile->if_stack->if_succeeded;
3817 output_line_command (pfile, 1, same_file);
3822 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3823 * leaves input ptr at the sharp sign found.
3824 * If ANY is nonzero, return at next directive of any sort.
3828 skip_if_group (pfile, any)
3833 struct directive *kt;
3834 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
3836 U_CHAR *beg_of_line = bp;
3838 register int ident_length;
3840 struct parse_marker line_start_mark;
3842 parse_set_mark (&line_start_mark, pfile);
3844 if (CPP_OPTIONS (pfile)->output_conditionals) {
3845 static char failed[] = "#failed\n";
3846 CPP_PUTS (pfile, failed, sizeof(failed)-1);
3848 output_line_command (pfile, 1, same_file);
3852 if (CPP_OPTIONS (pfile)->output_conditionals)
3854 cpp_buffer *pbuf = CPP_BUFFER (pfile);
3855 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
3856 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
3858 parse_move_mark (&line_start_mark, pfile);
3859 if (!CPP_TRADITIONAL (pfile))
3860 cpp_skip_hspace (pfile);
3864 int old_written = CPP_WRITTEN (pfile);
3865 cpp_skip_hspace (pfile);
3867 parse_name (pfile, GETC());
3868 ident_length = CPP_WRITTEN (pfile) - old_written;
3869 ident = pfile->token_buffer + old_written;
3870 pfile->limit = ident;
3872 if (ident_length == 0)
3873 goto not_a_directive;
3875 /* Handle # followed by a line number. */
3877 /* Avoid error for `###' and similar cases unless -pedantic. */
3880 for (kt = directive_table; kt->length >= 0; kt++)
3882 IF_STACK_FRAME *temp;
3883 if (ident_length == kt->length
3884 && strncmp (ident, kt->name, kt->length) == 0)
3886 /* If we are asked to return on next directive, do so now. */
3896 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3897 temp->next = pfile->if_stack;
3898 pfile->if_stack = temp;
3900 temp->lineno = CPP_BUFFER(pfile)->lineno;
3902 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
3903 temp->type = kt->type;
3907 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
3908 validate_else (pfile,
3909 kt->type == T_ELSE ? "#else" : "#endif");
3911 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
3914 "`#%s' not within a conditional", kt->name);
3917 else if (pfile->if_stack == save_if_stack)
3918 goto done; /* found what we came for */
3920 if (kt->type != T_ENDIF)
3922 if (pfile->if_stack->type == T_ELSE)
3923 cpp_error (pfile, "`#else' or `#elif' after `#else'");
3924 pfile->if_stack->type = kt->type;
3928 temp = pfile->if_stack;
3929 pfile->if_stack = temp->next;
3936 /* Don't let erroneous code go by. */
3937 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
3938 && CPP_PEDANTIC (pfile))
3939 cpp_pedwarn (pfile, "invalid preprocessor directive name");
3943 /* We're in the middle of a line. Skip the rest of it. */
3950 case '/': /* possible comment */
3951 c = skip_comment (pfile, NULL);
3958 old = CPP_WRITTEN (pfile);
3959 cpp_get_token (pfile);
3960 CPP_SET_WRITTEN (pfile, old);
3963 /* Char after backslash loses its special meaning. */
3964 if (PEEKC() == '\n')
3974 if (CPP_OPTIONS (pfile)->output_conditionals) {
3975 static char end_failed[] = "#endfailed\n";
3976 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
3979 pfile->only_seen_white = 1;
3980 parse_goto_mark (&line_start_mark, pfile);
3981 parse_clear_mark (&line_start_mark);
3985 * handle a #else directive. Do this by just continuing processing
3986 * without changing if_stack ; this is so that the error message
3987 * for missing #endif's etc. will point to the original #if. It
3988 * is possible that something different would be better.
3992 do_else (pfile, keyword, buf, limit)
3994 struct directive *keyword ATTRIBUTE_UNUSED;
3995 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3997 cpp_buffer *ip = CPP_BUFFER (pfile);
3999 if (CPP_PEDANTIC (pfile))
4000 validate_else (pfile, "#else");
4001 skip_rest_of_line (pfile);
4003 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4004 cpp_error (pfile, "`#else' not within a conditional");
4007 /* #ifndef can't have its special treatment for containing the whole file
4008 if it has a #else clause. */
4009 pfile->if_stack->control_macro = 0;
4011 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4012 cpp_error (pfile, "`#else' after `#else'");
4013 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4014 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4015 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4016 fprintf (stderr, ")\n");
4018 pfile->if_stack->type = T_ELSE;
4021 if (pfile->if_stack->if_succeeded)
4022 skip_if_group (pfile, 0);
4024 ++pfile->if_stack->if_succeeded; /* continue processing input */
4025 output_line_command (pfile, 1, same_file);
4031 * unstack after #endif command
4035 do_endif (pfile, keyword, buf, limit)
4037 struct directive *keyword ATTRIBUTE_UNUSED;
4038 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4040 if (CPP_PEDANTIC (pfile))
4041 validate_else (pfile, "#endif");
4042 skip_rest_of_line (pfile);
4044 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4045 cpp_error (pfile, "unbalanced `#endif'");
4048 IF_STACK_FRAME *temp = pfile->if_stack;
4049 pfile->if_stack = temp->next;
4050 if (temp->control_macro != 0)
4052 /* This #endif matched a #ifndef at the start of the file.
4053 See if it is at the end of the file. */
4054 struct parse_marker start_mark;
4057 parse_set_mark (&start_mark, pfile);
4061 cpp_skip_hspace (pfile);
4066 parse_goto_mark (&start_mark, pfile);
4067 parse_clear_mark (&start_mark);
4071 /* This #endif ends a #ifndef
4072 that contains all of the file (aside from whitespace).
4073 Arrange not to include the file again
4074 if the macro that was tested is defined. */
4075 struct cpp_buffer *ip;
4076 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
4077 if (ip->fname != NULL)
4079 ip->ihash->control_macro = temp->control_macro;
4083 output_line_command (pfile, 1, same_file);
4088 /* When an #else or #endif is found while skipping failed conditional,
4089 if -pedantic was specified, this is called to warn about text after
4090 the command name. P points to the first char after the command name. */
4093 validate_else (pfile, directive)
4098 cpp_skip_hspace (pfile);
4100 if (c != EOF && c != '\n')
4102 "text following `%s' violates ANSI standard", directive);
4105 /* Get the next token, and add it to the text in pfile->token_buffer.
4106 Return the kind of token we got. */
4109 cpp_get_token (pfile)
4112 register int c, c2, c3;
4114 long start_line, start_column;
4115 enum cpp_token token;
4116 struct cpp_options *opts = CPP_OPTIONS (pfile);
4117 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4123 if (CPP_BUFFER (pfile)->seen_eof)
4125 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4132 cpp_buffer *next_buf
4133 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4134 CPP_BUFFER (pfile)->seen_eof = 1;
4135 if (CPP_BUFFER (pfile)->nominal_fname
4136 && next_buf != CPP_NULL_BUFFER (pfile))
4138 /* We're about to return from an #include file.
4139 Emit #line information now (as part of the CPP_POP) result.
4140 But the #line refers to the file we will pop to. */
4141 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4142 CPP_BUFFER (pfile) = next_buf;
4143 pfile->input_stack_listing_current = 0;
4144 output_line_command (pfile, 0, leave_file);
4145 CPP_BUFFER (pfile) = cur_buffer;
4155 struct parse_marker start_mark;
4157 if (PEEKC () == '=')
4159 if (opts->put_out_comments)
4160 parse_set_mark (&start_mark, pfile);
4162 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4163 &start_line, &start_column);
4164 c = skip_comment (pfile, &newlines);
4165 if (opts->put_out_comments && (c == '/' || c == EOF))
4166 parse_clear_mark (&start_mark);
4171 cpp_error_with_line (pfile, start_line, start_column,
4172 "unterminated comment");
4175 c = '/'; /* Initial letter of comment. */
4177 /* Comments are equivalent to spaces.
4178 For -traditional, a comment is equivalent to nothing. */
4179 if (opts->put_out_comments)
4181 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4182 U_CHAR *start = pbuf->buf + start_mark.position;
4183 int len = pbuf->cur - start;
4184 CPP_RESERVE(pfile, 1 + len);
4185 CPP_PUTC_Q (pfile, c);
4186 CPP_PUTS_Q (pfile, start, len);
4187 pfile->lineno += newlines;
4188 parse_clear_mark (&start_mark);
4191 else if (CPP_TRADITIONAL (pfile))
4198 /* This may not work if cpp_get_token is called recursively,
4199 since many places look for horizontal space. */
4202 /* Copy the newlines into the output buffer, in order to
4203 avoid the pain of a #line every time a multiline comment
4205 CPP_RESERVE(pfile, newlines);
4206 while (--newlines >= 0)
4208 CPP_PUTC_Q (pfile, '\n');
4214 CPP_RESERVE(pfile, 1);
4215 CPP_PUTC_Q (pfile, ' ');
4219 if (opts->for_lint) {
4222 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4224 if (lintcmd != NULL) {
4225 /* I believe it is always safe to emit this newline: */
4227 bcopy ("#pragma lint ", (char *) obp, 13);
4229 bcopy (lintcmd, (char *) obp, cmdlen);
4234 bcopy (argbp, (char *) obp, arglen);
4238 /* OK, now bring us back to the state we were in before we entered
4239 this branch. We need #line because the newline for the pragma
4240 could mess things up. */
4241 output_line_command (pfile, 0, same_file);
4242 *(obp++) = ' '; /* just in case, if comments are copied thru */
4250 /* If this is expanding a macro definition, don't recognize
4251 preprocessor directives. */
4254 /* If this is expand_into_temp_buffer, recognize them
4255 only after an actual newline at this level,
4256 not at the beginning of the input level. */
4257 if (ip->fname == 0 && beg_of_line == ip->buf)
4263 if (!pfile->only_seen_white)
4265 if (handle_directive (pfile))
4266 return CPP_DIRECTIVE;
4267 pfile->only_seen_white = 0;
4272 /* A single quoted string is treated like a double -- some
4273 programs (e.g., troff) are perverse this way */
4274 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4275 &start_line, &start_column);
4276 old_written = CPP_WRITTEN (pfile);
4278 CPP_PUTC (pfile, c);
4284 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4286 /* try harder: this string crosses a macro expansion
4287 boundary. This can happen naturally if -traditional.
4288 Otherwise, only -D can make a macro with an unmatched
4290 cpp_buffer *next_buf
4291 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4292 (*CPP_BUFFER (pfile)->cleanup)
4293 (CPP_BUFFER (pfile), pfile);
4294 CPP_BUFFER (pfile) = next_buf;
4297 if (!CPP_TRADITIONAL (pfile))
4299 cpp_error_with_line (pfile, start_line, start_column,
4300 "unterminated string or character constant");
4301 if (pfile->multiline_string_line != start_line
4302 && pfile->multiline_string_line != 0)
4303 cpp_error_with_line (pfile,
4304 pfile->multiline_string_line, -1,
4305 "possible real start of unterminated constant");
4306 pfile->multiline_string_line = 0;
4310 CPP_PUTC (pfile, cc);
4314 /* Traditionally, end of line ends a string constant with
4315 no error. So exit the loop and record the new line. */
4316 if (CPP_TRADITIONAL (pfile))
4320 cpp_error_with_line (pfile, start_line, start_column,
4321 "unterminated character constant");
4324 if (CPP_PEDANTIC (pfile)
4325 && pfile->multiline_string_line == 0)
4327 cpp_pedwarn_with_line (pfile, start_line, start_column,
4328 "string constant runs past end of line");
4330 if (pfile->multiline_string_line == 0)
4331 pfile->multiline_string_line = start_line;
4338 /* Backslash newline is replaced by nothing at all. */
4339 CPP_ADJUST_WRITTEN (pfile, -1);
4344 /* ANSI stupidly requires that in \\ the second \
4345 is *not* prevented from combining with a newline. */
4348 CPP_PUTC (pfile, cc);
4360 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4361 CPP_PWRITTEN (pfile));
4362 pfile->only_seen_white = 0;
4363 return c == '\'' ? CPP_CHAR : CPP_STRING;
4366 if (!opts->dollars_in_ident)
4371 if (opts->cplusplus && PEEKC () == ':')
4380 if (c2 == c || c2 == '=')
4390 if (PEEKC () == '=')
4397 if (c2 == '-' && opts->chill)
4399 /* Chill style comment */
4400 if (opts->put_out_comments)
4401 parse_set_mark (&start_mark, pfile);
4402 FORWARD(1); /* Skip second '-'. */
4410 /* Don't consider final '\n' to be part of comment. */
4416 goto return_comment;
4418 if (c2 == '-' || c2 == '=' || c2 == '>')
4423 if (pfile->parsing_include_directive)
4427 CPP_PUTC (pfile, c);
4432 if (c == '\n' || c == EOF)
4435 "missing '>' in `#include <FILENAME>'");
4441 /* else fall through */
4450 CPP_RESERVE (pfile, 4);
4451 CPP_PUTC (pfile, c);
4452 CPP_PUTC (pfile, c2);
4456 CPP_PUTC_Q (pfile, GETC ());
4457 CPP_NUL_TERMINATE_Q (pfile);
4458 pfile->only_seen_white = 0;
4462 if (CPP_BUFFER (pfile)->has_escapes)
4467 if (pfile->output_escapes)
4468 CPP_PUTS (pfile, "@-", 2);
4469 parse_name (pfile, GETC ());
4472 else if (is_space [c])
4474 CPP_RESERVE (pfile, 2);
4475 if (pfile->output_escapes)
4476 CPP_PUTC_Q (pfile, '@');
4477 CPP_PUTC_Q (pfile, c);
4481 if (pfile->output_escapes)
4483 CPP_PUTS (pfile, "@@", 2);
4493 CPP_RESERVE(pfile, 2);
4494 CPP_PUTC_Q (pfile, '.');
4498 /* FIXME - misses the case "..\\\n." */
4499 if (c2 == '.' && PEEKN(1) == '.')
4501 CPP_RESERVE(pfile, 4);
4502 CPP_PUTC_Q (pfile, '.');
4503 CPP_PUTC_Q (pfile, '.');
4504 CPP_PUTC_Q (pfile, '.');
4506 CPP_NUL_TERMINATE_Q (pfile);
4507 pfile->only_seen_white = 0;
4514 pfile->only_seen_white = 0;
4516 CPP_RESERVE(pfile, 3);
4517 CPP_PUTC_Q (pfile, c);
4518 CPP_PUTC_Q (pfile, GETC ());
4519 CPP_NUL_TERMINATE_Q (pfile);
4525 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4527 CPP_PUTC (pfile, c);
4533 case '0': case '1': case '2': case '3': case '4':
4534 case '5': case '6': case '7': case '8': case '9':
4539 CPP_RESERVE (pfile, 2);
4540 CPP_PUTC_Q (pfile, c);
4545 if (!is_idchar[c] && c != '.'
4546 && ((c2 != 'e' && c2 != 'E'
4547 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4548 || (c != '+' && c != '-')))
4553 CPP_NUL_TERMINATE_Q (pfile);
4554 pfile->only_seen_white = 0;
4556 case 'b': case 'c': case 'd': case 'h': case 'o':
4557 case 'B': case 'C': case 'D': case 'H': case 'O':
4558 if (opts->chill && PEEKC () == '\'')
4560 pfile->only_seen_white = 0;
4561 CPP_RESERVE (pfile, 2);
4562 CPP_PUTC_Q (pfile, c);
4563 CPP_PUTC_Q (pfile, '\'');
4569 goto chill_number_eof;
4572 if (c == '\\' && PEEKC() == '\n')
4579 CPP_PUTC (pfile, c);
4583 CPP_RESERVE (pfile, 2);
4584 CPP_PUTC_Q (pfile, c);
4585 CPP_NUL_TERMINATE_Q (pfile);
4592 CPP_NUL_TERMINATE (pfile);
4599 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4600 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4601 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4602 case 'x': case 'y': case 'z':
4603 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4604 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4605 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4610 unsigned char *ident;
4611 int before_name_written = CPP_WRITTEN (pfile);
4613 parse_name (pfile, c);
4614 pfile->only_seen_white = 0;
4615 if (pfile->no_macro_expand)
4617 ident = pfile->token_buffer + before_name_written;
4618 ident_len = CPP_PWRITTEN (pfile) - ident;
4619 hp = cpp_lookup (pfile, ident, ident_len, -1);
4622 if (hp->type == T_DISABLED)
4624 if (pfile->output_escapes)
4625 { /* Return "@-IDENT", followed by '\0'. */
4627 CPP_RESERVE (pfile, 3);
4628 ident = pfile->token_buffer + before_name_written;
4629 CPP_ADJUST_WRITTEN (pfile, 2);
4630 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
4637 /* If macro wants an arglist, verify that a '(' follows.
4638 first skip all whitespace, copying it to the output
4639 after the macro name. Then, if there is no '(',
4640 decide this is not a macro call and leave things that way. */
4641 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
4643 struct parse_marker macro_mark;
4644 int is_macro_call, macbuf_whitespace = 0;
4646 parse_set_mark (¯o_mark, pfile);
4649 cpp_skip_hspace (pfile);
4651 is_macro_call = c == '(';
4660 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4662 if (macro_mark.position !=
4663 (CPP_BUFFER (pfile)->cur
4664 - CPP_BUFFER (pfile)->buf))
4665 macbuf_whitespace = 1;
4667 parse_clear_mark (¯o_mark);
4668 cpp_pop_buffer (pfile);
4669 parse_set_mark (¯o_mark, pfile);
4677 parse_goto_mark (¯o_mark, pfile);
4678 if (macbuf_whitespace)
4679 CPP_PUTC (pfile, ' ');
4681 parse_clear_mark (¯o_mark);
4685 /* This is now known to be a macro call. */
4687 /* it might not actually be a macro. */
4688 if (hp->type != T_MACRO) {
4689 int xbuf_len; U_CHAR *xbuf;
4690 CPP_SET_WRITTEN (pfile, before_name_written);
4691 special_symbol (hp, pfile);
4692 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
4693 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4694 CPP_SET_WRITTEN (pfile, before_name_written);
4695 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
4696 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4700 /* Expand the macro, reading arguments as needed,
4701 and push the expansion on the input stack. */
4702 macroexpand (pfile, hp);
4703 CPP_SET_WRITTEN (pfile, before_name_written);
4706 /* An extra "@ " is added to the end of a macro expansion
4707 to prevent accidental token pasting. We prefer to avoid
4708 unneeded extra spaces (for the sake of cpp-using tools like
4709 imake). Here we remove the space if it is safe to do so. */
4710 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
4711 && pfile->buffer->rlimit[-2] == '@'
4712 && pfile->buffer->rlimit[-1] == ' ')
4714 int c1 = pfile->buffer->rlimit[-3];
4715 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
4716 if (c2 == EOF || ! unsafe_chars (c1, c2))
4717 pfile->buffer->rlimit -= 2;
4722 case ' ': case '\t': case '\v': case '\r':
4725 CPP_PUTC (pfile, c);
4727 if (c == EOF || !is_hor_space[c])
4741 CPP_PUTC (pfile, c);
4742 if (pfile->only_seen_white == 0)
4743 pfile->only_seen_white = 1;
4745 output_line_command (pfile, 1, same_file);
4748 case '(': token = CPP_LPAREN; goto char1;
4749 case ')': token = CPP_RPAREN; goto char1;
4750 case '{': token = CPP_LBRACE; goto char1;
4751 case '}': token = CPP_RBRACE; goto char1;
4752 case ',': token = CPP_COMMA; goto char1;
4753 case ';': token = CPP_SEMICOLON; goto char1;
4759 pfile->only_seen_white = 0;
4760 CPP_PUTC (pfile, c);
4766 /* Like cpp_get_token, but skip spaces and comments. */
4769 cpp_get_non_space_token (pfile)
4772 int old_written = CPP_WRITTEN (pfile);
4775 enum cpp_token token = cpp_get_token (pfile);
4776 if (token != CPP_COMMENT && token != CPP_POP
4777 && token != CPP_HSPACE && token != CPP_VSPACE)
4779 CPP_SET_WRITTEN (pfile, old_written);
4783 /* Parse an identifier starting with C. */
4786 parse_name (pfile, c)
4787 cpp_reader *pfile; int c;
4793 if (c == '\\' && PEEKC() == '\n')
4802 if (c == '$' && CPP_PEDANTIC (pfile))
4803 cpp_pedwarn (pfile, "`$' in identifier");
4805 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
4806 CPP_PUTC_Q (pfile, c);
4811 CPP_NUL_TERMINATE_Q (pfile);
4815 /* This is called after options have been processed.
4816 * Check options for consistency, and setup for processing input
4817 * from the file named FNAME. (Use standard input if FNAME==NULL.)
4818 * Return 1 on success, 0 on failure.
4822 cpp_start_read (pfile, fname)
4826 struct cpp_options *opts = CPP_OPTIONS (pfile);
4827 struct cpp_pending *pend;
4831 struct include_hash *ih_fake;
4833 /* The code looks at the defaults through this pointer, rather than through
4834 the constant structure above. This pointer gets changed if an environment
4835 variable specifies other defaults. */
4836 struct default_include *include_defaults = include_defaults_array;
4838 /* Now that we know dollars_in_ident for real,
4839 reset is_idchar/is_idstart. */
4840 is_idchar['$'] = opts->dollars_in_ident;
4841 is_idstart['$'] = opts->dollars_in_ident;
4843 /* Add dirs from CPATH after dirs from -I. */
4844 /* There seems to be confusion about what CPATH should do,
4845 so for the moment it is not documented. */
4846 /* Some people say that CPATH should replace the standard include dirs,
4847 but that seems pointless: it comes before them, so it overrides them
4849 GET_ENV_PATH_LIST (p, "CPATH");
4850 if (p != 0 && ! opts->no_standard_includes)
4851 path_include (pfile, p);
4853 /* Do partial setup of input buffer for the sake of generating
4854 early #line directives (when -g is in effect). */
4855 fp = cpp_push_buffer (pfile, NULL, 0);
4858 if (opts->in_fname == NULL)
4859 opts->in_fname = "";
4860 fp->nominal_fname = fp->fname = opts->in_fname;
4863 /* Install __LINE__, etc. Must follow initialize_char_syntax
4864 and option processing. */
4865 initialize_builtins (pfile);
4867 /* Do standard #defines and assertions
4868 that identify system and machine type. */
4870 if (!opts->inhibit_predefs) {
4871 char *p = (char *) alloca (strlen (predefs) + 1);
4872 strcpy (p, predefs);
4875 while (*p == ' ' || *p == '\t')
4877 /* Handle -D options. */
4878 if (p[0] == '-' && p[1] == 'D') {
4880 while (*p && *p != ' ' && *p != '\t')
4884 if (opts->debug_output)
4885 output_line_command (pfile, 0, same_file);
4886 cpp_define (pfile, q);
4887 while (*p == ' ' || *p == '\t')
4889 } else if (p[0] == '-' && p[1] == 'A') {
4890 /* Handle -A options (assertions). */
4899 past_name = assertion;
4900 /* Locate end of name. */
4901 while (*past_name && *past_name != ' '
4902 && *past_name != '\t' && *past_name != '(')
4904 /* Locate `(' at start of value. */
4906 while (*value && (*value == ' ' || *value == '\t'))
4908 if (*value++ != '(')
4910 while (*value && (*value == ' ' || *value == '\t'))
4913 /* Locate end of value. */
4914 while (*past_value && *past_value != ' '
4915 && *past_value != '\t' && *past_value != ')')
4917 termination = past_value;
4918 while (*termination && (*termination == ' ' || *termination == '\t'))
4920 if (*termination++ != ')')
4922 if (*termination && *termination != ' ' && *termination != '\t')
4924 /* Temporarily null-terminate the value. */
4925 save_char = *termination;
4926 *termination = '\0';
4927 /* Install the assertion. */
4928 make_assertion (pfile, "-A", assertion);
4929 *termination = (char) save_char;
4931 while (*p == ' ' || *p == '\t')
4939 /* Now handle the command line options. */
4941 /* Do -U's, -D's and -A's in the order they were seen. */
4942 /* First reverse the list. */
4943 opts->pending = nreverse_pending (opts->pending);
4945 for (pend = opts->pending; pend; pend = pend->next)
4947 if (pend->cmd != NULL && pend->cmd[0] == '-')
4949 switch (pend->cmd[1])
4952 if (opts->debug_output)
4953 output_line_command (pfile, 0, same_file);
4954 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
4957 if (opts->debug_output)
4958 output_line_command (pfile, 0, same_file);
4959 cpp_define (pfile, pend->arg);
4962 make_assertion (pfile, "-A", pend->arg);
4968 opts->done_initializing = 1;
4970 { /* Read the appropriate environment variable and if it exists
4971 replace include_defaults with the listed path. */
4973 switch ((opts->objc << 1) + opts->cplusplus)
4976 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
4979 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
4982 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
4985 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
4988 /* If the environment var for this language is set,
4989 add to the default list of include directories. */
4991 char *nstore = (char *) alloca (strlen (epath) + 2);
4993 char *startp, *endp;
4995 for (num_dirs = 1, startp = epath; *startp; startp++)
4996 if (*startp == PATH_SEPARATOR)
4999 = (struct default_include *) xmalloc ((num_dirs
5000 * sizeof (struct default_include))
5001 + sizeof (include_defaults_array));
5002 startp = endp = epath;
5005 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5006 if ((*endp == PATH_SEPARATOR)
5008 strncpy (nstore, startp, endp-startp);
5010 strcpy (nstore, ".");
5012 nstore[endp-startp] = '\0';
5014 include_defaults[num_dirs].fname = savestring (nstore);
5015 include_defaults[num_dirs].component = 0;
5016 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5017 include_defaults[num_dirs].cxx_aware = 1;
5021 endp = startp = endp + 1;
5025 /* Put the usual defaults back in at the end. */
5026 bcopy ((char *) include_defaults_array,
5027 (char *) &include_defaults[num_dirs],
5028 sizeof (include_defaults_array));
5032 /* Unless -fnostdinc,
5033 tack on the standard include file dirs to the specified list */
5034 if (!opts->no_standard_includes) {
5035 struct default_include *p = include_defaults;
5036 char *specd_prefix = opts->include_prefix;
5037 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5038 int default_len = 0;
5039 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5040 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5041 default_len = strlen (default_prefix) - 7;
5042 default_prefix[default_len] = 0;
5044 /* Search "translated" versions of GNU directories.
5045 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5046 if (specd_prefix != 0 && default_len != 0)
5047 for (p = include_defaults; p->fname; p++) {
5048 /* Some standard dirs are only for C++. */
5050 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5051 /* Does this dir start with the prefix? */
5052 if (!strncmp (p->fname, default_prefix, default_len)) {
5053 /* Yes; change prefix and add to search list. */
5054 int this_len = strlen (specd_prefix)
5055 + strlen (p->fname) - default_len;
5056 char *str = (char *) xmalloc (this_len + 1);
5057 strcpy (str, specd_prefix);
5058 strcat (str, p->fname + default_len);
5060 append_include_chain (pfile, &opts->system_include,
5061 str, !p->cxx_aware);
5065 /* Search ordinary names for GNU include directories. */
5066 for (p = include_defaults; p->fname; p++) {
5067 /* Some standard dirs are only for C++. */
5069 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5070 char *str = update_path (p->fname, p->component);
5071 append_include_chain (pfile, &opts->system_include,
5072 str, !p->cxx_aware);
5077 merge_include_chains (opts);
5079 /* With -v, print the list of dirs to search. */
5080 if (opts->verbose) {
5081 struct file_name_list *p;
5082 fprintf (stderr, "#include \"...\" search starts here:\n");
5083 for (p = opts->quote_include; p; p = p->next) {
5084 if (p == opts->bracket_include)
5085 fprintf (stderr, "#include <...> search starts here:\n");
5086 fprintf (stderr, " %s\n", p->name);
5088 fprintf (stderr, "End of search list.\n");
5091 /* Copy the entire contents of the main input file into
5092 the stacked input buffer previously allocated for it. */
5093 if (fname == NULL || *fname == 0) {
5096 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
5097 cpp_pfatal_with_name (pfile, fname);
5099 /* -MG doesn't select the form of output and must be specified with one of
5100 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5101 inhibit compilation. */
5102 if (opts->print_deps_missing_files
5103 && (opts->print_deps == 0 || !opts->no_output))
5105 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
5109 /* Either of two environment variables can specify output of deps.
5110 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
5111 where OUTPUT_FILE is the file to write deps info to
5112 and DEPS_TARGET is the target to mention in the deps. */
5114 if (opts->print_deps == 0
5115 && (getenv ("SUNPRO_DEPENDENCIES") != 0
5116 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
5117 char *spec = getenv ("DEPENDENCIES_OUTPUT");
5123 spec = getenv ("SUNPRO_DEPENDENCIES");
5124 opts->print_deps = 2;
5127 opts->print_deps = 1;
5130 /* Find the space before the DEPS_TARGET, if there is one. */
5131 /* This should use index. (mrs) */
5132 while (*s != 0 && *s != ' ') s++;
5135 opts->deps_target = s + 1;
5136 output_file = (char *) xmalloc (s - spec + 1);
5137 bcopy (spec, output_file, s - spec);
5138 output_file[s - spec] = 0;
5142 opts->deps_target = 0;
5146 opts->deps_file = output_file;
5147 opts->print_deps_append = 1;
5150 /* For -M, print the expected object file name
5151 as the target of this Make-rule. */
5152 if (opts->print_deps)
5154 pfile->deps_allocated_size = 200;
5155 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
5156 pfile->deps_buffer[0] = 0;
5157 pfile->deps_size = 0;
5158 pfile->deps_column = 0;
5160 if (opts->deps_target)
5161 deps_output (pfile, opts->deps_target, ':');
5162 else if (*opts->in_fname == 0)
5163 deps_output (pfile, "-", ':');
5168 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
5169 ".cc", ".cxx", ".cpp", ".cp",
5173 /* Discard all directory prefixes from filename. */
5174 if ((q = rindex (opts->in_fname, '/')) != NULL
5175 #ifdef DIR_SEPARATOR
5176 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
5183 /* Copy remainder to mungable area. */
5184 p = (char *) alloca (strlen(q) + 8);
5187 /* Output P, but remove known suffixes. */
5190 /* Point to the filename suffix. */
5191 r = rindex (p, '.');
5192 /* Compare against the known suffixes. */
5194 while (known_suffixes[x] != 0)
5196 if (strncmp (known_suffixes[x], r, q - r) == 0)
5198 /* Make q point to the bit we're going to overwrite
5199 with an object suffix. */
5206 /* Supply our own suffix. */
5213 deps_output (pfile, p, ':');
5214 deps_output (pfile, opts->in_fname, ' ');
5219 /* Make sure data ends with a newline. And put a null after it. */
5221 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5222 /* Backslash-newline at end is not good enough. */
5223 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5224 fp->buf[fp->length++] = '\n';
5225 missing_newline = 1;
5227 fp->buf[fp->length] = '\0';
5229 /* Unless inhibited, convert trigraphs in the input. */
5235 /* Must call finclude() on the main input before processing
5236 -include switches; otherwise the -included text winds up
5237 after the main input. */
5238 ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
5240 ih_fake->next_this_file = 0;
5241 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5242 ih_fake->name = fname;
5243 ih_fake->control_macro = 0;
5244 ih_fake->buf = (char *)-1;
5246 if (!finclude (pfile, f, ih_fake))
5248 output_line_command (pfile, 0, same_file);
5249 pfile->only_seen_white = 2;
5251 /* The -imacros files can be scanned now, but the -include files
5252 have to be pushed onto the include stack and processed later,
5253 in the main loop calling cpp_get_token. That means the -include
5254 files have to be processed in reverse order of the pending list,
5255 which means the pending list has to be reversed again, which
5256 means the -imacros files have to be done separately and first. */
5258 pfile->no_record_file++;
5260 for (pend = opts->pending; pend; pend = pend->next)
5262 if (pend->cmd != NULL)
5264 if (strcmp (pend->cmd, "-imacros") == 0)
5266 int fd = open (pend->arg, O_RDONLY, 0666);
5269 cpp_perror_with_name (pfile, pend->arg);
5272 if (!cpp_push_buffer (pfile, NULL, 0))
5275 ih_fake = (struct include_hash *)
5276 xmalloc (sizeof (struct include_hash));
5278 ih_fake->next_this_file = 0;
5279 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5280 ih_fake->name = pend->arg;
5281 ih_fake->control_macro = 0;
5282 ih_fake->buf = (char *)-1;
5284 if (!finclude (pfile, fd, ih_fake))
5285 cpp_scan_buffer (pfile);
5291 opts->pending = nreverse_pending (opts->pending);
5292 for (pend = opts->pending; pend; pend = pend->next)
5294 if (pend->cmd != NULL)
5296 if (strcmp (pend->cmd, "-include") == 0)
5298 int fd = open (pend->arg, O_RDONLY, 0666);
5301 cpp_perror_with_name (pfile, pend->arg);
5304 if (!cpp_push_buffer (pfile, NULL, 0))
5307 ih_fake = (struct include_hash *)
5308 xmalloc (sizeof (struct include_hash));
5310 ih_fake->next_this_file = 0;
5311 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
5312 ih_fake->name = pend->arg;
5313 ih_fake->control_macro = 0;
5314 ih_fake->buf = (char *)-1;
5316 if (finclude (pfile, fd, ih_fake))
5317 output_line_command (pfile, 0, enter_file);
5321 pfile->no_record_file--;
5323 /* Free the pending list. */
5324 for (pend = opts->pending; pend; )
5326 struct cpp_pending *next = pend->next;
5330 opts->pending = NULL;
5336 cpp_reader_init (pfile)
5339 bzero ((char *) pfile, sizeof (cpp_reader));
5340 pfile->get_token = cpp_get_token;
5342 pfile->token_buffer_size = 200;
5343 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
5344 CPP_SET_WRITTEN (pfile, 0);
5346 pfile->timebuf = NULL;
5347 pfile->only_seen_white = 1;
5348 pfile->buffer = CPP_NULL_BUFFER(pfile);
5349 pfile->actual_dirs = NULL;
5352 static struct cpp_pending *
5353 nreverse_pending (list)
5354 struct cpp_pending *list;
5357 register struct cpp_pending *prev = 0, *next, *pend;
5358 for (pend = list; pend; pend = next)
5368 push_pending (pfile, cmd, arg)
5373 struct cpp_pending *pend
5374 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
5377 pend->next = CPP_OPTIONS (pfile)->pending;
5378 CPP_OPTIONS (pfile)->pending = pend;
5385 printf ("Usage: %s [switches] input output\n", progname);
5386 printf ("Switches:\n");
5387 printf (" -include <file> Include the contents of <file> before other files\n");
5388 printf (" -imacros <file> Accept definition of marcos in <file>\n");
5389 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
5390 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
5391 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
5392 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
5393 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
5394 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
5395 printf (" -nostdinc Do not search the system include directories\n");
5396 printf (" -nostdinc++ Do not search the system include directories for C++\n");
5397 printf (" -o <file> Put output into <file>\n");
5398 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
5399 printf (" -traditional Follow K&R pre-processor behaviour\n");
5400 printf (" -trigraphs Support ANSI C trigraphs\n");
5401 printf (" -lang-c Assume that the input sources are in C\n");
5402 printf (" -lang-c89 Assume that the input sources are in C89\n");
5403 printf (" -lang-c++ Assume that the input sources are in C++\n");
5404 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
5405 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
5406 printf (" -lang-asm Assume that the input sources are in assembler\n");
5407 printf (" -lang-chill Assume that the input sources are in Chill\n");
5408 printf (" -+ Allow parsing of C++ style features\n");
5409 printf (" -w Inhibit warning messages\n");
5410 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
5411 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
5412 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
5413 printf (" -Wno-comment{s} Do not warn about comments\n");
5414 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
5415 printf (" a string if -tradtional is specified\n");
5416 printf (" -Wno-traditional Do not warn about stringification\n");
5417 printf (" -Wundef Warn if an undefined macro is used by #if\n");
5418 printf (" -Wno-undef Do not warn about testing udefined macros\n");
5419 printf (" -Wimport Warn about the use of the #import directive\n");
5420 printf (" -Wno-import Do not warn about the use of #import\n");
5421 printf (" -Werror Treat all warnings as errors\n");
5422 printf (" -Wno-error Do not treat warnings as errors\n");
5423 printf (" -Wall Enable all preprocessor warnings\n");
5424 printf (" -M Generate make dependencies\n");
5425 printf (" -MM As -M, but ignore system header files\n");
5426 printf (" -MD As -M, but put output in a .d file\n");
5427 printf (" -MMD As -MD, but ignore system header files\n");
5428 printf (" -MG Treat missing header file as generated files\n");
5429 printf (" -g Include #define and #undef directives in the output\n");
5430 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
5431 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
5432 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
5433 printf (" -U<macro> Undefine <macro> \n");
5434 printf (" -u or -undef Do not predefine any macros\n");
5435 printf (" -v Display the version number\n");
5436 printf (" -H Print the name of header files as they are used\n");
5437 printf (" -C Do not discard comments\n");
5438 printf (" -dM Display a list of macro definitions active at end\n");
5439 printf (" -dD Preserve macro definitions in output\n");
5440 printf (" -dN As -dD except that only the names are preserved\n");
5441 printf (" -dI Include #include directives in the output\n");
5442 printf (" -ifoutput Describe skipped code blocks in output \n");
5443 printf (" -P Do not generate #line directives\n");
5444 printf (" -$ Do not include '$' in identifiers\n");
5445 printf (" -remap Remap file names when including files.\n");
5446 printf (" -h or --help Display this information\n");
5450 /* Handle one command-line option in (argc, argv).
5451 Can be called multiple times, to handle multiple sets of options.
5452 Returns number of strings consumed. */
5454 cpp_handle_option (pfile, argc, argv)
5459 struct cpp_options *opts = CPP_OPTIONS (pfile);
5462 if (user_label_prefix == NULL)
5463 user_label_prefix = USER_LABEL_PREFIX;
5465 if (argv[i][0] != '-') {
5466 if (opts->out_fname != NULL)
5469 cpp_fatal (pfile, "Too many arguments");
5471 else if (opts->in_fname != NULL)
5472 opts->out_fname = argv[i];
5474 opts->in_fname = argv[i];
5476 switch (argv[i][1]) {
5479 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
5482 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
5486 if (!strcmp (argv[i], "-fleading-underscore"))
5487 user_label_prefix = "_";
5488 else if (!strcmp (argv[i], "-fno-leading-underscore"))
5489 user_label_prefix = "";
5492 case 'I': /* Add directory to path for includes. */
5493 if (!strcmp (argv[i] + 2, "-"))
5495 if (! opts->ignore_srcdir)
5497 opts->ignore_srcdir = 1;
5498 /* Don't use any preceding -I directories for #include <...>. */
5499 opts->quote_include = opts->bracket_include;
5500 opts->bracket_include = 0;
5506 if (argv[i][2] != 0)
5507 fname = argv[i] + 2;
5508 else if (i + 1 == argc)
5509 goto missing_dirname;
5512 append_include_chain (pfile, &opts->bracket_include, fname, 0);
5517 /* Add directory to beginning of system include path, as a system
5518 include directory. */
5519 if (!strcmp (argv[i], "-isystem"))
5522 goto missing_filename;
5523 append_include_chain (pfile, &opts->system_include, argv[++i], 1);
5525 /* Add directory to end of path for includes,
5526 with the default prefix at the front of its name. */
5527 else if (!strcmp (argv[i], "-iwithprefix"))
5531 goto missing_dirname;
5534 if (opts->include_prefix != 0)
5536 fname = xmalloc (strlen (opts->include_prefix)
5537 + strlen (argv[i]) + 1);
5538 strcpy (fname, opts->include_prefix);
5539 strcat (fname, argv[i]);
5543 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5544 + strlen (argv[i]) + 1);
5545 strcpy (fname, GCC_INCLUDE_DIR);
5546 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5547 if (!strcmp (fname + strlen (fname) - 8, "/include"))
5548 fname[strlen (fname) - 7] = 0;
5549 strcat (fname, argv[i]);
5552 append_include_chain (pfile, &opts->system_include, fname, 0);
5554 /* Add directory to main path for includes,
5555 with the default prefix at the front of its name. */
5556 else if (!strcmp (argv[i], "-iwithprefix"))
5560 goto missing_dirname;
5563 if (opts->include_prefix != 0)
5565 fname = xmalloc (strlen (opts->include_prefix)
5566 + strlen (argv[i]) + 1);
5567 strcpy (fname, opts->include_prefix);
5568 strcat (fname, argv[i]);
5572 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5573 + strlen (argv[i]) + 1);
5574 strcpy (fname, GCC_INCLUDE_DIR);
5575 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5576 if (!strcmp (fname + strlen (fname) - 8, "/include"))
5577 fname[strlen (fname) - 7] = 0;
5578 strcat (fname, argv[i]);
5581 append_include_chain (pfile, &opts->bracket_include, fname, 0);
5583 /* Add directory to end of path for includes. */
5584 else if (!strcmp (argv[i], "-idirafter"))
5587 goto missing_dirname;
5588 append_include_chain (pfile, &opts->after_include, argv[++i], 0);
5590 else if (!strcmp (argv[i], "-include") || !strcmp (argv[i], "-imacros"))
5593 goto missing_filename;
5595 push_pending (pfile, argv[i], argv[i+1]), i++;
5597 else if (!strcmp (argv[i], "-iprefix"))
5600 goto missing_filename;
5602 opts->include_prefix = argv[++i];
5604 else if (!strcmp (argv[i], "-ifoutput"))
5605 opts->output_conditionals = 1;
5610 if (opts->out_fname != NULL)
5612 cpp_fatal (pfile, "Output filename specified twice");
5616 goto missing_filename;
5617 opts->out_fname = argv[++i];
5618 if (!strcmp (opts->out_fname, "-"))
5619 opts->out_fname = "";
5623 if (!strcmp (argv[i], "-pedantic"))
5624 CPP_PEDANTIC (pfile) = 1;
5625 else if (!strcmp (argv[i], "-pedantic-errors")) {
5626 CPP_PEDANTIC (pfile) = 1;
5627 opts->pedantic_errors = 1;
5630 else if (!strcmp (argv[i], "-pcp")) {
5631 char *pcp_fname = argv[++i];
5632 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
5633 ? fopen (pcp_fname, "w")
5634 : fdopen (dup (fileno (stdout)), "w"));
5635 if (pcp_outfile == 0)
5636 cpp_pfatal_with_name (pfile, pcp_fname);
5643 if (!strcmp (argv[i], "-traditional")) {
5644 opts->traditional = 1;
5645 opts->cplusplus_comments = 0;
5646 } else if (!strcmp (argv[i], "-trigraphs")) {
5648 opts->no_trigraphs = 0;
5653 if (! strcmp (argv[i], "-lang-c"))
5654 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5656 if (! strcmp (argv[i], "-lang-c89"))
5657 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
5659 if (! strcmp (argv[i], "-lang-c++"))
5660 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5662 if (! strcmp (argv[i], "-lang-objc"))
5663 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5665 if (! strcmp (argv[i], "-lang-objc++"))
5666 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5668 if (! strcmp (argv[i], "-lang-asm"))
5670 if (! strcmp (argv[i], "-lint"))
5672 if (! strcmp (argv[i], "-lang-chill"))
5673 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
5674 opts->traditional = 1, opts->no_trigraphs = 1;
5678 opts->cplusplus = 1, opts->cplusplus_comments = 1;
5682 opts->inhibit_warnings = 1;
5686 if (!strcmp (argv[i], "-Wtrigraphs"))
5687 opts->warn_trigraphs = 1;
5688 else if (!strcmp (argv[i], "-Wno-trigraphs"))
5689 opts->warn_trigraphs = 0;
5690 else if (!strcmp (argv[i], "-Wcomment"))
5691 opts->warn_comments = 1;
5692 else if (!strcmp (argv[i], "-Wno-comment"))
5693 opts->warn_comments = 0;
5694 else if (!strcmp (argv[i], "-Wcomments"))
5695 opts->warn_comments = 1;
5696 else if (!strcmp (argv[i], "-Wno-comments"))
5697 opts->warn_comments = 0;
5698 else if (!strcmp (argv[i], "-Wtraditional"))
5699 opts->warn_stringify = 1;
5700 else if (!strcmp (argv[i], "-Wno-traditional"))
5701 opts->warn_stringify = 0;
5702 else if (!strcmp (argv[i], "-Wundef"))
5703 opts->warn_undef = 1;
5704 else if (!strcmp (argv[i], "-Wno-undef"))
5705 opts->warn_undef = 0;
5706 else if (!strcmp (argv[i], "-Wimport"))
5707 opts->warn_import = 1;
5708 else if (!strcmp (argv[i], "-Wno-import"))
5709 opts->warn_import = 0;
5710 else if (!strcmp (argv[i], "-Werror"))
5711 opts->warnings_are_errors = 1;
5712 else if (!strcmp (argv[i], "-Wno-error"))
5713 opts->warnings_are_errors = 0;
5714 else if (!strcmp (argv[i], "-Wall"))
5716 opts->warn_trigraphs = 1;
5717 opts->warn_comments = 1;
5722 /* The style of the choices here is a bit mixed.
5723 The chosen scheme is a hybrid of keeping all options in one string
5724 and specifying each option in a separate argument:
5725 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
5726 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5727 -M[M][G][D file]. This is awkward to handle in specs, and is not
5729 /* ??? -MG must be specified in addition to one of -M or -MM.
5730 This can be relaxed in the future without breaking anything.
5731 The converse isn't true. */
5733 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
5734 if (!strcmp (argv[i], "-MG"))
5736 opts->print_deps_missing_files = 1;
5739 if (!strcmp (argv[i], "-M"))
5740 opts->print_deps = 2;
5741 else if (!strcmp (argv[i], "-MM"))
5742 opts->print_deps = 1;
5743 else if (!strcmp (argv[i], "-MD"))
5744 opts->print_deps = 2;
5745 else if (!strcmp (argv[i], "-MMD"))
5746 opts->print_deps = 1;
5747 /* For -MD and -MMD options, write deps on file named by next arg. */
5748 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
5751 goto missing_filename;
5752 opts->deps_file = argv[++i];
5756 /* For -M and -MM, write deps on standard output
5757 and suppress the usual output. */
5758 opts->no_output = 1;
5764 char *p = argv[i] + 2;
5766 while ((c = *p++) != 0) {
5767 /* Arg to -d specifies what parts of macros to dump */
5770 opts->dump_macros = dump_only;
5771 opts->no_output = 1;
5774 opts->dump_macros = dump_names;
5777 opts->dump_macros = dump_definitions;
5780 opts->dump_includes = 1;
5788 if (argv[i][2] == '3')
5789 opts->debug_output = 1;
5793 if (strcmp (argv[i], "--help") != 0)
5799 fprintf (stderr, "GNU CPP version %s", version_string);
5800 #ifdef TARGET_VERSION
5803 fprintf (stderr, "\n");
5808 opts->print_include_names = 1;
5812 if (argv[i][2] != 0)
5813 push_pending (pfile, "-D", argv[i] + 2);
5814 else if (i + 1 == argc)
5816 cpp_fatal (pfile, "Macro name missing after -D option");
5820 i++, push_pending (pfile, "-D", argv[i]);
5827 if (argv[i][2] != 0)
5829 else if (i + 1 == argc)
5831 cpp_fatal (pfile, "Assertion missing after -A option");
5837 if (!strcmp (p, "-")) {
5838 struct cpp_pending **ptr;
5839 /* -A- eliminates all predefined macros and assertions.
5840 Let's include also any that were specified earlier
5841 on the command line. That way we can get rid of any
5842 that were passed automatically in from GCC. */
5843 opts->inhibit_predefs = 1;
5844 for (ptr = &opts->pending; *ptr != NULL; )
5846 struct cpp_pending *pend = *ptr;
5847 if (pend->cmd && pend->cmd[0] == '-'
5848 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
5857 push_pending (pfile, "-A", p);
5862 case 'U': /* JF #undef something */
5863 if (argv[i][2] != 0)
5864 push_pending (pfile, "-U", argv[i] + 2);
5865 else if (i + 1 == argc)
5867 cpp_fatal (pfile, "Macro name missing after -U option");
5871 push_pending (pfile, "-U", argv[i+1]), i++;
5875 opts->put_out_comments = 1;
5878 case 'E': /* -E comes from cc -E; ignore it. */
5882 opts->no_line_commands = 1;
5885 case '$': /* Don't include $ in identifiers. */
5886 opts->dollars_in_ident = 0;
5890 if (!strcmp (argv[i], "-nostdinc"))
5891 /* -nostdinc causes no default include directories.
5892 You must specify all include-file directories with -I. */
5893 opts->no_standard_includes = 1;
5894 else if (!strcmp (argv[i], "-nostdinc++"))
5895 /* -nostdinc++ causes no default C++-specific include directories. */
5896 opts->no_standard_cplusplus_includes = 1;
5898 else if (!strcmp (argv[i], "-noprecomp"))
5904 if (!strcmp (argv[i], "-remap"))
5909 /* Sun compiler passes undocumented switch "-undef".
5910 Let's assume it means to inhibit the predefined symbols. */
5911 opts->inhibit_predefs = 1;
5914 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
5915 if (opts->in_fname == NULL) {
5916 opts->in_fname = "";
5918 } else if (opts->out_fname == NULL) {
5919 opts->out_fname = "";
5921 } /* else fall through into error */
5931 /* Handle command-line options in (argc, argv).
5932 Can be called multiple times, to handle multiple sets of options.
5933 Returns if an unrecognized option is seen.
5934 Returns number of strings consumed. */
5937 cpp_handle_options (pfile, argc, argv)
5943 int strings_processed;
5944 for (i = 0; i < argc; i += strings_processed)
5946 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
5947 if (strings_processed == 0)
5957 struct cpp_options *opts = CPP_OPTIONS (pfile);
5959 if (opts->print_deps)
5961 /* Stream on which to print the dependency information. */
5964 /* Don't actually write the deps file if compilation has failed. */
5965 if (pfile->errors == 0)
5967 char *deps_mode = opts->print_deps_append ? "a" : "w";
5968 if (opts->deps_file == 0)
5969 deps_stream = stdout;
5970 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
5971 cpp_pfatal_with_name (pfile, opts->deps_file);
5972 fputs (pfile->deps_buffer, deps_stream);
5973 putc ('\n', deps_stream);
5974 if (opts->deps_file)
5976 if (ferror (deps_stream) || fclose (deps_stream) != 0)
5977 cpp_fatal (pfile, "I/O error on output");
5983 /* Debugging: dump statistics on the include hash table. */
5985 struct include_hash *x;
5988 for(i = 0; i < ALL_INCLUDE_HASHSIZE; i++)
5990 x = pfile->all_include_files[i];
5997 fprintf(stderr, "%d/%d ", i, j);
5999 fputc('\n', stderr);
6005 /* Free resources used by PFILE.
6006 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6013 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6014 cpp_pop_buffer (pfile);
6016 if (pfile->token_buffer)
6018 free (pfile->token_buffer);
6019 pfile->token_buffer = NULL;
6022 if (pfile->deps_buffer)
6024 free (pfile->deps_buffer);
6025 pfile->deps_buffer = NULL;
6026 pfile->deps_allocated_size = 0;
6029 while (pfile->if_stack)
6031 IF_STACK_FRAME *temp = pfile->if_stack;
6032 pfile->if_stack = temp->next;
6036 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
6038 struct include_hash *imp = pfile->all_include_files[i];
6041 struct include_hash *next = imp->next;
6043 /* This gets freed elsewhere - I think. */
6049 pfile->all_include_files[i] = 0;
6052 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6054 while (pfile->assertion_hashtab[i])
6055 delete_assertion (pfile->assertion_hashtab[i]);
6058 cpp_hash_cleanup (pfile);
6062 do_assert (pfile, keyword, buf, limit)
6064 struct directive *keyword ATTRIBUTE_UNUSED;
6065 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
6067 long symstart; /* remember where symbol name starts */
6069 int sym_length; /* and how long it is */
6070 struct arglist *tokens = NULL;
6072 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6073 && !CPP_BUFFER (pfile)->system_header_p)
6074 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6076 cpp_skip_hspace (pfile);
6077 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6078 parse_name (pfile, GETC());
6079 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6082 cpp_skip_hspace (pfile);
6083 if (PEEKC() != '(') {
6084 cpp_error (pfile, "missing token-sequence in `#assert'");
6090 tokens = read_token_list (pfile, &error_flag);
6094 cpp_error (pfile, "empty token-sequence in `#assert'");
6097 cpp_skip_hspace (pfile);
6099 if (c != EOF && c != '\n')
6100 cpp_pedwarn (pfile, "junk at end of `#assert'");
6101 skip_rest_of_line (pfile);
6104 /* If this name isn't already an assertion name, make it one.
6105 Error if it was already in use in some other way. */
6108 ASSERTION_HASHNODE *hp;
6109 U_CHAR *symname = pfile->token_buffer + symstart;
6110 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6111 struct tokenlist_list *value
6112 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6114 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6116 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6117 cpp_error (pfile, "`defined' redefined as assertion");
6118 hp = assertion_install (pfile, symname, sym_length, hashcode);
6121 /* Add the spec'd token-sequence to the list of such. */
6122 value->tokens = tokens;
6123 value->next = hp->value;
6126 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6129 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6130 skip_rest_of_line (pfile);
6135 do_unassert (pfile, keyword, buf, limit)
6137 struct directive *keyword ATTRIBUTE_UNUSED;
6138 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
6140 long symstart; /* remember where symbol name starts */
6141 int sym_length; /* and how long it is */
6144 struct arglist *tokens = NULL;
6145 int tokens_specified = 0;
6147 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6148 && !CPP_BUFFER (pfile)->system_header_p)
6149 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6151 cpp_skip_hspace (pfile);
6153 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6154 parse_name (pfile, GETC());
6155 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6158 cpp_skip_hspace (pfile);
6159 if (PEEKC() == '(') {
6162 tokens = read_token_list (pfile, &error_flag);
6166 cpp_error (pfile, "empty token list in `#unassert'");
6170 tokens_specified = 1;
6173 cpp_skip_hspace (pfile);
6175 if (c != EOF && c != '\n')
6176 cpp_error (pfile, "junk at end of `#unassert'");
6177 skip_rest_of_line (pfile);
6180 ASSERTION_HASHNODE *hp;
6181 U_CHAR *symname = pfile->token_buffer + symstart;
6182 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6183 struct tokenlist_list *tail, *prev;
6185 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6189 /* If no token list was specified, then eliminate this assertion
6191 if (! tokens_specified)
6192 delete_assertion (hp);
6194 /* If a list of tokens was given, then delete any matching list. */
6199 struct tokenlist_list *next = tail->next;
6200 if (compare_token_lists (tail->tokens, tokens)) {
6204 hp->value = tail->next;
6205 free_token_list (tail->tokens);
6215 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6218 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6219 skip_rest_of_line (pfile);
6223 /* Test whether there is an assertion named NAME
6224 and optionally whether it has an asserted token list TOKENS.
6225 NAME is not null terminated; its length is SYM_LENGTH.
6226 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6229 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
6233 int tokens_specified;
6234 struct arglist *tokens;
6236 ASSERTION_HASHNODE *hp;
6237 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6239 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6240 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
6242 hp = assertion_lookup (pfile, name, sym_length, hashcode);
6244 /* It is not an assertion; just return false. */
6247 /* If no token list was specified, then value is 1. */
6248 if (! tokens_specified)
6252 struct tokenlist_list *tail;
6256 /* If a list of tokens was given,
6257 then succeed if the assertion records a matching list. */
6260 if (compare_token_lists (tail->tokens, tokens))
6265 /* Fail if the assertion has no matching list. */
6270 /* Compare two lists of tokens for equality including order of tokens. */
6273 compare_token_lists (l1, l2)
6274 struct arglist *l1, *l2;
6277 if (l1->length != l2->length)
6279 if (strncmp (l1->name, l2->name, l1->length))
6285 /* Succeed if both lists end at the same time. */
6290 reverse_token_list (tokens)
6291 struct arglist *tokens;
6293 register struct arglist *prev = 0, *this, *next;
6294 for (this = tokens; this; this = next)
6303 /* Read a space-separated list of tokens ending in a close parenthesis.
6304 Return a list of strings, in the order they were written.
6305 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
6307 static struct arglist *
6308 read_token_list (pfile, error_flag)
6312 struct arglist *token_ptrs = 0;
6317 FORWARD (1); /* Skip '(' */
6319 /* Loop over the assertion value tokens. */
6322 struct arglist *temp;
6323 long name_written = CPP_WRITTEN (pfile);
6326 cpp_skip_hspace (pfile);
6330 /* Find the end of the token. */
6333 CPP_PUTC (pfile, c);
6341 CPP_PUTC (pfile, c);
6343 else if (c == '"' || c == '\'')
6346 cpp_get_token (pfile);
6352 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
6353 && c != '"' && c != '\'')
6355 CPP_PUTC (pfile, c);
6358 if (c != EOF) FORWARD(-1);
6361 length = CPP_WRITTEN (pfile) - name_written;
6362 temp = (struct arglist *)
6363 xmalloc (sizeof (struct arglist) + length + 1);
6364 temp->name = (U_CHAR *) (temp + 1);
6365 bcopy ((char *) (pfile->token_buffer + name_written),
6366 (char *) temp->name, length);
6367 temp->name[length] = 0;
6368 temp->next = token_ptrs;
6370 temp->length = length;
6372 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
6374 if (c == EOF || c == '\n')
6377 "unterminated token sequence following `#' operator");
6382 /* We accumulated the names in reverse order.
6383 Now reverse them to get the proper order. */
6384 return reverse_token_list (token_ptrs);
6388 free_token_list (tokens)
6389 struct arglist *tokens;
6392 struct arglist *next = tokens->next;
6393 free (tokens->name);
6399 /* FIXME: savestring() should be renamed strdup() and should
6400 be moved into cppalloc.c. We can't do that right now because
6401 then we'd get multiple-symbol clashes with toplev.c and several
6407 unsigned size = strlen (input);
6408 char *output = xmalloc (size + 1);
6409 strcpy (output, input);
6413 /* Initialize PMARK to remember the current position of PFILE. */
6416 parse_set_mark (pmark, pfile)
6417 struct parse_marker *pmark;
6420 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6421 pmark->next = pbuf->marks;
6422 pbuf->marks = pmark;
6424 pmark->position = pbuf->cur - pbuf->buf;
6427 /* Cleanup PMARK - we no longer need it. */
6430 parse_clear_mark (pmark)
6431 struct parse_marker *pmark;
6433 struct parse_marker **pp = &pmark->buf->marks;
6434 for (; ; pp = &(*pp)->next) {
6435 if (*pp == NULL) abort ();
6436 if (*pp == pmark) break;
6441 /* Backup the current position of PFILE to that saved in PMARK. */
6444 parse_goto_mark (pmark, pfile)
6445 struct parse_marker *pmark;
6448 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6449 if (pbuf != pmark->buf)
6450 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
6451 pbuf->cur = pbuf->buf + pmark->position;
6454 /* Reset PMARK to point to the current position of PFILE. (Same
6455 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
6458 parse_move_mark (pmark, pfile)
6459 struct parse_marker *pmark;
6462 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6463 if (pbuf != pmark->buf)
6464 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
6465 pmark->position = pbuf->cur - pbuf->buf;
6469 cpp_read_check_assertion (pfile)
6472 int name_start = CPP_WRITTEN (pfile);
6473 int name_length, name_written;
6475 FORWARD (1); /* Skip '#' */
6476 cpp_skip_hspace (pfile);
6477 parse_name (pfile, GETC ());
6478 name_written = CPP_WRITTEN (pfile);
6479 name_length = name_written - name_start;
6480 cpp_skip_hspace (pfile);
6481 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
6484 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
6485 result = check_assertion (pfile,
6486 pfile->token_buffer + name_start, name_length,
6490 result = check_assertion (pfile,
6491 pfile->token_buffer + name_start, name_length,
6493 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
6498 cpp_print_file_and_line (pfile)
6501 cpp_buffer *ip = cpp_file_buffer (pfile);
6506 cpp_buf_line_and_col (ip, &line, &col);
6507 cpp_file_line_for_message (pfile, ip->nominal_fname,
6508 line, pfile->show_column ? col : -1);
6513 v_cpp_error (pfile, msg, ap)
6518 cpp_print_containing_files (pfile);
6519 cpp_print_file_and_line (pfile);
6520 v_cpp_message (pfile, 1, msg, ap);
6524 cpp_error VPROTO ((cpp_reader * pfile, const char *msg, ...))
6526 #ifndef ANSI_PROTOTYPES
6534 #ifndef ANSI_PROTOTYPES
6535 pfile = va_arg (ap, cpp_reader *);
6536 msg = va_arg (ap, const char *);
6539 v_cpp_error (pfile, msg, ap);
6543 /* Print error message but don't count it. */
6546 v_cpp_warning (pfile, msg, ap)
6551 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6554 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6557 cpp_print_containing_files (pfile);
6558 cpp_print_file_and_line (pfile);
6559 v_cpp_message (pfile, 0, msg, ap);
6563 cpp_warning VPROTO ((cpp_reader * pfile, const char *msg, ...))
6565 #ifndef ANSI_PROTOTYPES
6573 #ifndef ANSI_PROTOTYPES
6574 pfile = va_arg (ap, cpp_reader *);
6575 msg = va_arg (ap, const char *);
6578 v_cpp_warning (pfile, msg, ap);
6582 /* Print an error message and maybe count it. */
6585 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msg, ...))
6587 #ifndef ANSI_PROTOTYPES
6595 #ifndef ANSI_PROTOTYPES
6596 pfile = va_arg (ap, cpp_reader *);
6597 msg = va_arg (ap, const char *);
6600 if (CPP_OPTIONS (pfile)->pedantic_errors)
6601 v_cpp_error (pfile, msg, ap);
6603 v_cpp_warning (pfile, msg, ap);
6608 v_cpp_error_with_line (pfile, line, column, msg, ap)
6615 cpp_buffer *ip = cpp_file_buffer (pfile);
6617 cpp_print_containing_files (pfile);
6620 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6622 v_cpp_message (pfile, 1, msg, ap);
6626 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6628 #ifndef ANSI_PROTOTYPES
6638 #ifndef ANSI_PROTOTYPES
6639 pfile = va_arg (ap, cpp_reader *);
6640 line = va_arg (ap, int);
6641 column = va_arg (ap, int);
6642 msg = va_arg (ap, const char *);
6645 v_cpp_error_with_line(pfile, line, column, msg, ap);
6650 v_cpp_warning_with_line (pfile, line, column, msg, ap)
6659 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6662 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6665 cpp_print_containing_files (pfile);
6667 ip = cpp_file_buffer (pfile);
6670 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6672 v_cpp_message (pfile, 0, msg, ap);
6677 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6679 #ifndef ANSI_PROTOTYPES
6689 #ifndef ANSI_PROTOTYPES
6690 pfile = va_arg (ap, cpp_reader *);
6691 line = va_arg (ap, int);
6692 column = va_arg (ap, int);
6693 msg = va_arg (ap, const char *);
6696 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6702 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6704 #ifndef ANSI_PROTOTYPES
6714 #ifndef ANSI_PROTOTYPES
6715 pfile = va_arg (ap, cpp_reader *);
6716 line = va_arg (ap, int);
6717 column = va_arg (ap, int);
6718 msg = va_arg (ap, const char *);
6721 if (CPP_OPTIONS (pfile)->pedantic_errors)
6722 v_cpp_error_with_line (pfile, column, line, msg, ap);
6724 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6728 /* Report a warning (or an error if pedantic_errors)
6729 giving specified file name and line number, not current. */
6732 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line, const char *msg, ...))
6734 #ifndef ANSI_PROTOTYPES
6744 #ifndef ANSI_PROTOTYPES
6745 pfile = va_arg (ap, cpp_reader *);
6746 file = va_arg (ap, char *);
6747 line = va_arg (ap, int);
6748 msg = va_arg (ap, const char *);
6751 if (!CPP_OPTIONS (pfile)->pedantic_errors
6752 && CPP_OPTIONS (pfile)->inhibit_warnings)
6755 cpp_file_line_for_message (pfile, file, line, -1);
6756 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msg, ap);
6760 /* my_strerror - return the descriptive text associated with an
6764 my_strerror (errnum)
6770 #ifndef HAVE_STRERROR
6771 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
6773 result = strerror (errnum);
6776 /* VAXCRTL's strerror() takes an optional second argument, which only
6777 matters when the first argument is EVMSERR. However, it's simplest
6778 just to pass it unconditionally. `vaxc$errno' is declared in
6779 <errno.h>, and maintained by the library in parallel with `errno'.
6780 We assume that caller's `errnum' either matches the last setting of
6781 `errno' by the library or else does not have the value `EVMSERR'. */
6783 result = strerror (errnum, vaxc$errno);
6787 result = "undocumented I/O error";
6792 /* Error including a message from `errno'. */
6795 cpp_error_from_errno (pfile, name)
6799 cpp_message_from_errno (pfile, 1, name);
6803 cpp_message_from_errno (pfile, is_error, name)
6809 cpp_buffer *ip = cpp_file_buffer (pfile);
6811 cpp_print_containing_files (pfile);
6814 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
6816 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
6820 cpp_perror_with_name (pfile, name)
6824 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
6828 * No pre-compiled header file support.
6830 * Possibly different enum token codes for each C/C++ token.
6832 * Should clean up remaining directives to that do_XXX functions
6833 * only take two arguments and all have command_reads_line.
6835 * Find and cleanup remaining uses of static variables,
6837 * Support for trigraphs.
6839 * Support -dM flag (dump_all_macros).
6841 * Support for_lint flag.