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>
40 #include "gansidecl.h"
42 #ifndef GET_ENVIRONMENT
43 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ENV_VALUE = getenv (ENV_NAME)
46 extern char *update_path ();
50 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
51 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
53 /* Find the largest host integer type and set its size and type.
54 Watch out: on some crazy hosts `long' is shorter than `int'. */
58 # include <inttypes.h>
59 # define HOST_WIDE_INT intmax_t
61 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
62 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
63 # define HOST_WIDE_INT int
65 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
66 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
67 # define HOST_WIDE_INT long
69 # define HOST_WIDE_INT long long
76 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
80 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
83 /* By default, colon separates directories in a path. */
84 #ifndef PATH_SEPARATOR
85 #define PATH_SEPARATOR ':'
88 #ifndef STANDARD_INCLUDE_DIR
89 #define STANDARD_INCLUDE_DIR "/usr/include"
91 #ifndef INCLUDE_LEN_FUDGE
92 #define INCLUDE_LEN_FUDGE 0
95 /* Symbols to predefine. */
98 static char *predefs
= CPP_PREDEFINES
;
100 static char *predefs
= "";
103 /* We let tm.h override the types used here, to handle trivial differences
104 such as the choice of unsigned int or long unsigned int for size_t.
105 When machines start needing nontrivial differences in the size type,
106 it would be best to do something here to figure out automatically
107 from other information what type to use. */
109 /* The string value for __SIZE_TYPE__. */
112 #define SIZE_TYPE "long unsigned int"
115 /* The string value for __PTRDIFF_TYPE__. */
118 #define PTRDIFF_TYPE "long int"
121 /* The string value for __WCHAR_TYPE__. */
124 #define WCHAR_TYPE "int"
126 #define CPP_WCHAR_TYPE(PFILE) \
127 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
129 /* The string value for __USER_LABEL_PREFIX__ */
131 #ifndef USER_LABEL_PREFIX
132 #define USER_LABEL_PREFIX ""
135 /* The string value for __REGISTER_PREFIX__ */
137 #ifndef REGISTER_PREFIX
138 #define REGISTER_PREFIX ""
141 /* In the definition of a #assert name, this structure forms
142 a list of the individual values asserted.
143 Each value is itself a list of "tokens".
144 These are strings that are compared by name. */
146 struct tokenlist_list
{
147 struct tokenlist_list
*next
;
148 struct arglist
*tokens
;
151 struct assertion_hashnode
{
152 struct assertion_hashnode
*next
; /* double links for easy deletion */
153 struct assertion_hashnode
*prev
;
154 /* also, a back pointer to this node's hash
155 chain is kept, in case the node is the head
156 of the chain and gets deleted. */
157 struct assertion_hashnode
**bucket_hdr
;
158 int length
; /* length of token, for quick comparison */
159 U_CHAR
*name
; /* the actual name */
160 /* List of token-sequences. */
161 struct tokenlist_list
*value
;
164 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
165 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
167 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
168 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
169 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
170 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
171 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
172 (Note that it is false while we're expanding marco *arguments*.) */
173 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
175 /* Move all backslash-newline pairs out of embarrassing places.
176 Exchange all such pairs following BP
177 with any potentially-embarrassing characters that follow them.
178 Potentially-embarrassing characters are / and *
179 (because a backslash-newline inside a comment delimiter
180 would cause it not to be recognized). */
182 #define NEWLINE_FIX \
183 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
185 /* Same, but assume we've already read the potential '\\' into C. */
186 #define NEWLINE_FIX1(C) do { \
187 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
191 struct cpp_pending
*next
;
196 /* Forward declarations. */
200 void cpp_file_line_for_message
PARAMS ((char *, int, int));
201 void cpp_hash_cleanup
PARAMS ((cpp_reader
*));
203 void cpp_print_containing_files
PARAMS ((cpp_reader
*));
205 static void add_import ();
206 static void append_include_chain ();
207 static void make_assertion ();
208 static void path_include ();
209 static void initialize_builtins ();
210 static void initialize_char_syntax ();
211 extern void delete_macro ();
213 static void trigraph_pcp ();
215 static int finclude ();
216 static void validate_else ();
217 static int comp_def_part ();
219 extern void fancy_abort ();
221 static int lookup_import ();
222 static int redundant_include_p ();
223 static int is_system_include ();
224 static struct file_name_map
*read_name_map ();
225 static char *read_filename_string ();
226 static int open_include_file ();
227 static int check_macro_name ();
228 static int compare_defs ();
229 static int compare_token_lists ();
230 static HOST_WIDE_INT
eval_if_expression ();
231 static int change_newlines ();
233 static struct arglist
*read_token_list ();
234 static void free_token_list ();
235 static int safe_read ();
236 static void push_macro_expansion
PARAMS ((cpp_reader
*,
237 U_CHAR
*, int, HASHNODE
*));
238 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
239 extern char *xrealloc ();
240 static char *xcalloc ();
241 static char *savestring ();
243 static void conditional_skip ();
244 static void skip_if_group ();
245 static int parse_name
PARAMS ((cpp_reader
*, int));
247 /* Last arg to output_line_command. */
248 enum file_change_code
{same_file
, enter_file
, leave_file
};
250 /* External declarations. */
252 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
254 extern FILE *fdopen ();
255 extern char *version_string
;
256 extern struct tm
*localtime ();
258 /* These functions are declared to return int instead of void since they
259 are going to be placed in a table and some old compilers have trouble with
260 pointers to functions returning void. */
262 static int do_define ();
263 static int do_line ();
264 static int do_include ();
265 static int do_undef ();
266 static int do_error ();
267 static int do_pragma ();
268 static int do_ident ();
270 static int do_xifdef ();
271 static int do_else ();
272 static int do_elif ();
273 static int do_endif ();
274 #ifdef SCCS_DIRECTIVE
275 static int do_sccs ();
277 static int do_once ();
278 static int do_assert ();
279 static int do_unassert ();
280 static int do_warning ();
282 struct file_name_list
284 struct file_name_list
*next
;
286 /* If the following is nonzero, it is a macro name.
287 Don't include the file again if that macro is defined. */
288 U_CHAR
*control_macro
;
289 /* If the following is nonzero, it is a C-language system include
291 int c_system_include_path
;
292 /* Mapping of file names for this directory. */
293 struct file_name_map
*name_map
;
294 /* Non-zero if name_map is valid. */
298 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
299 via the same directory as the file that #included it. */
300 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
302 /* #include "file" looks in source file dir, then stack. */
303 /* #include <file> just looks in the stack. */
304 /* -I directories are added to the end, then the defaults are added. */
306 static struct default_include
{
307 char *fname
; /* The name of the directory. */
308 char *component
; /* The component containing the directory */
309 int cplusplus
; /* Only look here if we're compiling C++. */
310 int cxx_aware
; /* Includes in this directory don't need to
311 be wrapped in extern "C" when compiling
313 } include_defaults_array
[]
314 #ifdef INCLUDE_DEFAULTS
318 /* Pick up GNU C++ specific include files. */
319 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
320 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 1 },
322 /* This is the dir for fixincludes. Put it just before
323 the files that we fix. */
324 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
325 /* For cross-compilation, this dir name is generated
326 automatically in Makefile.in. */
327 { CROSS_INCLUDE_DIR
, "GCC",0, 0 },
328 #ifdef TOOL_INCLUDE_DIR
329 /* This is another place that the target system's headers might be. */
330 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
332 #else /* not CROSS_COMPILE */
333 #ifdef LOCAL_INCLUDE_DIR
334 /* This should be /usr/local/include and should come before
335 the fixincludes-fixed header files. */
336 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
338 #ifdef TOOL_INCLUDE_DIR
339 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
340 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
341 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
343 /* This is the dir for fixincludes. Put it just before
344 the files that we fix. */
345 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
346 /* Some systems have an extra dir of include files. */
347 #ifdef SYSTEM_INCLUDE_DIR
348 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
350 #ifndef STANDARD_INCLUDE_COMPONENT
351 #define STANDARD_INCLUDE_COMPONENT 0
353 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
354 #endif /* not CROSS_COMPILE */
357 #endif /* no INCLUDE_DEFAULTS */
359 /* `struct directive' defines one #-directive, including how to handle it. */
362 int length
; /* Length of name */
363 int (*func
)(); /* Function to handle directive */
364 char *name
; /* Name of directive */
365 enum node_type type
; /* Code which describes which directive. */
366 char command_reads_line
; /* One if rest of line is read by func. */
369 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
370 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
372 /* Here is the actual list of #-directives, most-often-used first.
373 The initialize_builtins function assumes #define is the very first. */
375 static struct directive directive_table
[] = {
376 { 6, do_define
, "define", T_DEFINE
},
377 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
378 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
379 { 7, do_include
, "include", T_INCLUDE
, 1},
380 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
381 { 6, do_include
, "import", T_IMPORT
, 1},
382 { 5, do_endif
, "endif", T_ENDIF
, 1},
383 { 4, do_else
, "else", T_ELSE
, 1},
384 { 2, do_if
, "if", T_IF
, 1},
385 { 4, do_elif
, "elif", T_ELIF
, 1},
386 { 5, do_undef
, "undef", T_UNDEF
},
387 { 5, do_error
, "error", T_ERROR
},
388 { 7, do_warning
, "warning", T_WARNING
},
389 { 6, do_pragma
, "pragma", T_PRAGMA
},
390 { 4, do_line
, "line", T_LINE
, 1},
391 { 5, do_ident
, "ident", T_IDENT
, 1},
392 #ifdef SCCS_DIRECTIVE
393 { 4, do_sccs
, "sccs", T_SCCS
},
395 { 6, do_assert
, "assert", T_ASSERT
, 1},
396 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
397 { -1, 0, "", T_UNUSED
},
400 /* table to tell if char can be part of a C identifier. */
401 U_CHAR is_idchar
[256];
402 /* table to tell if char can be first char of a c identifier. */
403 U_CHAR is_idstart
[256];
404 /* table to tell if c is horizontal space. */
405 U_CHAR is_hor_space
[256];
406 /* table to tell if c is horizontal or vertical space. */
407 static U_CHAR is_space
[256];
409 /* Initialize syntactic classifications of characters. */
412 initialize_char_syntax (opts
)
413 struct cpp_options
*opts
;
418 * Set up is_idchar and is_idstart tables. These should be
419 * faster than saying (is_alpha (c) || c == '_'), etc.
420 * Set up these things before calling any routines tthat
423 for (i
= 'a'; i
<= 'z'; i
++) {
424 is_idchar
[i
- 'a' + 'A'] = 1;
426 is_idstart
[i
- 'a' + 'A'] = 1;
429 for (i
= '0'; i
<= '9'; i
++)
433 is_idchar
['$'] = opts
->dollars_in_ident
;
434 is_idstart
['$'] = opts
->dollars_in_ident
;
436 /* horizontal space table */
437 is_hor_space
[' '] = 1;
438 is_hor_space
['\t'] = 1;
439 is_hor_space
['\v'] = 1;
440 is_hor_space
['\f'] = 1;
441 is_hor_space
['\r'] = 1;
452 /* Place into PFILE a quoted string representing the string SRC.
453 Caller must reserve enough space in pfile->token_buffer. */
456 quote_string (pfile
, src
)
462 CPP_PUTC_Q (pfile
, '\"');
464 switch ((c
= *src
++))
468 CPP_PUTC_Q (pfile
, c
);
471 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
472 CPP_ADJUST_WRITTEN (pfile
, 4);
478 CPP_PUTC_Q (pfile
, '\\');
479 CPP_PUTC_Q (pfile
, c
);
483 CPP_PUTC_Q (pfile
, '\"');
484 CPP_NUL_TERMINATE_Q (pfile
);
489 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
492 cpp_grow_buffer (pfile
, n
)
496 long old_written
= CPP_WRITTEN (pfile
);
497 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
498 pfile
->token_buffer
= (U_CHAR
*)
499 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
500 CPP_SET_WRITTEN (pfile
, old_written
);
505 * process a given definition string, for initialization
506 * If STR is just an identifier, define it with value 1.
507 * If STR has anything after the identifier, then it should
508 * be identifier=definition.
512 cpp_define (pfile
, str
)
522 cpp_error (pfile
, "malformed option `-D %s'", str
);
525 while (is_idchar
[*++p
])
529 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
530 strcpy ((char *)buf
, str
);
531 strcat ((char *)buf
, " 1");
535 cpp_error (pfile
, "malformed option `-D %s'", str
);
541 /* Copy the entire option so we can modify it. */
542 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
543 strncpy (buf
, str
, p
- str
);
544 /* Change the = to a space. */
546 /* Scan for any backslash-newline and remove it. */
551 if (*p
== '\\' && p
[1] == '\n')
559 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
562 /* Process the string STR as if it appeared as the body of a #assert.
563 OPTION is the option name for which STR was the argument. */
566 make_assertion (pfile
, option
, str
)
573 /* Copy the entire option so we can modify it. */
574 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
575 strcpy ((char *) buf
, str
);
576 /* Scan for any backslash-newline and remove it. */
580 if (*p
== '\\' && p
[1] == '\n')
589 if (!is_idstart
[*p
]) {
590 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
593 while (is_idchar
[*++p
])
595 while (*p
== ' ' || *p
== '\t') p
++;
596 if (! (*p
== 0 || *p
== '(')) {
597 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
601 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
603 do_assert (pfile
, NULL
, NULL
, NULL
);
604 cpp_pop_buffer (pfile
);
608 /* Append a chain of `struct file_name_list's
609 to the end of the main include chain.
610 FIRST is the beginning of the chain to append, and LAST is the end. */
613 append_include_chain (pfile
, first
, last
)
615 struct file_name_list
*first
, *last
;
617 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
618 struct file_name_list
*dir
;
623 if (opts
->include
== 0)
624 opts
->include
= first
;
626 opts
->last_include
->next
= first
;
628 if (opts
->first_bracket_include
== 0)
629 opts
->first_bracket_include
= first
;
631 for (dir
= first
; ; dir
= dir
->next
) {
632 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
633 if (len
> pfile
->max_include_len
)
634 pfile
->max_include_len
= len
;
640 opts
->last_include
= last
;
643 /* Add output to `deps_buffer' for the -M switch.
644 STRING points to the text to be output.
645 SPACER is ':' for targets, ' ' for dependencies, zero for text
646 to be inserted literally. */
649 deps_output (pfile
, string
, spacer
)
654 int size
= strlen (string
);
659 #ifndef MAX_OUTPUT_COLUMNS
660 #define MAX_OUTPUT_COLUMNS 72
663 && pfile
->deps_column
> 0
664 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
666 deps_output (pfile
, " \\\n ", 0);
667 pfile
->deps_column
= 0;
670 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
672 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
673 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
674 pfile
->deps_allocated_size
);
676 if (spacer
== ' ' && pfile
->deps_column
> 0)
677 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
678 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
679 pfile
->deps_size
+= size
;
680 pfile
->deps_column
+= size
;
682 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
683 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
686 /* Given a colon-separated list of file names PATH,
687 add all the names to the search path for include files. */
690 path_include (pfile
, path
)
702 struct file_name_list
*dirtmp
;
704 /* Find the end of this name. */
705 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
707 /* An empty name in the path stands for the current directory. */
708 name
= (char *) xmalloc (2);
712 /* Otherwise use the directory that is named. */
713 name
= (char *) xmalloc (q
- p
+ 1);
714 bcopy (p
, name
, q
- p
);
718 dirtmp
= (struct file_name_list
*)
719 xmalloc (sizeof (struct file_name_list
));
720 dirtmp
->next
= 0; /* New one goes on the end */
721 dirtmp
->control_macro
= 0;
722 dirtmp
->c_system_include_path
= 0;
723 dirtmp
->fname
= name
;
724 dirtmp
->got_name_map
= 0;
725 append_include_chain (pfile
, dirtmp
, dirtmp
);
727 /* Advance past this name. */
731 /* Skip the colon. */
737 cpp_options_init (opts
)
740 bzero ((char *) opts
, sizeof *opts
);
741 opts
->in_fname
= NULL
;
742 opts
->out_fname
= NULL
;
744 /* Initialize is_idchar to allow $. */
745 opts
->dollars_in_ident
= 1;
746 initialize_char_syntax (opts
);
748 opts
->no_line_commands
= 0;
749 opts
->no_trigraphs
= 1;
750 opts
->put_out_comments
= 0;
751 opts
->print_include_names
= 0;
752 opts
->dump_macros
= dump_none
;
756 opts
->cplusplus_comments
= 0;
763 opts
->pedantic_errors
= 0;
764 opts
->inhibit_warnings
= 0;
765 opts
->warn_comments
= 0;
766 opts
->warn_import
= 1;
767 opts
->warnings_are_errors
= 0;
771 null_underflow (pfile
)
772 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
778 null_cleanup (pbuf
, pfile
)
779 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
780 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
786 macro_cleanup (pbuf
, pfile
)
788 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
790 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
791 if (macro
->type
== T_DISABLED
)
792 macro
->type
= T_MACRO
;
793 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
799 file_cleanup (pbuf
, pfile
)
801 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
811 /* Assuming we have read '/'.
812 If this is the start of a comment (followed by '*' or '/'),
813 skip to the end of the comment, and return ' '.
814 Return EOF if we reached the end of file before the end of the comment.
815 If not the start of a comment, return '/'. */
818 skip_comment (pfile
, linep
)
823 while (PEEKC() == '\\' && PEEKN(1) == '\n')
838 while (c
== '\\' && PEEKC() == '\n')
842 FORWARD(1), c
= GETC();
844 if (prev_c
== '*' && c
== '/')
846 if (c
== '\n' && linep
)
850 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
857 return ' '; /* Allow // to be terminated by EOF. */
858 while (c
== '\\' && PEEKC() == '\n')
867 /* Don't consider final '\n' to be part of comment. */
877 /* Skip whitespace \-newline and comments. Does not macro-expand. */
880 cpp_skip_hspace (pfile
)
890 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
891 cpp_pedwarn (pfile
, "%s in preprocessing directive",
892 c
== '\f' ? "formfeed" : "vertical tab");
898 c
= skip_comment (pfile
, NULL
);
901 if (c
== EOF
|| c
== '/')
904 else if (c
== '\\' && PEEKN(1) == '\n') {
907 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
908 && is_hor_space
[PEEKN(1)])
914 /* Read the rest of the current line.
915 The line is appended to PFILE's output buffer. */
918 copy_rest_of_line (pfile
)
921 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
938 goto scan_directive_token
;
942 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '/'))
943 goto scan_directive_token
;
947 if (CPP_PEDANTIC (pfile
))
948 cpp_pedwarn (pfile
, "%s in preprocessing directive",
949 c
== '\f' ? "formfeed" : "vertical tab");
955 scan_directive_token
:
957 cpp_get_token (pfile
);
963 CPP_NUL_TERMINATE (pfile
);
967 skip_rest_of_line (pfile
)
970 long old
= CPP_WRITTEN (pfile
);
971 copy_rest_of_line (pfile
);
972 CPP_SET_WRITTEN (pfile
, old
);
975 /* Handle a possible # directive.
976 '#' has already been read. */
979 handle_directive (pfile
)
982 register struct directive
*kt
;
985 U_CHAR
*ident
, *line_end
;
986 long old_written
= CPP_WRITTEN (pfile
);
988 cpp_skip_hspace (pfile
);
991 if (c
>= '0' && c
<= '9')
993 /* Handle # followed by a line number. */
994 if (CPP_PEDANTIC (pfile
))
995 cpp_pedwarn (pfile
, "`#' followed by integer");
996 do_line (pfile
, NULL
);
997 goto done_a_directive
;
1000 /* Now find the directive name. */
1001 CPP_PUTC (pfile
, '#');
1002 parse_name (pfile
, GETC());
1003 ident
= pfile
->token_buffer
+ old_written
+ 1;
1004 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1005 if (ident_length
== 0 && PEEKC() == '\n')
1007 /* A line of just `#' becomes blank. */
1008 goto done_a_directive
;
1012 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1014 while (is_idchar
[*p
]) {
1015 if (*p
< '0' || *p
> '9')
1019 /* Avoid error for `###' and similar cases unless -pedantic. */
1021 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1023 if (pedantic
&& !lang_asm
)
1024 cpp_warning (pfile
, "invalid preprocessor directive");
1030 cpp_error (pfile
, "invalid preprocessor directive name");
1036 * Decode the keyword and call the appropriate expansion
1037 * routine, after moving the input pointer up to the next line.
1039 for (kt
= directive_table
; ; kt
++) {
1040 if (kt
->length
<= 0)
1041 goto not_a_directive
;
1042 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1046 if (kt
->command_reads_line
)
1050 /* Nonzero means do not delete comments within the directive.
1051 #define needs this when -traditional. */
1052 int comments
= CPP_TRADITIONAL (pfile
) && kt
->type
== T_DEFINE
;
1053 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1054 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1055 after_ident
= CPP_WRITTEN (pfile
);
1056 copy_rest_of_line (pfile
);
1057 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1060 /* We may want to pass through #define, #pragma, and #include.
1061 Other directives may create output, but we don't want the directive
1062 itself out, so we pop it now. For example conditionals may emit
1063 #failed ... #endfailed stuff. But note that popping the buffer
1064 means the parameters to kt->func may point after pfile->limit
1065 so these parameters are invalid as soon as something gets appended
1066 to the token_buffer. */
1068 line_end
= CPP_PWRITTEN (pfile
);
1069 if (! (kt
->type
== T_DEFINE
1070 || kt
->type
== T_PRAGMA
1071 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
1072 && CPP_OPTIONS (pfile
)->dump_includes
)))
1073 CPP_SET_WRITTEN (pfile
, old_written
);
1075 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1077 if (kt
->type
== T_DEFINE
)
1079 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1081 /* Skip "#define". */
1082 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
1084 SKIP_WHITE_SPACE (p
);
1085 while (is_idchar
[*p
]) p
++;
1087 CPP_PUTC (pfile
, '\n');
1089 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
1090 CPP_SET_WRITTEN (pfile
, old_written
);
1100 /* Pass a directive through to the output file.
1101 BUF points to the contents of the directive, as a contiguous string.
1102 LIMIT points to the first character past the end of the directive.
1103 KEYWORD is the keyword-table entry for the directive. */
1106 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1107 U_CHAR
*buf
, *limit
;
1109 struct directive
*keyword
;
1111 register unsigned keyword_length
= keyword
->length
;
1113 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1114 CPP_PUTC_Q (pfile
, '#');
1115 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1116 if (limit
!= buf
&& buf
[0] != ' ')
1117 CPP_PUTC_Q (pfile
, ' ');
1118 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1120 CPP_PUTS_Q (pfile
, '\n');
1121 /* Count the line we have just made in the output,
1122 to get in sync properly. */
1127 /* The arglist structure is built by do_define to tell
1128 collect_definition where the argument names begin. That
1129 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1130 would contain pointers to the strings x, y, and z.
1131 Collect_definition would then build a DEFINITION node,
1132 with reflist nodes pointing to the places x, y, and z had
1133 appeared. So the arglist is just convenience data passed
1134 between these two routines. It is not kept around after
1135 the current #define has been processed and entered into the
1139 struct arglist
*next
;
1146 /* Read a replacement list for a macro with parameters.
1147 Build the DEFINITION structure.
1148 Reads characters of text starting at BUF until END.
1149 ARGLIST specifies the formal parameters to look for
1150 in the text of the definition; NARGS is the number of args
1151 in that list, or -1 for a macro name that wants no argument list.
1152 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1153 and NAMELEN is its length in characters.
1155 Note that comments, backslash-newlines, and leading white space
1156 have already been deleted from the argument. */
1159 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1161 U_CHAR
*buf
, *limit
;
1163 struct arglist
*arglist
;
1166 register U_CHAR
*p
, *lastp
, *exp_p
;
1167 struct reflist
*endpat
= NULL
;
1168 /* Pointer to first nonspace after last ## seen. */
1170 /* Pointer to first nonspace after last single-# seen. */
1171 U_CHAR
*stringify
= 0;
1173 int expected_delimiter
= '\0';
1175 /* Scan thru the replacement list, ignoring comments and quoted
1176 strings, picking up on the macro calls. It does a linear search
1177 thru the arg list on every potential symbol. Profiling might say
1178 that something smarter should happen. */
1183 /* Find the beginning of the trailing whitespace. */
1185 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1187 /* Allocate space for the text in the macro definition.
1188 Leading and trailing whitespace chars need 2 bytes each.
1189 Each other input char may or may not need 1 byte,
1190 so this is an upper bound. The extra 5 are for invented
1191 leading and trailing newline-marker and final null. */
1192 maxsize
= (sizeof (DEFINITION
)
1194 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1198 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1200 defn
->nargs
= nargs
;
1201 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1206 /* Add one initial space escape-marker to prevent accidental
1207 token-pasting (often removed by macroexpand). */
1211 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1212 cpp_error (pfile
, "`##' at start of macro definition");
1216 /* Process the main body of the definition. */
1218 int skipped_arg
= 0;
1219 register U_CHAR c
= *p
++;
1223 if (!CPP_TRADITIONAL (pfile
)) {
1227 if (expected_delimiter
!= '\0') {
1228 if (c
== expected_delimiter
)
1229 expected_delimiter
= '\0';
1231 expected_delimiter
= c
;
1235 if (p
< limit
&& expected_delimiter
) {
1236 /* In a string, backslash goes through
1237 and makes next char ordinary. */
1243 /* An '@' in a string or character constant stands for itself,
1244 and does not need to be escaped. */
1245 if (!expected_delimiter
)
1250 /* # is ordinary inside a string. */
1251 if (expected_delimiter
)
1253 if (p
< limit
&& *p
== '#') {
1254 /* ##: concatenate preceding and following tokens. */
1255 /* Take out the first #, discard preceding whitespace. */
1257 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1259 /* Skip the second #. */
1261 /* Discard following whitespace. */
1262 SKIP_WHITE_SPACE (p
);
1265 cpp_error (pfile
, "`##' at end of macro definition");
1266 } else if (nargs
>= 0) {
1267 /* Single #: stringify following argument ref.
1268 Don't leave the # in the expansion. */
1270 SKIP_WHITE_SPACE (p
);
1271 if (p
== limit
|| ! is_idstart
[*p
]
1272 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' || p
[1] == '"')))
1274 "`#' operator is not followed by a macro argument name");
1281 /* In -traditional mode, recognize arguments inside strings and
1282 character constants, and ignore special properties of #.
1283 Arguments inside strings are considered "stringified", but no
1284 extra quote marks are supplied. */
1288 if (expected_delimiter
!= '\0') {
1289 if (c
== expected_delimiter
)
1290 expected_delimiter
= '\0';
1292 expected_delimiter
= c
;
1296 /* Backslash quotes delimiters and itself, but not macro args. */
1297 if (expected_delimiter
!= 0 && p
< limit
1298 && (*p
== expected_delimiter
|| *p
== '\\')) {
1305 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1308 /* If we find a comment that wasn't removed by handle_directive,
1309 this must be -traditional. So replace the comment with
1313 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1316 /* Mark this as a concatenation-point, as if it had been ##. */
1324 /* Handle the start of a symbol. */
1325 if (is_idchar
[c
] && nargs
> 0) {
1326 U_CHAR
*id_beg
= p
- 1;
1330 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1331 id_len
= p
- id_beg
;
1334 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
1335 register struct arglist
*arg
;
1337 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1338 struct reflist
*tpat
;
1340 if (arg
->name
[0] == c
1341 && arg
->length
== id_len
1342 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1343 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1344 if (CPP_TRADITIONAL (pfile
)) {
1345 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1349 "macro arg `%.*s' would be stringified with -traditional.",
1353 /* If ANSI, don't actually substitute inside a string. */
1354 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1356 /* make a pat node for this arg and append it to the end of
1358 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1360 tpat
->raw_before
= concat
== id_beg
;
1361 tpat
->raw_after
= 0;
1362 tpat
->rest_args
= arg
->rest_args
;
1363 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1364 ? expected_delimiter
!= '\0'
1365 : stringify
== id_beg
);
1368 defn
->pattern
= tpat
;
1370 endpat
->next
= tpat
;
1373 tpat
->argno
= arg
->argno
;
1374 tpat
->nchars
= exp_p
- lastp
;
1376 register U_CHAR
*p1
= p
;
1377 SKIP_WHITE_SPACE (p1
);
1378 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1379 tpat
->raw_after
= 1;
1381 lastp
= exp_p
; /* place to start copying from next time */
1388 /* If this was not a macro arg, copy it into the expansion. */
1389 if (! skipped_arg
) {
1390 register U_CHAR
*lim1
= p
;
1394 if (stringify
== id_beg
)
1396 "`#' operator should be followed by a macro argument name");
1401 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1403 /* If ANSI, put in a "@ " marker to prevent token pasting.
1404 But not if "inside a string" (which in ANSI mode
1405 happens only for -D option). */
1412 defn
->length
= exp_p
- defn
->expansion
;
1414 /* Crash now if we overrun the allocated size. */
1415 if (defn
->length
+ 1 > maxsize
)
1419 /* This isn't worth the time it takes. */
1420 /* give back excess storage */
1421 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1428 * special extension string that can be added to the last macro argument to
1429 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1430 * #define wow(a, b...) process (b, a, b)
1431 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1432 * { wow (one, two); } -> { process (two, one, two); }
1433 * if this "rest_arg" is used with the concat token '##' and if it is not
1434 * supplied then the token attached to with ## will not be outputted. Ex:
1435 * #define wow (a, b...) process (b ## , a, ## b)
1436 * { wow (1, 2); } -> { process (2, 1, 2); }
1437 * { wow (one); } -> { process (one); {
1439 static char rest_extension
[] = "...";
1440 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1442 /* Create a DEFINITION node from a #define directive. Arguments are
1443 as for do_define. */
1446 create_definition (buf
, limit
, pfile
, predefinition
)
1447 U_CHAR
*buf
, *limit
;
1451 U_CHAR
*bp
; /* temp ptr into input buffer */
1452 U_CHAR
*symname
; /* remember where symbol name starts */
1453 int sym_length
; /* and how long it is */
1456 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1458 int arglengths
= 0; /* Accumulate lengths of arg names
1459 plus number of args. */
1461 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1465 while (is_hor_space
[*bp
])
1468 symname
= bp
; /* remember where it starts */
1470 sym_length
= check_macro_name (pfile
, bp
, "macro");
1473 /* Lossage will occur if identifiers or control keywords are broken
1474 across lines using backslash. This is not the right place to take
1478 struct arglist
*arg_ptrs
= NULL
;
1481 bp
++; /* skip '(' */
1482 SKIP_WHITE_SPACE (bp
);
1484 /* Loop over macro argument names. */
1485 while (*bp
!= ')') {
1486 struct arglist
*temp
;
1488 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1490 temp
->next
= arg_ptrs
;
1491 temp
->argno
= argno
++;
1492 temp
->rest_args
= 0;
1496 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1498 if (!is_idstart
[*bp
])
1499 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1501 /* Find the end of the arg name. */
1502 while (is_idchar
[*bp
]) {
1504 /* do we have a "special" rest-args extension here? */
1505 if (limit
- bp
> REST_EXTENSION_LENGTH
1506 && strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1508 temp
->rest_args
= 1;
1512 temp
->length
= bp
- temp
->name
;
1514 bp
+= REST_EXTENSION_LENGTH
;
1515 arglengths
+= temp
->length
+ 2;
1516 SKIP_WHITE_SPACE (bp
);
1517 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1518 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1523 SKIP_WHITE_SPACE (bp
);
1526 cpp_error (pfile
, "unterminated parameter list in `#define'");
1530 struct arglist
*otemp
;
1532 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1533 if (temp
->length
== otemp
->length
1534 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1537 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1538 (void) strncpy (name
, temp
->name
, temp
->length
);
1539 name
[temp
->length
] = '\0';
1541 "duplicate argument name `%s' in `#define'", name
);
1547 ++bp
; /* skip paren */
1548 SKIP_WHITE_SPACE (bp
);
1549 /* now everything from bp before limit is the definition. */
1550 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1551 defn
->rest_args
= rest_args
;
1553 /* Now set defn->args.argnames to the result of concatenating
1554 the argument names in reverse order
1555 with comma-space between them. */
1556 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1558 struct arglist
*temp
;
1560 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1561 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1563 if (temp
->next
!= 0) {
1564 defn
->args
.argnames
[i
++] = ',';
1565 defn
->args
.argnames
[i
++] = ' ';
1568 defn
->args
.argnames
[i
] = 0;
1571 /* Simple expansion or empty definition. */
1575 if (is_hor_space
[*bp
]) {
1577 SKIP_WHITE_SPACE (bp
);
1580 case '!': case '"': case '#': case '%': case '&': case '\'':
1581 case ')': case '*': case '+': case ',': case '-': case '.':
1582 case '/': case ':': case ';': case '<': case '=': case '>':
1583 case '?': case '[': case '\\': case ']': case '^': case '{':
1584 case '|': case '}': case '~':
1585 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1586 sym_length
, symname
);
1590 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1591 sym_length
, symname
);
1596 /* now everything from bp before limit is the definition. */
1597 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1598 defn
->args
.argnames
= (U_CHAR
*) "";
1604 /* OP is null if this is a predefinition */
1605 defn
->predefined
= predefinition
;
1607 mdef
.symnam
= symname
;
1608 mdef
.symlen
= sym_length
;
1617 /* Check a purported macro name SYMNAME, and yield its length.
1618 USAGE is the kind of name this is intended for. */
1621 check_macro_name (pfile
, symname
, usage
)
1629 for (p
= symname
; is_idchar
[*p
]; p
++)
1631 sym_length
= p
- symname
;
1633 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
1634 cpp_error (pfile
, "invalid %s name", usage
);
1635 else if (!is_idstart
[*symname
]) {
1636 U_CHAR
*msg
; /* what pain... */
1637 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1638 bcopy (symname
, msg
, sym_length
);
1639 msg
[sym_length
] = 0;
1640 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1642 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1643 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1648 /* Return zero if two DEFINITIONs are isomorphic. */
1651 compare_defs (pfile
, d1
, d2
)
1653 DEFINITION
*d1
, *d2
;
1655 register struct reflist
*a1
, *a2
;
1656 register U_CHAR
*p1
= d1
->expansion
;
1657 register U_CHAR
*p2
= d2
->expansion
;
1660 if (d1
->nargs
!= d2
->nargs
)
1662 if (CPP_PEDANTIC (pfile
)
1663 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1665 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1666 a1
= a1
->next
, a2
= a2
->next
) {
1667 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1668 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1669 || a1
->argno
!= a2
->argno
1670 || a1
->stringify
!= a2
->stringify
1671 || a1
->raw_before
!= a2
->raw_before
1672 || a1
->raw_after
!= a2
->raw_after
)
1680 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1681 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1686 /* Return 1 if two parts of two macro definitions are effectively different.
1687 One of the parts starts at BEG1 and has LEN1 chars;
1688 the other has LEN2 chars at BEG2.
1689 Any sequence of whitespace matches any other sequence of whitespace.
1690 FIRST means these parts are the first of a macro definition;
1691 so ignore leading whitespace entirely.
1692 LAST means these parts are the last of a macro definition;
1693 so ignore trailing whitespace entirely. */
1696 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1698 U_CHAR
*beg1
, *beg2
;
1702 register U_CHAR
*end1
= beg1
+ len1
;
1703 register U_CHAR
*end2
= beg2
+ len2
;
1705 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1706 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1709 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1710 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1712 while (beg1
!= end1
&& beg2
!= end2
) {
1713 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1714 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1715 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1716 } else if (*beg1
== *beg2
) {
1720 return (beg1
!= end1
) || (beg2
!= end2
);
1723 /* Process a #define command.
1724 BUF points to the contents of the #define command, as a contiguous string.
1725 LIMIT points to the first character past the end of the definition.
1726 KEYWORD is the keyword-table entry for #define,
1727 or NULL for a "predefined" macro. */
1730 do_define (pfile
, keyword
, buf
, limit
)
1732 struct directive
*keyword
;
1733 U_CHAR
*buf
, *limit
;
1740 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1741 if (pcp_outfile
&& keyword
)
1742 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1745 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1749 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1751 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1754 /* Redefining a precompiled key is ok. */
1755 if (hp
->type
== T_PCSTRING
)
1757 /* Redefining a macro is ok if the definitions are the same. */
1758 else if (hp
->type
== T_MACRO
)
1759 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
1760 /* Redefining a constant is ok with -D. */
1761 else if (hp
->type
== T_CONST
)
1762 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1763 /* Print the warning if it's not ok. */
1766 U_CHAR
*msg
; /* what pain... */
1768 /* If we are passing through #define and #undef directives, do
1769 that for this re-definition now. */
1770 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1771 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1773 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1775 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1776 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1777 cpp_pedwarn (pfile
, msg
);
1778 if (hp
->type
== T_MACRO
)
1779 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1780 "this is the location of the previous definition");
1782 /* Replace the old definition. */
1784 hp
->value
.defn
= mdef
.defn
;
1788 /* If we are passing through #define and #undef directives, do
1789 that for this new definition now. */
1790 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1791 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1792 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1793 (char *) mdef
.defn
, hashcode
);
1803 /* This structure represents one parsed argument in a macro call.
1804 `raw' points to the argument text as written (`raw_length' is its length).
1805 `expanded' points to the argument's macro-expansion
1806 (its length is `expand_length').
1807 `stringified_length' is the length the argument would have
1809 `use_count' is the number of times this macro arg is substituted
1810 into the macro. If the actual use count exceeds 10,
1811 the value stored is 10. */
1813 /* raw and expanded are relative to ARG_BASE */
1814 #define ARG_BASE ((pfile)->token_buffer)
1817 /* Strings relative to pfile->token_buffer */
1818 long raw
, expanded
, stringified
;
1819 int raw_length
, expand_length
;
1820 int stringified_length
;
1825 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1826 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1827 as the new input buffer.
1828 Return the new buffer, or NULL on failure. */
1831 cpp_push_buffer (pfile
, buffer
, length
)
1836 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1837 if (buf
== pfile
->buffer_stack
)
1839 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1844 bzero ((char *) buf
, sizeof (cpp_buffer
));
1845 CPP_BUFFER (pfile
) = buf
;
1846 buf
->if_stack
= pfile
->if_stack
;
1847 buf
->cleanup
= null_cleanup
;
1848 buf
->underflow
= null_underflow
;
1849 buf
->buf
= buf
->cur
= buffer
;
1850 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1856 cpp_pop_buffer (pfile
)
1859 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1860 (*buf
->cleanup
) (buf
, pfile
);
1861 return ++CPP_BUFFER (pfile
);
1864 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1865 Pop the buffer when done. */
1868 cpp_scan_buffer (pfile
)
1871 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1874 enum cpp_token token
= cpp_get_token (pfile
);
1875 if (token
== CPP_EOF
) /* Should not happen ... */
1877 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1879 cpp_pop_buffer (pfile
);
1886 * Rescan a string (which may have escape marks) into pfile's buffer.
1887 * Place the result in pfile->token_buffer.
1889 * The input is copied before it is scanned, so it is safe to pass
1890 * it something from the token_buffer that will get overwritten
1891 * (because it follows CPP_WRITTEN). This is used by do_include.
1895 cpp_expand_to_buffer (pfile
, buf
, length
)
1900 register cpp_buffer
*ip
;
1904 U_CHAR
*limit
= buf
+ length
;
1907 int odepth
= indepth
;
1913 /* Set up the input on the input stack. */
1915 buf1
= (U_CHAR
*) alloca (length
+ 1);
1917 register U_CHAR
*p1
= buf
;
1918 register U_CHAR
*p2
= buf1
;
1925 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1928 ip
->has_escapes
= 1;
1930 ip
->lineno
= obuf
.lineno
= 1;
1933 /* Scan the input, create the output. */
1934 cpp_scan_buffer (pfile
);
1937 if (indepth
!= odepth
)
1941 CPP_NUL_TERMINATE (pfile
);
1946 adjust_position (buf
, limit
, linep
, colp
)
1956 (*linep
)++, (*colp
) = 1;
1962 /* Move line_base forward, updating lineno and colno. */
1965 update_position (pbuf
)
1966 register cpp_buffer
*pbuf
;
1968 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
1969 unsigned char *new_pos
= pbuf
->cur
;
1970 register struct parse_marker
*mark
;
1971 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
1973 if (pbuf
->buf
+ mark
->position
< new_pos
)
1974 new_pos
= pbuf
->buf
+ mark
->position
;
1976 pbuf
->line_base
+= new_pos
- old_pos
;
1977 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
1981 cpp_buf_line_and_col (pbuf
, linep
, colp
)
1982 register cpp_buffer
*pbuf
;
1990 *linep
= pbuf
->lineno
;
1991 *colp
= pbuf
->colno
;
1992 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2001 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2004 cpp_file_buffer (pfile
)
2007 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2009 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2010 if (ip
->fname
!= NULL
)
2016 count_newlines (buf
, limit
)
2017 register U_CHAR
*buf
;
2018 register U_CHAR
*limit
;
2020 register long count
= 0;
2031 * write out a #line command, for instance, after an #include file.
2032 * If CONDITIONAL is nonzero, we can omit the #line if it would
2033 * appear to be a no-op, and we can output a few newlines instead
2034 * if we want to increase the line number by a small amount.
2035 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2039 output_line_command (pfile
, conditional
, file_change
)
2042 enum file_change_code file_change
;
2045 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2047 if (ip
->fname
== NULL
)
2050 update_position (ip
);
2052 if (CPP_OPTIONS (pfile
)->no_line_commands
2053 || CPP_OPTIONS (pfile
)->no_output
)
2056 line
= CPP_BUFFER (pfile
)->lineno
;
2057 col
= CPP_BUFFER (pfile
)->colno
;
2058 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2060 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2064 if (line
== pfile
->lineno
)
2067 /* If the inherited line number is a little too small,
2068 output some newlines instead of a #line command. */
2069 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2070 CPP_RESERVE (pfile
, 20);
2071 while (line
> pfile
->lineno
) {
2072 CPP_PUTC_Q (pfile
, '\n');
2080 /* Don't output a line number of 0 if we can help it. */
2081 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2082 && *ip
->bufp
== '\n') {
2088 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2090 #ifdef OUTPUT_LINE_COMMANDS
2091 static char sharp_line
[] = "#line ";
2093 static char sharp_line
[] = "# ";
2095 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2098 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
2099 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2101 quote_string (pfile
, ip
->nominal_fname
);
2102 if (file_change
!= same_file
) {
2103 CPP_PUTC_Q (pfile
, ' ');
2104 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2106 /* Tell cc1 if following text comes from a system header file. */
2107 if (ip
->system_header_p
) {
2108 CPP_PUTC_Q (pfile
, ' ');
2109 CPP_PUTC_Q (pfile
, '3');
2111 #ifndef NO_IMPLICIT_EXTERN_C
2112 /* Tell cc1plus if following text should be treated as C. */
2113 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2114 CPP_PUTC_Q (pfile
, ' ');
2115 CPP_PUTC_Q (pfile
, '4');
2118 CPP_PUTC_Q (pfile
, '\n');
2119 pfile
->lineno
= line
;
2123 * Parse a macro argument and append the info on PFILE's token_buffer.
2124 * REST_ARGS means to absorb the rest of the args.
2125 * Return nonzero to indicate a syntax error.
2128 static enum cpp_token
2129 macarg (pfile
, rest_args
)
2134 enum cpp_token token
;
2135 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2136 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2138 /* Try to parse as much of the argument as exists at this
2139 input stack level. */
2140 pfile
->no_macro_expand
++;
2143 token
= cpp_get_token (pfile
);
2149 /* If we've hit end of file, it's an error (reported by caller).
2150 Ditto if it's the end of cpp_expand_to_buffer text.
2151 If we've hit end of macro, just continue. */
2152 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2163 /* if we've returned to lowest level and
2164 we aren't absorbing all args */
2165 if (paren
== 0 && rest_args
== 0)
2169 /* Remove ',' or ')' from argument buffer. */
2170 CPP_ADJUST_WRITTEN (pfile
, -1);
2177 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2178 pfile
->no_macro_expand
--;
2183 /* Turn newlines to spaces in the string of length LENGTH at START,
2184 except inside of string constants.
2185 The string is copied into itself with its beginning staying fixed. */
2188 change_newlines (start
, length
)
2192 register U_CHAR
*ibp
;
2193 register U_CHAR
*obp
;
2194 register U_CHAR
*limit
;
2198 limit
= start
+ length
;
2201 while (ibp
< limit
) {
2202 *obp
++ = c
= *ibp
++;
2207 /* Notice and skip strings, so that we don't delete newlines in them. */
2210 while (ibp
< limit
) {
2211 *obp
++ = c
= *ibp
++;
2214 if (c
== '\n' && quotec
== '\'')
2230 if (!pfile
->timebuf
) {
2231 time_t t
= time ((time_t *) 0);
2232 pfile
->timebuf
= localtime (&t
);
2234 return pfile
->timebuf
;
2237 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2238 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2242 * expand things like __FILE__. Place the expansion into the output
2243 * buffer *without* rescanning.
2247 special_symbol (hp
, pfile
)
2254 cpp_buffer
*ip
= NULL
;
2257 int paren
= 0; /* For special `defined' keyword */
2260 if (pcp_outfile
&& pcp_inside_if
2261 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2263 "Predefined macro `%s' used inside `#if' during precompilation",
2267 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2269 if (ip
== CPP_NULL_BUFFER (pfile
))
2271 cpp_error (pfile
, "cccp error: not in any file?!");
2272 return; /* the show must go on */
2274 if (ip
->fname
!= NULL
)
2284 if (hp
->type
== T_BASE_FILE
)
2286 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2287 ip
= CPP_PREV_BUFFER (ip
);
2289 string
= ip
->nominal_fname
;
2293 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2294 quote_string (pfile
, string
);
2298 case T_INCLUDE_LEVEL
:
2300 ip
= CPP_BUFFER (pfile
);
2301 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2302 if (ip
->fname
!= NULL
)
2305 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2306 sprintf (buf
, "%d", true_indepth
- 1);
2310 buf
= (char *) alloca (3 + strlen (version_string
));
2311 sprintf (buf
, "\"%s\"", version_string
);
2314 #ifndef NO_BUILTIN_SIZE_TYPE
2320 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2321 case T_PTRDIFF_TYPE
:
2327 buf
= CPP_WCHAR_TYPE (pfile
);
2330 case T_USER_LABEL_PREFIX_TYPE
:
2331 buf
= USER_LABEL_PREFIX
;
2334 case T_REGISTER_PREFIX_TYPE
:
2335 buf
= REGISTER_PREFIX
;
2339 buf
= (char *) alloca (4 * sizeof (int));
2340 sprintf (buf
, "%d", hp
->value
.ival
);
2341 #ifdef STDC_0_IN_SYSTEM_HEADERS
2342 if (ip
->system_header_p
2343 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2344 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2348 if (pcp_inside_if
&& pcp_outfile
)
2349 /* Output a precondition for this macro use */
2350 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2356 long line
= ip
->lineno
;
2357 long col
= ip
->colno
;
2358 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2360 buf
= (char *) alloca (10);
2361 sprintf (buf
, "%ld", line
);
2367 buf
= (char *) alloca (20);
2368 timebuf
= timestamp (pfile
);
2369 if (hp
->type
== T_DATE
)
2370 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2371 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2373 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2377 case T_SPEC_DEFINED
:
2378 buf
= " 0 "; /* Assume symbol is not defined */
2379 ip
= CPP_BUFFER (pfile
);
2380 SKIP_WHITE_SPACE (ip
->cur
);
2381 if (*ip
->cur
== '(')
2384 ip
->cur
++; /* Skip over the paren */
2385 SKIP_WHITE_SPACE (ip
->cur
);
2388 if (!is_idstart
[*ip
->cur
])
2390 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2392 if ((hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1)))
2395 if (pcp_outfile
&& pcp_inside_if
2396 && (hp
->type
== T_CONST
2397 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2398 /* Output a precondition for this macro use. */
2399 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2405 if (pcp_outfile
&& pcp_inside_if
)
2407 /* Output a precondition for this macro use */
2408 U_CHAR
*cp
= ip
->bufp
;
2409 fprintf (pcp_outfile
, "#undef ");
2410 while (is_idchar
[*cp
]) /* Ick! */
2411 fputc (*cp
++, pcp_outfile
);
2412 putc ('\n', pcp_outfile
);
2415 while (is_idchar
[*ip
->cur
])
2417 SKIP_WHITE_SPACE (ip
->cur
);
2420 if (*ip
->cur
!= ')')
2428 cpp_error (pfile
, "`defined' without an identifier");
2432 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2436 CPP_RESERVE (pfile
, len
+ 1);
2437 CPP_PUTS_Q (pfile
, buf
, len
);
2438 CPP_NUL_TERMINATE_Q (pfile
);
2443 /* Write out a #define command for the special named MACRO_NAME
2444 to PFILE's token_buffer. */
2447 dump_special_to_buffer (pfile
, macro_name
)
2451 static char define_directive
[] = "#define ";
2452 int macro_name_length
= strlen (macro_name
);
2453 output_line_command (pfile
, 0, same_file
);
2454 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2455 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2456 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2457 CPP_PUTC_Q (pfile
, ' ');
2458 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2459 CPP_PUTC (pfile
, '\n');
2462 /* Initialize the built-in macros. */
2465 initialize_builtins (pfile
)
2468 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2469 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2470 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2471 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2472 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2473 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2474 #ifndef NO_BUILTIN_SIZE_TYPE
2475 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2477 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2478 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2480 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2481 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2482 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2483 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2484 if (!CPP_TRADITIONAL (pfile
))
2485 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2486 if (CPP_OPTIONS (pfile
)->objc
)
2487 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2488 /* This is supplied using a -D by the compiler driver
2489 so that it is present only when truly compiling with GNU C. */
2490 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2492 if (CPP_OPTIONS (pfile
)->debug_output
)
2494 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2495 dump_special_to_buffer (pfile
, "__VERSION__");
2496 #ifndef NO_BUILTIN_SIZE_TYPE
2497 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2499 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2500 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2502 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2503 dump_special_to_buffer (pfile
, "__DATE__");
2504 dump_special_to_buffer (pfile
, "__TIME__");
2505 if (!CPP_TRADITIONAL (pfile
))
2506 dump_special_to_buffer (pfile
, "__STDC__");
2507 if (CPP_OPTIONS (pfile
)->objc
)
2508 dump_special_to_buffer (pfile
, "__OBJC__");
2512 /* Return 1 iff a token ending in C1 followed directly by a token C2
2513 could cause mis-tokenization. */
2516 unsafe_chars (c1
, c2
)
2522 if (c2
== c1
|| c2
== '=')
2526 case '0': case '1': case '2': case '3': case '4':
2527 case '5': case '6': case '7': case '8': case '9':
2528 case 'e': case 'E': case 'p': case 'P':
2529 if (c2
== '-' || c2
== '+')
2530 return 1; /* could extend a pre-processing number */
2533 if (c2
== '\'' || c2
== '\"')
2534 return 1; /* Could turn into L"xxx" or L'xxx'. */
2538 case 'a': case 'b': case 'c': case 'd': case 'f':
2539 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2540 case 'm': case 'n': case 'o': case 'q': case 'r':
2541 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2543 case 'A': case 'B': case 'C': case 'D': case 'F':
2544 case 'G': case 'H': case 'I': case 'J': case 'K':
2545 case 'M': case 'N': case 'O': case 'Q': case 'R':
2546 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2548 /* We're in the middle of either a name or a pre-processing number. */
2549 return (is_idchar
[c2
] || c2
== '.');
2550 case '<': case '>': case '!': case '%': case '#': case ':':
2551 case '^': case '&': case '|': case '*': case '/': case '=':
2552 return (c2
== c1
|| c2
== '=');
2557 /* Expand a macro call.
2558 HP points to the symbol that is the macro being called.
2559 Put the result of expansion onto the input stack
2560 so that subsequent input by our caller will use it.
2562 If macro wants arguments, caller has already verified that
2563 an argument list follows; arguments come from the input stack. */
2566 macroexpand (pfile
, hp
)
2571 DEFINITION
*defn
= hp
->value
.defn
;
2572 register U_CHAR
*xbuf
;
2573 long start_line
, start_column
;
2575 struct argdata
*args
;
2576 long old_written
= CPP_WRITTEN (pfile
);
2578 int start_line
= instack
[indepth
].lineno
;
2580 int rest_args
, rest_zero
;
2584 CHECK_DEPTH (return;);
2588 /* This macro is being used inside a #if, which means it must be */
2589 /* recorded as a precondition. */
2590 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2591 dump_single_macro (hp
, pcp_outfile
);
2594 pfile
->output_escapes
++;
2595 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2597 nargs
= defn
->nargs
;
2601 enum cpp_token token
;
2603 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2605 for (i
= 0; i
< nargs
; i
++)
2607 args
[i
].raw
= args
[i
].expanded
= 0;
2608 args
[i
].raw_length
= 0;
2609 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2610 args
[i
].use_count
= 0;
2613 /* Parse all the macro args that are supplied. I counts them.
2614 The first NARGS args are stored in ARGS.
2615 The rest are discarded. If rest_args is set then we assume
2616 macarg absorbed the rest of the args. */
2620 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2625 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2627 /* if we are working on last arg which absorbs rest of args... */
2628 if (i
== nargs
- 1 && defn
->rest_args
)
2630 args
[i
].raw
= CPP_WRITTEN (pfile
);
2631 token
= macarg (pfile
, rest_args
);
2632 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2633 args
[i
].newlines
= 0; /* FIXME */
2636 token
= macarg (pfile
, 0);
2637 if (token
== CPP_EOF
|| token
== CPP_POP
)
2639 cpp_error_with_line (pfile
, start_line
, start_column
,
2640 "unterminated macro call");
2644 } while (token
== CPP_COMMA
);
2646 /* If we got one arg but it was just whitespace, call that 0 args. */
2649 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2650 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2651 /* cpp.texi says for foo ( ) we provide one argument.
2652 However, if foo wants just 0 arguments, treat this as 0. */
2654 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2659 /* Don't output an error message if we have already output one for
2660 a parse error above. */
2662 if (nargs
== 0 && i
> 0)
2664 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2668 /* traditional C allows foo() if foo wants one argument. */
2669 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2671 /* the rest args token is allowed to absorb 0 tokens */
2672 else if (i
== nargs
- 1 && defn
->rest_args
)
2675 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2677 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2679 cpp_error (pfile
, "macro `%s' used with only %d args",
2685 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2689 /* If macro wants zero args, we parsed the arglist for checking only.
2690 Read directly from the macro definition. */
2693 xbuf
= defn
->expansion
;
2694 xbuf_len
= defn
->length
;
2698 register U_CHAR
*exp
= defn
->expansion
;
2699 register int offset
; /* offset in expansion,
2700 copied a piece at a time */
2701 register int totlen
; /* total amount of exp buffer filled so far */
2703 register struct reflist
*ap
, *last_ap
;
2705 /* Macro really takes args. Compute the expansion of this call. */
2707 /* Compute length in characters of the macro's expansion.
2708 Also count number of times each arg is used. */
2709 xbuf_len
= defn
->length
;
2710 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2714 register struct argdata
*arg
= &args
[ap
->argno
];
2715 /* Stringify if it hasn't already been */
2716 if (arg
->stringified_length
< 0)
2718 int arglen
= arg
->raw_length
;
2722 /* Initially need_space is -1. Otherwise, 1 means the
2723 previous character was a space, but we suppressed it;
2724 0 means the previous character was a non-space. */
2725 int need_space
= -1;
2727 arg
->stringified
= CPP_WRITTEN (pfile
);
2728 if (!CPP_TRADITIONAL (pfile
))
2729 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2730 for (; i
< arglen
; i
++)
2732 c
= (ARG_BASE
+ arg
->raw
)[i
];
2736 /* Internal sequences of whitespace are replaced by
2737 one space except within an string or char token.*/
2740 if (CPP_WRITTEN (pfile
) > arg
->stringified
2741 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2743 /* "@ " escape markers are removed */
2744 CPP_ADJUST_WRITTEN (pfile
, -1);
2747 if (need_space
== 0)
2751 else if (need_space
> 0)
2752 CPP_PUTC (pfile
, ' ');
2767 else if (c
== '\"' || c
== '\'')
2771 /* Escape these chars */
2772 if (c
== '\"' || (in_string
&& c
== '\\'))
2773 CPP_PUTC (pfile
, '\\');
2775 CPP_PUTC (pfile
, c
);
2778 CPP_RESERVE (pfile
, 4);
2779 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2781 CPP_ADJUST_WRITTEN (pfile
, 4);
2784 if (!CPP_TRADITIONAL (pfile
))
2785 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2786 arg
->stringified_length
2787 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2789 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2791 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2792 /* Add 4 for two newline-space markers to prevent
2793 token concatenation. */
2794 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2797 /* We have an ordinary (expanded) occurrence of the arg.
2798 So compute its expansion, if we have not already. */
2799 if (args
[ap
->argno
].expand_length
< 0)
2801 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2802 cpp_expand_to_buffer (pfile
,
2803 ARG_BASE
+ args
[ap
->argno
].raw
,
2804 args
[ap
->argno
].raw_length
);
2806 args
[ap
->argno
].expand_length
2807 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2810 /* Add 4 for two newline-space markers to prevent
2811 token concatenation. */
2812 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2814 if (args
[ap
->argno
].use_count
< 10)
2815 args
[ap
->argno
].use_count
++;
2818 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2820 /* Generate in XBUF the complete expansion
2821 with arguments substituted in.
2822 TOTLEN is the total size generated so far.
2823 OFFSET is the index in the definition
2824 of where we are copying from. */
2825 offset
= totlen
= 0;
2826 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2827 last_ap
= ap
, ap
= ap
->next
)
2829 register struct argdata
*arg
= &args
[ap
->argno
];
2830 int count_before
= totlen
;
2832 /* Add chars to XBUF. */
2833 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2834 xbuf
[totlen
++] = exp
[offset
];
2836 /* If followed by an empty rest arg with concatenation,
2837 delete the last run of nonwhite chars. */
2838 if (rest_zero
&& totlen
> count_before
2839 && ((ap
->rest_args
&& ap
->raw_before
)
2840 || (last_ap
!= NULL
&& last_ap
->rest_args
2841 && last_ap
->raw_after
)))
2843 /* Delete final whitespace. */
2844 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2847 /* Delete the nonwhites before them. */
2848 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2852 if (ap
->stringify
!= 0)
2854 bcopy (ARG_BASE
+ arg
->stringified
,
2855 xbuf
+ totlen
, arg
->stringified_length
);
2856 totlen
+= arg
->stringified_length
;
2858 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2860 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2861 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2864 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2865 while (p1
!= l1
&& is_idchar
[*p1
])
2866 xbuf
[totlen
++] = *p1
++;
2867 /* Delete any no-reexpansion marker that follows
2868 an identifier at the beginning of the argument
2869 if the argument is concatenated with what precedes it. */
2870 if (p1
[0] == '@' && p1
[1] == '-')
2875 /* Arg is concatenated after: delete trailing whitespace,
2876 whitespace markers, and no-reexpansion markers. */
2879 if (is_space
[l1
[-1]]) l1
--;
2880 else if (l1
[-1] == '-')
2882 U_CHAR
*p2
= l1
- 1;
2883 /* If a `-' is preceded by an odd number of newlines then it
2884 and the last newline are a no-reexpansion marker. */
2885 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2886 if ((l1
- 1 - p2
) & 1) {
2895 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2900 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2901 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2902 && !CPP_TRADITIONAL(pfile
)
2903 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2905 xbuf
[totlen
++] = '@';
2906 xbuf
[totlen
++] = ' ';
2909 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2910 totlen
+= arg
->expand_length
;
2912 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2913 && !CPP_TRADITIONAL(pfile
)
2914 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2916 xbuf
[totlen
++] = '@';
2917 xbuf
[totlen
++] = ' ';
2920 /* If a macro argument with newlines is used multiple times,
2921 then only expand the newlines once. This avoids creating
2922 output lines which don't correspond to any input line,
2923 which confuses gdb and gcov. */
2924 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2926 /* Don't bother doing change_newlines for subsequent
2930 = change_newlines (expanded
, arg
->expand_length
);
2934 if (totlen
> xbuf_len
)
2938 /* if there is anything left of the definition
2939 after handling the arg list, copy that in too. */
2941 for (i
= offset
; i
< defn
->length
; i
++)
2943 /* if we've reached the end of the macro */
2946 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2947 && last_ap
->raw_after
))
2948 xbuf
[totlen
++] = exp
[i
];
2956 pfile
->output_escapes
--;
2958 /* Now put the expansion on the input stack
2959 so our caller will commence reading from it. */
2960 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2961 CPP_BUFFER (pfile
)->has_escapes
= 1;
2963 /* Pop the space we've used in the token_buffer for argument expansion. */
2964 CPP_SET_WRITTEN (pfile
, old_written
);
2966 /* Recursive macro use sometimes works traditionally.
2967 #define foo(x,y) bar (x (y,0), y)
2970 if (!CPP_TRADITIONAL (pfile
))
2971 hp
->type
= T_DISABLED
;
2975 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
2977 register U_CHAR
*xbuf
;
2981 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
2984 mbuf
->cleanup
= macro_cleanup
;
2987 /* The first chars of the expansion should be a "@ " added by
2988 collect_expansion. This is to prevent accidental token-pasting
2989 between the text preceding the macro invocation, and the macro
2992 We would like to avoid adding unneeded spaces (for the sake of
2993 tools that use cpp, such as imake). In some common cases we can
2994 tell that it is safe to omit the space.
2996 The character before the macro invocation cannot have been an
2997 idchar (or else it would have been pasted with the idchars of
2998 the macro name). Therefore, if the first non-space character
2999 of the expansion is an idchar, we do not need the extra space
3000 to prevent token pasting.
3002 Also, we don't need the extra space if the first char is '(',
3003 or some other (less common) characters. */
3005 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3006 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3007 || xbuf
[2] == '\"'))
3011 /* Like cpp_get_token, except that it does not read past end-of-line.
3012 Also, horizontal space is skipped, and macros are popped. */
3014 static enum cpp_token
3015 get_directive_token (pfile
)
3020 long old_written
= CPP_WRITTEN (pfile
);
3021 enum cpp_token token
;
3022 cpp_skip_hspace (pfile
);
3023 if (PEEKC () == '\n')
3025 token
= cpp_get_token (pfile
);
3029 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3031 /* ... else fall though ... */
3032 case CPP_HSPACE
: case CPP_COMMENT
:
3033 CPP_SET_WRITTEN (pfile
, old_written
);
3041 /* Handle #include and #import.
3042 This function expects to see "fname" or <fname> on the input.
3044 The input is normally in part of the output_buffer following
3045 CPP_WRITTEN, and will get overwritten by output_line_command.
3046 I.e. in input file specification has been popped by handle_directive.
3050 do_include (pfile
, keyword
, unused1
, unused2
)
3052 struct directive
*keyword
;
3053 U_CHAR
*unused1
, *unused2
;
3055 int importing
= (keyword
->type
== T_IMPORT
);
3056 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3057 char *fname
; /* Dynamically allocated fname buffer */
3059 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3060 enum cpp_token token
;
3062 /* Chain of dirs to search */
3063 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3064 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3065 struct file_name_list
*searchptr
= 0;
3066 long old_written
= CPP_WRITTEN (pfile
);
3070 int f
; /* file number */
3072 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3079 f
= -1; /* JF we iz paranoid! */
3081 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3084 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
3086 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
3089 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3090 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3091 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3093 pfile
->import_warning
= 1;
3094 cpp_warning (pfile
, "using `#import' is not recommended");
3095 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3096 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3097 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3098 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3099 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3100 fprintf (stderr
, " ... <real contents of file> ...\n");
3101 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3102 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3103 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3104 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3107 pfile
->parsing_include_directive
++;
3108 token
= get_directive_token (pfile
);
3109 pfile
->parsing_include_directive
--;
3111 if (token
== CPP_STRING
)
3113 /* FIXME - check no trailing garbage */
3114 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3115 fend
= CPP_PWRITTEN (pfile
) - 1;
3116 if (fbeg
[-1] == '<')
3119 /* If -I-, start with the first -I dir after the -I-. */
3120 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3121 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3123 /* If -I- was specified, don't search current dir, only spec'd ones. */
3124 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3126 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3127 /* We have "filename". Figure out directory this source
3128 file is coming from and put it on the front of the list. */
3130 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3135 if ((nam
= fp
->nominal_fname
) != NULL
)
3137 /* Found a named file. Figure out dir of the file,
3138 and put it in front of the search list. */
3139 dsp
[0].next
= search_start
;
3142 ep
= rindex (nam
, '/');
3144 ep
= rindex (nam
, ']');
3145 if (ep
== NULL
) ep
= rindex (nam
, '>');
3146 if (ep
== NULL
) ep
= rindex (nam
, ':');
3147 if (ep
!= NULL
) ep
++;
3152 dsp
[0].fname
= (char *) alloca (n
+ 1);
3153 strncpy (dsp
[0].fname
, nam
, n
);
3154 dsp
[0].fname
[n
] = '\0';
3155 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3156 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3160 dsp
[0].fname
= 0; /* Current directory */
3162 dsp
[0].got_name_map
= 0;
3169 else if (token
== CPP_NAME
)
3172 * Support '#include xyz' like VAX-C to allow for easy use of all the
3173 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3174 * code from case '<' is repeated here) and generates a warning.
3177 "VAX-C-style include specification found, use '#include <filename.h>' !");
3179 /* If -I-, start with the first -I dir after the -I-. */
3180 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3181 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3182 fbeg
= pfile
->token_buffer
+ old_written
;
3183 fend
= CPP_PWRITTEN (pfile
);
3189 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3190 CPP_SET_WRITTEN (pfile
, old_written
);
3191 skip_rest_of_line (pfile
);
3197 token
= get_directive_token (pfile
);
3198 if (token
!= CPP_VSPACE
)
3200 cpp_error (pfile
, "junk at end of `#include'");
3201 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3202 token
= get_directive_token (pfile
);
3205 /* For #include_next, skip in the search path
3206 past the dir in which the containing file was found. */
3209 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3210 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3211 if (fp
->fname
!= NULL
)
3213 /* fp->dir is null if the containing file was specified with
3214 an absolute file name. In that case, don't skip anything. */
3215 if (fp
->dir
== SELF_DIR_DUMMY
)
3216 search_start
= CPP_OPTIONS (pfile
)->include
;
3218 search_start
= fp
->dir
->next
;
3223 CPP_SET_WRITTEN (pfile
, old_written
);
3229 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3233 /* Allocate this permanently, because it gets stored in the definitions
3235 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3236 /* + 2 above for slash and terminating null. */
3237 /* + 2 added for '.h' on VMS (to support '#include filename') */
3239 /* If specified file name is absolute, just open it. */
3242 strncpy (fname
, fbeg
, flen
);
3244 if (redundant_include_p (pfile
, fname
))
3247 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3249 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3251 return 0; /* Already included this file */
3253 /* Search directory path, trying to open the file.
3254 Copy each filename tried into FNAME. */
3256 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3257 if (searchptr
->fname
) {
3258 /* The empty string in a search path is ignored.
3259 This makes it possible to turn off entirely
3260 a standard piece of the list. */
3261 if (searchptr
->fname
[0] == 0)
3263 strcpy (fname
, searchptr
->fname
);
3264 strcat (fname
, "/");
3265 fname
[strlen (fname
) + flen
] = 0;
3269 strncat (fname
, fbeg
, flen
);
3271 /* Change this 1/2 Unix 1/2 VMS file specification into a
3272 full VMS file specification */
3273 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3274 /* Fix up the filename */
3275 hack_vms_include_specification (fname
);
3277 /* This is a normal VMS filespec, so use it unchanged. */
3278 strncpy (fname
, fbeg
, flen
);
3280 /* if it's '#include filename', add the missing .h */
3281 if (index(fname
,'.')==NULL
) {
3282 strcat (fname
, ".h");
3286 /* ??? There are currently 3 separate mechanisms for avoiding processing
3287 of redundant include files: #import, #pragma once, and
3288 redundant_include_p. It would be nice if they were unified. */
3289 if (redundant_include_p (pfile
, fname
))
3292 f
= lookup_import (pfile
, fname
, searchptr
);
3294 f
= open_include_file (pfile
, fname
, searchptr
);
3296 return 0; /* Already included this file */
3298 else if (f
== -1 && errno
== EACCES
)
3299 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3309 /* A file that was not found. */
3310 strncpy (fname
, fbeg
, flen
);
3312 /* If generating dependencies and -MG was specified, we assume missing
3313 files are leaf files, living in the same directory as the source file
3314 or other similar place; these missing files may be generated from
3315 other files and may not exist yet (eg: y.tab.h). */
3317 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3318 && CPP_PRINT_DEPS (pfile
)
3319 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3321 /* If it was requested as a system header file,
3322 then assume it belongs in the first place to look for such. */
3325 for (searchptr
= search_start
; searchptr
;
3326 searchptr
= searchptr
->next
)
3328 if (searchptr
->fname
)
3332 if (searchptr
->fname
[0] == 0)
3334 p
= (char *) alloca (strlen (searchptr
->fname
)
3335 + strlen (fname
) + 2);
3336 strcpy (p
, searchptr
->fname
);
3339 deps_output (pfile
, p
, ' ');
3346 /* Otherwise, omit the directory, as if the file existed
3347 in the directory with the source. */
3348 deps_output (pfile
, fname
, ' ');
3351 /* If -M was specified, and this header file won't be added to the
3352 dependency list, then don't count this as an error, because we can
3353 still produce correct output. Otherwise, we can't produce correct
3354 output, because there may be dependencies we need inside the missing
3355 file, and we don't know what directory this missing file exists in.*/
3356 else if (CPP_PRINT_DEPS (pfile
)
3357 && (CPP_PRINT_DEPS (pfile
)
3358 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3359 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3360 else if (search_start
)
3361 cpp_error_from_errno (pfile
, fname
);
3363 cpp_error (pfile
, "No include path in which to find %s", fname
);
3366 /* Check to see if this include file is a once-only include file.
3369 struct file_name_list
*ptr
;
3371 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3372 if (!strcmp (ptr
->fname
, fname
)) {
3374 return 0; /* This file was once'd. */
3378 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3379 if (!strcmp (ptr
->fname
, fname
))
3380 break; /* This file was included before. */
3384 /* This is the first time for this file. */
3385 /* Add it to list of files included. */
3387 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3388 ptr
->control_macro
= 0;
3389 ptr
->c_system_include_path
= 0;
3390 ptr
->next
= pfile
->all_include_files
;
3391 pfile
->all_include_files
= ptr
;
3392 ptr
->fname
= savestring (fname
);
3393 ptr
->got_name_map
= 0;
3395 /* For -M, add this file to the dependencies. */
3396 if (CPP_PRINT_DEPS (pfile
)
3397 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3398 deps_output (pfile
, fname
, ' ');
3401 /* Handle -H option. */
3402 if (CPP_OPTIONS(pfile
)->print_include_names
)
3404 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3405 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3407 fprintf (stderr
, "%s\n", fname
);
3411 pfile
->system_include_depth
++;
3413 /* Actually process the file. */
3415 /* Record file on "seen" list for #import. */
3416 add_import (pfile
, f
, fname
);
3418 pcftry
= (char *) alloca (strlen (fname
) + 30);
3430 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3432 pcf
= open (pcftry
, O_RDONLY
, 0666);
3438 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3440 || stat_f
.st_dev
!= s
.st_dev
)
3442 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3443 /* Don't need it any more. */
3448 /* Don't need it at all. */
3453 } while (pcf
!= -1 && !pcfbuf
);
3457 /* Actually process the file */
3458 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3460 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3461 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3463 output_line_command (pfile
, 0, enter_file
);
3464 pfile
->only_seen_white
= 2;
3468 pfile
->system_include_depth
--;
3473 /* Return nonzero if there is no need to include file NAME
3474 because it has already been included and it contains a conditional
3475 to make a repeated include do nothing. */
3478 redundant_include_p (pfile
, name
)
3482 struct file_name_list
*l
= pfile
->all_include_files
;
3483 for (; l
; l
= l
->next
)
3484 if (! strcmp (name
, l
->fname
)
3486 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3491 /* Return nonzero if the given FILENAME is an absolute pathname which
3492 designates a file within one of the known "system" include file
3493 directories. We assume here that if the given FILENAME looks like
3494 it is the name of a file which resides either directly in a "system"
3495 include file directory, or within any subdirectory thereof, then the
3496 given file must be a "system" include file. This function tells us
3497 if we should suppress pedantic errors/warnings for the given FILENAME.
3499 The value is 2 if the file is a C-language system header file
3500 for which C++ should (on most systems) assume `extern "C"'. */
3503 is_system_include (pfile
, filename
)
3505 register char *filename
;
3507 struct file_name_list
*searchptr
;
3509 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3510 searchptr
= searchptr
->next
)
3511 if (searchptr
->fname
) {
3512 register char *sys_dir
= searchptr
->fname
;
3513 register unsigned length
= strlen (sys_dir
);
3515 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3517 if (searchptr
->c_system_include_path
)
3528 * Install a name in the assertion hash table.
3530 * If LEN is >= 0, it is the length of the name.
3531 * Otherwise, compute the length by scanning the entire name.
3533 * If HASH is >= 0, it is the precomputed hash code.
3534 * Otherwise, compute the hash code.
3537 static ASSERTION_HASHNODE
*
3538 assertion_install (pfile
, name
, len
, hash
)
3544 register ASSERTION_HASHNODE
*hp
;
3545 register int i
, bucket
;
3546 register U_CHAR
*p
, *q
;
3548 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3549 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3551 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3552 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3553 pfile
->assertion_hashtab
[bucket
] = hp
;
3555 if (hp
->next
!= NULL
)
3556 hp
->next
->prev
= hp
;
3559 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3562 for (i
= 0; i
< len
; i
++)
3568 * find the most recent hash node for name "name" (ending with first
3569 * non-identifier char) installed by install
3571 * If LEN is >= 0, it is the length of the name.
3572 * Otherwise, compute the length by scanning the entire name.
3574 * If HASH is >= 0, it is the precomputed hash code.
3575 * Otherwise, compute the hash code.
3578 static ASSERTION_HASHNODE
*
3579 assertion_lookup (pfile
, name
, len
, hash
)
3585 register ASSERTION_HASHNODE
*bucket
;
3587 bucket
= pfile
->assertion_hashtab
[hash
];
3589 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3591 bucket
= bucket
->next
;
3597 delete_assertion (hp
)
3598 ASSERTION_HASHNODE
*hp
;
3600 struct tokenlist_list
*tail
;
3601 if (hp
->prev
!= NULL
)
3602 hp
->prev
->next
= hp
->next
;
3603 if (hp
->next
!= NULL
)
3604 hp
->next
->prev
= hp
->prev
;
3606 for (tail
= hp
->value
; tail
; )
3608 struct tokenlist_list
*next
= tail
->next
;
3609 free_token_list (tail
->tokens
);
3614 /* Make sure that the bucket chain header that
3615 the deleted guy was on points to the right thing afterwards. */
3616 if (hp
== *hp
->bucket_hdr
)
3617 *hp
->bucket_hdr
= hp
->next
;
3622 /* Convert a character string literal into a nul-terminated string.
3623 The input string is [IN ... LIMIT).
3624 The result is placed in RESULT. RESULT can be the same as IN.
3625 The value returned in the end of the string written to RESULT,
3626 or NULL on error. */
3629 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3631 register U_CHAR
*result
, *in
, *limit
;
3651 char *bpc
= (char *) in
;
3652 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3653 in
= (U_CHAR
*) bpc
;
3655 *result
++ = (U_CHAR
)c
;
3658 /* else fall through */
3668 * interpret #line command. Remembers previously seen fnames
3669 * in its very own hash table.
3671 #define FNAME_HASHSIZE 37
3674 do_line (pfile
, keyword
)
3676 struct directive
*keyword
;
3678 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3680 long old_written
= CPP_WRITTEN (pfile
);
3681 enum file_change_code file_change
= same_file
;
3682 enum cpp_token token
;
3684 token
= get_directive_token (pfile
);
3686 if (token
!= CPP_NUMBER
3687 || !ISDIGIT(pfile
->token_buffer
[old_written
]))
3689 cpp_error (pfile
, "invalid format `#line' command");
3690 goto bad_line_directive
;
3693 /* The Newline at the end of this line remains to be processed.
3694 To put the next line at the specified line number,
3695 we must store a line number now that is one less. */
3696 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3697 CPP_SET_WRITTEN (pfile
, old_written
);
3699 /* NEW_LINENO is one less than the actual line number here. */
3700 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3701 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3703 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3704 if (PEEKC() && !is_space
[PEEKC()]) {
3705 cpp_error (pfile
, "invalid format `#line' command");
3706 goto bad_line_directive
;
3710 token
= get_directive_token (pfile
);
3712 if (token
== CPP_STRING
) {
3713 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3715 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3716 HASHNODE
*hp
, **hash_bucket
;
3721 /* Turn the file name, which is a character string literal,
3722 into a null-terminated string. Do this in place. */
3723 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3724 if (end_name
== NULL
)
3726 cpp_error (pfile
, "invalid format `#line' command");
3727 goto bad_line_directive
;
3730 fname_length
= end_name
- fname
;
3732 num_start
= CPP_WRITTEN (pfile
);
3733 token
= get_directive_token (pfile
);
3734 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3735 p
= pfile
->token_buffer
+ num_start
;
3736 if (CPP_PEDANTIC (pfile
))
3737 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3739 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3741 cpp_error (pfile
, "invalid format `#line' command");
3742 goto bad_line_directive
;
3745 file_change
= enter_file
;
3747 file_change
= leave_file
;
3749 ip
->system_header_p
= 1;
3750 else /* if (*p == 4) */
3751 ip
->system_header_p
= 2;
3753 CPP_SET_WRITTEN (pfile
, num_start
);
3754 token
= get_directive_token (pfile
);
3755 p
= pfile
->token_buffer
+ num_start
;
3756 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3757 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3758 token
= get_directive_token (pfile
);
3760 if (token
!= CPP_VSPACE
) {
3761 cpp_error (pfile
, "invalid format `#line' command");
3762 goto bad_line_directive
;
3766 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3767 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3768 if (hp
->length
== fname_length
3769 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3770 ip
->nominal_fname
= hp
->value
.cpval
;
3774 /* Didn't find it; cons up a new one. */
3775 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3776 hp
->next
= *hash_bucket
;
3779 hp
->length
= fname_length
;
3780 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3781 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3784 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3785 cpp_error (pfile
, "invalid format `#line' command");
3786 goto bad_line_directive
;
3789 ip
->lineno
= new_lineno
;
3791 skip_rest_of_line (pfile
);
3792 CPP_SET_WRITTEN (pfile
, old_written
);
3793 output_line_command (pfile
, 0, file_change
);
3798 * remove the definition of a symbol from the symbol table.
3799 * according to un*x /lib/cpp, it is not an error to undef
3800 * something that has no definitions, so it isn't one here either.
3804 do_undef (pfile
, keyword
, buf
, limit
)
3806 struct directive
*keyword
;
3807 U_CHAR
*buf
, *limit
;
3811 U_CHAR
*orig_buf
= buf
;
3814 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3815 if (pcp_outfile
&& keyword
)
3816 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3819 SKIP_WHITE_SPACE (buf
);
3820 sym_length
= check_macro_name (pfile
, buf
, "macro");
3822 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3824 /* If we are generating additional info for debugging (with -g) we
3825 need to pass through all effective #undef commands. */
3826 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3827 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3828 if (hp
->type
!= T_MACRO
)
3829 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3833 if (CPP_PEDANTIC (pfile
)) {
3835 SKIP_WHITE_SPACE (buf
);
3837 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3843 * Report an error detected by the program we are processing.
3844 * Use the text of the line in the error message.
3845 * (We use error because it prints the filename & line#.)
3849 do_error (pfile
, keyword
, buf
, limit
)
3851 struct directive
*keyword
;
3852 U_CHAR
*buf
, *limit
;
3854 int length
= limit
- buf
;
3855 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3856 bcopy (buf
, copy
, length
);
3858 SKIP_WHITE_SPACE (copy
);
3859 cpp_error (pfile
, "#error %s", copy
);
3864 * Report a warning detected by the program we are processing.
3865 * Use the text of the line in the warning message, then continue.
3866 * (We use error because it prints the filename & line#.)
3870 do_warning (pfile
, keyword
, buf
, limit
)
3872 struct directive
*keyword
;
3873 U_CHAR
*buf
, *limit
;
3875 int length
= limit
- buf
;
3876 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3877 bcopy (buf
, copy
, length
);
3879 SKIP_WHITE_SPACE (copy
);
3881 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3882 cpp_pedwarn ("ANSI C does not allow `#warning'");
3884 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3885 if -pedantic-errors is given, #warning should cause an error. */
3886 cpp_pedwarn (pfile
, "#warning %s", copy
);
3890 /* Remember the name of the current file being read from so that we can
3891 avoid ever including it again. */
3897 cpp_buffer
*ip
= NULL
;
3898 struct file_name_list
*new;
3900 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3902 if (ip
== CPP_NULL_BUFFER (pfile
))
3904 if (ip
->fname
!= NULL
)
3909 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3910 new->next
= pfile
->dont_repeat_files
;
3911 pfile
->dont_repeat_files
= new;
3912 new->fname
= savestring (ip
->fname
);
3913 new->control_macro
= 0;
3914 new->got_name_map
= 0;
3915 new->c_system_include_path
= 0;
3920 /* Report program identification. */
3923 do_ident (pfile
, keyword
, buf
, limit
)
3925 struct directive
*keyword
;
3926 U_CHAR
*buf
, *limit
;
3928 /* long old_written = CPP_WRITTEN (pfile);*/
3930 /* Allow #ident in system headers, since that's not user's fault. */
3931 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3932 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3934 /* Leave rest of line to be read by later calls to cpp_get_token. */
3939 /* #pragma and its argument line have already been copied to the output file.
3940 Just check for some recognized pragmas that need validation here. */
3943 do_pragma (pfile
, keyword
, buf
, limit
)
3945 struct directive
*keyword
;
3946 U_CHAR
*buf
, *limit
;
3948 while (*buf
== ' ' || *buf
== '\t')
3950 if (!strncmp (buf
, "once", 4)) {
3951 /* Allow #pragma once in system headers, since that's not the user's
3953 if (!CPP_BUFFER (pfile
)->system_header_p
)
3954 cpp_warning (pfile
, "`#pragma once' is obsolete");
3958 if (!strncmp (buf
, "implementation", 14)) {
3959 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3960 been included yet. */
3961 struct file_name_list
*ptr
;
3962 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
3964 SKIP_WHITE_SPACE (p
);
3965 if (*p
== '\n' || *p
!= '\"')
3969 p
= (U_CHAR
*) index (fname
, '\"');
3970 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
3972 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3973 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
3974 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
3975 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
3977 "`#pragma implementation' for `%s' appears after file is included",
3986 /* This was a fun hack, but #pragma seems to start to be useful.
3987 By failing to recognize it, we pass it through unchanged to cc1. */
3990 * the behavior of the #pragma directive is implementation defined.
3991 * this implementation defines it as follows.
3998 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
4001 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4003 execl ("/usr/games/hack", "#pragma", 0);
4004 execl ("/usr/games/rogue", "#pragma", 0);
4005 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4006 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4008 fatal ("You are in a maze of twisty compiler features, all different");
4012 #ifdef SCCS_DIRECTIVE
4013 /* Just ignore #sccs, on systems where we define it at all. */
4016 do_sccs (pfile
, keyword
, buf
, limit
)
4018 struct directive
*keyword
;
4019 U_CHAR
*buf
, *limit
;
4021 if (CPP_PEDANTIC (pfile
))
4022 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4028 * handle #if command by
4029 * 1) inserting special `defined' keyword into the hash table
4030 * that gets turned into 0 or 1 by special_symbol (thus,
4031 * if the luser has a symbol called `defined' already, it won't
4032 * work inside the #if command)
4033 * 2) rescan the input into a temporary output buffer
4034 * 3) pass the output buffer to the yacc parser and collect a value
4035 * 4) clean up the mess left from steps 1 and 2.
4036 * 5) call conditional_skip to skip til the next #endif (etc.),
4037 * or not, depending on the value from step 3.
4041 do_if (pfile
, keyword
, buf
, limit
)
4043 struct directive
*keyword
;
4044 U_CHAR
*buf
, *limit
;
4046 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4047 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4052 * handle a #elif directive by not changing if_stack either.
4053 * see the comment above do_else.
4057 do_elif (pfile
, keyword
, buf
, limit
)
4059 struct directive
*keyword
;
4060 U_CHAR
*buf
, *limit
;
4062 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4063 cpp_error (pfile
, "`#elif' not within a conditional");
4066 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4067 cpp_error (pfile
, "`#elif' after `#else'");
4069 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4071 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4072 && strcmp (pfile
->if_stack
->fname
,
4073 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4074 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4075 fprintf (stderr
, ")\n");
4077 pfile
->if_stack
->type
= T_ELIF
;
4080 if (pfile
->if_stack
->if_succeeded
)
4081 skip_if_group (pfile
, 0);
4083 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4085 skip_if_group (pfile
, 0);
4087 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4088 output_line_command (pfile
, 1, same_file
);
4095 * evaluate a #if expression in BUF, of length LENGTH,
4096 * then parse the result as a C expression and return the value as an int.
4099 static HOST_WIDE_INT
4100 eval_if_expression (pfile
, buf
, length
)
4105 HASHNODE
*save_defined
;
4106 HOST_WIDE_INT value
;
4107 long old_written
= CPP_WRITTEN (pfile
);
4109 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4110 pfile
->pcp_inside_if
= 1;
4112 value
= cpp_parse_expr (pfile
);
4113 pfile
->pcp_inside_if
= 0;
4114 delete_macro (save_defined
); /* clean up special symbol */
4116 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4122 * routine to handle ifdef/ifndef. Try to look up the symbol,
4123 * then do or don't skip to the #endif/#else/#elif depending
4124 * on what directive is actually being processed.
4128 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4130 struct directive
*keyword
;
4131 U_CHAR
*unused1
, *unused2
;
4134 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4137 enum cpp_token token
;
4138 int start_of_file
= 0;
4139 U_CHAR
*control_macro
= 0;
4140 int old_written
= CPP_WRITTEN (pfile
);
4142 /* Detect a #ifndef at start of file (not counting comments). */
4143 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4144 start_of_file
= pfile
->only_seen_white
== 2;
4146 pfile
->no_macro_expand
++;
4147 token
= get_directive_token (pfile
);
4148 pfile
->no_macro_expand
--;
4150 ident
= pfile
->token_buffer
+ old_written
;
4151 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4152 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4154 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4156 skip
= (keyword
->type
== T_IFDEF
);
4157 if (! CPP_TRADITIONAL (pfile
))
4158 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4160 else if (token
== CPP_NAME
)
4162 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4163 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4164 if (start_of_file
&& !skip
)
4166 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4167 bcopy (ident
, control_macro
, ident_length
+ 1);
4172 skip
= (keyword
->type
== T_IFDEF
);
4173 if (! CPP_TRADITIONAL (pfile
))
4174 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4177 if (!CPP_TRADITIONAL (pfile
))
4179 cpp_skip_hspace (pfile
);
4181 if (c
!= EOF
&& c
!= '\n')
4182 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4184 skip_rest_of_line (pfile
);
4188 /* Output a precondition for this macro. */
4189 if (hp
&& hp
->value
.defn
->predefined
)
4190 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4193 fprintf (pcp_outfile
, "#undef ");
4194 while (is_idchar
[*cp
]) /* Ick! */
4195 fputc (*cp
++, pcp_outfile
);
4196 putc ('\n', pcp_outfile
);
4200 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4204 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4205 If this is a #ifndef starting at the beginning of a file,
4206 CONTROL_MACRO is the macro name tested by the #ifndef.
4207 Otherwise, CONTROL_MACRO is 0. */
4210 conditional_skip (pfile
, skip
, type
, control_macro
)
4213 enum node_type type
;
4214 U_CHAR
*control_macro
;
4216 IF_STACK_FRAME
*temp
;
4218 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4219 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4221 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4223 temp
->next
= pfile
->if_stack
;
4224 temp
->control_macro
= control_macro
;
4225 pfile
->if_stack
= temp
;
4227 pfile
->if_stack
->type
= type
;
4230 skip_if_group (pfile
, 0);
4233 ++pfile
->if_stack
->if_succeeded
;
4234 output_line_command (pfile
, 1, same_file
);
4239 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4240 * leaves input ptr at the sharp sign found.
4241 * If ANY is nonzero, return at next directive of any sort.
4245 skip_if_group (pfile
, any
)
4250 struct directive
*kt
;
4251 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4253 U_CHAR
*beg_of_line
= bp
;
4255 register int ident_length
;
4257 struct parse_marker line_start_mark
;
4259 parse_set_mark (&line_start_mark
, pfile
);
4261 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4262 static char failed
[] = "#failed\n";
4263 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4265 output_line_command (pfile
, 1, same_file
);
4269 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4271 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4272 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4273 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4275 parse_move_mark (&line_start_mark
, pfile
);
4276 if (!CPP_TRADITIONAL (pfile
))
4277 cpp_skip_hspace (pfile
);
4281 int old_written
= CPP_WRITTEN (pfile
);
4282 cpp_skip_hspace (pfile
);
4284 parse_name (pfile
, GETC());
4285 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4286 ident
= pfile
->token_buffer
+ old_written
;
4287 pfile
->limit
= ident
;
4289 if (ident_length
== 0)
4290 goto not_a_directive
;
4292 /* Handle # followed by a line number. */
4294 /* Avoid error for `###' and similar cases unless -pedantic. */
4297 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4299 IF_STACK_FRAME
*temp
;
4300 if (ident_length
== kt
->length
4301 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4303 /* If we are asked to return on next directive, do so now. */
4313 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4314 temp
->next
= pfile
->if_stack
;
4315 pfile
->if_stack
= temp
;
4317 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4319 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4320 temp
->type
= kt
->type
;
4324 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4325 validate_else (pfile
,
4326 kt
->type
== T_ELSE
? "#else" : "#endif");
4328 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4331 "`#%s' not within a conditional", kt
->name
);
4334 else if (pfile
->if_stack
== save_if_stack
)
4335 goto done
; /* found what we came for */
4337 if (kt
->type
!= T_ENDIF
)
4339 if (pfile
->if_stack
->type
== T_ELSE
)
4340 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4341 pfile
->if_stack
->type
= kt
->type
;
4345 temp
= pfile
->if_stack
;
4346 pfile
->if_stack
= temp
->next
;
4353 /* Don't let erroneous code go by. */
4354 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4355 && CPP_PEDANTIC (pfile
))
4356 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4360 /* We're in the middle of a line. Skip the rest of it. */
4367 case '/': /* possible comment */
4368 c
= skip_comment (pfile
, NULL
);
4375 old
= CPP_WRITTEN (pfile
);
4376 cpp_get_token (pfile
);
4377 CPP_SET_WRITTEN (pfile
, old
);
4380 /* Char after backslash loses its special meaning. */
4381 if (PEEKC() == '\n')
4391 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4392 static char end_failed
[] = "#endfailed\n";
4393 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4396 pfile
->only_seen_white
= 1;
4397 parse_goto_mark (&line_start_mark
, pfile
);
4398 parse_clear_mark (&line_start_mark
);
4402 * handle a #else directive. Do this by just continuing processing
4403 * without changing if_stack ; this is so that the error message
4404 * for missing #endif's etc. will point to the original #if. It
4405 * is possible that something different would be better.
4409 do_else (pfile
, keyword
, buf
, limit
)
4411 struct directive
*keyword
;
4412 U_CHAR
*buf
, *limit
;
4414 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4416 if (CPP_PEDANTIC (pfile
))
4417 validate_else (pfile
, "#else");
4418 skip_rest_of_line (pfile
);
4420 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4421 cpp_error (pfile
, "`#else' not within a conditional");
4424 /* #ifndef can't have its special treatment for containing the whole file
4425 if it has a #else clause. */
4426 pfile
->if_stack
->control_macro
= 0;
4428 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4429 cpp_error (pfile
, "`#else' after `#else'");
4430 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4431 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4432 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4433 fprintf (stderr
, ")\n");
4435 pfile
->if_stack
->type
= T_ELSE
;
4438 if (pfile
->if_stack
->if_succeeded
)
4439 skip_if_group (pfile
, 0);
4441 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4442 output_line_command (pfile
, 1, same_file
);
4448 * unstack after #endif command
4452 do_endif (pfile
, keyword
, buf
, limit
)
4454 struct directive
*keyword
;
4455 U_CHAR
*buf
, *limit
;
4457 if (CPP_PEDANTIC (pfile
))
4458 validate_else (pfile
, "#endif");
4459 skip_rest_of_line (pfile
);
4461 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4462 cpp_error (pfile
, "unbalanced `#endif'");
4465 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4466 pfile
->if_stack
= temp
->next
;
4467 if (temp
->control_macro
!= 0)
4469 /* This #endif matched a #ifndef at the start of the file.
4470 See if it is at the end of the file. */
4471 struct parse_marker start_mark
;
4474 parse_set_mark (&start_mark
, pfile
);
4478 cpp_skip_hspace (pfile
);
4483 parse_goto_mark (&start_mark
, pfile
);
4484 parse_clear_mark (&start_mark
);
4488 /* If we get here, this #endif ends a #ifndef
4489 that contains all of the file (aside from whitespace).
4490 Arrange not to include the file again
4491 if the macro that was tested is defined.
4493 Do not do this for the top-level file in a -include or any
4494 file in a -imacros. */
4498 && ! (indepth
== 1 && pfile
->no_record_file
)
4499 && ! (pfile
->no_record_file
&& no_output
))
4502 struct file_name_list
*ifile
= pfile
->all_include_files
;
4504 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4506 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4508 ifile
->control_macro
= temp
->control_macro
;
4516 output_line_command (pfile
, 1, same_file
);
4521 /* When an #else or #endif is found while skipping failed conditional,
4522 if -pedantic was specified, this is called to warn about text after
4523 the command name. P points to the first char after the command name. */
4526 validate_else (pfile
, directive
)
4531 cpp_skip_hspace (pfile
);
4533 if (c
!= EOF
&& c
!= '\n')
4535 "text following `%s' violates ANSI standard", directive
);
4538 /* Get the next token, and add it to the text in pfile->token_buffer.
4539 Return the kind of token we got. */
4542 cpp_get_token (pfile
)
4545 register int c
, c2
, c3
;
4547 long start_line
, start_column
;
4548 enum cpp_token token
;
4549 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4550 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4556 if (CPP_BUFFER (pfile
)->seen_eof
)
4558 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4565 cpp_buffer
*next_buf
4566 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4567 CPP_BUFFER (pfile
)->seen_eof
= 1;
4568 if (CPP_BUFFER (pfile
)->nominal_fname
4569 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4571 /* We're about to return from an #include file.
4572 Emit #line information now (as part of the CPP_POP) result.
4573 But the #line refers to the file we will pop to. */
4574 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4575 CPP_BUFFER (pfile
) = next_buf
;
4576 pfile
->input_stack_listing_current
= 0;
4577 output_line_command (pfile
, 0, leave_file
);
4578 CPP_BUFFER (pfile
) = cur_buffer
;
4588 struct parse_marker start_mark
;
4590 if (PEEKC () == '=')
4592 if (opts
->put_out_comments
)
4593 parse_set_mark (&start_mark
, pfile
);
4595 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4596 &start_line
, &start_column
);
4597 c
= skip_comment (pfile
, &newlines
);
4598 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4599 parse_clear_mark (&start_mark
);
4604 cpp_error_with_line (pfile
, start_line
, start_column
,
4605 "unterminated comment");
4608 c
= '/'; /* Initial letter of comment. */
4610 /* Comments are equivalent to spaces.
4611 For -traditional, a comment is equivalent to nothing. */
4612 if (opts
->put_out_comments
)
4614 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4615 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4616 int len
= pbuf
->cur
- start
;
4617 CPP_RESERVE(pfile
, 1 + len
);
4618 CPP_PUTC_Q (pfile
, c
);
4619 CPP_PUTS_Q (pfile
, start
, len
);
4620 pfile
->lineno
+= newlines
;
4621 parse_clear_mark (&start_mark
);
4624 else if (CPP_TRADITIONAL (pfile
))
4631 /* This may not work if cpp_get_token is called recursively,
4632 since many places look for horizontal space. */
4635 /* Copy the newlines into the output buffer, in order to
4636 avoid the pain of a #line every time a multiline comment
4638 CPP_RESERVE(pfile
, newlines
);
4639 while (--newlines
>= 0)
4641 CPP_PUTC_Q (pfile
, '\n');
4647 CPP_RESERVE(pfile
, 1);
4648 CPP_PUTC_Q (pfile
, ' ');
4652 if (opts
->for_lint
) {
4655 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4657 if (lintcmd
!= NULL
) {
4658 /* I believe it is always safe to emit this newline: */
4660 bcopy ("#pragma lint ", (char *) obp
, 13);
4662 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4667 bcopy (argbp
, (char *) obp
, arglen
);
4671 /* OK, now bring us back to the state we were in before we entered
4672 this branch. We need #line because the newline for the pragma
4673 could mess things up. */
4674 output_line_command (pfile
, 0, same_file
);
4675 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4683 /* If this is expanding a macro definition, don't recognize
4684 preprocessor directives. */
4687 /* If this is expand_into_temp_buffer, recognize them
4688 only after an actual newline at this level,
4689 not at the beginning of the input level. */
4690 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4696 if (!pfile
->only_seen_white
)
4698 if (handle_directive (pfile
))
4699 return CPP_DIRECTIVE
;
4700 pfile
->only_seen_white
= 0;
4705 /* A single quoted string is treated like a double -- some
4706 programs (e.g., troff) are perverse this way */
4707 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4708 &start_line
, &start_column
);
4709 old_written
= CPP_WRITTEN (pfile
);
4711 CPP_PUTC (pfile
, c
);
4717 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4719 /* try harder: this string crosses a macro expansion
4720 boundary. This can happen naturally if -traditional.
4721 Otherwise, only -D can make a macro with an unmatched
4723 cpp_buffer
*next_buf
4724 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4725 (*CPP_BUFFER (pfile
)->cleanup
)
4726 (CPP_BUFFER (pfile
), pfile
);
4727 CPP_BUFFER (pfile
) = next_buf
;
4730 if (!CPP_TRADITIONAL (pfile
))
4732 cpp_error_with_line (pfile
, start_line
, start_column
,
4733 "unterminated string or character constant");
4734 if (pfile
->multiline_string_line
!= start_line
4735 && pfile
->multiline_string_line
!= 0)
4736 cpp_error_with_line (pfile
,
4737 pfile
->multiline_string_line
, -1,
4738 "possible real start of unterminated constant");
4739 pfile
->multiline_string_line
= 0;
4743 CPP_PUTC (pfile
, cc
);
4747 /* Traditionally, end of line ends a string constant with
4748 no error. So exit the loop and record the new line. */
4749 if (CPP_TRADITIONAL (pfile
))
4753 cpp_error_with_line (pfile
, start_line
, start_column
,
4754 "unterminated character constant");
4757 if (CPP_PEDANTIC (pfile
)
4758 && pfile
->multiline_string_line
== 0)
4760 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4761 "string constant runs past end of line");
4763 if (pfile
->multiline_string_line
== 0)
4764 pfile
->multiline_string_line
= start_line
;
4771 /* Backslash newline is replaced by nothing at all. */
4772 CPP_ADJUST_WRITTEN (pfile
, -1);
4777 /* ANSI stupidly requires that in \\ the second \
4778 is *not* prevented from combining with a newline. */
4781 CPP_PUTC (pfile
, cc
);
4793 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4794 CPP_PWRITTEN (pfile
));
4795 pfile
->only_seen_white
= 0;
4796 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4799 if (!opts
->dollars_in_ident
)
4804 if (opts
->cplusplus
&& PEEKC () == ':')
4813 if (c2
== c
|| c2
== '=')
4823 if (PEEKC () == '=')
4830 if (c2
== '-' && opts
->chill
)
4832 /* Chill style comment */
4833 if (opts
->put_out_comments
)
4834 parse_set_mark (&start_mark
, pfile
);
4835 FORWARD(1); /* Skip second '-'. */
4843 /* Don't consider final '\n' to be part of comment. */
4849 goto return_comment
;
4851 if (c2
== '-' || c2
== '=' || c2
== '>')
4856 if (pfile
->parsing_include_directive
)
4860 CPP_PUTC (pfile
, c
);
4865 if (c
== '\n' || c
== EOF
)
4868 "missing '>' in `#include <FILENAME>'");
4874 /* else fall through */
4883 CPP_RESERVE (pfile
, 4);
4884 CPP_PUTC (pfile
, c
);
4885 CPP_PUTC (pfile
, c2
);
4889 CPP_PUTC_Q (pfile
, GETC ());
4890 CPP_NUL_TERMINATE_Q (pfile
);
4891 pfile
->only_seen_white
= 0;
4895 if (CPP_BUFFER (pfile
)->has_escapes
)
4900 if (pfile
->output_escapes
)
4901 CPP_PUTS (pfile
, "@-", 2);
4902 parse_name (pfile
, GETC ());
4905 else if (is_space
[c
])
4907 CPP_RESERVE (pfile
, 2);
4908 if (pfile
->output_escapes
)
4909 CPP_PUTC_Q (pfile
, '@');
4910 CPP_PUTC_Q (pfile
, c
);
4914 if (pfile
->output_escapes
)
4916 CPP_PUTS (pfile
, "@@", 2);
4926 CPP_RESERVE(pfile
, 2);
4927 CPP_PUTC_Q (pfile
, '.');
4931 /* FIXME - misses the case "..\\\n." */
4932 if (c2
== '.' && PEEKN(1) == '.')
4934 CPP_RESERVE(pfile
, 4);
4935 CPP_PUTC_Q (pfile
, '.');
4936 CPP_PUTC_Q (pfile
, '.');
4937 CPP_PUTC_Q (pfile
, '.');
4939 CPP_NUL_TERMINATE_Q (pfile
);
4940 pfile
->only_seen_white
= 0;
4947 pfile
->only_seen_white
= 0;
4949 CPP_RESERVE(pfile
, 3);
4950 CPP_PUTC_Q (pfile
, c
);
4951 CPP_PUTC_Q (pfile
, GETC ());
4952 CPP_NUL_TERMINATE_Q (pfile
);
4958 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4960 CPP_PUTC (pfile
, c
);
4966 case '0': case '1': case '2': case '3': case '4':
4967 case '5': case '6': case '7': case '8': case '9':
4972 CPP_RESERVE (pfile
, 2);
4973 CPP_PUTC_Q (pfile
, c
);
4978 if (!is_idchar
[c
] && c
!= '.'
4979 && ((c2
!= 'e' && c2
!= 'E'
4980 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
4981 || (c
!= '+' && c
!= '-')))
4986 CPP_NUL_TERMINATE_Q (pfile
);
4987 pfile
->only_seen_white
= 0;
4989 case 'b': case 'c': case 'd': case 'h': case 'o':
4990 case 'B': case 'C': case 'D': case 'H': case 'O':
4991 if (opts
->chill
&& PEEKC () == '\'')
4993 pfile
->only_seen_white
= 0;
4994 CPP_RESERVE (pfile
, 2);
4995 CPP_PUTC_Q (pfile
, c
);
4996 CPP_PUTC_Q (pfile
, '\'');
5002 goto chill_number_eof
;
5005 if (c
== '\\' && PEEKC() == '\n')
5012 CPP_PUTC (pfile
, c
);
5016 CPP_RESERVE (pfile
, 2);
5017 CPP_PUTC_Q (pfile
, c
);
5018 CPP_NUL_TERMINATE_Q (pfile
);
5025 CPP_NUL_TERMINATE (pfile
);
5032 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5033 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5034 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5035 case 'x': case 'y': case 'z':
5036 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5037 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5038 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5043 unsigned char *ident
;
5044 int before_name_written
= CPP_WRITTEN (pfile
);
5046 parse_name (pfile
, c
);
5047 pfile
->only_seen_white
= 0;
5048 if (pfile
->no_macro_expand
)
5050 ident
= pfile
->token_buffer
+ before_name_written
;
5051 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5052 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5055 if (hp
->type
== T_DISABLED
)
5057 if (pfile
->output_escapes
)
5058 { /* Return "@-IDENT", followed by '\0'. */
5060 CPP_RESERVE (pfile
, 3);
5061 ident
= pfile
->token_buffer
+ before_name_written
;
5062 CPP_ADJUST_WRITTEN (pfile
, 2);
5063 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5070 /* If macro wants an arglist, verify that a '(' follows.
5071 first skip all whitespace, copying it to the output
5072 after the macro name. Then, if there is no '(',
5073 decide this is not a macro call and leave things that way. */
5074 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5076 struct parse_marker macro_mark
;
5078 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5080 cpp_buffer
*next_buf
;
5081 cpp_skip_hspace (pfile
);
5082 if (PEEKC () != EOF
)
5084 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5085 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5086 CPP_BUFFER (pfile
) = next_buf
;
5088 parse_set_mark (¯o_mark
, pfile
);
5091 cpp_skip_hspace (pfile
);
5093 is_macro_call
= c
== '(';
5099 parse_goto_mark (¯o_mark
, pfile
);
5100 parse_clear_mark (¯o_mark
);
5104 /* This is now known to be a macro call. */
5106 /* it might not actually be a macro. */
5107 if (hp
->type
!= T_MACRO
) {
5108 int xbuf_len
; U_CHAR
*xbuf
;
5109 CPP_SET_WRITTEN (pfile
, before_name_written
);
5110 special_symbol (hp
, pfile
);
5111 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5112 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5113 CPP_SET_WRITTEN (pfile
, before_name_written
);
5114 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5115 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5119 /* Expand the macro, reading arguments as needed,
5120 and push the expansion on the input stack. */
5121 macroexpand (pfile
, hp
);
5122 CPP_SET_WRITTEN (pfile
, before_name_written
);
5125 /* An extra "@ " is added to the end of a macro expansion
5126 to prevent accidental token pasting. We prefer to avoid
5127 unneeded extra spaces (for the sake of cpp-using tools like
5128 imake). Here we remove the space if it is safe to do so. */
5129 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5130 && pfile
->buffer
->rlimit
[-2] == '@'
5131 && pfile
->buffer
->rlimit
[-1] == ' ')
5133 int c1
= pfile
->buffer
->rlimit
[-3];
5134 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5135 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5136 pfile
->buffer
->rlimit
-= 2;
5141 case ' ': case '\t': case '\v': case '\r':
5144 CPP_PUTC (pfile
, c
);
5146 if (c
== EOF
|| !is_hor_space
[c
])
5160 CPP_PUTC (pfile
, c
);
5161 if (pfile
->only_seen_white
== 0)
5162 pfile
->only_seen_white
= 1;
5164 output_line_command (pfile
, 1, same_file
);
5167 case '(': token
= CPP_LPAREN
; goto char1
;
5168 case ')': token
= CPP_RPAREN
; goto char1
;
5169 case '{': token
= CPP_LBRACE
; goto char1
;
5170 case '}': token
= CPP_RBRACE
; goto char1
;
5171 case ',': token
= CPP_COMMA
; goto char1
;
5172 case ';': token
= CPP_SEMICOLON
; goto char1
;
5178 pfile
->only_seen_white
= 0;
5179 CPP_PUTC (pfile
, c
);
5185 /* Like cpp_get_token, but skip spaces and comments. */
5188 cpp_get_non_space_token (pfile
)
5191 int old_written
= CPP_WRITTEN (pfile
);
5194 enum cpp_token token
= cpp_get_token (pfile
);
5195 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5196 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5198 CPP_SET_WRITTEN (pfile
, old_written
);
5202 /* Parse an identifier starting with C. */
5205 parse_name (pfile
, c
)
5206 cpp_reader
*pfile
; int c
;
5212 if (c
== '\\' && PEEKC() == '\n')
5221 if (c
== '$' && CPP_PEDANTIC (pfile
))
5222 cpp_pedwarn ("`$' in identifier");
5224 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5225 CPP_PUTC_Q (pfile
, c
);
5230 CPP_NUL_TERMINATE_Q (pfile
);
5235 /* Maintain and search list of included files, for #import. */
5237 /* Hash a file name for import_hash_table. */
5245 while (*f
) val
+= *f
++;
5246 return (val
%IMPORT_HASH_SIZE
);
5249 /* Search for file FILENAME in import_hash_table.
5250 Return -2 if found, either a matching name or a matching inode.
5251 Otherwise, open the file and return a file descriptor if successful
5252 or -1 if unsuccessful. */
5255 lookup_import (pfile
, filename
, searchptr
)
5258 struct file_name_list
*searchptr
;
5260 struct import_file
*i
;
5266 hashval
= import_hash (filename
);
5268 /* Attempt to find file in list of already included files */
5269 i
= pfile
->import_hash_table
[hashval
];
5272 if (!strcmp (filename
, i
->name
))
5273 return -2; /* return found */
5276 /* Open it and try a match on inode/dev */
5277 fd
= open_include_file (pfile
, filename
, searchptr
);
5281 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5282 i
= pfile
->import_hash_table
[h
];
5284 /* Compare the inode and the device.
5285 Supposedly on some systems the inode is not a scalar. */
5286 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5287 && i
->dev
== sb
.st_dev
) {
5289 return -2; /* return found */
5294 return fd
; /* Not found, return open file */
5297 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5300 add_import (pfile
, fd
, fname
)
5305 struct import_file
*i
;
5309 hashval
= import_hash (fname
);
5311 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5312 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5313 strcpy (i
->name
, fname
);
5314 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5316 i
->next
= pfile
->import_hash_table
[hashval
];
5317 pfile
->import_hash_table
[hashval
] = i
;
5320 /* The file_name_map structure holds a mapping of file names for a
5321 particular directory. This mapping is read from the file named
5322 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5323 map filenames on a file system with severe filename restrictions,
5324 such as DOS. The format of the file name map file is just a series
5325 of lines with two tokens on each line. The first token is the name
5326 to map, and the second token is the actual name to use. */
5328 struct file_name_map
5330 struct file_name_map
*map_next
;
5335 #define FILE_NAME_MAP_FILE "header.gcc"
5337 /* Read a space delimited string of unlimited length from a stdio
5341 read_filename_string (ch
, f
)
5349 set
= alloc
= xmalloc (len
+ 1);
5353 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5355 if (set
- alloc
== len
)
5358 alloc
= xrealloc (alloc
, len
+ 1);
5359 set
= alloc
+ len
/ 2;
5369 /* This structure holds a linked list of file name maps, one per directory. */
5371 struct file_name_map_list
5373 struct file_name_map_list
*map_list_next
;
5374 char *map_list_name
;
5375 struct file_name_map
*map_list_map
;
5378 /* Read the file name map file for DIRNAME. */
5380 static struct file_name_map
*
5381 read_name_map (pfile
, dirname
)
5385 register struct file_name_map_list
*map_list_ptr
;
5389 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5390 map_list_ptr
= map_list_ptr
->map_list_next
)
5391 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5392 return map_list_ptr
->map_list_map
;
5394 map_list_ptr
= ((struct file_name_map_list
*)
5395 xmalloc (sizeof (struct file_name_map_list
)));
5396 map_list_ptr
->map_list_name
= savestring (dirname
);
5397 map_list_ptr
->map_list_map
= NULL
;
5399 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5400 strcpy (name
, dirname
);
5403 strcat (name
, FILE_NAME_MAP_FILE
);
5404 f
= fopen (name
, "r");
5406 map_list_ptr
->map_list_map
= NULL
;
5410 int dirlen
= strlen (dirname
);
5412 while ((ch
= getc (f
)) != EOF
)
5415 struct file_name_map
*ptr
;
5419 from
= read_filename_string (ch
, f
);
5420 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5422 to
= read_filename_string (ch
, f
);
5424 ptr
= ((struct file_name_map
*)
5425 xmalloc (sizeof (struct file_name_map
)));
5426 ptr
->map_from
= from
;
5428 /* Make the real filename absolute. */
5433 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5434 strcpy (ptr
->map_to
, dirname
);
5435 ptr
->map_to
[dirlen
] = '/';
5436 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5440 ptr
->map_next
= map_list_ptr
->map_list_map
;
5441 map_list_ptr
->map_list_map
= ptr
;
5443 while ((ch
= getc (f
)) != '\n')
5450 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5451 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5453 return map_list_ptr
->map_list_map
;
5456 /* Try to open include file FILENAME. SEARCHPTR is the directory
5457 being tried from the include file search path. This function maps
5458 filenames on file systems based on information read by
5462 open_include_file (pfile
, filename
, searchptr
)
5465 struct file_name_list
*searchptr
;
5467 if (CPP_OPTIONS (pfile
)->remap
)
5469 register struct file_name_map
*map
;
5470 register char *from
;
5473 if (searchptr
&& ! searchptr
->got_name_map
)
5475 searchptr
->name_map
= read_name_map (pfile
,
5477 ? searchptr
->fname
: ".");
5478 searchptr
->got_name_map
= 1;
5481 /* First check the mapping for the directory we are using. */
5482 if (searchptr
&& searchptr
->name_map
)
5485 if (searchptr
->fname
)
5486 from
+= strlen (searchptr
->fname
) + 1;
5487 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5489 if (! strcmp (map
->map_from
, from
))
5491 /* Found a match. */
5492 return open (map
->map_to
, O_RDONLY
, 0666);
5497 /* Try to find a mapping file for the particular directory we are
5498 looking in. Thus #include <sys/types.h> will look up sys/types.h
5499 in /usr/include/header.gcc and look up types.h in
5500 /usr/include/sys/header.gcc. */
5501 p
= rindex (filename
, '/');
5506 && strlen (searchptr
->fname
) == p
- filename
5507 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5509 /* FILENAME is in SEARCHPTR, which we've already checked. */
5510 return open (filename
, O_RDONLY
, 0666);
5520 dir
= (char *) alloca (p
- filename
+ 1);
5521 bcopy (filename
, dir
, p
- filename
);
5522 dir
[p
- filename
] = '\0';
5525 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5526 if (! strcmp (map
->map_from
, from
))
5527 return open (map
->map_to
, O_RDONLY
, 0666);
5530 return open (filename
, O_RDONLY
, 0666);
5533 /* Process the contents of include file FNAME, already open on descriptor F,
5535 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5536 "system" include directories (as decided by the `is_system_include'
5538 DIRPTR is the link in the dir path through which this file was found,
5539 or 0 if the file name was absolute or via the current directory.
5540 Return 1 on success, 0 on failure.
5542 The caller is responsible for the cpp_push_buffer. */
5545 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5549 int system_header_p
;
5550 struct file_name_list
*dirptr
;
5556 cpp_buffer
*fp
; /* For input stack frame */
5558 int missing_newline
= 0;
5561 if (fstat (f
, &st
) < 0)
5563 cpp_perror_with_name (pfile
, fname
);
5565 cpp_pop_buffer (pfile
);
5569 fp
= CPP_BUFFER (pfile
);
5570 fp
->nominal_fname
= fp
->fname
= fname
;
5575 fp
->system_header_p
= system_header_p
;
5578 fp
->cleanup
= file_cleanup
;
5580 if (S_ISREG (st
.st_mode
)) {
5581 st_size
= (size_t) st
.st_size
;
5582 if (st_size
!= st
.st_size
|| st_size
+ 2 < st_size
) {
5583 cpp_error (pfile
, "file `%s' too large", fname
);
5587 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5588 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5591 /* Read the file contents, knowing that st_size is an upper bound
5592 on the number of bytes we can read. */
5593 length
= safe_read (f
, fp
->buf
, st_size
);
5594 fp
->rlimit
= fp
->buf
+ length
;
5595 if (length
< 0) goto nope
;
5597 else if (S_ISDIR (st
.st_mode
)) {
5598 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5602 /* Cannot count its file size before reading.
5603 First read the entire file into heap and
5604 copy them into buffer on stack. */
5609 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5612 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5614 goto nope
; /* error! */
5616 if (st_size
!= bsize
)
5617 break; /* End of file */
5619 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5625 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5626 /* Backslash-newline at end is not good enough. */
5627 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5628 fp
->buf
[length
++] = '\n';
5630 missing_newline
= 1;
5633 fp
->buf
[length
] = '\0';
5634 fp
->rlimit
= fp
->buf
+ length
;
5636 /* Close descriptor now, so nesting does not use lots of descriptors. */
5639 /* Must do this before calling trigraph_pcp, so that the correct file name
5640 will be printed in warning messages. */
5642 pfile
->input_stack_listing_current
= 0;
5652 if (missing_newline
)
5655 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5656 pedwarn ("file does not end in newline");
5659 input_file_stack_tick
++;
5666 cpp_perror_with_name (pfile
, fname
);
5672 /* This is called after options have been processed.
5673 * Check options for consistency, and setup for processing input
5674 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5675 * Return 1 on success, 0 on failure.
5679 cpp_start_read (pfile
, fname
)
5683 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5684 struct cpp_pending
*pend
;
5689 /* The code looks at the defaults through this pointer, rather than through
5690 the constant structure above. This pointer gets changed if an environment
5691 variable specifies other defaults. */
5692 struct default_include
*include_defaults
= include_defaults_array
;
5694 /* Add dirs from CPATH after dirs from -I. */
5695 /* There seems to be confusion about what CPATH should do,
5696 so for the moment it is not documented. */
5697 /* Some people say that CPATH should replace the standard include dirs,
5698 but that seems pointless: it comes before them, so it overrides them
5700 GET_ENVIRONMENT (p
, "CPATH");
5701 if (p
!= 0 && ! opts
->no_standard_includes
)
5702 path_include (pfile
, p
);
5704 /* Now that dollars_in_ident is known, initialize is_idchar. */
5705 initialize_char_syntax (opts
);
5707 /* Do partial setup of input buffer for the sake of generating
5708 early #line directives (when -g is in effect). */
5709 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5712 if (opts
->in_fname
== NULL
)
5713 opts
->in_fname
= "";
5714 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5717 /* Install __LINE__, etc. Must follow initialize_char_syntax
5718 and option processing. */
5719 initialize_builtins (pfile
);
5721 /* Do standard #defines and assertions
5722 that identify system and machine type. */
5724 if (!opts
->inhibit_predefs
) {
5725 char *p
= (char *) alloca (strlen (predefs
) + 1);
5726 strcpy (p
, predefs
);
5729 while (*p
== ' ' || *p
== '\t')
5731 /* Handle -D options. */
5732 if (p
[0] == '-' && p
[1] == 'D') {
5734 while (*p
&& *p
!= ' ' && *p
!= '\t')
5738 if (opts
->debug_output
)
5739 output_line_command (pfile
, 0, same_file
);
5740 cpp_define (pfile
, q
);
5741 while (*p
== ' ' || *p
== '\t')
5743 } else if (p
[0] == '-' && p
[1] == 'A') {
5744 /* Handle -A options (assertions). */
5753 past_name
= assertion
;
5754 /* Locate end of name. */
5755 while (*past_name
&& *past_name
!= ' '
5756 && *past_name
!= '\t' && *past_name
!= '(')
5758 /* Locate `(' at start of value. */
5760 while (*value
&& (*value
== ' ' || *value
== '\t'))
5762 if (*value
++ != '(')
5764 while (*value
&& (*value
== ' ' || *value
== '\t'))
5767 /* Locate end of value. */
5768 while (*past_value
&& *past_value
!= ' '
5769 && *past_value
!= '\t' && *past_value
!= ')')
5771 termination
= past_value
;
5772 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5774 if (*termination
++ != ')')
5776 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5778 /* Temporarily null-terminate the value. */
5779 save_char
= *termination
;
5780 *termination
= '\0';
5781 /* Install the assertion. */
5782 make_assertion (pfile
, "-A", assertion
);
5783 *termination
= (char) save_char
;
5785 while (*p
== ' ' || *p
== '\t')
5793 /* Now handle the command line options. */
5795 /* Do -U's, -D's and -A's in the order they were seen. */
5796 /* First reverse the list. */
5797 opts
->pending
= nreverse_pending (opts
->pending
);
5799 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5801 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5803 switch (pend
->cmd
[1])
5806 if (opts
->debug_output
)
5807 output_line_command (pfile
, 0, same_file
);
5808 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5811 if (opts
->debug_output
)
5812 output_line_command (pfile
, 0, same_file
);
5813 cpp_define (pfile
, pend
->arg
);
5816 make_assertion (pfile
, "-A", pend
->arg
);
5822 opts
->done_initializing
= 1;
5824 { /* Read the appropriate environment variable and if it exists
5825 replace include_defaults with the listed path. */
5827 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5830 GET_ENVIRONMENT (epath
, "C_INCLUDE_PATH");
5833 GET_ENVIRONMENT (epath
, "CPLUS_INCLUDE_PATH");
5836 GET_ENVIRONMENT (epath
, "OBJC_INCLUDE_PATH");
5839 GET_ENVIRONMENT (epath
, "OBJCPLUS_INCLUDE_PATH");
5842 /* If the environment var for this language is set,
5843 add to the default list of include directories. */
5845 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5847 char *startp
, *endp
;
5849 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5850 if (*startp
== PATH_SEPARATOR
)
5853 = (struct default_include
*) xmalloc ((num_dirs
5854 * sizeof (struct default_include
))
5855 + sizeof (include_defaults_array
));
5856 startp
= endp
= epath
;
5859 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5860 if ((*endp
== PATH_SEPARATOR
)
5862 strncpy (nstore
, startp
, endp
-startp
);
5864 strcpy (nstore
, ".");
5866 nstore
[endp
-startp
] = '\0';
5868 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5869 include_defaults
[num_dirs
].component
= 0;
5870 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5871 include_defaults
[num_dirs
].cxx_aware
= 1;
5875 endp
= startp
= endp
+ 1;
5879 /* Put the usual defaults back in at the end. */
5880 bcopy ((char *) include_defaults_array
,
5881 (char *) &include_defaults
[num_dirs
],
5882 sizeof (include_defaults_array
));
5886 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5887 opts
->first_system_include
= opts
->before_system
;
5889 /* Unless -fnostdinc,
5890 tack on the standard include file dirs to the specified list */
5891 if (!opts
->no_standard_includes
) {
5892 struct default_include
*p
= include_defaults
;
5893 char *specd_prefix
= opts
->include_prefix
;
5894 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5895 int default_len
= 0;
5896 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5897 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5898 default_len
= strlen (default_prefix
) - 7;
5899 default_prefix
[default_len
] = 0;
5901 /* Search "translated" versions of GNU directories.
5902 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5903 if (specd_prefix
!= 0 && default_len
!= 0)
5904 for (p
= include_defaults
; p
->fname
; p
++) {
5905 /* Some standard dirs are only for C++. */
5907 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5908 /* Does this dir start with the prefix? */
5909 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5910 /* Yes; change prefix and add to search list. */
5911 struct file_name_list
*new
5912 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5913 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5914 char *str
= (char *) xmalloc (this_len
+ 1);
5915 strcpy (str
, specd_prefix
);
5916 strcat (str
, p
->fname
+ default_len
);
5918 new->control_macro
= 0;
5919 new->c_system_include_path
= !p
->cxx_aware
;
5920 new->got_name_map
= 0;
5921 append_include_chain (pfile
, new, new);
5922 if (opts
->first_system_include
== 0)
5923 opts
->first_system_include
= new;
5927 /* Search ordinary names for GNU include directories. */
5928 for (p
= include_defaults
; p
->fname
; p
++) {
5929 /* Some standard dirs are only for C++. */
5931 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5932 struct file_name_list
*new
5933 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5934 new->control_macro
= 0;
5935 new->c_system_include_path
= !p
->cxx_aware
;
5936 new->fname
= update_path (p
->fname
, p
->component
);
5937 new->got_name_map
= 0;
5938 append_include_chain (pfile
, new, new);
5939 if (opts
->first_system_include
== 0)
5940 opts
->first_system_include
= new;
5945 /* Tack the after_include chain at the end of the include chain. */
5946 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5947 if (opts
->first_system_include
== 0)
5948 opts
->first_system_include
= opts
->after_include
;
5950 /* With -v, print the list of dirs to search. */
5951 if (opts
->verbose
) {
5952 struct file_name_list
*p
;
5953 fprintf (stderr
, "#include \"...\" search starts here:\n");
5954 for (p
= opts
->include
; p
; p
= p
->next
) {
5955 if (p
== opts
->first_bracket_include
)
5956 fprintf (stderr
, "#include <...> search starts here:\n");
5957 fprintf (stderr
, " %s\n", p
->fname
);
5959 fprintf (stderr
, "End of search list.\n");
5962 /* Scan the -imacros files before the main input.
5963 Much like #including them, but with no_output set
5964 so that only their macro definitions matter. */
5966 opts
->no_output
++; pfile
->no_record_file
++;
5967 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5969 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
5971 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5974 cpp_perror_with_name (pfile
, pend
->arg
);
5977 if (!cpp_push_buffer (pfile
, NULL
, 0))
5979 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
5980 cpp_scan_buffer (pfile
);
5983 opts
->no_output
--; pfile
->no_record_file
--;
5985 /* Copy the entire contents of the main input file into
5986 the stacked input buffer previously allocated for it. */
5987 if (fname
== NULL
|| *fname
== 0) {
5990 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
5991 cpp_pfatal_with_name (pfile
, fname
);
5993 /* -MG doesn't select the form of output and must be specified with one of
5994 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5995 inhibit compilation. */
5996 if (opts
->print_deps_missing_files
5997 && (opts
->print_deps
== 0 || !opts
->no_output
))
5999 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
6003 /* Either of two environment variables can specify output of deps.
6004 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6005 where OUTPUT_FILE is the file to write deps info to
6006 and DEPS_TARGET is the target to mention in the deps. */
6008 if (opts
->print_deps
== 0
6009 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6010 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6011 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6017 spec
= getenv ("SUNPRO_DEPENDENCIES");
6018 opts
->print_deps
= 2;
6021 opts
->print_deps
= 1;
6024 /* Find the space before the DEPS_TARGET, if there is one. */
6025 /* This should use index. (mrs) */
6026 while (*s
!= 0 && *s
!= ' ') s
++;
6029 opts
->deps_target
= s
+ 1;
6030 output_file
= (char *) xmalloc (s
- spec
+ 1);
6031 bcopy (spec
, output_file
, s
- spec
);
6032 output_file
[s
- spec
] = 0;
6036 opts
->deps_target
= 0;
6040 opts
->deps_file
= output_file
;
6041 opts
->print_deps_append
= 1;
6044 /* For -M, print the expected object file name
6045 as the target of this Make-rule. */
6046 if (opts
->print_deps
)
6048 pfile
->deps_allocated_size
= 200;
6049 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6050 pfile
->deps_buffer
[0] = 0;
6051 pfile
->deps_size
= 0;
6052 pfile
->deps_column
= 0;
6054 if (opts
->deps_target
)
6055 deps_output (pfile
, opts
->deps_target
, ':');
6056 else if (*opts
->in_fname
== 0)
6057 deps_output (pfile
, "-", ':');
6062 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
6063 ".cc", ".cxx", ".cpp", ".cp",
6067 /* Discard all directory prefixes from filename. */
6068 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6069 #ifdef DIR_SEPARATOR
6070 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6077 /* Copy remainder to mungable area. */
6078 p
= (char *) alloca (strlen(q
) + 8);
6081 /* Output P, but remove known suffixes. */
6084 /* Point to the filename suffix. */
6085 r
= rindex (p
, '.');
6086 /* Compare against the known suffixes. */
6088 while (known_suffixes
[x
] != 0)
6090 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
6092 /* Make q point to the bit we're going to overwrite
6093 with an object suffix. */
6100 /* Supply our own suffix. */
6107 deps_output (pfile
, p
, ':');
6108 deps_output (pfile
, opts
->in_fname
, ' ');
6113 /* Make sure data ends with a newline. And put a null after it. */
6115 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6116 /* Backslash-newline at end is not good enough. */
6117 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6118 fp
->buf
[fp
->length
++] = '\n';
6119 missing_newline
= 1;
6121 fp
->buf
[fp
->length
] = '\0';
6123 /* Unless inhibited, convert trigraphs in the input. */
6129 /* Scan the -include files before the main input.
6130 We push these in reverse order, so that the first one is handled first. */
6132 pfile
->no_record_file
++;
6133 opts
->pending
= nreverse_pending (opts
->pending
);
6134 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6136 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6138 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6141 cpp_perror_with_name (pfile
, pend
->arg
);
6144 if (!cpp_push_buffer (pfile
, NULL
, 0))
6146 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6149 pfile
->no_record_file
--;
6151 /* Free the pending list. */
6152 for (pend
= opts
->pending
; pend
; )
6154 struct cpp_pending
*next
= pend
->next
;
6158 opts
->pending
= NULL
;
6161 /* Scan the input, processing macros and directives. */
6163 rescan (&outbuf
, 0);
6165 if (missing_newline
)
6168 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6169 pedwarn ("file does not end in newline");
6172 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6173 output_line_command (pfile
, 0, same_file
);
6178 cpp_reader_init (pfile
)
6181 bzero ((char *) pfile
, sizeof (cpp_reader
));
6182 pfile
->get_token
= cpp_get_token
;
6184 pfile
->token_buffer_size
= 200;
6185 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6186 CPP_SET_WRITTEN (pfile
, 0);
6188 pfile
->system_include_depth
= 0;
6189 pfile
->dont_repeat_files
= 0;
6190 pfile
->all_include_files
= 0;
6191 pfile
->max_include_len
= 0;
6192 pfile
->timebuf
= NULL
;
6193 pfile
->only_seen_white
= 1;
6194 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6197 static struct cpp_pending
*
6198 nreverse_pending (list
)
6199 struct cpp_pending
*list
;
6202 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6203 for (pend
= list
; pend
; pend
= next
)
6213 push_pending (pfile
, cmd
, arg
)
6218 struct cpp_pending
*pend
6219 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6222 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6223 CPP_OPTIONS (pfile
)->pending
= pend
;
6226 /* Handle command-line options in (argc, argv).
6227 Can be called multiple times, to handle multiple sets of options.
6228 Returns if an unrecognized option is seen.
6229 Returns number of handled arguments. */
6232 cpp_handle_options (pfile
, argc
, argv
)
6238 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6239 for (i
= 0; i
< argc
; i
++) {
6240 if (argv
[i
][0] != '-') {
6241 if (opts
->out_fname
!= NULL
)
6243 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6246 else if (opts
->in_fname
!= NULL
)
6247 opts
->out_fname
= argv
[i
];
6249 opts
->in_fname
= argv
[i
];
6251 switch (argv
[i
][1]) {
6254 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6257 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6261 if (!strcmp (argv
[i
], "-include")
6262 || !strcmp (argv
[i
], "-imacros")) {
6264 goto missing_filename
;
6266 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6268 if (!strcmp (argv
[i
], "-iprefix")) {
6270 goto missing_filename
;
6272 opts
->include_prefix
= argv
[++i
];
6274 if (!strcmp (argv
[i
], "-ifoutput")) {
6275 opts
->output_conditionals
= 1;
6277 if (!strcmp (argv
[i
], "-isystem")) {
6278 struct file_name_list
*dirtmp
;
6281 goto missing_filename
;
6283 dirtmp
= (struct file_name_list
*)
6284 xmalloc (sizeof (struct file_name_list
));
6286 dirtmp
->control_macro
= 0;
6287 dirtmp
->c_system_include_path
= 1;
6288 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6289 strcpy (dirtmp
->fname
, argv
[++i
]);
6290 dirtmp
->got_name_map
= 0;
6292 if (opts
->before_system
== 0)
6293 opts
->before_system
= dirtmp
;
6295 opts
->last_before_system
->next
= dirtmp
;
6296 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6298 /* Add directory to end of path for includes,
6299 with the default prefix at the front of its name. */
6300 if (!strcmp (argv
[i
], "-iwithprefix")) {
6301 struct file_name_list
*dirtmp
;
6304 if (opts
->include_prefix
!= 0)
6305 prefix
= opts
->include_prefix
;
6307 prefix
= savestring (GCC_INCLUDE_DIR
);
6308 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6309 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6310 prefix
[strlen (prefix
) - 7] = 0;
6313 dirtmp
= (struct file_name_list
*)
6314 xmalloc (sizeof (struct file_name_list
));
6315 dirtmp
->next
= 0; /* New one goes on the end */
6316 dirtmp
->control_macro
= 0;
6317 dirtmp
->c_system_include_path
= 0;
6319 goto missing_dirname
;
6321 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6322 + strlen (prefix
) + 1);
6323 strcpy (dirtmp
->fname
, prefix
);
6324 strcat (dirtmp
->fname
, argv
[++i
]);
6325 dirtmp
->got_name_map
= 0;
6327 if (opts
->after_include
== 0)
6328 opts
->after_include
= dirtmp
;
6330 opts
->last_after_include
->next
= dirtmp
;
6331 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6333 /* Add directory to main path for includes,
6334 with the default prefix at the front of its name. */
6335 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6336 struct file_name_list
*dirtmp
;
6339 if (opts
->include_prefix
!= 0)
6340 prefix
= opts
->include_prefix
;
6342 prefix
= savestring (GCC_INCLUDE_DIR
);
6343 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6344 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6345 prefix
[strlen (prefix
) - 7] = 0;
6348 dirtmp
= (struct file_name_list
*)
6349 xmalloc (sizeof (struct file_name_list
));
6350 dirtmp
->next
= 0; /* New one goes on the end */
6351 dirtmp
->control_macro
= 0;
6352 dirtmp
->c_system_include_path
= 0;
6354 goto missing_dirname
;
6356 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6357 + strlen (prefix
) + 1);
6358 strcpy (dirtmp
->fname
, prefix
);
6359 strcat (dirtmp
->fname
, argv
[++i
]);
6360 dirtmp
->got_name_map
= 0;
6362 append_include_chain (pfile
, dirtmp
, dirtmp
);
6364 /* Add directory to end of path for includes. */
6365 if (!strcmp (argv
[i
], "-idirafter")) {
6366 struct file_name_list
*dirtmp
;
6368 dirtmp
= (struct file_name_list
*)
6369 xmalloc (sizeof (struct file_name_list
));
6370 dirtmp
->next
= 0; /* New one goes on the end */
6371 dirtmp
->control_macro
= 0;
6372 dirtmp
->c_system_include_path
= 0;
6374 goto missing_dirname
;
6376 dirtmp
->fname
= argv
[++i
];
6377 dirtmp
->got_name_map
= 0;
6379 if (opts
->after_include
== 0)
6380 opts
->after_include
= dirtmp
;
6382 opts
->last_after_include
->next
= dirtmp
;
6383 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6388 if (opts
->out_fname
!= NULL
)
6390 cpp_fatal (pfile
, "Output filename specified twice");
6394 goto missing_filename
;
6395 opts
->out_fname
= argv
[++i
];
6396 if (!strcmp (opts
->out_fname
, "-"))
6397 opts
->out_fname
= "";
6401 if (!strcmp (argv
[i
], "-pedantic"))
6402 CPP_PEDANTIC (pfile
) = 1;
6403 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6404 CPP_PEDANTIC (pfile
) = 1;
6405 opts
->pedantic_errors
= 1;
6408 else if (!strcmp (argv
[i
], "-pcp")) {
6409 char *pcp_fname
= argv
[++i
];
6410 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6411 ? fopen (pcp_fname
, "w")
6412 : fdopen (dup (fileno (stdout
)), "w"));
6413 if (pcp_outfile
== 0)
6414 cpp_pfatal_with_name (pfile
, pcp_fname
);
6421 if (!strcmp (argv
[i
], "-traditional")) {
6422 opts
->traditional
= 1;
6423 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6425 opts
->no_trigraphs
= 0;
6430 if (! strcmp (argv
[i
], "-lang-c"))
6431 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6433 if (! strcmp (argv
[i
], "-lang-c89"))
6434 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
6436 if (! strcmp (argv
[i
], "-lang-c++"))
6437 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6439 if (! strcmp (argv
[i
], "-lang-objc"))
6440 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6442 if (! strcmp (argv
[i
], "-lang-objc++"))
6443 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6445 if (! strcmp (argv
[i
], "-lang-asm"))
6447 if (! strcmp (argv
[i
], "-lint"))
6449 if (! strcmp (argv
[i
], "-lang-chill"))
6450 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6451 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6455 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6459 opts
->inhibit_warnings
= 1;
6463 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6464 opts
->warn_trigraphs
= 1;
6465 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6466 opts
->warn_trigraphs
= 0;
6467 else if (!strcmp (argv
[i
], "-Wcomment"))
6468 opts
->warn_comments
= 1;
6469 else if (!strcmp (argv
[i
], "-Wno-comment"))
6470 opts
->warn_comments
= 0;
6471 else if (!strcmp (argv
[i
], "-Wcomments"))
6472 opts
->warn_comments
= 1;
6473 else if (!strcmp (argv
[i
], "-Wno-comments"))
6474 opts
->warn_comments
= 0;
6475 else if (!strcmp (argv
[i
], "-Wtraditional"))
6476 opts
->warn_stringify
= 1;
6477 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6478 opts
->warn_stringify
= 0;
6479 else if (!strcmp (argv
[i
], "-Wundef"))
6480 opts
->warn_undef
= 1;
6481 else if (!strcmp (argv
[i
], "-Wno-undef"))
6482 opts
->warn_undef
= 0;
6483 else if (!strcmp (argv
[i
], "-Wimport"))
6484 opts
->warn_import
= 1;
6485 else if (!strcmp (argv
[i
], "-Wno-import"))
6486 opts
->warn_import
= 0;
6487 else if (!strcmp (argv
[i
], "-Werror"))
6488 opts
->warnings_are_errors
= 1;
6489 else if (!strcmp (argv
[i
], "-Wno-error"))
6490 opts
->warnings_are_errors
= 0;
6491 else if (!strcmp (argv
[i
], "-Wall"))
6493 opts
->warn_trigraphs
= 1;
6494 opts
->warn_comments
= 1;
6499 /* The style of the choices here is a bit mixed.
6500 The chosen scheme is a hybrid of keeping all options in one string
6501 and specifying each option in a separate argument:
6502 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6503 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6504 -M[M][G][D file]. This is awkward to handle in specs, and is not
6506 /* ??? -MG must be specified in addition to one of -M or -MM.
6507 This can be relaxed in the future without breaking anything.
6508 The converse isn't true. */
6510 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6511 if (!strcmp (argv
[i
], "-MG"))
6513 opts
->print_deps_missing_files
= 1;
6516 if (!strcmp (argv
[i
], "-M"))
6517 opts
->print_deps
= 2;
6518 else if (!strcmp (argv
[i
], "-MM"))
6519 opts
->print_deps
= 1;
6520 else if (!strcmp (argv
[i
], "-MD"))
6521 opts
->print_deps
= 2;
6522 else if (!strcmp (argv
[i
], "-MMD"))
6523 opts
->print_deps
= 1;
6524 /* For -MD and -MMD options, write deps on file named by next arg. */
6525 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6528 goto missing_filename
;
6529 opts
->deps_file
= argv
[++i
];
6533 /* For -M and -MM, write deps on standard output
6534 and suppress the usual output. */
6535 opts
->no_output
= 1;
6541 char *p
= argv
[i
] + 2;
6543 while ((c
= *p
++) != 0) {
6544 /* Arg to -d specifies what parts of macros to dump */
6547 opts
->dump_macros
= dump_only
;
6548 opts
->no_output
= 1;
6551 opts
->dump_macros
= dump_names
;
6554 opts
->dump_macros
= dump_definitions
;
6557 opts
->dump_includes
= 1;
6565 if (argv
[i
][2] == '3')
6566 opts
->debug_output
= 1;
6570 fprintf (stderr
, "GNU CPP version %s", version_string
);
6571 #ifdef TARGET_VERSION
6574 fprintf (stderr
, "\n");
6579 opts
->print_include_names
= 1;
6583 if (argv
[i
][2] != 0)
6584 push_pending (pfile
, "-D", argv
[i
] + 2);
6585 else if (i
+ 1 == argc
)
6587 cpp_fatal (pfile
, "Macro name missing after -D option");
6591 i
++, push_pending (pfile
, "-D", argv
[i
]);
6598 if (argv
[i
][2] != 0)
6600 else if (i
+ 1 == argc
)
6602 cpp_fatal (pfile
, "Assertion missing after -A option");
6608 if (!strcmp (p
, "-")) {
6609 struct cpp_pending
**ptr
;
6610 /* -A- eliminates all predefined macros and assertions.
6611 Let's include also any that were specified earlier
6612 on the command line. That way we can get rid of any
6613 that were passed automatically in from GCC. */
6614 opts
->inhibit_predefs
= 1;
6615 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6617 struct cpp_pending
*pend
= *ptr
;
6618 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6619 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6628 push_pending (pfile
, "-A", p
);
6633 case 'U': /* JF #undef something */
6634 if (argv
[i
][2] != 0)
6635 push_pending (pfile
, "-U", argv
[i
] + 2);
6636 else if (i
+ 1 == argc
)
6638 cpp_fatal (pfile
, "Macro name missing after -U option", NULL
);
6642 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6646 opts
->put_out_comments
= 1;
6649 case 'E': /* -E comes from cc -E; ignore it. */
6653 opts
->no_line_commands
= 1;
6656 case '$': /* Don't include $ in identifiers. */
6657 opts
->dollars_in_ident
= 0;
6660 case 'I': /* Add directory to path for includes. */
6662 struct file_name_list
*dirtmp
;
6664 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6665 && !strcmp (argv
[i
] + 2, "-")) {
6666 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6667 /* Don't use any preceding -I directories for #include <...>. */
6668 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6671 dirtmp
= (struct file_name_list
*)
6672 xmalloc (sizeof (struct file_name_list
));
6673 dirtmp
->next
= 0; /* New one goes on the end */
6674 dirtmp
->control_macro
= 0;
6675 dirtmp
->c_system_include_path
= 0;
6676 if (argv
[i
][2] != 0)
6677 dirtmp
->fname
= argv
[i
] + 2;
6678 else if (i
+ 1 == argc
)
6679 goto missing_dirname
;
6681 dirtmp
->fname
= argv
[++i
];
6682 dirtmp
->got_name_map
= 0;
6683 append_include_chain (pfile
, dirtmp
, dirtmp
);
6689 if (!strcmp (argv
[i
], "-nostdinc"))
6690 /* -nostdinc causes no default include directories.
6691 You must specify all include-file directories with -I. */
6692 opts
->no_standard_includes
= 1;
6693 else if (!strcmp (argv
[i
], "-nostdinc++"))
6694 /* -nostdinc++ causes no default C++-specific include directories. */
6695 opts
->no_standard_cplusplus_includes
= 1;
6697 else if (!strcmp (argv
[i
], "-noprecomp"))
6703 if (!strcmp (argv
[i
], "-remap"))
6708 /* Sun compiler passes undocumented switch "-undef".
6709 Let's assume it means to inhibit the predefined symbols. */
6710 opts
->inhibit_predefs
= 1;
6713 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6714 if (opts
->in_fname
== NULL
) {
6715 opts
->in_fname
= "";
6717 } else if (opts
->out_fname
== NULL
) {
6718 opts
->out_fname
= "";
6720 } /* else fall through into error */
6734 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6736 if (opts
->print_deps
)
6738 /* Stream on which to print the dependency information. */
6741 /* Don't actually write the deps file if compilation has failed. */
6742 if (pfile
->errors
== 0)
6744 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6745 if (opts
->deps_file
== 0)
6746 deps_stream
= stdout
;
6747 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6748 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6749 fputs (pfile
->deps_buffer
, deps_stream
);
6750 putc ('\n', deps_stream
);
6751 if (opts
->deps_file
)
6753 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6754 cpp_fatal (pfile
, "I/O error on output");
6760 /* Free resources used by PFILE.
6761 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6768 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6769 cpp_pop_buffer (pfile
);
6771 if (pfile
->token_buffer
)
6773 free (pfile
->token_buffer
);
6774 pfile
->token_buffer
= NULL
;
6777 if (pfile
->deps_buffer
)
6779 free (pfile
->deps_buffer
);
6780 pfile
->deps_buffer
= NULL
;
6781 pfile
->deps_allocated_size
= 0;
6784 while (pfile
->if_stack
)
6786 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6787 pfile
->if_stack
= temp
->next
;
6791 while (pfile
->dont_repeat_files
)
6793 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6794 pfile
->dont_repeat_files
= temp
->next
;
6799 while (pfile
->all_include_files
)
6801 struct file_name_list
*temp
= pfile
->all_include_files
;
6802 pfile
->all_include_files
= temp
->next
;
6807 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6809 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6812 struct import_file
*next
= imp
->next
;
6817 pfile
->import_hash_table
[i
] = 0;
6820 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6822 while (pfile
->assertion_hashtab
[i
])
6823 delete_assertion (pfile
->assertion_hashtab
[i
]);
6826 cpp_hash_cleanup (pfile
);
6830 do_assert (pfile
, keyword
, buf
, limit
)
6832 struct directive
*keyword
;
6833 U_CHAR
*buf
, *limit
;
6835 long symstart
; /* remember where symbol name starts */
6837 int sym_length
; /* and how long it is */
6838 struct arglist
*tokens
= NULL
;
6840 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6841 && !CPP_BUFFER (pfile
)->system_header_p
)
6842 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6844 cpp_skip_hspace (pfile
);
6845 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6846 parse_name (pfile
, GETC());
6847 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6850 cpp_skip_hspace (pfile
);
6851 if (PEEKC() != '(') {
6852 cpp_error (pfile
, "missing token-sequence in `#assert'");
6858 tokens
= read_token_list (pfile
, &error_flag
);
6862 cpp_error (pfile
, "empty token-sequence in `#assert'");
6865 cpp_skip_hspace (pfile
);
6867 if (c
!= EOF
&& c
!= '\n')
6868 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6869 skip_rest_of_line (pfile
);
6872 /* If this name isn't already an assertion name, make it one.
6873 Error if it was already in use in some other way. */
6876 ASSERTION_HASHNODE
*hp
;
6877 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6878 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6879 struct tokenlist_list
*value
6880 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6882 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6884 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6885 cpp_error (pfile
, "`defined' redefined as assertion");
6886 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6889 /* Add the spec'd token-sequence to the list of such. */
6890 value
->tokens
= tokens
;
6891 value
->next
= hp
->value
;
6894 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6897 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6898 skip_rest_of_line (pfile
);
6903 do_unassert (pfile
, keyword
, buf
, limit
)
6905 struct directive
*keyword
;
6906 U_CHAR
*buf
, *limit
;
6908 long symstart
; /* remember where symbol name starts */
6909 int sym_length
; /* and how long it is */
6912 struct arglist
*tokens
= NULL
;
6913 int tokens_specified
= 0;
6915 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6916 && !CPP_BUFFER (pfile
)->system_header_p
)
6917 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6919 cpp_skip_hspace (pfile
);
6921 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6922 parse_name (pfile
, GETC());
6923 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6926 cpp_skip_hspace (pfile
);
6927 if (PEEKC() == '(') {
6930 tokens
= read_token_list (pfile
, &error_flag
);
6934 cpp_error (pfile
, "empty token list in `#unassert'");
6938 tokens_specified
= 1;
6941 cpp_skip_hspace (pfile
);
6943 if (c
!= EOF
&& c
!= '\n')
6944 cpp_error (pfile
, "junk at end of `#unassert'");
6945 skip_rest_of_line (pfile
);
6948 ASSERTION_HASHNODE
*hp
;
6949 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6950 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6951 struct tokenlist_list
*tail
, *prev
;
6953 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6957 /* If no token list was specified, then eliminate this assertion
6959 if (! tokens_specified
)
6960 delete_assertion (hp
);
6962 /* If a list of tokens was given, then delete any matching list. */
6967 struct tokenlist_list
*next
= tail
->next
;
6968 if (compare_token_lists (tail
->tokens
, tokens
)) {
6972 hp
->value
= tail
->next
;
6973 free_token_list (tail
->tokens
);
6983 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6986 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6987 skip_rest_of_line (pfile
);
6991 /* Test whether there is an assertion named NAME
6992 and optionally whether it has an asserted token list TOKENS.
6993 NAME is not null terminated; its length is SYM_LENGTH.
6994 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6997 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
7001 int tokens_specified
;
7002 struct arglist
*tokens
;
7004 ASSERTION_HASHNODE
*hp
;
7005 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
7007 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
7008 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
7010 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
7012 /* It is not an assertion; just return false. */
7015 /* If no token list was specified, then value is 1. */
7016 if (! tokens_specified
)
7020 struct tokenlist_list
*tail
;
7024 /* If a list of tokens was given,
7025 then succeed if the assertion records a matching list. */
7028 if (compare_token_lists (tail
->tokens
, tokens
))
7033 /* Fail if the assertion has no matching list. */
7038 /* Compare two lists of tokens for equality including order of tokens. */
7041 compare_token_lists (l1
, l2
)
7042 struct arglist
*l1
, *l2
;
7045 if (l1
->length
!= l2
->length
)
7047 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7053 /* Succeed if both lists end at the same time. */
7058 reverse_token_list (tokens
)
7059 struct arglist
*tokens
;
7061 register struct arglist
*prev
= 0, *this, *next
;
7062 for (this = tokens
; this; this = next
)
7071 /* Read a space-separated list of tokens ending in a close parenthesis.
7072 Return a list of strings, in the order they were written.
7073 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7075 static struct arglist
*
7076 read_token_list (pfile
, error_flag
)
7080 struct arglist
*token_ptrs
= 0;
7085 FORWARD (1); /* Skip '(' */
7087 /* Loop over the assertion value tokens. */
7090 struct arglist
*temp
;
7091 long name_written
= CPP_WRITTEN (pfile
);
7094 cpp_skip_hspace (pfile
);
7098 /* Find the end of the token. */
7101 CPP_PUTC (pfile
, c
);
7109 CPP_PUTC (pfile
, c
);
7111 else if (c
== '"' || c
== '\'')
7114 cpp_get_token (pfile
);
7120 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7121 && c
!= '"' && c
!= '\'')
7123 CPP_PUTC (pfile
, c
);
7126 if (c
!= EOF
) FORWARD(-1);
7129 length
= CPP_WRITTEN (pfile
) - name_written
;
7130 temp
= (struct arglist
*)
7131 xmalloc (sizeof (struct arglist
) + length
+ 1);
7132 temp
->name
= (U_CHAR
*) (temp
+ 1);
7133 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7134 (char *) temp
->name
, length
);
7135 temp
->name
[length
] = 0;
7136 temp
->next
= token_ptrs
;
7138 temp
->length
= length
;
7140 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7142 if (c
== EOF
|| c
== '\n')
7145 "unterminated token sequence following `#' operator");
7150 /* We accumulated the names in reverse order.
7151 Now reverse them to get the proper order. */
7152 return reverse_token_list (token_ptrs
);
7156 free_token_list (tokens
)
7157 struct arglist
*tokens
;
7160 struct arglist
*next
= tokens
->next
;
7161 free (tokens
->name
);
7167 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7168 retrying if necessary. If MAX_READ_LEN is defined, read at most
7169 that bytes at a time. Return a negative value if an error occurs,
7170 otherwise return the actual number of bytes read,
7171 which must be LEN unless end-of-file was reached. */
7174 safe_read (desc
, ptr
, len
)
7179 int left
, rcount
, nchars
;
7185 if (rcount
> MAX_READ_LEN
)
7186 rcount
= MAX_READ_LEN
;
7188 nchars
= read (desc
, ptr
, rcount
);
7206 xcalloc (number
, size
)
7207 unsigned number
, size
;
7209 register unsigned total
= number
* size
;
7210 register char *ptr
= (char *) xmalloc (total
);
7219 unsigned size
= strlen (input
);
7220 char *output
= xmalloc (size
+ 1);
7221 strcpy (output
, input
);
7225 /* Initialize PMARK to remember the current position of PFILE. */
7228 parse_set_mark (pmark
, pfile
)
7229 struct parse_marker
*pmark
;
7232 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7233 pmark
->next
= pbuf
->marks
;
7234 pbuf
->marks
= pmark
;
7236 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7239 /* Cleanup PMARK - we no longer need it. */
7242 parse_clear_mark (pmark
)
7243 struct parse_marker
*pmark
;
7245 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7246 for (; ; pp
= &(*pp
)->next
) {
7247 if (*pp
== NULL
) abort ();
7248 if (*pp
== pmark
) break;
7253 /* Backup the current position of PFILE to that saved in PMARK. */
7256 parse_goto_mark (pmark
, pfile
)
7257 struct parse_marker
*pmark
;
7260 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7261 if (pbuf
!= pmark
->buf
)
7262 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7263 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7266 /* Reset PMARK to point to the current position of PFILE. (Same
7267 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7270 parse_move_mark (pmark
, pfile
)
7271 struct parse_marker
*pmark
;
7274 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7275 if (pbuf
!= pmark
->buf
)
7276 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7277 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7281 cpp_read_check_assertion (pfile
)
7284 int name_start
= CPP_WRITTEN (pfile
);
7285 int name_length
, name_written
;
7287 FORWARD (1); /* Skip '#' */
7288 cpp_skip_hspace (pfile
);
7289 parse_name (pfile
, GETC ());
7290 name_written
= CPP_WRITTEN (pfile
);
7291 name_length
= name_written
- name_start
;
7292 cpp_skip_hspace (pfile
);
7293 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7296 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7297 result
= check_assertion (pfile
,
7298 pfile
->token_buffer
+ name_start
, name_length
,
7302 result
= check_assertion (pfile
,
7303 pfile
->token_buffer
+ name_start
, name_length
,
7305 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7310 cpp_print_file_and_line (pfile
)
7313 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7318 cpp_buf_line_and_col (ip
, &line
, &col
);
7319 cpp_file_line_for_message (ip
->nominal_fname
,
7320 line
, pfile
->show_column
? col
: -1);
7325 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7328 char *arg1
, *arg2
, *arg3
;
7330 cpp_print_containing_files (pfile
);
7331 cpp_print_file_and_line (pfile
);
7332 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7335 /* Print error message but don't count it. */
7338 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7341 char *arg1
, *arg2
, *arg3
;
7343 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7346 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7349 cpp_print_containing_files (pfile
);
7350 cpp_print_file_and_line (pfile
);
7351 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7354 /* Print an error message and maybe count it. */
7357 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7360 char *arg1
, *arg2
, *arg3
;
7362 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7363 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7365 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7369 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7373 char *arg1
, *arg2
, *arg3
;
7375 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7377 cpp_print_containing_files (pfile
);
7380 cpp_file_line_for_message (ip
->nominal_fname
, line
, column
);
7382 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7386 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7390 char *arg1
, *arg2
, *arg3
;
7394 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7397 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7400 cpp_print_containing_files (pfile
);
7402 ip
= cpp_file_buffer (pfile
);
7405 cpp_file_line_for_message (ip
->nominal_fname
, line
, column
);
7407 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7411 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7415 char *arg1
, *arg2
, *arg3
;
7417 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7418 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7420 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7423 /* Report a warning (or an error if pedantic_errors)
7424 giving specified file name and line number, not current. */
7427 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7432 char *arg1
, *arg2
, *arg3
;
7434 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7435 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7438 cpp_file_line_for_message (file
, line
, -1);
7439 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7440 msg
, arg1
, arg2
, arg3
);
7443 /* This defines "errno" properly for VMS, and gives us EACCES. */
7450 #ifndef HAVE_STRERROR
7451 extern int sys_nerr
;
7452 extern char *sys_errlist
[];
7453 #else /* HAVE_STRERROR */
7457 char *strerror (int,...);
7460 /* my_strerror - return the descriptive text associated with an
7464 my_strerror (errnum
)
7470 #ifndef HAVE_STRERROR
7471 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7473 result
= strerror (errnum
);
7476 /* VAXCRTL's strerror() takes an optional second argument, which only
7477 matters when the first argument is EVMSERR. However, it's simplest
7478 just to pass it unconditionally. `vaxc$errno' is declared in
7479 <errno.h>, and maintained by the library in parallel with `errno'.
7480 We assume that caller's `errnum' either matches the last setting of
7481 `errno' by the library or else does not have the value `EVMSERR'. */
7483 result
= strerror (errnum
, vaxc$errno
);
7487 result
= "undocumented I/O error";
7492 /* Error including a message from `errno'. */
7495 cpp_error_from_errno (pfile
, name
)
7500 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7502 cpp_print_containing_files (pfile
);
7505 cpp_file_line_for_message (ip
->nominal_fname
, ip
->lineno
, -1);
7507 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (e
));
7511 cpp_perror_with_name (pfile
, name
)
7515 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7519 * No pre-compiled header file support.
7521 * Possibly different enum token codes for each C/C++ token.
7523 * Should clean up remaining directives to that do_XXX functions
7524 * only take two arguments and all have command_reads_line.
7526 * Find and cleanup remaining uses of static variables,
7528 * Support for trigraphs.
7530 * Support -dM flag (dump_all_macros).
7532 * Support for_lint flag.