2 Copyright (C) 1986, 87, 89, 92-7, 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. */
35 #ifdef TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
46 #ifdef HAVE_SYS_TIMES_H
47 #include <sys/times.h>
50 #ifdef HAVE_SYS_RESOURCE_H
51 # include <sys/resource.h>
69 # ifdef HAVE_STRINGS_H
74 /* This defines "errno" properly for VMS, and gives us EACCES. */
79 #include "gansidecl.h"
81 #ifdef NEED_DECLARATION_INDEX
82 extern char *index ();
85 #ifdef NEED_DECLARATION_RINDEX
86 extern char *rindex ();
89 #ifdef NEED_DECLARATION_GETENV
90 extern char *getenv ();
93 extern char *update_path ();
101 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
102 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
104 /* Find the largest host integer type and set its size and type.
105 Watch out: on some crazy hosts `long' is shorter than `int'. */
107 #ifndef HOST_WIDE_INT
109 # include <inttypes.h>
110 # define HOST_WIDE_INT intmax_t
112 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
113 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
114 # define HOST_WIDE_INT int
116 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
117 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
118 # define HOST_WIDE_INT long
120 # define HOST_WIDE_INT long long
127 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
131 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
134 /* By default, colon separates directories in a path. */
135 #ifndef PATH_SEPARATOR
136 #define PATH_SEPARATOR ':'
139 #ifndef STANDARD_INCLUDE_DIR
140 #define STANDARD_INCLUDE_DIR "/usr/include"
142 #ifndef INCLUDE_LEN_FUDGE
143 #define INCLUDE_LEN_FUDGE 0
146 /* Symbols to predefine. */
148 #ifdef CPP_PREDEFINES
149 static char *predefs
= CPP_PREDEFINES
;
151 static char *predefs
= "";
154 /* We let tm.h override the types used here, to handle trivial differences
155 such as the choice of unsigned int or long unsigned int for size_t.
156 When machines start needing nontrivial differences in the size type,
157 it would be best to do something here to figure out automatically
158 from other information what type to use. */
160 /* The string value for __SIZE_TYPE__. */
163 #define SIZE_TYPE "long unsigned int"
166 /* The string value for __PTRDIFF_TYPE__. */
169 #define PTRDIFF_TYPE "long int"
172 /* The string value for __WCHAR_TYPE__. */
175 #define WCHAR_TYPE "int"
177 #define CPP_WCHAR_TYPE(PFILE) \
178 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
180 /* The string value for __USER_LABEL_PREFIX__ */
182 #ifndef USER_LABEL_PREFIX
183 #define USER_LABEL_PREFIX ""
186 /* The string value for __REGISTER_PREFIX__ */
188 #ifndef REGISTER_PREFIX
189 #define REGISTER_PREFIX ""
192 /* In the definition of a #assert name, this structure forms
193 a list of the individual values asserted.
194 Each value is itself a list of "tokens".
195 These are strings that are compared by name. */
197 struct tokenlist_list
{
198 struct tokenlist_list
*next
;
199 struct arglist
*tokens
;
202 struct assertion_hashnode
{
203 struct assertion_hashnode
*next
; /* double links for easy deletion */
204 struct assertion_hashnode
*prev
;
205 /* also, a back pointer to this node's hash
206 chain is kept, in case the node is the head
207 of the chain and gets deleted. */
208 struct assertion_hashnode
**bucket_hdr
;
209 int length
; /* length of token, for quick comparison */
210 U_CHAR
*name
; /* the actual name */
211 /* List of token-sequences. */
212 struct tokenlist_list
*value
;
215 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
216 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
218 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
219 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
220 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
221 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
222 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
223 (Note that it is false while we're expanding marco *arguments*.) */
224 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
226 /* Move all backslash-newline pairs out of embarrassing places.
227 Exchange all such pairs following BP
228 with any potentially-embarrassing characters that follow them.
229 Potentially-embarrassing characters are / and *
230 (because a backslash-newline inside a comment delimiter
231 would cause it not to be recognized). */
233 #define NEWLINE_FIX \
234 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
236 /* Same, but assume we've already read the potential '\\' into C. */
237 #define NEWLINE_FIX1(C) do { \
238 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
242 struct cpp_pending
*next
;
247 /* Forward declarations. */
251 void cpp_file_line_for_message
PARAMS ((cpp_reader
*, char *, int, int));
252 void cpp_hash_cleanup
PARAMS ((cpp_reader
*));
254 void cpp_print_containing_files
PARAMS ((cpp_reader
*));
256 static void add_import ();
257 static void append_include_chain ();
258 static void make_assertion ();
259 static void path_include ();
260 static void initialize_builtins ();
261 static void initialize_char_syntax ();
262 extern void delete_macro ();
264 static void trigraph_pcp ();
266 static int finclude ();
267 static void validate_else ();
268 static int comp_def_part ();
270 extern void fancy_abort ();
272 static int lookup_import ();
273 static int redundant_include_p ();
274 static int is_system_include ();
275 static struct file_name_map
*read_name_map ();
276 static char *read_filename_string ();
277 static int open_include_file ();
278 static int check_macro_name ();
279 static int compare_defs ();
280 static int compare_token_lists ();
281 static HOST_WIDE_INT
eval_if_expression ();
282 static int change_newlines ();
284 static struct arglist
*read_token_list ();
285 static void free_token_list ();
286 static int safe_read ();
287 static void push_macro_expansion
PARAMS ((cpp_reader
*,
288 U_CHAR
*, int, HASHNODE
*));
289 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
290 extern char *xrealloc ();
291 static char *xcalloc ();
292 static char *savestring ();
294 static void conditional_skip ();
295 static void skip_if_group ();
297 /* Last arg to output_line_command. */
298 enum file_change_code
{same_file
, enter_file
, leave_file
};
300 /* External declarations. */
302 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
304 extern FILE *fdopen ();
305 extern char *version_string
;
306 extern struct tm
*localtime ();
308 /* These functions are declared to return int instead of void since they
309 are going to be placed in a table and some old compilers have trouble with
310 pointers to functions returning void. */
312 static int do_define ();
313 static int do_line ();
314 static int do_include ();
315 static int do_undef ();
316 static int do_error ();
317 static int do_pragma ();
318 static int do_ident ();
320 static int do_xifdef ();
321 static int do_else ();
322 static int do_elif ();
323 static int do_endif ();
324 static int do_sccs ();
325 static int do_once ();
326 static int do_assert ();
327 static int do_unassert ();
328 static int do_warning ();
330 struct file_name_list
332 struct file_name_list
*next
;
334 /* If the following is nonzero, it is a macro name.
335 Don't include the file again if that macro is defined. */
336 U_CHAR
*control_macro
;
337 /* If the following is nonzero, it is a C-language system include
339 int c_system_include_path
;
340 /* Mapping of file names for this directory. */
341 struct file_name_map
*name_map
;
342 /* Non-zero if name_map is valid. */
346 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
347 via the same directory as the file that #included it. */
348 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
350 /* #include "file" looks in source file dir, then stack. */
351 /* #include <file> just looks in the stack. */
352 /* -I directories are added to the end, then the defaults are added. */
354 static struct default_include
{
355 char *fname
; /* The name of the directory. */
356 char *component
; /* The component containing the directory */
357 int cplusplus
; /* Only look here if we're compiling C++. */
358 int cxx_aware
; /* Includes in this directory don't need to
359 be wrapped in extern "C" when compiling
361 } include_defaults_array
[]
362 #ifdef INCLUDE_DEFAULTS
366 /* Pick up GNU C++ specific include files. */
367 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
368 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 1 },
370 /* This is the dir for fixincludes. Put it just before
371 the files that we fix. */
372 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
373 /* For cross-compilation, this dir name is generated
374 automatically in Makefile.in. */
375 { CROSS_INCLUDE_DIR
, "GCC",0, 0 },
376 #ifdef TOOL_INCLUDE_DIR
377 /* This is another place that the target system's headers might be. */
378 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
380 #else /* not CROSS_COMPILE */
381 #ifdef LOCAL_INCLUDE_DIR
382 /* This should be /usr/local/include and should come before
383 the fixincludes-fixed header files. */
384 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
386 #ifdef TOOL_INCLUDE_DIR
387 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
388 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
389 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
391 /* This is the dir for fixincludes. Put it just before
392 the files that we fix. */
393 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
394 /* Some systems have an extra dir of include files. */
395 #ifdef SYSTEM_INCLUDE_DIR
396 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
398 #ifndef STANDARD_INCLUDE_COMPONENT
399 #define STANDARD_INCLUDE_COMPONENT 0
401 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
402 #endif /* not CROSS_COMPILE */
405 #endif /* no INCLUDE_DEFAULTS */
407 /* `struct directive' defines one #-directive, including how to handle it. */
410 int length
; /* Length of name */
411 int (*func
)(); /* Function to handle directive */
412 char *name
; /* Name of directive */
413 enum node_type type
; /* Code which describes which directive. */
414 char command_reads_line
; /* One if rest of line is read by func. */
417 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
419 /* Here is the actual list of #-directives, most-often-used first.
420 The initialize_builtins function assumes #define is the very first. */
422 static struct directive directive_table
[] = {
423 { 6, do_define
, "define", T_DEFINE
},
424 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
425 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
426 { 7, do_include
, "include", T_INCLUDE
, 1},
427 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
428 { 6, do_include
, "import", T_IMPORT
, 1},
429 { 5, do_endif
, "endif", T_ENDIF
, 1},
430 { 4, do_else
, "else", T_ELSE
, 1},
431 { 2, do_if
, "if", T_IF
, 1},
432 { 4, do_elif
, "elif", T_ELIF
, 1},
433 { 5, do_undef
, "undef", T_UNDEF
},
434 { 5, do_error
, "error", T_ERROR
},
435 { 7, do_warning
, "warning", T_WARNING
},
436 { 6, do_pragma
, "pragma", T_PRAGMA
},
437 { 4, do_line
, "line", T_LINE
, 1},
438 { 5, do_ident
, "ident", T_IDENT
, 1},
439 #ifdef SCCS_DIRECTIVE
440 { 4, do_sccs
, "sccs", T_SCCS
},
442 { 6, do_assert
, "assert", T_ASSERT
, 1},
443 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
444 { -1, 0, "", T_UNUSED
},
447 /* table to tell if char can be part of a C identifier. */
448 U_CHAR is_idchar
[256];
449 /* table to tell if char can be first char of a c identifier. */
450 U_CHAR is_idstart
[256];
451 /* table to tell if c is horizontal space. */
452 U_CHAR is_hor_space
[256];
453 /* table to tell if c is horizontal or vertical space. */
454 static U_CHAR is_space
[256];
456 /* Initialize syntactic classifications of characters. */
459 initialize_char_syntax (opts
)
460 struct cpp_options
*opts
;
465 * Set up is_idchar and is_idstart tables. These should be
466 * faster than saying (is_alpha (c) || c == '_'), etc.
467 * Set up these things before calling any routines tthat
470 for (i
= 'a'; i
<= 'z'; i
++) {
471 is_idchar
[i
- 'a' + 'A'] = 1;
473 is_idstart
[i
- 'a' + 'A'] = 1;
476 for (i
= '0'; i
<= '9'; i
++)
480 is_idchar
['$'] = opts
->dollars_in_ident
;
481 is_idstart
['$'] = opts
->dollars_in_ident
;
483 /* horizontal space table */
484 is_hor_space
[' '] = 1;
485 is_hor_space
['\t'] = 1;
486 is_hor_space
['\v'] = 1;
487 is_hor_space
['\f'] = 1;
488 is_hor_space
['\r'] = 1;
499 /* Place into PFILE a quoted string representing the string SRC.
500 Caller must reserve enough space in pfile->token_buffer. */
503 quote_string (pfile
, src
)
509 CPP_PUTC_Q (pfile
, '\"');
511 switch ((c
= *src
++))
515 CPP_PUTC_Q (pfile
, c
);
518 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
519 CPP_ADJUST_WRITTEN (pfile
, 4);
525 CPP_PUTC_Q (pfile
, '\\');
526 CPP_PUTC_Q (pfile
, c
);
530 CPP_PUTC_Q (pfile
, '\"');
531 CPP_NUL_TERMINATE_Q (pfile
);
536 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
539 cpp_grow_buffer (pfile
, n
)
543 long old_written
= CPP_WRITTEN (pfile
);
544 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
545 pfile
->token_buffer
= (U_CHAR
*)
546 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
547 CPP_SET_WRITTEN (pfile
, old_written
);
552 * process a given definition string, for initialization
553 * If STR is just an identifier, define it with value 1.
554 * If STR has anything after the identifier, then it should
555 * be identifier=definition.
559 cpp_define (pfile
, str
)
569 cpp_error (pfile
, "malformed option `-D %s'", str
);
572 while (is_idchar
[*++p
])
576 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
577 strcpy ((char *)buf
, str
);
578 strcat ((char *)buf
, " 1");
582 cpp_error (pfile
, "malformed option `-D %s'", str
);
588 /* Copy the entire option so we can modify it. */
589 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
590 strncpy (buf
, str
, p
- str
);
591 /* Change the = to a space. */
593 /* Scan for any backslash-newline and remove it. */
598 if (*p
== '\\' && p
[1] == '\n')
606 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
609 /* Process the string STR as if it appeared as the body of a #assert.
610 OPTION is the option name for which STR was the argument. */
613 make_assertion (pfile
, option
, str
)
620 /* Copy the entire option so we can modify it. */
621 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
622 strcpy ((char *) buf
, str
);
623 /* Scan for any backslash-newline and remove it. */
627 if (*p
== '\\' && p
[1] == '\n')
636 if (!is_idstart
[*p
]) {
637 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
640 while (is_idchar
[*++p
])
642 while (*p
== ' ' || *p
== '\t') p
++;
643 if (! (*p
== 0 || *p
== '(')) {
644 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
648 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
650 do_assert (pfile
, NULL
, NULL
, NULL
);
651 cpp_pop_buffer (pfile
);
655 /* Append a chain of `struct file_name_list's
656 to the end of the main include chain.
657 FIRST is the beginning of the chain to append, and LAST is the end. */
660 append_include_chain (pfile
, first
, last
)
662 struct file_name_list
*first
, *last
;
664 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
665 struct file_name_list
*dir
;
670 if (opts
->include
== 0)
671 opts
->include
= first
;
673 opts
->last_include
->next
= first
;
675 if (opts
->first_bracket_include
== 0)
676 opts
->first_bracket_include
= first
;
678 for (dir
= first
; ; dir
= dir
->next
) {
679 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
680 if (len
> pfile
->max_include_len
)
681 pfile
->max_include_len
= len
;
687 opts
->last_include
= last
;
690 /* Add output to `deps_buffer' for the -M switch.
691 STRING points to the text to be output.
692 SPACER is ':' for targets, ' ' for dependencies, zero for text
693 to be inserted literally. */
696 deps_output (pfile
, string
, spacer
)
701 int size
= strlen (string
);
706 #ifndef MAX_OUTPUT_COLUMNS
707 #define MAX_OUTPUT_COLUMNS 72
710 && pfile
->deps_column
> 0
711 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
713 deps_output (pfile
, " \\\n ", 0);
714 pfile
->deps_column
= 0;
717 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
719 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
720 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
721 pfile
->deps_allocated_size
);
723 if (spacer
== ' ' && pfile
->deps_column
> 0)
724 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
725 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
726 pfile
->deps_size
+= size
;
727 pfile
->deps_column
+= size
;
729 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
730 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
733 /* Given a colon-separated list of file names PATH,
734 add all the names to the search path for include files. */
737 path_include (pfile
, path
)
749 struct file_name_list
*dirtmp
;
751 /* Find the end of this name. */
752 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
754 /* An empty name in the path stands for the current directory. */
755 name
= (char *) xmalloc (2);
759 /* Otherwise use the directory that is named. */
760 name
= (char *) xmalloc (q
- p
+ 1);
761 bcopy (p
, name
, q
- p
);
765 dirtmp
= (struct file_name_list
*)
766 xmalloc (sizeof (struct file_name_list
));
767 dirtmp
->next
= 0; /* New one goes on the end */
768 dirtmp
->control_macro
= 0;
769 dirtmp
->c_system_include_path
= 0;
770 dirtmp
->fname
= name
;
771 dirtmp
->got_name_map
= 0;
772 append_include_chain (pfile
, dirtmp
, dirtmp
);
774 /* Advance past this name. */
778 /* Skip the colon. */
784 cpp_options_init (opts
)
787 bzero ((char *) opts
, sizeof *opts
);
788 opts
->in_fname
= NULL
;
789 opts
->out_fname
= NULL
;
791 /* Initialize is_idchar to allow $. */
792 opts
->dollars_in_ident
= 1;
793 initialize_char_syntax (opts
);
795 opts
->no_line_commands
= 0;
796 opts
->no_trigraphs
= 1;
797 opts
->put_out_comments
= 0;
798 opts
->print_include_names
= 0;
799 opts
->dump_macros
= dump_none
;
803 opts
->cplusplus_comments
= 0;
810 opts
->pedantic_errors
= 0;
811 opts
->inhibit_warnings
= 0;
812 opts
->warn_comments
= 0;
813 opts
->warn_import
= 1;
814 opts
->warnings_are_errors
= 0;
818 null_underflow (pfile
)
825 null_cleanup (pbuf
, pfile
)
833 macro_cleanup (pbuf
, pfile
)
837 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
838 if (macro
->type
== T_DISABLED
)
839 macro
->type
= T_MACRO
;
840 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
846 file_cleanup (pbuf
, pfile
)
858 /* Assuming we have read '/'.
859 If this is the start of a comment (followed by '*' or '/'),
860 skip to the end of the comment, and return ' '.
861 Return EOF if we reached the end of file before the end of the comment.
862 If not the start of a comment, return '/'. */
865 skip_comment (pfile
, linep
)
870 while (PEEKC() == '\\' && PEEKN(1) == '\n')
885 while (c
== '\\' && PEEKC() == '\n')
889 FORWARD(1), c
= GETC();
891 if (prev_c
== '*' && c
== '/')
893 if (c
== '\n' && linep
)
897 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
904 return ' '; /* Allow // to be terminated by EOF. */
905 while (c
== '\\' && PEEKC() == '\n')
914 /* Don't consider final '\n' to be part of comment. */
924 /* Skip whitespace \-newline and comments. Does not macro-expand. */
927 cpp_skip_hspace (pfile
)
937 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
938 cpp_pedwarn (pfile
, "%s in preprocessing directive",
939 c
== '\f' ? "formfeed" : "vertical tab");
945 c
= skip_comment (pfile
, NULL
);
948 if (c
== EOF
|| c
== '/')
951 else if (c
== '\\' && PEEKN(1) == '\n') {
954 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
955 && is_hor_space
[PEEKN(1)])
961 /* Read the rest of the current line.
962 The line is appended to PFILE's output buffer. */
965 copy_rest_of_line (pfile
)
968 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
985 goto scan_directive_token
;
989 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '/'))
990 goto scan_directive_token
;
994 if (CPP_PEDANTIC (pfile
))
995 cpp_pedwarn (pfile
, "%s in preprocessing directive",
996 c
== '\f' ? "formfeed" : "vertical tab");
1002 scan_directive_token
:
1004 cpp_get_token (pfile
);
1007 CPP_PUTC (pfile
, c
);
1010 CPP_NUL_TERMINATE (pfile
);
1014 skip_rest_of_line (pfile
)
1017 long old
= CPP_WRITTEN (pfile
);
1018 copy_rest_of_line (pfile
);
1019 CPP_SET_WRITTEN (pfile
, old
);
1022 /* Handle a possible # directive.
1023 '#' has already been read. */
1026 handle_directive (pfile
)
1029 register struct directive
*kt
;
1032 U_CHAR
*ident
, *line_end
;
1033 long old_written
= CPP_WRITTEN (pfile
);
1035 cpp_skip_hspace (pfile
);
1038 if (c
>= '0' && c
<= '9')
1040 /* Handle # followed by a line number. */
1041 if (CPP_PEDANTIC (pfile
))
1042 cpp_pedwarn (pfile
, "`#' followed by integer");
1043 do_line (pfile
, NULL
);
1044 goto done_a_directive
;
1047 /* Now find the directive name. */
1048 CPP_PUTC (pfile
, '#');
1049 parse_name (pfile
, GETC());
1050 ident
= pfile
->token_buffer
+ old_written
+ 1;
1051 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1052 if (ident_length
== 0 && PEEKC() == '\n')
1054 /* A line of just `#' becomes blank. */
1055 goto done_a_directive
;
1059 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1061 while (is_idchar
[*p
]) {
1062 if (*p
< '0' || *p
> '9')
1066 /* Avoid error for `###' and similar cases unless -pedantic. */
1068 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1070 if (pedantic
&& !lang_asm
)
1071 cpp_warning (pfile
, "invalid preprocessor directive");
1077 cpp_error (pfile
, "invalid preprocessor directive name");
1083 * Decode the keyword and call the appropriate expansion
1084 * routine, after moving the input pointer up to the next line.
1086 for (kt
= directive_table
; ; kt
++) {
1087 if (kt
->length
<= 0)
1088 goto not_a_directive
;
1089 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1093 if (kt
->command_reads_line
)
1097 /* Nonzero means do not delete comments within the directive.
1098 #define needs this when -traditional. */
1099 int comments
= CPP_TRADITIONAL (pfile
) && kt
->type
== T_DEFINE
;
1100 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1101 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1102 after_ident
= CPP_WRITTEN (pfile
);
1103 copy_rest_of_line (pfile
);
1104 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1107 /* We may want to pass through #define, #pragma, and #include.
1108 Other directives may create output, but we don't want the directive
1109 itself out, so we pop it now. For example conditionals may emit
1110 #failed ... #endfailed stuff. But note that popping the buffer
1111 means the parameters to kt->func may point after pfile->limit
1112 so these parameters are invalid as soon as something gets appended
1113 to the token_buffer. */
1115 line_end
= CPP_PWRITTEN (pfile
);
1116 if (! (kt
->type
== T_DEFINE
1117 || kt
->type
== T_PRAGMA
1118 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
1119 && CPP_OPTIONS (pfile
)->dump_includes
)))
1120 CPP_SET_WRITTEN (pfile
, old_written
);
1122 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1124 if (kt
->type
== T_DEFINE
)
1126 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1128 /* Skip "#define". */
1129 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
1131 SKIP_WHITE_SPACE (p
);
1132 while (is_idchar
[*p
]) p
++;
1134 CPP_PUTC (pfile
, '\n');
1136 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
1137 CPP_SET_WRITTEN (pfile
, old_written
);
1147 /* Pass a directive through to the output file.
1148 BUF points to the contents of the directive, as a contiguous string.
1149 LIMIT points to the first character past the end of the directive.
1150 KEYWORD is the keyword-table entry for the directive. */
1153 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1154 U_CHAR
*buf
, *limit
;
1156 struct directive
*keyword
;
1158 register unsigned keyword_length
= keyword
->length
;
1160 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1161 CPP_PUTC_Q (pfile
, '#');
1162 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1163 if (limit
!= buf
&& buf
[0] != ' ')
1164 CPP_PUTC_Q (pfile
, ' ');
1165 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1167 CPP_PUTS_Q (pfile
, '\n');
1168 /* Count the line we have just made in the output,
1169 to get in sync properly. */
1174 /* The arglist structure is built by do_define to tell
1175 collect_definition where the argument names begin. That
1176 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1177 would contain pointers to the strings x, y, and z.
1178 Collect_definition would then build a DEFINITION node,
1179 with reflist nodes pointing to the places x, y, and z had
1180 appeared. So the arglist is just convenience data passed
1181 between these two routines. It is not kept around after
1182 the current #define has been processed and entered into the
1186 struct arglist
*next
;
1193 /* Read a replacement list for a macro with parameters.
1194 Build the DEFINITION structure.
1195 Reads characters of text starting at BUF until END.
1196 ARGLIST specifies the formal parameters to look for
1197 in the text of the definition; NARGS is the number of args
1198 in that list, or -1 for a macro name that wants no argument list.
1199 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1200 and NAMELEN is its length in characters.
1202 Note that comments, backslash-newlines, and leading white space
1203 have already been deleted from the argument. */
1206 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1208 U_CHAR
*buf
, *limit
;
1210 struct arglist
*arglist
;
1213 register U_CHAR
*p
, *lastp
, *exp_p
;
1214 struct reflist
*endpat
= NULL
;
1215 /* Pointer to first nonspace after last ## seen. */
1217 /* Pointer to first nonspace after last single-# seen. */
1218 U_CHAR
*stringify
= 0;
1220 int expected_delimiter
= '\0';
1222 /* Scan thru the replacement list, ignoring comments and quoted
1223 strings, picking up on the macro calls. It does a linear search
1224 thru the arg list on every potential symbol. Profiling might say
1225 that something smarter should happen. */
1230 /* Find the beginning of the trailing whitespace. */
1232 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1234 /* Allocate space for the text in the macro definition.
1235 Leading and trailing whitespace chars need 2 bytes each.
1236 Each other input char may or may not need 1 byte,
1237 so this is an upper bound. The extra 5 are for invented
1238 leading and trailing newline-marker and final null. */
1239 maxsize
= (sizeof (DEFINITION
)
1241 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1245 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1247 defn
->nargs
= nargs
;
1248 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1253 /* Add one initial space escape-marker to prevent accidental
1254 token-pasting (often removed by macroexpand). */
1258 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1259 cpp_error (pfile
, "`##' at start of macro definition");
1263 /* Process the main body of the definition. */
1265 int skipped_arg
= 0;
1266 register U_CHAR c
= *p
++;
1270 if (!CPP_TRADITIONAL (pfile
)) {
1274 if (expected_delimiter
!= '\0') {
1275 if (c
== expected_delimiter
)
1276 expected_delimiter
= '\0';
1278 expected_delimiter
= c
;
1282 if (p
< limit
&& expected_delimiter
) {
1283 /* In a string, backslash goes through
1284 and makes next char ordinary. */
1290 /* An '@' in a string or character constant stands for itself,
1291 and does not need to be escaped. */
1292 if (!expected_delimiter
)
1297 /* # is ordinary inside a string. */
1298 if (expected_delimiter
)
1300 if (p
< limit
&& *p
== '#') {
1301 /* ##: concatenate preceding and following tokens. */
1302 /* Take out the first #, discard preceding whitespace. */
1304 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1306 /* Skip the second #. */
1308 /* Discard following whitespace. */
1309 SKIP_WHITE_SPACE (p
);
1312 cpp_error (pfile
, "`##' at end of macro definition");
1313 } else if (nargs
>= 0) {
1314 /* Single #: stringify following argument ref.
1315 Don't leave the # in the expansion. */
1317 SKIP_WHITE_SPACE (p
);
1318 if (p
== limit
|| ! is_idstart
[*p
]
1319 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' || p
[1] == '"')))
1321 "`#' operator is not followed by a macro argument name");
1328 /* In -traditional mode, recognize arguments inside strings and
1329 and character constants, and ignore special properties of #.
1330 Arguments inside strings are considered "stringified", but no
1331 extra quote marks are supplied. */
1335 if (expected_delimiter
!= '\0') {
1336 if (c
== expected_delimiter
)
1337 expected_delimiter
= '\0';
1339 expected_delimiter
= c
;
1343 /* Backslash quotes delimiters and itself, but not macro args. */
1344 if (expected_delimiter
!= 0 && p
< limit
1345 && (*p
== expected_delimiter
|| *p
== '\\')) {
1352 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1355 /* If we find a comment that wasn't removed by handle_directive,
1356 this must be -traditional. So replace the comment with
1360 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1363 /* Mark this as a concatenation-point, as if it had been ##. */
1371 /* Handle the start of a symbol. */
1372 if (is_idchar
[c
] && nargs
> 0) {
1373 U_CHAR
*id_beg
= p
- 1;
1377 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1378 id_len
= p
- id_beg
;
1381 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
1382 register struct arglist
*arg
;
1384 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1385 struct reflist
*tpat
;
1387 if (arg
->name
[0] == c
1388 && arg
->length
== id_len
1389 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1390 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1391 if (CPP_TRADITIONAL (pfile
)) {
1392 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1396 "macro arg `%.*s' would be stringified with -traditional.",
1400 /* If ANSI, don't actually substitute inside a string. */
1401 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1403 /* make a pat node for this arg and append it to the end of
1405 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1407 tpat
->raw_before
= concat
== id_beg
;
1408 tpat
->raw_after
= 0;
1409 tpat
->rest_args
= arg
->rest_args
;
1410 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1411 ? expected_delimiter
!= '\0'
1412 : stringify
== id_beg
);
1415 defn
->pattern
= tpat
;
1417 endpat
->next
= tpat
;
1420 tpat
->argno
= arg
->argno
;
1421 tpat
->nchars
= exp_p
- lastp
;
1423 register U_CHAR
*p1
= p
;
1424 SKIP_WHITE_SPACE (p1
);
1425 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1426 tpat
->raw_after
= 1;
1428 lastp
= exp_p
; /* place to start copying from next time */
1435 /* If this was not a macro arg, copy it into the expansion. */
1436 if (! skipped_arg
) {
1437 register U_CHAR
*lim1
= p
;
1441 if (stringify
== id_beg
)
1443 "`#' operator should be followed by a macro argument name");
1448 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1450 /* If ANSI, put in a "@ " marker to prevent token pasting.
1451 But not if "inside a string" (which in ANSI mode
1452 happens only for -D option). */
1459 defn
->length
= exp_p
- defn
->expansion
;
1461 /* Crash now if we overrun the allocated size. */
1462 if (defn
->length
+ 1 > maxsize
)
1466 /* This isn't worth the time it takes. */
1467 /* give back excess storage */
1468 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1475 * special extension string that can be added to the last macro argument to
1476 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1477 * #define wow(a, b...) process (b, a, b)
1478 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1479 * { wow (one, two); } -> { process (two, one, two); }
1480 * if this "rest_arg" is used with the concat token '##' and if it is not
1481 * supplied then the token attached to with ## will not be outputted. Ex:
1482 * #define wow (a, b...) process (b ## , a, ## b)
1483 * { wow (1, 2); } -> { process (2, 1, 2); }
1484 * { wow (one); } -> { process (one); {
1486 static char rest_extension
[] = "...";
1487 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1489 /* Create a DEFINITION node from a #define directive. Arguments are
1490 as for do_define. */
1493 create_definition (buf
, limit
, pfile
, predefinition
)
1494 U_CHAR
*buf
, *limit
;
1498 U_CHAR
*bp
; /* temp ptr into input buffer */
1499 U_CHAR
*symname
; /* remember where symbol name starts */
1500 int sym_length
; /* and how long it is */
1503 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1505 int arglengths
= 0; /* Accumulate lengths of arg names
1506 plus number of args. */
1508 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1512 while (is_hor_space
[*bp
])
1515 symname
= bp
; /* remember where it starts */
1517 sym_length
= check_macro_name (pfile
, bp
, "macro");
1520 /* Lossage will occur if identifiers or control keywords are broken
1521 across lines using backslash. This is not the right place to take
1525 struct arglist
*arg_ptrs
= NULL
;
1528 bp
++; /* skip '(' */
1529 SKIP_WHITE_SPACE (bp
);
1531 /* Loop over macro argument names. */
1532 while (*bp
!= ')') {
1533 struct arglist
*temp
;
1535 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1537 temp
->next
= arg_ptrs
;
1538 temp
->argno
= argno
++;
1539 temp
->rest_args
= 0;
1543 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1545 if (!is_idstart
[*bp
])
1546 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1548 /* Find the end of the arg name. */
1549 while (is_idchar
[*bp
]) {
1551 /* do we have a "special" rest-args extension here? */
1552 if (limit
- bp
> REST_EXTENSION_LENGTH
1553 && strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1555 temp
->rest_args
= 1;
1559 temp
->length
= bp
- temp
->name
;
1561 bp
+= REST_EXTENSION_LENGTH
;
1562 arglengths
+= temp
->length
+ 2;
1563 SKIP_WHITE_SPACE (bp
);
1564 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1565 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1570 SKIP_WHITE_SPACE (bp
);
1573 cpp_error (pfile
, "unterminated parameter list in `#define'");
1577 struct arglist
*otemp
;
1579 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1580 if (temp
->length
== otemp
->length
1581 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1584 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1585 (void) strncpy (name
, temp
->name
, temp
->length
);
1586 name
[temp
->length
] = '\0';
1588 "duplicate argument name `%s' in `#define'", name
);
1594 ++bp
; /* skip paren */
1595 SKIP_WHITE_SPACE (bp
);
1596 /* now everything from bp before limit is the definition. */
1597 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1598 defn
->rest_args
= rest_args
;
1600 /* Now set defn->args.argnames to the result of concatenating
1601 the argument names in reverse order
1602 with comma-space between them. */
1603 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1605 struct arglist
*temp
;
1607 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1608 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1610 if (temp
->next
!= 0) {
1611 defn
->args
.argnames
[i
++] = ',';
1612 defn
->args
.argnames
[i
++] = ' ';
1615 defn
->args
.argnames
[i
] = 0;
1618 /* Simple expansion or empty definition. */
1622 if (is_hor_space
[*bp
]) {
1624 SKIP_WHITE_SPACE (bp
);
1627 case '!': case '"': case '#': case '%': case '&': case '\'':
1628 case ')': case '*': case '+': case ',': case '-': case '.':
1629 case '/': case ':': case ';': case '<': case '=': case '>':
1630 case '?': case '[': case '\\': case ']': case '^': case '{':
1631 case '|': case '}': case '~':
1632 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1633 sym_length
, symname
);
1637 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1638 sym_length
, symname
);
1643 /* now everything from bp before limit is the definition. */
1644 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1645 defn
->args
.argnames
= (U_CHAR
*) "";
1651 /* OP is null if this is a predefinition */
1652 defn
->predefined
= predefinition
;
1654 mdef
.symnam
= symname
;
1655 mdef
.symlen
= sym_length
;
1664 /* Check a purported macro name SYMNAME, and yield its length.
1665 USAGE is the kind of name this is intended for. */
1668 check_macro_name (pfile
, symname
, usage
)
1676 for (p
= symname
; is_idchar
[*p
]; p
++)
1678 sym_length
= p
- symname
;
1680 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
1681 cpp_error (pfile
, "invalid %s name", usage
);
1682 else if (!is_idstart
[*symname
]) {
1683 U_CHAR
*msg
; /* what pain... */
1684 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1685 bcopy (symname
, msg
, sym_length
);
1686 msg
[sym_length
] = 0;
1687 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1689 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1690 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1695 /* Return zero if two DEFINITIONs are isomorphic. */
1698 compare_defs (pfile
, d1
, d2
)
1700 DEFINITION
*d1
, *d2
;
1702 register struct reflist
*a1
, *a2
;
1703 register U_CHAR
*p1
= d1
->expansion
;
1704 register U_CHAR
*p2
= d2
->expansion
;
1707 if (d1
->nargs
!= d2
->nargs
)
1709 if (CPP_PEDANTIC (pfile
)
1710 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1712 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1713 a1
= a1
->next
, a2
= a2
->next
) {
1714 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1715 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1716 || a1
->argno
!= a2
->argno
1717 || a1
->stringify
!= a2
->stringify
1718 || a1
->raw_before
!= a2
->raw_before
1719 || a1
->raw_after
!= a2
->raw_after
)
1727 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1728 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1733 /* Return 1 if two parts of two macro definitions are effectively different.
1734 One of the parts starts at BEG1 and has LEN1 chars;
1735 the other has LEN2 chars at BEG2.
1736 Any sequence of whitespace matches any other sequence of whitespace.
1737 FIRST means these parts are the first of a macro definition;
1738 so ignore leading whitespace entirely.
1739 LAST means these parts are the last of a macro definition;
1740 so ignore trailing whitespace entirely. */
1743 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1745 U_CHAR
*beg1
, *beg2
;
1749 register U_CHAR
*end1
= beg1
+ len1
;
1750 register U_CHAR
*end2
= beg2
+ len2
;
1752 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1753 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1756 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1757 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1759 while (beg1
!= end1
&& beg2
!= end2
) {
1760 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1761 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1762 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1763 } else if (*beg1
== *beg2
) {
1767 return (beg1
!= end1
) || (beg2
!= end2
);
1770 /* Process a #define command.
1771 BUF points to the contents of the #define command, as a contiguous string.
1772 LIMIT points to the first character past the end of the definition.
1773 KEYWORD is the keyword-table entry for #define,
1774 or NULL for a "predefined" macro. */
1777 do_define (pfile
, keyword
, buf
, limit
)
1779 struct directive
*keyword
;
1780 U_CHAR
*buf
, *limit
;
1787 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1788 if (pcp_outfile
&& keyword
)
1789 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1792 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1796 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1798 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1801 /* Redefining a precompiled key is ok. */
1802 if (hp
->type
== T_PCSTRING
)
1804 /* Redefining a macro is ok if the definitions are the same. */
1805 else if (hp
->type
== T_MACRO
)
1806 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
1807 /* Redefining a constant is ok with -D. */
1808 else if (hp
->type
== T_CONST
)
1809 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1810 /* Print the warning if it's not ok. */
1813 U_CHAR
*msg
; /* what pain... */
1815 /* If we are passing through #define and #undef directives, do
1816 that for this re-definition now. */
1817 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1818 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1820 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1822 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1823 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1824 cpp_pedwarn (pfile
, msg
);
1825 if (hp
->type
== T_MACRO
)
1826 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1827 "this is the location of the previous definition");
1829 /* Replace the old definition. */
1831 hp
->value
.defn
= mdef
.defn
;
1835 /* If we are passing through #define and #undef directives, do
1836 that for this new definition now. */
1837 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1838 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1839 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1840 (char *) mdef
.defn
, hashcode
);
1850 /* This structure represents one parsed argument in a macro call.
1851 `raw' points to the argument text as written (`raw_length' is its length).
1852 `expanded' points to the argument's macro-expansion
1853 (its length is `expand_length').
1854 `stringified_length' is the length the argument would have
1856 `use_count' is the number of times this macro arg is substituted
1857 into the macro. If the actual use count exceeds 10,
1858 the value stored is 10. */
1860 /* raw and expanded are relative to ARG_BASE */
1861 #define ARG_BASE ((pfile)->token_buffer)
1864 /* Strings relative to pfile->token_buffer */
1865 long raw
, expanded
, stringified
;
1866 int raw_length
, expand_length
;
1867 int stringified_length
;
1872 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1873 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1874 as the new input buffer.
1875 Return the new buffer, or NULL on failure. */
1878 cpp_push_buffer (pfile
, buffer
, length
)
1883 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1884 if (buf
== pfile
->buffer_stack
)
1886 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1891 bzero ((char *) buf
, sizeof (cpp_buffer
));
1892 CPP_BUFFER (pfile
) = buf
;
1893 buf
->if_stack
= pfile
->if_stack
;
1894 buf
->cleanup
= null_cleanup
;
1895 buf
->underflow
= null_underflow
;
1896 buf
->buf
= buf
->cur
= buffer
;
1897 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1903 cpp_pop_buffer (pfile
)
1906 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1907 (*buf
->cleanup
) (buf
, pfile
);
1908 return ++CPP_BUFFER (pfile
);
1911 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1912 Pop the buffer when done. */
1915 cpp_scan_buffer (pfile
)
1918 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1921 enum cpp_token token
= cpp_get_token (pfile
);
1922 if (token
== CPP_EOF
) /* Should not happen ... */
1924 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1926 cpp_pop_buffer (pfile
);
1933 * Rescan a string (which may have escape marks) into pfile's buffer.
1934 * Place the result in pfile->token_buffer.
1936 * The input is copied before it is scanned, so it is safe to pass
1937 * it something from the token_buffer that will get overwritten
1938 * (because it follows CPP_WRITTEN). This is used by do_include.
1942 cpp_expand_to_buffer (pfile
, buf
, length
)
1947 register cpp_buffer
*ip
;
1951 U_CHAR
*limit
= buf
+ length
;
1954 int odepth
= indepth
;
1960 /* Set up the input on the input stack. */
1962 buf1
= (U_CHAR
*) alloca (length
+ 1);
1964 register U_CHAR
*p1
= buf
;
1965 register U_CHAR
*p2
= buf1
;
1972 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1975 ip
->has_escapes
= 1;
1977 ip
->lineno
= obuf
.lineno
= 1;
1980 /* Scan the input, create the output. */
1981 cpp_scan_buffer (pfile
);
1984 if (indepth
!= odepth
)
1988 CPP_NUL_TERMINATE (pfile
);
1993 adjust_position (buf
, limit
, linep
, colp
)
2003 (*linep
)++, (*colp
) = 1;
2009 /* Move line_base forward, updating lineno and colno. */
2012 update_position (pbuf
)
2013 register cpp_buffer
*pbuf
;
2015 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
2016 unsigned char *new_pos
= pbuf
->cur
;
2017 register struct parse_marker
*mark
;
2018 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
2020 if (pbuf
->buf
+ mark
->position
< new_pos
)
2021 new_pos
= pbuf
->buf
+ mark
->position
;
2023 pbuf
->line_base
+= new_pos
- old_pos
;
2024 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
2028 cpp_buf_line_and_col (pbuf
, linep
, colp
)
2029 register cpp_buffer
*pbuf
;
2037 *linep
= pbuf
->lineno
;
2038 *colp
= pbuf
->colno
;
2039 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2048 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2051 cpp_file_buffer (pfile
)
2054 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2056 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2057 if (ip
->fname
!= NULL
)
2063 count_newlines (buf
, limit
)
2064 register U_CHAR
*buf
;
2065 register U_CHAR
*limit
;
2067 register long count
= 0;
2078 * write out a #line command, for instance, after an #include file.
2079 * If CONDITIONAL is nonzero, we can omit the #line if it would
2080 * appear to be a no-op, and we can output a few newlines instead
2081 * if we want to increase the line number by a small amount.
2082 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2086 output_line_command (pfile
, conditional
, file_change
)
2089 enum file_change_code file_change
;
2092 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2094 if (ip
->fname
== NULL
)
2097 update_position (ip
);
2099 if (CPP_OPTIONS (pfile
)->no_line_commands
2100 || CPP_OPTIONS (pfile
)->no_output
)
2103 line
= CPP_BUFFER (pfile
)->lineno
;
2104 col
= CPP_BUFFER (pfile
)->colno
;
2105 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2107 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2111 if (line
== pfile
->lineno
)
2114 /* If the inherited line number is a little too small,
2115 output some newlines instead of a #line command. */
2116 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2117 CPP_RESERVE (pfile
, 20);
2118 while (line
> pfile
->lineno
) {
2119 CPP_PUTC_Q (pfile
, '\n');
2127 /* Don't output a line number of 0 if we can help it. */
2128 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2129 && *ip
->bufp
== '\n') {
2135 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2137 #ifdef OUTPUT_LINE_COMMANDS
2138 static char sharp_line
[] = "#line ";
2140 static char sharp_line
[] = "# ";
2142 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2145 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
2146 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2148 quote_string (pfile
, ip
->nominal_fname
);
2149 if (file_change
!= same_file
) {
2150 CPP_PUTC_Q (pfile
, ' ');
2151 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2153 /* Tell cc1 if following text comes from a system header file. */
2154 if (ip
->system_header_p
) {
2155 CPP_PUTC_Q (pfile
, ' ');
2156 CPP_PUTC_Q (pfile
, '3');
2158 #ifndef NO_IMPLICIT_EXTERN_C
2159 /* Tell cc1plus if following text should be treated as C. */
2160 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2161 CPP_PUTC_Q (pfile
, ' ');
2162 CPP_PUTC_Q (pfile
, '4');
2165 CPP_PUTC_Q (pfile
, '\n');
2166 pfile
->lineno
= line
;
2170 * Parse a macro argument and append the info on PFILE's token_buffer.
2171 * REST_ARGS means to absorb the rest of the args.
2172 * Return nonzero to indicate a syntax error.
2175 static enum cpp_token
2176 macarg (pfile
, rest_args
)
2181 enum cpp_token token
;
2182 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2183 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2185 /* Try to parse as much of the argument as exists at this
2186 input stack level. */
2187 pfile
->no_macro_expand
++;
2190 token
= cpp_get_token (pfile
);
2196 /* If we've hit end of file, it's an error (reported by caller).
2197 Ditto if it's the end of cpp_expand_to_buffer text.
2198 If we've hit end of macro, just continue. */
2199 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2210 /* if we've returned to lowest level and
2211 we aren't absorbing all args */
2212 if (paren
== 0 && rest_args
== 0)
2216 /* Remove ',' or ')' from argument buffer. */
2217 CPP_ADJUST_WRITTEN (pfile
, -1);
2224 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2225 pfile
->no_macro_expand
--;
2230 /* Turn newlines to spaces in the string of length LENGTH at START,
2231 except inside of string constants.
2232 The string is copied into itself with its beginning staying fixed. */
2235 change_newlines (start
, length
)
2239 register U_CHAR
*ibp
;
2240 register U_CHAR
*obp
;
2241 register U_CHAR
*limit
;
2245 limit
= start
+ length
;
2248 while (ibp
< limit
) {
2249 *obp
++ = c
= *ibp
++;
2254 /* Notice and skip strings, so that we don't delete newlines in them. */
2257 while (ibp
< limit
) {
2258 *obp
++ = c
= *ibp
++;
2261 if (c
== '\n' && quotec
== '\'')
2277 if (!pfile
->timebuf
) {
2278 time_t t
= time ((time_t *) 0);
2279 pfile
->timebuf
= localtime (&t
);
2281 return pfile
->timebuf
;
2284 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2285 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2289 * expand things like __FILE__. Place the expansion into the output
2290 * buffer *without* rescanning.
2294 special_symbol (hp
, pfile
)
2301 cpp_buffer
*ip
= NULL
;
2304 int paren
= 0; /* For special `defined' keyword */
2307 if (pcp_outfile
&& pcp_inside_if
2308 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2310 "Predefined macro `%s' used inside `#if' during precompilation",
2314 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2316 if (ip
== CPP_NULL_BUFFER (pfile
))
2318 cpp_error (pfile
, "cccp error: not in any file?!");
2319 return; /* the show must go on */
2321 if (ip
->fname
!= NULL
)
2331 if (hp
->type
== T_BASE_FILE
)
2333 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2334 ip
= CPP_PREV_BUFFER (ip
);
2336 string
= ip
->nominal_fname
;
2340 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2341 quote_string (pfile
, string
);
2345 case T_INCLUDE_LEVEL
:
2347 ip
= CPP_BUFFER (pfile
);
2348 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2349 if (ip
->fname
!= NULL
)
2352 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2353 sprintf (buf
, "%d", true_indepth
- 1);
2357 buf
= (char *) alloca (3 + strlen (version_string
));
2358 sprintf (buf
, "\"%s\"", version_string
);
2361 #ifndef NO_BUILTIN_SIZE_TYPE
2367 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2368 case T_PTRDIFF_TYPE
:
2374 buf
= CPP_WCHAR_TYPE (pfile
);
2377 case T_USER_LABEL_PREFIX_TYPE
:
2378 buf
= USER_LABEL_PREFIX
;
2381 case T_REGISTER_PREFIX_TYPE
:
2382 buf
= REGISTER_PREFIX
;
2386 buf
= (char *) alloca (4 * sizeof (int));
2387 sprintf (buf
, "%d", hp
->value
.ival
);
2388 #ifdef STDC_0_IN_SYSTEM_HEADERS
2389 if (ip
->system_header_p
2390 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2391 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2395 if (pcp_inside_if
&& pcp_outfile
)
2396 /* Output a precondition for this macro use */
2397 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2403 long line
= ip
->lineno
;
2404 long col
= ip
->colno
;
2405 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2407 buf
= (char *) alloca (10);
2408 sprintf (buf
, "%ld", line
);
2414 buf
= (char *) alloca (20);
2415 timebuf
= timestamp (pfile
);
2416 if (hp
->type
== T_DATE
)
2417 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2418 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2420 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2424 case T_SPEC_DEFINED
:
2425 buf
= " 0 "; /* Assume symbol is not defined */
2426 ip
= CPP_BUFFER (pfile
);
2427 SKIP_WHITE_SPACE (ip
->cur
);
2428 if (*ip
->cur
== '(')
2431 ip
->cur
++; /* Skip over the paren */
2432 SKIP_WHITE_SPACE (ip
->cur
);
2435 if (!is_idstart
[*ip
->cur
])
2437 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2439 if ((hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1)))
2442 if (pcp_outfile
&& pcp_inside_if
2443 && (hp
->type
== T_CONST
2444 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2445 /* Output a precondition for this macro use. */
2446 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2452 if (pcp_outfile
&& pcp_inside_if
)
2454 /* Output a precondition for this macro use */
2455 U_CHAR
*cp
= ip
->bufp
;
2456 fprintf (pcp_outfile
, "#undef ");
2457 while (is_idchar
[*cp
]) /* Ick! */
2458 fputc (*cp
++, pcp_outfile
);
2459 putc ('\n', pcp_outfile
);
2462 while (is_idchar
[*ip
->cur
])
2464 SKIP_WHITE_SPACE (ip
->cur
);
2467 if (*ip
->cur
!= ')')
2475 cpp_error (pfile
, "`defined' without an identifier");
2479 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2483 CPP_RESERVE (pfile
, len
+ 1);
2484 CPP_PUTS_Q (pfile
, buf
, len
);
2485 CPP_NUL_TERMINATE_Q (pfile
);
2490 /* Write out a #define command for the special named MACRO_NAME
2491 to PFILE's token_buffer. */
2494 dump_special_to_buffer (pfile
, macro_name
)
2498 static char define_directive
[] = "#define ";
2499 int macro_name_length
= strlen (macro_name
);
2500 output_line_command (pfile
, 0, same_file
);
2501 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2502 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2503 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2504 CPP_PUTC_Q (pfile
, ' ');
2505 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2506 CPP_PUTC (pfile
, '\n');
2509 /* Initialize the built-in macros. */
2512 initialize_builtins (pfile
)
2515 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2516 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2517 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2518 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2519 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2520 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2521 #ifndef NO_BUILTIN_SIZE_TYPE
2522 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2524 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2525 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2527 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2528 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2529 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2530 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2531 if (!CPP_TRADITIONAL (pfile
))
2532 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2533 if (CPP_OPTIONS (pfile
)->objc
)
2534 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2535 /* This is supplied using a -D by the compiler driver
2536 so that it is present only when truly compiling with GNU C. */
2537 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2539 if (CPP_OPTIONS (pfile
)->debug_output
)
2541 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2542 dump_special_to_buffer (pfile
, "__VERSION__");
2543 #ifndef NO_BUILTIN_SIZE_TYPE
2544 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2546 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2547 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2549 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2550 dump_special_to_buffer (pfile
, "__DATE__");
2551 dump_special_to_buffer (pfile
, "__TIME__");
2552 if (!CPP_TRADITIONAL (pfile
))
2553 dump_special_to_buffer (pfile
, "__STDC__");
2554 if (CPP_OPTIONS (pfile
)->objc
)
2555 dump_special_to_buffer (pfile
, "__OBJC__");
2559 /* Return 1 iff a token ending in C1 followed directly by a token C2
2560 could cause mis-tokenization. */
2563 unsafe_chars (c1
, c2
)
2569 if (c2
== c1
|| c2
== '=')
2573 case '0': case '1': case '2': case '3': case '4':
2574 case '5': case '6': case '7': case '8': case '9':
2575 case 'e': case 'E': case 'p': case 'P':
2576 if (c2
== '-' || c2
== '+')
2577 return 1; /* could extend a pre-processing number */
2580 if (c2
== '\'' || c2
== '\"')
2581 return 1; /* Could turn into L"xxx" or L'xxx'. */
2585 case 'a': case 'b': case 'c': case 'd': case 'f':
2586 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2587 case 'm': case 'n': case 'o': case 'q': case 'r':
2588 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2590 case 'A': case 'B': case 'C': case 'D': case 'F':
2591 case 'G': case 'H': case 'I': case 'J': case 'K':
2592 case 'M': case 'N': case 'O': case 'Q': case 'R':
2593 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2595 /* We're in the middle of either a name or a pre-processing number. */
2596 return (is_idchar
[c2
] || c2
== '.');
2597 case '<': case '>': case '!': case '%': case '#': case ':':
2598 case '^': case '&': case '|': case '*': case '/': case '=':
2599 return (c2
== c1
|| c2
== '=');
2604 /* Expand a macro call.
2605 HP points to the symbol that is the macro being called.
2606 Put the result of expansion onto the input stack
2607 so that subsequent input by our caller will use it.
2609 If macro wants arguments, caller has already verified that
2610 an argument list follows; arguments come from the input stack. */
2613 macroexpand (pfile
, hp
)
2618 DEFINITION
*defn
= hp
->value
.defn
;
2619 register U_CHAR
*xbuf
;
2620 long start_line
, start_column
;
2622 struct argdata
*args
;
2623 long old_written
= CPP_WRITTEN (pfile
);
2625 int start_line
= instack
[indepth
].lineno
;
2627 int rest_args
, rest_zero
;
2631 CHECK_DEPTH (return;);
2635 /* This macro is being used inside a #if, which means it must be */
2636 /* recorded as a precondition. */
2637 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2638 dump_single_macro (hp
, pcp_outfile
);
2641 pfile
->output_escapes
++;
2642 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2644 nargs
= defn
->nargs
;
2648 enum cpp_token token
;
2650 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2652 for (i
= 0; i
< nargs
; i
++)
2654 args
[i
].raw
= args
[i
].expanded
= 0;
2655 args
[i
].raw_length
= 0;
2656 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2657 args
[i
].use_count
= 0;
2660 /* Parse all the macro args that are supplied. I counts them.
2661 The first NARGS args are stored in ARGS.
2662 The rest are discarded. If rest_args is set then we assume
2663 macarg absorbed the rest of the args. */
2667 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2672 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2674 /* if we are working on last arg which absorbs rest of args... */
2675 if (i
== nargs
- 1 && defn
->rest_args
)
2677 args
[i
].raw
= CPP_WRITTEN (pfile
);
2678 token
= macarg (pfile
, rest_args
);
2679 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2680 args
[i
].newlines
= 0; /* FIXME */
2683 token
= macarg (pfile
, 0);
2684 if (token
== CPP_EOF
|| token
== CPP_POP
)
2686 cpp_error_with_line (pfile
, start_line
, start_column
,
2687 "unterminated macro call");
2691 } while (token
== CPP_COMMA
);
2693 /* If we got one arg but it was just whitespace, call that 0 args. */
2696 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2697 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2698 /* cpp.texi says for foo ( ) we provide one argument.
2699 However, if foo wants just 0 arguments, treat this as 0. */
2701 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2706 /* Don't output an error message if we have already output one for
2707 a parse error above. */
2709 if (nargs
== 0 && i
> 0)
2711 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2715 /* traditional C allows foo() if foo wants one argument. */
2716 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2718 /* the rest args token is allowed to absorb 0 tokens */
2719 else if (i
== nargs
- 1 && defn
->rest_args
)
2722 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2724 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2726 cpp_error (pfile
, "macro `%s' used with only %d args",
2732 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2736 /* If macro wants zero args, we parsed the arglist for checking only.
2737 Read directly from the macro definition. */
2740 xbuf
= defn
->expansion
;
2741 xbuf_len
= defn
->length
;
2745 register U_CHAR
*exp
= defn
->expansion
;
2746 register int offset
; /* offset in expansion,
2747 copied a piece at a time */
2748 register int totlen
; /* total amount of exp buffer filled so far */
2750 register struct reflist
*ap
, *last_ap
;
2752 /* Macro really takes args. Compute the expansion of this call. */
2754 /* Compute length in characters of the macro's expansion.
2755 Also count number of times each arg is used. */
2756 xbuf_len
= defn
->length
;
2757 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2761 register struct argdata
*arg
= &args
[ap
->argno
];
2762 /* Stringify it it hasn't already been */
2763 if (arg
->stringified_length
< 0)
2765 int arglen
= arg
->raw_length
;
2769 /* Initially need_space is -1. Otherwise, 1 means the
2770 previous character was a space, but we suppressed it;
2771 0 means the previous character was a non-space. */
2772 int need_space
= -1;
2774 arg
->stringified
= CPP_WRITTEN (pfile
);
2775 if (!CPP_TRADITIONAL (pfile
))
2776 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2777 for (; i
< arglen
; i
++)
2779 c
= (ARG_BASE
+ arg
->raw
)[i
];
2783 /* Internal sequences of whitespace are replaced by
2784 one space except within an string or char token.*/
2787 if (CPP_WRITTEN (pfile
) > arg
->stringified
2788 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2790 /* "@ " escape markers are removed */
2791 CPP_ADJUST_WRITTEN (pfile
, -1);
2794 if (need_space
== 0)
2798 else if (need_space
> 0)
2799 CPP_PUTC (pfile
, ' ');
2814 else if (c
== '\"' || c
== '\'')
2818 /* Escape these chars */
2819 if (c
== '\"' || (in_string
&& c
== '\\'))
2820 CPP_PUTC (pfile
, '\\');
2822 CPP_PUTC (pfile
, c
);
2825 CPP_RESERVE (pfile
, 4);
2826 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2828 CPP_ADJUST_WRITTEN (pfile
, 4);
2831 if (!CPP_TRADITIONAL (pfile
))
2832 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2833 arg
->stringified_length
2834 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2836 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2838 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2839 /* Add 4 for two newline-space markers to prevent
2840 token concatenation. */
2841 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2844 /* We have an ordinary (expanded) occurrence of the arg.
2845 So compute its expansion, if we have not already. */
2846 if (args
[ap
->argno
].expand_length
< 0)
2848 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2849 cpp_expand_to_buffer (pfile
,
2850 ARG_BASE
+ args
[ap
->argno
].raw
,
2851 args
[ap
->argno
].raw_length
);
2853 args
[ap
->argno
].expand_length
2854 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2857 /* Add 4 for two newline-space markers to prevent
2858 token concatenation. */
2859 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2861 if (args
[ap
->argno
].use_count
< 10)
2862 args
[ap
->argno
].use_count
++;
2865 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2867 /* Generate in XBUF the complete expansion
2868 with arguments substituted in.
2869 TOTLEN is the total size generated so far.
2870 OFFSET is the index in the definition
2871 of where we are copying from. */
2872 offset
= totlen
= 0;
2873 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2874 last_ap
= ap
, ap
= ap
->next
)
2876 register struct argdata
*arg
= &args
[ap
->argno
];
2877 int count_before
= totlen
;
2879 /* Add chars to XBUF. */
2880 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2881 xbuf
[totlen
++] = exp
[offset
];
2883 /* If followed by an empty rest arg with concatenation,
2884 delete the last run of nonwhite chars. */
2885 if (rest_zero
&& totlen
> count_before
2886 && ((ap
->rest_args
&& ap
->raw_before
)
2887 || (last_ap
!= NULL
&& last_ap
->rest_args
2888 && last_ap
->raw_after
)))
2890 /* Delete final whitespace. */
2891 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2894 /* Delete the nonwhites before them. */
2895 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2899 if (ap
->stringify
!= 0)
2901 bcopy (ARG_BASE
+ arg
->stringified
,
2902 xbuf
+ totlen
, arg
->stringified_length
);
2903 totlen
+= arg
->stringified_length
;
2905 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2907 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2908 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2911 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2912 while (p1
!= l1
&& is_idchar
[*p1
])
2913 xbuf
[totlen
++] = *p1
++;
2914 /* Delete any no-reexpansion marker that follows
2915 an identifier at the beginning of the argument
2916 if the argument is concatenated with what precedes it. */
2917 if (p1
[0] == '@' && p1
[1] == '-')
2922 /* Arg is concatenated after: delete trailing whitespace,
2923 whitespace markers, and no-reexpansion markers. */
2926 if (is_space
[l1
[-1]]) l1
--;
2927 else if (l1
[-1] == '-')
2929 U_CHAR
*p2
= l1
- 1;
2930 /* If a `-' is preceded by an odd number of newlines then it
2931 and the last newline are a no-reexpansion marker. */
2932 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2933 if ((l1
- 1 - p2
) & 1) {
2942 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2947 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2948 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2949 && !CPP_TRADITIONAL(pfile
)
2950 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2952 xbuf
[totlen
++] = '@';
2953 xbuf
[totlen
++] = ' ';
2956 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2957 totlen
+= arg
->expand_length
;
2959 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2960 && !CPP_TRADITIONAL(pfile
)
2961 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2963 xbuf
[totlen
++] = '@';
2964 xbuf
[totlen
++] = ' ';
2967 /* If a macro argument with newlines is used multiple times,
2968 then only expand the newlines once. This avoids creating
2969 output lines which don't correspond to any input line,
2970 which confuses gdb and gcov. */
2971 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2973 /* Don't bother doing change_newlines for subsequent
2977 = change_newlines (expanded
, arg
->expand_length
);
2981 if (totlen
> xbuf_len
)
2985 /* if there is anything left of the definition
2986 after handling the arg list, copy that in too. */
2988 for (i
= offset
; i
< defn
->length
; i
++)
2990 /* if we've reached the end of the macro */
2993 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2994 && last_ap
->raw_after
))
2995 xbuf
[totlen
++] = exp
[i
];
3003 pfile
->output_escapes
--;
3005 /* Now put the expansion on the input stack
3006 so our caller will commence reading from it. */
3007 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
3008 CPP_BUFFER (pfile
)->has_escapes
= 1;
3010 /* Pop the space we've used in the token_buffer for argument expansion. */
3011 CPP_SET_WRITTEN (pfile
, old_written
);
3013 /* Recursive macro use sometimes works traditionally.
3014 #define foo(x,y) bar (x (y,0), y)
3017 if (!CPP_TRADITIONAL (pfile
))
3018 hp
->type
= T_DISABLED
;
3022 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
3024 register U_CHAR
*xbuf
;
3028 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
3031 mbuf
->cleanup
= macro_cleanup
;
3034 /* The first chars of the expansion should be a "@ " added by
3035 collect_expansion. This is to prevent accidental token-pasting
3036 between the text preceding the macro invocation, and the macro
3039 We would like to avoid adding unneeded spaces (for the sake of
3040 tools that use cpp, such as imake). In some common cases we can
3041 tell that it is safe to omit the space.
3043 The character before the macro invocation cannot have been an
3044 idchar (or else it would have been pasted with the idchars of
3045 the macro name). Therefore, if the first non-space character
3046 of the expansion is an idchar, we do not need the extra space
3047 to prevent token pasting.
3049 Also, we don't need the extra space if the first char is '(',
3050 or some other (less common) characters. */
3052 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3053 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3054 || xbuf
[2] == '\"'))
3058 /* Like cpp_get_token, except that it does not read past end-of-line.
3059 Also, horizontal space is skipped, and macros are popped. */
3061 static enum cpp_token
3062 get_directive_token (pfile
)
3067 long old_written
= CPP_WRITTEN (pfile
);
3068 enum cpp_token token
;
3069 cpp_skip_hspace (pfile
);
3070 if (PEEKC () == '\n')
3072 token
= cpp_get_token (pfile
);
3076 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3078 /* ... else fall though ... */
3079 case CPP_HSPACE
: case CPP_COMMENT
:
3080 CPP_SET_WRITTEN (pfile
, old_written
);
3088 /* Handle #include and #import.
3089 This function expects to see "fname" or <fname> on the input.
3091 The input is normally in part of the output_buffer following
3092 CPP_WRITTEN, and will get overwritten by output_line_command.
3093 I.e. in input file specification has been popped by handle_directive.
3097 do_include (pfile
, keyword
, unused1
, unused2
)
3099 struct directive
*keyword
;
3100 U_CHAR
*unused1
, *unused2
;
3102 int importing
= (keyword
->type
== T_IMPORT
);
3103 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3104 char *fname
; /* Dynamically allocated fname buffer */
3106 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3107 enum cpp_token token
;
3109 /* Chain of dirs to search */
3110 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3111 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3112 struct file_name_list
*searchptr
= 0;
3113 long old_written
= CPP_WRITTEN (pfile
);
3117 int f
; /* file number */
3119 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3126 f
= -1; /* JF we iz paranoid! */
3128 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3131 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
3133 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
3136 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3137 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3138 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3140 pfile
->import_warning
= 1;
3141 cpp_warning (pfile
, "using `#import' is not recommended");
3142 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3143 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3144 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3145 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3146 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3147 fprintf (stderr
, " ... <real contents of file> ...\n");
3148 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3149 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3150 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3151 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3154 pfile
->parsing_include_directive
++;
3155 token
= get_directive_token (pfile
);
3156 pfile
->parsing_include_directive
--;
3158 if (token
== CPP_STRING
)
3160 /* FIXME - check no trailing garbage */
3161 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3162 fend
= CPP_PWRITTEN (pfile
) - 1;
3163 if (fbeg
[-1] == '<')
3166 /* If -I-, start with the first -I dir after the -I-. */
3167 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3168 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3170 /* If -I- was specified, don't search current dir, only spec'd ones. */
3171 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3173 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3174 /* We have "filename". Figure out directory this source
3175 file is coming from and put it on the front of the list. */
3177 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3182 if ((nam
= fp
->nominal_fname
) != NULL
)
3184 /* Found a named file. Figure out dir of the file,
3185 and put it in front of the search list. */
3186 dsp
[0].next
= search_start
;
3189 ep
= rindex (nam
, '/');
3191 ep
= rindex (nam
, ']');
3192 if (ep
== NULL
) ep
= rindex (nam
, '>');
3193 if (ep
== NULL
) ep
= rindex (nam
, ':');
3194 if (ep
!= NULL
) ep
++;
3199 dsp
[0].fname
= (char *) alloca (n
+ 1);
3200 strncpy (dsp
[0].fname
, nam
, n
);
3201 dsp
[0].fname
[n
] = '\0';
3202 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3203 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3207 dsp
[0].fname
= 0; /* Current directory */
3209 dsp
[0].got_name_map
= 0;
3216 else if (token
== CPP_NAME
)
3219 * Support '#include xyz' like VAX-C to allow for easy use of all the
3220 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3221 * code from case '<' is repeated here) and generates a warning.
3224 "VAX-C-style include specification found, use '#include <filename.h>' !");
3226 /* If -I-, start with the first -I dir after the -I-. */
3227 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3228 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3229 fbeg
= pfile
->token_buffer
+ old_written
;
3230 fend
= CPP_PWRITTEN (pfile
);
3236 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3237 CPP_SET_WRITTEN (pfile
, old_written
);
3238 skip_rest_of_line (pfile
);
3244 token
= get_directive_token (pfile
);
3245 if (token
!= CPP_VSPACE
)
3247 cpp_error (pfile
, "junk at end of `#include'");
3248 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3249 token
= get_directive_token (pfile
);
3252 /* For #include_next, skip in the search path
3253 past the dir in which the containing file was found. */
3256 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3257 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3258 if (fp
->fname
!= NULL
)
3260 /* fp->dir is null if the containing file was specified with
3261 an absolute file name. In that case, don't skip anything. */
3262 if (fp
->dir
== SELF_DIR_DUMMY
)
3263 search_start
= CPP_OPTIONS (pfile
)->include
;
3265 search_start
= fp
->dir
->next
;
3270 CPP_SET_WRITTEN (pfile
, old_written
);
3276 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3280 /* Allocate this permanently, because it gets stored in the definitions
3282 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3283 /* + 2 above for slash and terminating null. */
3284 /* + 2 added for '.h' on VMS (to support '#include filename') */
3286 /* If specified file name is absolute, just open it. */
3289 strncpy (fname
, fbeg
, flen
);
3291 if (redundant_include_p (pfile
, fname
))
3294 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3296 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3298 return 0; /* Already included this file */
3300 /* Search directory path, trying to open the file.
3301 Copy each filename tried into FNAME. */
3303 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3304 if (searchptr
->fname
) {
3305 /* The empty string in a search path is ignored.
3306 This makes it possible to turn off entirely
3307 a standard piece of the list. */
3308 if (searchptr
->fname
[0] == 0)
3310 strcpy (fname
, searchptr
->fname
);
3311 strcat (fname
, "/");
3312 fname
[strlen (fname
) + flen
] = 0;
3316 strncat (fname
, fbeg
, flen
);
3318 /* Change this 1/2 Unix 1/2 VMS file specification into a
3319 full VMS file specification */
3320 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3321 /* Fix up the filename */
3322 hack_vms_include_specification (fname
);
3324 /* This is a normal VMS filespec, so use it unchanged. */
3325 strncpy (fname
, fbeg
, flen
);
3327 /* if it's '#include filename', add the missing .h */
3328 if (index(fname
,'.')==NULL
) {
3329 strcat (fname
, ".h");
3333 /* ??? There are currently 3 separate mechanisms for avoiding processing
3334 of redundant include files: #import, #pragma once, and
3335 redundant_include_p. It would be nice if they were unified. */
3336 if (redundant_include_p (pfile
, fname
))
3339 f
= lookup_import (pfile
, fname
, searchptr
);
3341 f
= open_include_file (pfile
, fname
, searchptr
);
3343 return 0; /* Already included this file */
3345 else if (f
== -1 && errno
== EACCES
)
3346 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3356 /* A file that was not found. */
3357 strncpy (fname
, fbeg
, flen
);
3359 /* If generating dependencies and -MG was specified, we assume missing
3360 files are leaf files, living in the same directory as the source file
3361 or other similar place; these missing files may be generated from
3362 other files and may not exist yet (eg: y.tab.h). */
3364 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3365 && CPP_PRINT_DEPS (pfile
)
3366 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3368 /* If it was requested as a system header file,
3369 then assume it belongs in the first place to look for such. */
3372 for (searchptr
= search_start
; searchptr
;
3373 searchptr
= searchptr
->next
)
3375 if (searchptr
->fname
)
3379 if (searchptr
->fname
[0] == 0)
3381 p
= (char *) alloca (strlen (searchptr
->fname
)
3382 + strlen (fname
) + 2);
3383 strcpy (p
, searchptr
->fname
);
3386 deps_output (pfile
, p
, ' ');
3393 /* Otherwise, omit the directory, as if the file existed
3394 in the directory with the source. */
3395 deps_output (pfile
, fname
, ' ');
3398 /* If -M was specified, and this header file won't be added to the
3399 dependency list, then don't count this as an error, because we can
3400 still produce correct output. Otherwise, we can't produce correct
3401 output, because there may be dependencies we need inside the missing
3402 file, and we don't know what directory this missing file exists in.*/
3403 else if (CPP_PRINT_DEPS (pfile
)
3404 && (CPP_PRINT_DEPS (pfile
)
3405 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3406 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3407 else if (search_start
)
3408 cpp_error_from_errno (pfile
, fname
);
3410 cpp_error (pfile
, "No include path in which to find %s", fname
);
3413 /* Check to see if this include file is a once-only include file.
3416 struct file_name_list
*ptr
;
3418 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3419 if (!strcmp (ptr
->fname
, fname
)) {
3421 return 0; /* This file was once'd. */
3425 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3426 if (!strcmp (ptr
->fname
, fname
))
3427 break; /* This file was included before. */
3431 /* This is the first time for this file. */
3432 /* Add it to list of files included. */
3434 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3435 ptr
->control_macro
= 0;
3436 ptr
->c_system_include_path
= 0;
3437 ptr
->next
= pfile
->all_include_files
;
3438 pfile
->all_include_files
= ptr
;
3439 ptr
->fname
= savestring (fname
);
3440 ptr
->got_name_map
= 0;
3442 /* For -M, add this file to the dependencies. */
3443 if (CPP_PRINT_DEPS (pfile
)
3444 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3445 deps_output (pfile
, fname
, ' ');
3448 /* Handle -H option. */
3449 if (CPP_OPTIONS(pfile
)->print_include_names
)
3451 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3452 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3454 fprintf (stderr
, "%s\n", fname
);
3458 pfile
->system_include_depth
++;
3460 /* Actually process the file. */
3462 /* Record file on "seen" list for #import. */
3463 add_import (pfile
, f
, fname
);
3465 pcftry
= (char *) alloca (strlen (fname
) + 30);
3477 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3479 pcf
= open (pcftry
, O_RDONLY
, 0666);
3485 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3487 || stat_f
.st_dev
!= s
.st_dev
)
3489 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3490 /* Don't need it any more. */
3495 /* Don't need it at all. */
3500 } while (pcf
!= -1 && !pcfbuf
);
3504 /* Actually process the file */
3505 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3507 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3508 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3510 output_line_command (pfile
, 0, enter_file
);
3511 pfile
->only_seen_white
= 2;
3515 pfile
->system_include_depth
--;
3520 /* Return nonzero if there is no need to include file NAME
3521 because it has already been included and it contains a conditional
3522 to make a repeated include do nothing. */
3525 redundant_include_p (pfile
, name
)
3529 struct file_name_list
*l
= pfile
->all_include_files
;
3530 for (; l
; l
= l
->next
)
3531 if (! strcmp (name
, l
->fname
)
3533 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3538 /* Return nonzero if the given FILENAME is an absolute pathname which
3539 designates a file within one of the known "system" include file
3540 directories. We assume here that if the given FILENAME looks like
3541 it is the name of a file which resides either directly in a "system"
3542 include file directory, or within any subdirectory thereof, then the
3543 given file must be a "system" include file. This function tells us
3544 if we should suppress pedantic errors/warnings for the given FILENAME.
3546 The value is 2 if the file is a C-language system header file
3547 for which C++ should (on most systems) assume `extern "C"'. */
3550 is_system_include (pfile
, filename
)
3552 register char *filename
;
3554 struct file_name_list
*searchptr
;
3556 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3557 searchptr
= searchptr
->next
)
3558 if (searchptr
->fname
) {
3559 register char *sys_dir
= searchptr
->fname
;
3560 register unsigned length
= strlen (sys_dir
);
3562 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3564 if (searchptr
->c_system_include_path
)
3575 * Install a name in the assertion hash table.
3577 * If LEN is >= 0, it is the length of the name.
3578 * Otherwise, compute the length by scanning the entire name.
3580 * If HASH is >= 0, it is the precomputed hash code.
3581 * Otherwise, compute the hash code.
3584 static ASSERTION_HASHNODE
*
3585 assertion_install (pfile
, name
, len
, hash
)
3591 register ASSERTION_HASHNODE
*hp
;
3592 register int i
, bucket
;
3593 register U_CHAR
*p
, *q
;
3595 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3596 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3598 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3599 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3600 pfile
->assertion_hashtab
[bucket
] = hp
;
3602 if (hp
->next
!= NULL
)
3603 hp
->next
->prev
= hp
;
3606 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3609 for (i
= 0; i
< len
; i
++)
3615 * find the most recent hash node for name name (ending with first
3616 * non-identifier char) installed by install
3618 * If LEN is >= 0, it is the length of the name.
3619 * Otherwise, compute the length by scanning the entire name.
3621 * If HASH is >= 0, it is the precomputed hash code.
3622 * Otherwise, compute the hash code.
3625 static ASSERTION_HASHNODE
*
3626 assertion_lookup (pfile
, name
, len
, hash
)
3632 register ASSERTION_HASHNODE
*bucket
;
3634 bucket
= pfile
->assertion_hashtab
[hash
];
3636 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3638 bucket
= bucket
->next
;
3644 delete_assertion (hp
)
3645 ASSERTION_HASHNODE
*hp
;
3647 struct tokenlist_list
*tail
;
3648 if (hp
->prev
!= NULL
)
3649 hp
->prev
->next
= hp
->next
;
3650 if (hp
->next
!= NULL
)
3651 hp
->next
->prev
= hp
->prev
;
3653 for (tail
= hp
->value
; tail
; )
3655 struct tokenlist_list
*next
= tail
->next
;
3656 free_token_list (tail
->tokens
);
3661 /* Make sure that the bucket chain header that
3662 the deleted guy was on points to the right thing afterwards. */
3663 if (hp
== *hp
->bucket_hdr
)
3664 *hp
->bucket_hdr
= hp
->next
;
3669 /* Convert a character string literal into a nul-terminated string.
3670 The input string is [IN ... LIMIT).
3671 The result is placed in RESULT. RESULT can be the same as IN.
3672 The value returned in the end of the string written to RESULT,
3673 or NULL on error. */
3676 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3678 register U_CHAR
*result
, *in
, *limit
;
3698 char *bpc
= (char *) in
;
3699 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3700 in
= (U_CHAR
*) bpc
;
3702 *result
++ = (U_CHAR
)c
;
3705 /* else fall through */
3715 * interpret #line command. Remembers previously seen fnames
3716 * in its very own hash table.
3718 #define FNAME_HASHSIZE 37
3721 do_line (pfile
, keyword
)
3723 struct directive
*keyword
;
3725 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3727 long old_written
= CPP_WRITTEN (pfile
);
3728 enum file_change_code file_change
= same_file
;
3729 enum cpp_token token
;
3731 token
= get_directive_token (pfile
);
3733 if (token
!= CPP_NUMBER
3734 || !isdigit(pfile
->token_buffer
[old_written
]))
3736 cpp_error (pfile
, "invalid format `#line' command");
3737 goto bad_line_directive
;
3740 /* The Newline at the end of this line remains to be processed.
3741 To put the next line at the specified line number,
3742 we must store a line number now that is one less. */
3743 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3744 CPP_SET_WRITTEN (pfile
, old_written
);
3746 /* NEW_LINENO is one less than the actual line number here. */
3747 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3748 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3750 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3751 if (PEEKC() && !is_space
[PEEKC()]) {
3752 cpp_error (pfile
, "invalid format `#line' command");
3753 goto bad_line_directive
;
3757 token
= get_directive_token (pfile
);
3759 if (token
== CPP_STRING
) {
3760 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3762 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3763 HASHNODE
*hp
, **hash_bucket
;
3768 /* Turn the file name, which is a character string literal,
3769 into a null-terminated string. Do this in place. */
3770 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3771 if (end_name
== NULL
)
3773 cpp_error (pfile
, "invalid format `#line' command");
3774 goto bad_line_directive
;
3777 fname_length
= end_name
- fname
;
3779 num_start
= CPP_WRITTEN (pfile
);
3780 token
= get_directive_token (pfile
);
3781 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3782 p
= pfile
->token_buffer
+ num_start
;
3783 if (CPP_PEDANTIC (pfile
))
3784 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3786 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3788 cpp_error (pfile
, "invalid format `#line' command");
3789 goto bad_line_directive
;
3792 file_change
= enter_file
;
3794 file_change
= leave_file
;
3796 ip
->system_header_p
= 1;
3797 else /* if (*p == 4) */
3798 ip
->system_header_p
= 2;
3800 CPP_SET_WRITTEN (pfile
, num_start
);
3801 token
= get_directive_token (pfile
);
3802 p
= pfile
->token_buffer
+ num_start
;
3803 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3804 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3805 token
= get_directive_token (pfile
);
3807 if (token
!= CPP_VSPACE
) {
3808 cpp_error (pfile
, "invalid format `#line' command");
3809 goto bad_line_directive
;
3813 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3814 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3815 if (hp
->length
== fname_length
3816 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3817 ip
->nominal_fname
= hp
->value
.cpval
;
3821 /* Didn't find it; cons up a new one. */
3822 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3823 hp
->next
= *hash_bucket
;
3826 hp
->length
= fname_length
;
3827 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3828 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3831 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3832 cpp_error (pfile
, "invalid format `#line' command");
3833 goto bad_line_directive
;
3836 ip
->lineno
= new_lineno
;
3838 skip_rest_of_line (pfile
);
3839 CPP_SET_WRITTEN (pfile
, old_written
);
3840 output_line_command (pfile
, 0, file_change
);
3845 * remove the definition of a symbol from the symbol table.
3846 * according to un*x /lib/cpp, it is not an error to undef
3847 * something that has no definitions, so it isn't one here either.
3851 do_undef (pfile
, keyword
, buf
, limit
)
3853 struct directive
*keyword
;
3854 U_CHAR
*buf
, *limit
;
3858 U_CHAR
*orig_buf
= buf
;
3861 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3862 if (pcp_outfile
&& keyword
)
3863 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3866 SKIP_WHITE_SPACE (buf
);
3867 sym_length
= check_macro_name (pfile
, buf
, "macro");
3869 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3871 /* If we are generating additional info for debugging (with -g) we
3872 need to pass through all effective #undef commands. */
3873 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3874 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3875 if (hp
->type
!= T_MACRO
)
3876 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3880 if (CPP_PEDANTIC (pfile
)) {
3882 SKIP_WHITE_SPACE (buf
);
3884 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3890 * Report an error detected by the program we are processing.
3891 * Use the text of the line in the error message.
3892 * (We use error because it prints the filename & line#.)
3896 do_error (pfile
, keyword
, buf
, limit
)
3898 struct directive
*keyword
;
3899 U_CHAR
*buf
, *limit
;
3901 int length
= limit
- buf
;
3902 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3903 bcopy (buf
, copy
, length
);
3905 SKIP_WHITE_SPACE (copy
);
3906 cpp_error (pfile
, "#error %s", copy
);
3911 * Report a warning detected by the program we are processing.
3912 * Use the text of the line in the warning message, then continue.
3913 * (We use error because it prints the filename & line#.)
3917 do_warning (pfile
, keyword
, buf
, limit
)
3919 struct directive
*keyword
;
3920 U_CHAR
*buf
, *limit
;
3922 int length
= limit
- buf
;
3923 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3924 bcopy (buf
, copy
, length
);
3926 SKIP_WHITE_SPACE (copy
);
3927 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3928 if -pedantic-errors is given, #warning should cause an error. */
3929 cpp_pedwarn (pfile
, "#warning %s", copy
);
3933 /* Remember the name of the current file being read from so that we can
3934 avoid ever including it again. */
3940 cpp_buffer
*ip
= NULL
;
3941 struct file_name_list
*new;
3943 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3945 if (ip
== CPP_NULL_BUFFER (pfile
))
3947 if (ip
->fname
!= NULL
)
3952 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3953 new->next
= pfile
->dont_repeat_files
;
3954 pfile
->dont_repeat_files
= new;
3955 new->fname
= savestring (ip
->fname
);
3956 new->control_macro
= 0;
3957 new->got_name_map
= 0;
3958 new->c_system_include_path
= 0;
3963 /* Report program identification. */
3966 do_ident (pfile
, keyword
, buf
, limit
)
3968 struct directive
*keyword
;
3969 U_CHAR
*buf
, *limit
;
3971 /* long old_written = CPP_WRITTEN (pfile);*/
3973 /* Allow #ident in system headers, since that's not user's fault. */
3974 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3975 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3977 /* Leave rest of line to be read by later calls to cpp_get_token. */
3982 /* #pragma and its argument line have already been copied to the output file.
3983 Just check for some recognized pragmas that need validation here. */
3986 do_pragma (pfile
, keyword
, buf
, limit
)
3988 struct directive
*keyword
;
3989 U_CHAR
*buf
, *limit
;
3991 while (*buf
== ' ' || *buf
== '\t')
3993 if (!strncmp (buf
, "once", 4)) {
3994 /* Allow #pragma once in system headers, since that's not the user's
3996 if (!CPP_BUFFER (pfile
)->system_header_p
)
3997 cpp_warning (pfile
, "`#pragma once' is obsolete");
4001 if (!strncmp (buf
, "implementation", 14)) {
4002 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4003 been included yet. */
4004 struct file_name_list
*ptr
;
4005 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
4007 SKIP_WHITE_SPACE (p
);
4008 if (*p
== '\n' || *p
!= '\"')
4012 p
= (U_CHAR
*) index (fname
, '\"');
4013 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
4015 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
4016 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
4017 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
4018 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
4020 "`#pragma implementation' for `%s' appears after file is included",
4029 /* This was a fun hack, but #pragma seems to start to be useful.
4030 By failing to recognize it, we pass it through unchanged to cc1. */
4033 * the behavior of the #pragma directive is implementation defined.
4034 * this implementation defines it as follows.
4041 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
4044 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4046 execl ("/usr/games/hack", "#pragma", 0);
4047 execl ("/usr/games/rogue", "#pragma", 0);
4048 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4049 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4051 fatal ("You are in a maze of twisty compiler features, all different");
4055 /* Just ignore #sccs, on systems where we define it at all. */
4058 do_sccs (pfile
, keyword
, buf
, limit
)
4060 struct directive
*keyword
;
4061 U_CHAR
*buf
, *limit
;
4063 if (CPP_PEDANTIC (pfile
))
4064 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4069 * handle #if command by
4070 * 1) inserting special `defined' keyword into the hash table
4071 * that gets turned into 0 or 1 by special_symbol (thus,
4072 * if the luser has a symbol called `defined' already, it won't
4073 * work inside the #if command)
4074 * 2) rescan the input into a temporary output buffer
4075 * 3) pass the output buffer to the yacc parser and collect a value
4076 * 4) clean up the mess left from steps 1 and 2.
4077 * 5) call conditional_skip to skip til the next #endif (etc.),
4078 * or not, depending on the value from step 3.
4082 do_if (pfile
, keyword
, buf
, limit
)
4084 struct directive
*keyword
;
4085 U_CHAR
*buf
, *limit
;
4087 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4088 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4093 * handle a #elif directive by not changing if_stack either.
4094 * see the comment above do_else.
4098 do_elif (pfile
, keyword
, buf
, limit
)
4100 struct directive
*keyword
;
4101 U_CHAR
*buf
, *limit
;
4103 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4104 cpp_error (pfile
, "`#elif' not within a conditional");
4107 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4108 cpp_error (pfile
, "`#elif' after `#else'");
4110 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4112 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4113 && strcmp (pfile
->if_stack
->fname
,
4114 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4115 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4116 fprintf (stderr
, ")\n");
4118 pfile
->if_stack
->type
= T_ELIF
;
4121 if (pfile
->if_stack
->if_succeeded
)
4122 skip_if_group (pfile
, 0);
4124 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4126 skip_if_group (pfile
, 0);
4128 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4129 output_line_command (pfile
, 1, same_file
);
4136 * evaluate a #if expression in BUF, of length LENGTH,
4137 * then parse the result as a C expression and return the value as an int.
4140 static HOST_WIDE_INT
4141 eval_if_expression (pfile
, buf
, length
)
4146 HASHNODE
*save_defined
;
4147 HOST_WIDE_INT value
;
4148 long old_written
= CPP_WRITTEN (pfile
);
4150 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4151 pfile
->pcp_inside_if
= 1;
4153 value
= cpp_parse_expr (pfile
);
4154 pfile
->pcp_inside_if
= 0;
4155 delete_macro (save_defined
); /* clean up special symbol */
4157 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4163 * routine to handle ifdef/ifndef. Try to look up the symbol,
4164 * then do or don't skip to the #endif/#else/#elif depending
4165 * on what directive is actually being processed.
4169 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4171 struct directive
*keyword
;
4172 U_CHAR
*unused1
, *unused2
;
4175 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4178 enum cpp_token token
;
4179 int start_of_file
= 0;
4180 U_CHAR
*control_macro
= 0;
4181 int old_written
= CPP_WRITTEN (pfile
);
4183 /* Detect a #ifndef at start of file (not counting comments). */
4184 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4185 start_of_file
= pfile
->only_seen_white
== 2;
4187 pfile
->no_macro_expand
++;
4188 token
= get_directive_token (pfile
);
4189 pfile
->no_macro_expand
--;
4191 ident
= pfile
->token_buffer
+ old_written
;
4192 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4193 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4195 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4197 skip
= (keyword
->type
== T_IFDEF
);
4198 if (! CPP_TRADITIONAL (pfile
))
4199 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4201 else if (token
== CPP_NAME
)
4203 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4204 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4205 if (start_of_file
&& !skip
)
4207 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4208 bcopy (ident
, control_macro
, ident_length
+ 1);
4213 skip
= (keyword
->type
== T_IFDEF
);
4214 if (! CPP_TRADITIONAL (pfile
))
4215 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4218 if (!CPP_TRADITIONAL (pfile
))
4220 cpp_skip_hspace (pfile
);
4222 if (c
!= EOF
&& c
!= '\n')
4223 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4225 skip_rest_of_line (pfile
);
4229 /* Output a precondition for this macro. */
4230 if (hp
&& hp
->value
.defn
->predefined
)
4231 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4234 fprintf (pcp_outfile
, "#undef ");
4235 while (is_idchar
[*cp
]) /* Ick! */
4236 fputc (*cp
++, pcp_outfile
);
4237 putc ('\n', pcp_outfile
);
4241 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4245 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4246 If this is a #ifndef starting at the beginning of a file,
4247 CONTROL_MACRO is the macro name tested by the #ifndef.
4248 Otherwise, CONTROL_MACRO is 0. */
4251 conditional_skip (pfile
, skip
, type
, control_macro
)
4254 enum node_type type
;
4255 U_CHAR
*control_macro
;
4257 IF_STACK_FRAME
*temp
;
4259 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4260 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4262 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4264 temp
->next
= pfile
->if_stack
;
4265 temp
->control_macro
= control_macro
;
4266 pfile
->if_stack
= temp
;
4268 pfile
->if_stack
->type
= type
;
4271 skip_if_group (pfile
, 0);
4274 ++pfile
->if_stack
->if_succeeded
;
4275 output_line_command (pfile
, 1, same_file
);
4280 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4281 * leaves input ptr at the sharp sign found.
4282 * If ANY is nonzero, return at next directive of any sort.
4286 skip_if_group (pfile
, any
)
4291 struct directive
*kt
;
4292 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4294 U_CHAR
*beg_of_line
= bp
;
4296 register int ident_length
;
4298 struct parse_marker line_start_mark
;
4300 parse_set_mark (&line_start_mark
, pfile
);
4302 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4303 static char failed
[] = "#failed\n";
4304 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4306 output_line_command (pfile
, 1, same_file
);
4310 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4312 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4313 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4314 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4316 parse_move_mark (&line_start_mark
, pfile
);
4317 if (!CPP_TRADITIONAL (pfile
))
4318 cpp_skip_hspace (pfile
);
4322 int old_written
= CPP_WRITTEN (pfile
);
4323 cpp_skip_hspace (pfile
);
4325 parse_name (pfile
, GETC());
4326 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4327 ident
= pfile
->token_buffer
+ old_written
;
4328 pfile
->limit
= ident
;
4330 if (ident_length
== 0)
4331 goto not_a_directive
;
4333 /* Handle # followed by a line number. */
4335 /* Avoid error for `###' and similar cases unless -pedantic. */
4338 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4340 IF_STACK_FRAME
*temp
;
4341 if (ident_length
== kt
->length
4342 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4344 /* If we are asked to return on next directive, do so now. */
4354 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4355 temp
->next
= pfile
->if_stack
;
4356 pfile
->if_stack
= temp
;
4358 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4360 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4361 temp
->type
= kt
->type
;
4365 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4366 validate_else (pfile
,
4367 kt
->type
== T_ELSE
? "#else" : "#endif");
4369 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4372 "`#%s' not within a conditional", kt
->name
);
4375 else if (pfile
->if_stack
== save_if_stack
)
4376 goto done
; /* found what we came for */
4378 if (kt
->type
!= T_ENDIF
)
4380 if (pfile
->if_stack
->type
== T_ELSE
)
4381 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4382 pfile
->if_stack
->type
= kt
->type
;
4386 temp
= pfile
->if_stack
;
4387 pfile
->if_stack
= temp
->next
;
4394 /* Don't let erroneous code go by. */
4395 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4396 && CPP_PEDANTIC (pfile
))
4397 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4401 /* We're in the middle of a line. Skip the rest of it. */
4408 case '/': /* possible comment */
4409 c
= skip_comment (pfile
, NULL
);
4416 old
= CPP_WRITTEN (pfile
);
4417 cpp_get_token (pfile
);
4418 CPP_SET_WRITTEN (pfile
, old
);
4421 /* Char after backslash loses its special meaning. */
4422 if (PEEKC() == '\n')
4432 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4433 static char end_failed
[] = "#endfailed\n";
4434 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4437 pfile
->only_seen_white
= 1;
4438 parse_goto_mark (&line_start_mark
, pfile
);
4439 parse_clear_mark (&line_start_mark
);
4443 * handle a #else directive. Do this by just continuing processing
4444 * without changing if_stack ; this is so that the error message
4445 * for missing #endif's etc. will point to the original #if. It
4446 * is possible that something different would be better.
4450 do_else (pfile
, keyword
, buf
, limit
)
4452 struct directive
*keyword
;
4453 U_CHAR
*buf
, *limit
;
4455 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4457 if (CPP_PEDANTIC (pfile
))
4458 validate_else (pfile
, "#else");
4459 skip_rest_of_line (pfile
);
4461 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4462 cpp_error (pfile
, "`#else' not within a conditional");
4465 /* #ifndef can't have its special treatment for containing the whole file
4466 if it has a #else clause. */
4467 pfile
->if_stack
->control_macro
= 0;
4469 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4470 cpp_error (pfile
, "`#else' after `#else'");
4471 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4472 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4473 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4474 fprintf (stderr
, ")\n");
4476 pfile
->if_stack
->type
= T_ELSE
;
4479 if (pfile
->if_stack
->if_succeeded
)
4480 skip_if_group (pfile
, 0);
4482 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4483 output_line_command (pfile
, 1, same_file
);
4489 * unstack after #endif command
4493 do_endif (pfile
, keyword
, buf
, limit
)
4495 struct directive
*keyword
;
4496 U_CHAR
*buf
, *limit
;
4498 if (CPP_PEDANTIC (pfile
))
4499 validate_else (pfile
, "#endif");
4500 skip_rest_of_line (pfile
);
4502 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4503 cpp_error (pfile
, "unbalanced `#endif'");
4506 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4507 pfile
->if_stack
= temp
->next
;
4508 if (temp
->control_macro
!= 0)
4510 /* This #endif matched a #ifndef at the start of the file.
4511 See if it is at the end of the file. */
4512 struct parse_marker start_mark
;
4515 parse_set_mark (&start_mark
, pfile
);
4519 cpp_skip_hspace (pfile
);
4524 parse_goto_mark (&start_mark
, pfile
);
4525 parse_clear_mark (&start_mark
);
4529 /* If we get here, this #endif ends a #ifndef
4530 that contains all of the file (aside from whitespace).
4531 Arrange not to include the file again
4532 if the macro that was tested is defined.
4534 Do not do this for the top-level file in a -include or any
4535 file in a -imacros. */
4539 && ! (indepth
== 1 && pfile
->no_record_file
)
4540 && ! (pfile
->no_record_file
&& no_output
))
4543 struct file_name_list
*ifile
= pfile
->all_include_files
;
4545 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4547 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4549 ifile
->control_macro
= temp
->control_macro
;
4557 output_line_command (pfile
, 1, same_file
);
4562 /* When an #else or #endif is found while skipping failed conditional,
4563 if -pedantic was specified, this is called to warn about text after
4564 the command name. P points to the first char after the command name. */
4567 validate_else (pfile
, directive
)
4572 cpp_skip_hspace (pfile
);
4574 if (c
!= EOF
&& c
!= '\n')
4576 "text following `%s' violates ANSI standard", directive
);
4579 /* Get the next token, and add it to the text in pfile->token_buffer.
4580 Return the kind of token we got. */
4583 cpp_get_token (pfile
)
4586 register int c
, c2
, c3
;
4588 long start_line
, start_column
;
4589 enum cpp_token token
;
4590 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4591 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4597 if (CPP_BUFFER (pfile
)->seen_eof
)
4599 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4606 cpp_buffer
*next_buf
4607 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4608 CPP_BUFFER (pfile
)->seen_eof
= 1;
4609 if (CPP_BUFFER (pfile
)->nominal_fname
4610 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4612 /* We're about to return from an #include file.
4613 Emit #line information now (as part of the CPP_POP) result.
4614 But the #line refers to the file we will pop to. */
4615 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4616 CPP_BUFFER (pfile
) = next_buf
;
4617 pfile
->input_stack_listing_current
= 0;
4618 output_line_command (pfile
, 0, leave_file
);
4619 CPP_BUFFER (pfile
) = cur_buffer
;
4629 struct parse_marker start_mark
;
4631 if (PEEKC () == '=')
4633 if (opts
->put_out_comments
)
4634 parse_set_mark (&start_mark
, pfile
);
4636 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4637 &start_line
, &start_column
);
4638 c
= skip_comment (pfile
, &newlines
);
4639 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4640 parse_clear_mark (&start_mark
);
4645 cpp_error_with_line (pfile
, start_line
, start_column
,
4646 "unterminated comment");
4649 c
= '/'; /* Initial letter of comment. */
4651 /* Comments are equivalent to spaces.
4652 For -traditional, a comment is equivalent to nothing. */
4653 if (opts
->put_out_comments
)
4655 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4656 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4657 int len
= pbuf
->cur
- start
;
4658 CPP_RESERVE(pfile
, 1 + len
);
4659 CPP_PUTC_Q (pfile
, c
);
4660 CPP_PUTS_Q (pfile
, start
, len
);
4661 pfile
->lineno
+= newlines
;
4662 parse_clear_mark (&start_mark
);
4665 else if (CPP_TRADITIONAL (pfile
))
4672 /* This may not work if cpp_get_token is called recursively,
4673 since many places look for horizontal space. */
4676 /* Copy the newlines into the output buffer, in order to
4677 avoid the pain of a #line every time a multiline comment
4679 CPP_RESERVE(pfile
, newlines
);
4680 while (--newlines
>= 0)
4682 CPP_PUTC_Q (pfile
, '\n');
4688 CPP_RESERVE(pfile
, 1);
4689 CPP_PUTC_Q (pfile
, ' ');
4693 if (opts
->for_lint
) {
4696 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4698 if (lintcmd
!= NULL
) {
4699 /* I believe it is always safe to emit this newline: */
4701 bcopy ("#pragma lint ", (char *) obp
, 13);
4703 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4708 bcopy (argbp
, (char *) obp
, arglen
);
4712 /* OK, now bring us back to the state we were in before we entered
4713 this branch. We need #line because the newline for the pragma
4714 could mess things up. */
4715 output_line_command (pfile
, 0, same_file
);
4716 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4724 /* If this is expanding a macro definition, don't recognize
4725 preprocessor directives. */
4728 /* If this is expand_into_temp_buffer, recognize them
4729 only after an actual newline at this level,
4730 not at the beginning of the input level. */
4731 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4737 if (!pfile
->only_seen_white
)
4739 if (handle_directive (pfile
))
4740 return CPP_DIRECTIVE
;
4741 pfile
->only_seen_white
= 0;
4746 /* A single quoted string is treated like a double -- some
4747 programs (e.g., troff) are perverse this way */
4748 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4749 &start_line
, &start_column
);
4750 old_written
= CPP_WRITTEN (pfile
);
4752 CPP_PUTC (pfile
, c
);
4758 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4760 /* try harder: this string crosses a macro expansion
4761 boundary. This can happen naturally if -traditional.
4762 Otherwise, only -D can make a macro with an unmatched
4764 cpp_buffer
*next_buf
4765 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4766 (*CPP_BUFFER (pfile
)->cleanup
)
4767 (CPP_BUFFER (pfile
), pfile
);
4768 CPP_BUFFER (pfile
) = next_buf
;
4771 if (!CPP_TRADITIONAL (pfile
))
4773 cpp_error_with_line (pfile
, start_line
, start_column
,
4774 "unterminated string or character constant");
4775 if (pfile
->multiline_string_line
!= start_line
4776 && pfile
->multiline_string_line
!= 0)
4777 cpp_error_with_line (pfile
,
4778 pfile
->multiline_string_line
, -1,
4779 "possible real start of unterminated constant");
4780 pfile
->multiline_string_line
= 0;
4784 CPP_PUTC (pfile
, cc
);
4788 /* Traditionally, end of line ends a string constant with
4789 no error. So exit the loop and record the new line. */
4790 if (CPP_TRADITIONAL (pfile
))
4794 cpp_error_with_line (pfile
, start_line
, start_column
,
4795 "unterminated character constant");
4798 if (CPP_PEDANTIC (pfile
)
4799 && pfile
->multiline_string_line
== 0)
4801 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4802 "string constant runs past end of line");
4804 if (pfile
->multiline_string_line
== 0)
4805 pfile
->multiline_string_line
= start_line
;
4812 /* Backslash newline is replaced by nothing at all. */
4813 CPP_ADJUST_WRITTEN (pfile
, -1);
4818 /* ANSI stupidly requires that in \\ the second \
4819 is *not* prevented from combining with a newline. */
4822 CPP_PUTC (pfile
, cc
);
4834 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4835 CPP_PWRITTEN (pfile
));
4836 pfile
->only_seen_white
= 0;
4837 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4840 if (!opts
->dollars_in_ident
)
4845 if (opts
->cplusplus
&& PEEKC () == ':')
4854 if (c2
== c
|| c2
== '=')
4864 if (PEEKC () == '=')
4871 if (c2
== '-' && opts
->chill
)
4873 /* Chill style comment */
4874 if (opts
->put_out_comments
)
4875 parse_set_mark (&start_mark
, pfile
);
4876 FORWARD(1); /* Skip second '-'. */
4884 /* Don't consider final '\n' to be part of comment. */
4890 goto return_comment
;
4892 if (c2
== '-' || c2
== '=' || c2
== '>')
4897 if (pfile
->parsing_include_directive
)
4901 CPP_PUTC (pfile
, c
);
4906 if (c
== '\n' || c
== EOF
)
4909 "missing '>' in `#include <FILENAME>'");
4915 /* else fall through */
4924 CPP_RESERVE (pfile
, 4);
4925 CPP_PUTC (pfile
, c
);
4926 CPP_PUTC (pfile
, c2
);
4930 CPP_PUTC_Q (pfile
, GETC ());
4931 CPP_NUL_TERMINATE_Q (pfile
);
4932 pfile
->only_seen_white
= 0;
4936 if (CPP_BUFFER (pfile
)->has_escapes
)
4941 if (pfile
->output_escapes
)
4942 CPP_PUTS (pfile
, "@-", 2);
4943 parse_name (pfile
, GETC ());
4946 else if (is_space
[c
])
4948 CPP_RESERVE (pfile
, 2);
4949 if (pfile
->output_escapes
)
4950 CPP_PUTC_Q (pfile
, '@');
4951 CPP_PUTC_Q (pfile
, c
);
4955 if (pfile
->output_escapes
)
4957 CPP_PUTS (pfile
, "@@", 2);
4967 CPP_RESERVE(pfile
, 2);
4968 CPP_PUTC_Q (pfile
, '.');
4972 /* FIXME - misses the case "..\\\n." */
4973 if (c2
== '.' && PEEKN(1) == '.')
4975 CPP_RESERVE(pfile
, 4);
4976 CPP_PUTC_Q (pfile
, '.');
4977 CPP_PUTC_Q (pfile
, '.');
4978 CPP_PUTC_Q (pfile
, '.');
4980 CPP_NUL_TERMINATE_Q (pfile
);
4981 pfile
->only_seen_white
= 0;
4988 pfile
->only_seen_white
= 0;
4990 CPP_RESERVE(pfile
, 3);
4991 CPP_PUTC_Q (pfile
, c
);
4992 CPP_PUTC_Q (pfile
, GETC ());
4993 CPP_NUL_TERMINATE_Q (pfile
);
4999 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
5001 CPP_PUTC (pfile
, c
);
5007 case '0': case '1': case '2': case '3': case '4':
5008 case '5': case '6': case '7': case '8': case '9':
5013 CPP_RESERVE (pfile
, 2);
5014 CPP_PUTC_Q (pfile
, c
);
5019 if (!is_idchar
[c
] && c
!= '.'
5020 && ((c2
!= 'e' && c2
!= 'E'
5021 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
5022 || (c
!= '+' && c
!= '-')))
5027 CPP_NUL_TERMINATE_Q (pfile
);
5028 pfile
->only_seen_white
= 0;
5030 case 'b': case 'c': case 'd': case 'h': case 'o':
5031 case 'B': case 'C': case 'D': case 'H': case 'O':
5032 if (opts
->chill
&& PEEKC () == '\'')
5034 pfile
->only_seen_white
= 0;
5035 CPP_RESERVE (pfile
, 2);
5036 CPP_PUTC_Q (pfile
, c
);
5037 CPP_PUTC_Q (pfile
, '\'');
5043 goto chill_number_eof
;
5046 if (c
== '\\' && PEEKC() == '\n')
5053 CPP_PUTC (pfile
, c
);
5057 CPP_RESERVE (pfile
, 2);
5058 CPP_PUTC_Q (pfile
, c
);
5059 CPP_NUL_TERMINATE_Q (pfile
);
5066 CPP_NUL_TERMINATE (pfile
);
5073 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5074 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5075 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5076 case 'x': case 'y': case 'z':
5077 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5078 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5079 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5084 unsigned char *ident
;
5085 int before_name_written
= CPP_WRITTEN (pfile
);
5087 parse_name (pfile
, c
);
5088 pfile
->only_seen_white
= 0;
5089 if (pfile
->no_macro_expand
)
5091 ident
= pfile
->token_buffer
+ before_name_written
;
5092 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5093 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5096 if (hp
->type
== T_DISABLED
)
5098 if (pfile
->output_escapes
)
5099 { /* Return "@-IDENT", followed by '\0'. */
5101 CPP_RESERVE (pfile
, 3);
5102 ident
= pfile
->token_buffer
+ before_name_written
;
5103 CPP_ADJUST_WRITTEN (pfile
, 2);
5104 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5111 /* If macro wants an arglist, verify that a '(' follows.
5112 first skip all whitespace, copying it to the output
5113 after the macro name. Then, if there is no '(',
5114 decide this is not a macro call and leave things that way. */
5115 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5117 struct parse_marker macro_mark
;
5119 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5121 cpp_buffer
*next_buf
;
5122 cpp_skip_hspace (pfile
);
5123 if (PEEKC () != EOF
)
5125 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5126 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5127 CPP_BUFFER (pfile
) = next_buf
;
5129 parse_set_mark (¯o_mark
, pfile
);
5132 cpp_skip_hspace (pfile
);
5134 is_macro_call
= c
== '(';
5140 parse_goto_mark (¯o_mark
, pfile
);
5141 parse_clear_mark (¯o_mark
);
5145 /* This is now known to be a macro call. */
5147 /* it might not actually be a macro. */
5148 if (hp
->type
!= T_MACRO
) {
5149 int xbuf_len
; U_CHAR
*xbuf
;
5150 CPP_SET_WRITTEN (pfile
, before_name_written
);
5151 special_symbol (hp
, pfile
);
5152 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5153 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5154 CPP_SET_WRITTEN (pfile
, before_name_written
);
5155 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5156 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5160 /* Expand the macro, reading arguments as needed,
5161 and push the expansion on the input stack. */
5162 macroexpand (pfile
, hp
);
5163 CPP_SET_WRITTEN (pfile
, before_name_written
);
5166 /* An extra "@ " is added to the end of a macro expansion
5167 to prevent accidental token pasting. We prefer to avoid
5168 unneeded extra spaces (for the sake of cpp-using tools like
5169 imake). Here we remove the space if it is safe to do so. */
5170 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5171 && pfile
->buffer
->rlimit
[-2] == '@'
5172 && pfile
->buffer
->rlimit
[-1] == ' ')
5174 int c1
= pfile
->buffer
->rlimit
[-3];
5175 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5176 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5177 pfile
->buffer
->rlimit
-= 2;
5182 case ' ': case '\t': case '\v': case '\r':
5185 CPP_PUTC (pfile
, c
);
5187 if (c
== EOF
|| !is_hor_space
[c
])
5201 CPP_PUTC (pfile
, c
);
5202 if (pfile
->only_seen_white
== 0)
5203 pfile
->only_seen_white
= 1;
5205 output_line_command (pfile
, 1, same_file
);
5208 case '(': token
= CPP_LPAREN
; goto char1
;
5209 case ')': token
= CPP_RPAREN
; goto char1
;
5210 case '{': token
= CPP_LBRACE
; goto char1
;
5211 case '}': token
= CPP_RBRACE
; goto char1
;
5212 case ',': token
= CPP_COMMA
; goto char1
;
5213 case ';': token
= CPP_SEMICOLON
; goto char1
;
5219 pfile
->only_seen_white
= 0;
5220 CPP_PUTC (pfile
, c
);
5226 /* Like cpp_get_token, but skip spaces and comments. */
5229 cpp_get_non_space_token (pfile
)
5232 int old_written
= CPP_WRITTEN (pfile
);
5235 enum cpp_token token
= cpp_get_token (pfile
);
5236 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5237 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5239 CPP_SET_WRITTEN (pfile
, old_written
);
5243 /* Parse an identifier starting with C. */
5246 parse_name (pfile
, c
)
5247 cpp_reader
*pfile
; int c
;
5253 if (c
== '\\' && PEEKC() == '\n')
5262 if (c
== '$' && CPP_PEDANTIC (pfile
))
5263 cpp_pedwarn ("`$' in identifier");
5265 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5266 CPP_PUTC_Q (pfile
, c
);
5271 CPP_NUL_TERMINATE_Q (pfile
);
5276 /* Maintain and search list of included files, for #import. */
5278 /* Hash a file name for import_hash_table. */
5286 while (*f
) val
+= *f
++;
5287 return (val
%IMPORT_HASH_SIZE
);
5290 /* Search for file FILENAME in import_hash_table.
5291 Return -2 if found, either a matching name or a matching inode.
5292 Otherwise, open the file and return a file descriptor if successful
5293 or -1 if unsuccessful. */
5296 lookup_import (pfile
, filename
, searchptr
)
5299 struct file_name_list
*searchptr
;
5301 struct import_file
*i
;
5307 hashval
= import_hash (filename
);
5309 /* Attempt to find file in list of already included files */
5310 i
= pfile
->import_hash_table
[hashval
];
5313 if (!strcmp (filename
, i
->name
))
5314 return -2; /* return found */
5317 /* Open it and try a match on inode/dev */
5318 fd
= open_include_file (pfile
, filename
, searchptr
);
5322 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5323 i
= pfile
->import_hash_table
[h
];
5325 /* Compare the inode and the device.
5326 Supposedly on some systems the inode is not a scalar. */
5327 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5328 && i
->dev
== sb
.st_dev
) {
5330 return -2; /* return found */
5335 return fd
; /* Not found, return open file */
5338 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5341 add_import (pfile
, fd
, fname
)
5346 struct import_file
*i
;
5350 hashval
= import_hash (fname
);
5352 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5353 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5354 strcpy (i
->name
, fname
);
5355 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5357 i
->next
= pfile
->import_hash_table
[hashval
];
5358 pfile
->import_hash_table
[hashval
] = i
;
5361 /* The file_name_map structure holds a mapping of file names for a
5362 particular directory. This mapping is read from the file named
5363 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5364 map filenames on a file system with severe filename restrictions,
5365 such as DOS. The format of the file name map file is just a series
5366 of lines with two tokens on each line. The first token is the name
5367 to map, and the second token is the actual name to use. */
5369 struct file_name_map
5371 struct file_name_map
*map_next
;
5376 #define FILE_NAME_MAP_FILE "header.gcc"
5378 /* Read a space delimited string of unlimited length from a stdio
5382 read_filename_string (ch
, f
)
5390 set
= alloc
= xmalloc (len
+ 1);
5394 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5396 if (set
- alloc
== len
)
5399 alloc
= xrealloc (alloc
, len
+ 1);
5400 set
= alloc
+ len
/ 2;
5410 /* This structure holds a linked list of file name maps, one per directory. */
5412 struct file_name_map_list
5414 struct file_name_map_list
*map_list_next
;
5415 char *map_list_name
;
5416 struct file_name_map
*map_list_map
;
5419 /* Read the file name map file for DIRNAME. */
5421 static struct file_name_map
*
5422 read_name_map (pfile
, dirname
)
5426 register struct file_name_map_list
*map_list_ptr
;
5430 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5431 map_list_ptr
= map_list_ptr
->map_list_next
)
5432 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5433 return map_list_ptr
->map_list_map
;
5435 map_list_ptr
= ((struct file_name_map_list
*)
5436 xmalloc (sizeof (struct file_name_map_list
)));
5437 map_list_ptr
->map_list_name
= savestring (dirname
);
5438 map_list_ptr
->map_list_map
= NULL
;
5440 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5441 strcpy (name
, dirname
);
5444 strcat (name
, FILE_NAME_MAP_FILE
);
5445 f
= fopen (name
, "r");
5447 map_list_ptr
->map_list_map
= NULL
;
5451 int dirlen
= strlen (dirname
);
5453 while ((ch
= getc (f
)) != EOF
)
5456 struct file_name_map
*ptr
;
5460 from
= read_filename_string (ch
, f
);
5461 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5463 to
= read_filename_string (ch
, f
);
5465 ptr
= ((struct file_name_map
*)
5466 xmalloc (sizeof (struct file_name_map
)));
5467 ptr
->map_from
= from
;
5469 /* Make the real filename absolute. */
5474 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5475 strcpy (ptr
->map_to
, dirname
);
5476 ptr
->map_to
[dirlen
] = '/';
5477 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5481 ptr
->map_next
= map_list_ptr
->map_list_map
;
5482 map_list_ptr
->map_list_map
= ptr
;
5484 while ((ch
= getc (f
)) != '\n')
5491 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5492 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5494 return map_list_ptr
->map_list_map
;
5497 /* Try to open include file FILENAME. SEARCHPTR is the directory
5498 being tried from the include file search path. This function maps
5499 filenames on file systems based on information read by
5503 open_include_file (pfile
, filename
, searchptr
)
5506 struct file_name_list
*searchptr
;
5508 if (CPP_OPTIONS (pfile
)->remap
)
5510 register struct file_name_map
*map
;
5511 register char *from
;
5514 if (searchptr
&& ! searchptr
->got_name_map
)
5516 searchptr
->name_map
= read_name_map (pfile
,
5518 ? searchptr
->fname
: ".");
5519 searchptr
->got_name_map
= 1;
5522 /* First check the mapping for the directory we are using. */
5523 if (searchptr
&& searchptr
->name_map
)
5526 if (searchptr
->fname
)
5527 from
+= strlen (searchptr
->fname
) + 1;
5528 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5530 if (! strcmp (map
->map_from
, from
))
5532 /* Found a match. */
5533 return open (map
->map_to
, O_RDONLY
, 0666);
5538 /* Try to find a mapping file for the particular directory we are
5539 looking in. Thus #include <sys/types.h> will look up sys/types.h
5540 in /usr/include/header.gcc and look up types.h in
5541 /usr/include/sys/header.gcc. */
5542 p
= rindex (filename
, '/');
5547 && strlen (searchptr
->fname
) == p
- filename
5548 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5550 /* FILENAME is in SEARCHPTR, which we've already checked. */
5551 return open (filename
, O_RDONLY
, 0666);
5561 dir
= (char *) alloca (p
- filename
+ 1);
5562 bcopy (filename
, dir
, p
- filename
);
5563 dir
[p
- filename
] = '\0';
5566 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5567 if (! strcmp (map
->map_from
, from
))
5568 return open (map
->map_to
, O_RDONLY
, 0666);
5571 return open (filename
, O_RDONLY
, 0666);
5574 /* Process the contents of include file FNAME, already open on descriptor F,
5576 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5577 "system" include directories (as decided by the `is_system_include'
5579 DIRPTR is the link in the dir path through which this file was found,
5580 or 0 if the file name was absolute or via the current directory.
5581 Return 1 on success, 0 on failure.
5583 The caller is responsible for the cpp_push_buffer. */
5586 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5590 int system_header_p
;
5591 struct file_name_list
*dirptr
;
5597 cpp_buffer
*fp
; /* For input stack frame */
5599 int missing_newline
= 0;
5602 if (fstat (f
, &st
) < 0)
5604 cpp_perror_with_name (pfile
, fname
);
5606 cpp_pop_buffer (pfile
);
5610 fp
= CPP_BUFFER (pfile
);
5611 fp
->nominal_fname
= fp
->fname
= fname
;
5616 fp
->system_header_p
= system_header_p
;
5619 fp
->cleanup
= file_cleanup
;
5621 if (S_ISREG (st
.st_mode
)) {
5622 st_size
= (size_t) st
.st_size
;
5623 if (st_size
!= st
.st_size
|| st_size
+ 2 < st_size
) {
5624 cpp_error (pfile
, "file `%s' too large", fname
);
5628 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5629 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5632 /* Read the file contents, knowing that st_size is an upper bound
5633 on the number of bytes we can read. */
5634 length
= safe_read (f
, fp
->buf
, st_size
);
5635 fp
->rlimit
= fp
->buf
+ length
;
5636 if (length
< 0) goto nope
;
5638 else if (S_ISDIR (st
.st_mode
)) {
5639 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5643 /* Cannot count its file size before reading.
5644 First read the entire file into heap and
5645 copy them into buffer on stack. */
5650 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5653 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5655 goto nope
; /* error! */
5657 if (st_size
!= bsize
)
5658 break; /* End of file */
5660 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5666 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5667 /* Backslash-newline at end is not good enough. */
5668 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5669 fp
->buf
[length
++] = '\n';
5671 missing_newline
= 1;
5674 fp
->buf
[length
] = '\0';
5675 fp
->rlimit
= fp
->buf
+ length
;
5677 /* Close descriptor now, so nesting does not use lots of descriptors. */
5680 /* Must do this before calling trigraph_pcp, so that the correct file name
5681 will be printed in warning messages. */
5683 pfile
->input_stack_listing_current
= 0;
5693 if (missing_newline
)
5696 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5697 pedwarn ("file does not end in newline");
5700 input_file_stack_tick
++;
5707 cpp_perror_with_name (pfile
, fname
);
5713 /* This is called after options have been processed.
5714 * Check options for consistency, and setup for processing input
5715 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5716 * Return 1 on success, 0 on failure.
5720 cpp_start_read (pfile
, fname
)
5724 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5725 struct cpp_pending
*pend
;
5730 /* The code looks at the defaults through this pointer, rather than through
5731 the constant structure above. This pointer gets changed if an environment
5732 variable specifies other defaults. */
5733 struct default_include
*include_defaults
= include_defaults_array
;
5735 /* Add dirs from CPATH after dirs from -I. */
5736 /* There seems to be confusion about what CPATH should do,
5737 so for the moment it is not documented. */
5738 /* Some people say that CPATH should replace the standard include dirs,
5739 but that seems pointless: it comes before them, so it overrides them
5741 p
= (char *) getenv ("CPATH");
5742 if (p
!= 0 && ! opts
->no_standard_includes
)
5743 path_include (pfile
, p
);
5745 /* Now that dollars_in_ident is known, initialize is_idchar. */
5746 initialize_char_syntax (opts
);
5748 /* Do partial setup of input buffer for the sake of generating
5749 early #line directives (when -g is in effect). */
5750 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5753 if (opts
->in_fname
== NULL
)
5754 opts
->in_fname
= "";
5755 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5758 /* Install __LINE__, etc. Must follow initialize_char_syntax
5759 and option processing. */
5760 initialize_builtins (pfile
);
5762 /* Do standard #defines and assertions
5763 that identify system and machine type. */
5765 if (!opts
->inhibit_predefs
) {
5766 char *p
= (char *) alloca (strlen (predefs
) + 1);
5767 strcpy (p
, predefs
);
5770 while (*p
== ' ' || *p
== '\t')
5772 /* Handle -D options. */
5773 if (p
[0] == '-' && p
[1] == 'D') {
5775 while (*p
&& *p
!= ' ' && *p
!= '\t')
5779 if (opts
->debug_output
)
5780 output_line_command (pfile
, 0, same_file
);
5781 cpp_define (pfile
, q
);
5782 while (*p
== ' ' || *p
== '\t')
5784 } else if (p
[0] == '-' && p
[1] == 'A') {
5785 /* Handle -A options (assertions). */
5794 past_name
= assertion
;
5795 /* Locate end of name. */
5796 while (*past_name
&& *past_name
!= ' '
5797 && *past_name
!= '\t' && *past_name
!= '(')
5799 /* Locate `(' at start of value. */
5801 while (*value
&& (*value
== ' ' || *value
== '\t'))
5803 if (*value
++ != '(')
5805 while (*value
&& (*value
== ' ' || *value
== '\t'))
5808 /* Locate end of value. */
5809 while (*past_value
&& *past_value
!= ' '
5810 && *past_value
!= '\t' && *past_value
!= ')')
5812 termination
= past_value
;
5813 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5815 if (*termination
++ != ')')
5817 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5819 /* Temporarily null-terminate the value. */
5820 save_char
= *termination
;
5821 *termination
= '\0';
5822 /* Install the assertion. */
5823 make_assertion (pfile
, "-A", assertion
);
5824 *termination
= (char) save_char
;
5826 while (*p
== ' ' || *p
== '\t')
5834 /* Now handle the command line options. */
5836 /* Do -U's, -D's and -A's in the order they were seen. */
5837 /* First reverse the list. */
5838 opts
->pending
= nreverse_pending (opts
->pending
);
5840 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5842 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5844 switch (pend
->cmd
[1])
5847 if (opts
->debug_output
)
5848 output_line_command (pfile
, 0, same_file
);
5849 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5852 if (opts
->debug_output
)
5853 output_line_command (pfile
, 0, same_file
);
5854 cpp_define (pfile
, pend
->arg
);
5857 make_assertion (pfile
, "-A", pend
->arg
);
5863 opts
->done_initializing
= 1;
5865 { /* Read the appropriate environment variable and if it exists
5866 replace include_defaults with the listed path. */
5868 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5871 epath
= getenv ("C_INCLUDE_PATH");
5874 epath
= getenv ("CPLUS_INCLUDE_PATH");
5877 epath
= getenv ("OBJC_INCLUDE_PATH");
5880 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
5883 /* If the environment var for this language is set,
5884 add to the default list of include directories. */
5886 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5888 char *startp
, *endp
;
5890 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5891 if (*startp
== PATH_SEPARATOR
)
5894 = (struct default_include
*) xmalloc ((num_dirs
5895 * sizeof (struct default_include
))
5896 + sizeof (include_defaults_array
));
5897 startp
= endp
= epath
;
5900 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5901 if ((*endp
== PATH_SEPARATOR
)
5903 strncpy (nstore
, startp
, endp
-startp
);
5905 strcpy (nstore
, ".");
5907 nstore
[endp
-startp
] = '\0';
5909 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5910 include_defaults
[num_dirs
].component
= 0;
5911 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5912 include_defaults
[num_dirs
].cxx_aware
= 1;
5916 endp
= startp
= endp
+ 1;
5920 /* Put the usual defaults back in at the end. */
5921 bcopy ((char *) include_defaults_array
,
5922 (char *) &include_defaults
[num_dirs
],
5923 sizeof (include_defaults_array
));
5927 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5928 opts
->first_system_include
= opts
->before_system
;
5930 /* Unless -fnostdinc,
5931 tack on the standard include file dirs to the specified list */
5932 if (!opts
->no_standard_includes
) {
5933 struct default_include
*p
= include_defaults
;
5934 char *specd_prefix
= opts
->include_prefix
;
5935 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5936 int default_len
= 0;
5937 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5938 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5939 default_len
= strlen (default_prefix
) - 7;
5940 default_prefix
[default_len
] = 0;
5942 /* Search "translated" versions of GNU directories.
5943 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5944 if (specd_prefix
!= 0 && default_len
!= 0)
5945 for (p
= include_defaults
; p
->fname
; p
++) {
5946 /* Some standard dirs are only for C++. */
5948 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5949 /* Does this dir start with the prefix? */
5950 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5951 /* Yes; change prefix and add to search list. */
5952 struct file_name_list
*new
5953 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5954 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5955 char *str
= (char *) xmalloc (this_len
+ 1);
5956 strcpy (str
, specd_prefix
);
5957 strcat (str
, p
->fname
+ default_len
);
5959 new->control_macro
= 0;
5960 new->c_system_include_path
= !p
->cxx_aware
;
5961 new->got_name_map
= 0;
5962 append_include_chain (pfile
, new, new);
5963 if (opts
->first_system_include
== 0)
5964 opts
->first_system_include
= new;
5968 /* Search ordinary names for GNU include directories. */
5969 for (p
= include_defaults
; p
->fname
; p
++) {
5970 /* Some standard dirs are only for C++. */
5972 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5973 struct file_name_list
*new
5974 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5975 new->control_macro
= 0;
5976 new->c_system_include_path
= !p
->cxx_aware
;
5977 new->fname
= update_path (p
->fname
, p
->component
);
5978 new->got_name_map
= 0;
5979 append_include_chain (pfile
, new, new);
5980 if (opts
->first_system_include
== 0)
5981 opts
->first_system_include
= new;
5986 /* Tack the after_include chain at the end of the include chain. */
5987 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5988 if (opts
->first_system_include
== 0)
5989 opts
->first_system_include
= opts
->after_include
;
5991 /* With -v, print the list of dirs to search. */
5992 if (opts
->verbose
) {
5993 struct file_name_list
*p
;
5994 fprintf (stderr
, "#include \"...\" search starts here:\n");
5995 for (p
= opts
->include
; p
; p
= p
->next
) {
5996 if (p
== opts
->first_bracket_include
)
5997 fprintf (stderr
, "#include <...> search starts here:\n");
5998 fprintf (stderr
, " %s\n", p
->fname
);
6000 fprintf (stderr
, "End of search list.\n");
6003 /* Scan the -imacros files before the main input.
6004 Much like #including them, but with no_output set
6005 so that only their macro definitions matter. */
6007 opts
->no_output
++; pfile
->no_record_file
++;
6008 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6010 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
6012 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6015 cpp_perror_with_name (pfile
, pend
->arg
);
6018 if (!cpp_push_buffer (pfile
, NULL
, 0))
6020 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6021 cpp_scan_buffer (pfile
);
6024 opts
->no_output
--; pfile
->no_record_file
--;
6026 /* Copy the entire contents of the main input file into
6027 the stacked input buffer previously allocated for it. */
6028 if (fname
== NULL
|| *fname
== 0) {
6031 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
6032 cpp_pfatal_with_name (pfile
, fname
);
6034 /* -MG doesn't select the form of output and must be specified with one of
6035 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6036 inhibit compilation. */
6037 if (opts
->print_deps_missing_files
6038 && (opts
->print_deps
== 0 || !opts
->no_output
))
6040 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
6044 /* Either of two environment variables can specify output of deps.
6045 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6046 where OUTPUT_FILE is the file to write deps info to
6047 and DEPS_TARGET is the target to mention in the deps. */
6049 if (opts
->print_deps
== 0
6050 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6051 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6052 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6058 spec
= getenv ("SUNPRO_DEPENDENCIES");
6059 opts
->print_deps
= 2;
6062 opts
->print_deps
= 1;
6065 /* Find the space before the DEPS_TARGET, if there is one. */
6066 /* This should use index. (mrs) */
6067 while (*s
!= 0 && *s
!= ' ') s
++;
6070 opts
->deps_target
= s
+ 1;
6071 output_file
= (char *) xmalloc (s
- spec
+ 1);
6072 bcopy (spec
, output_file
, s
- spec
);
6073 output_file
[s
- spec
] = 0;
6077 opts
->deps_target
= 0;
6081 opts
->deps_file
= output_file
;
6082 opts
->print_deps_append
= 1;
6085 /* For -M, print the expected object file name
6086 as the target of this Make-rule. */
6087 if (opts
->print_deps
)
6089 pfile
->deps_allocated_size
= 200;
6090 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6091 pfile
->deps_buffer
[0] = 0;
6092 pfile
->deps_size
= 0;
6093 pfile
->deps_column
= 0;
6095 if (opts
->deps_target
)
6096 deps_output (pfile
, opts
->deps_target
, ':');
6097 else if (*opts
->in_fname
== 0)
6098 deps_output (pfile
, "-", ':');
6103 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
6104 ".cc", ".cxx", ".cpp", ".cp",
6108 /* Discard all directory prefixes from filename. */
6109 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6110 #ifdef DIR_SEPARATOR
6111 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6118 /* Copy remainder to mungable area. */
6119 p
= (char *) alloca (strlen(q
) + 8);
6122 /* Output P, but remove known suffixes. */
6125 /* Point to the filename suffix. */
6126 r
= rindex (p
, '.');
6127 /* Compare against the known suffixes. */
6129 while (known_suffixes
[x
] != 0)
6131 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
6133 /* Make q point to the bit we're going to overwrite
6134 with an object suffix. */
6141 /* Supply our own suffix. */
6148 deps_output (pfile
, p
, ':');
6149 deps_output (pfile
, opts
->in_fname
, ' ');
6154 /* Make sure data ends with a newline. And put a null after it. */
6156 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6157 /* Backslash-newline at end is not good enough. */
6158 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6159 fp
->buf
[fp
->length
++] = '\n';
6160 missing_newline
= 1;
6162 fp
->buf
[fp
->length
] = '\0';
6164 /* Unless inhibited, convert trigraphs in the input. */
6170 /* Scan the -include files before the main input.
6171 We push these in reverse order, so that the first one is handled first. */
6173 pfile
->no_record_file
++;
6174 opts
->pending
= nreverse_pending (opts
->pending
);
6175 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6177 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6179 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6182 cpp_perror_with_name (pfile
, pend
->arg
);
6185 if (!cpp_push_buffer (pfile
, NULL
, 0))
6187 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6190 pfile
->no_record_file
--;
6192 /* Free the pending list. */
6193 for (pend
= opts
->pending
; pend
; )
6195 struct cpp_pending
*next
= pend
->next
;
6199 opts
->pending
= NULL
;
6202 /* Scan the input, processing macros and directives. */
6204 rescan (&outbuf
, 0);
6206 if (missing_newline
)
6209 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6210 pedwarn ("file does not end in newline");
6213 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6214 output_line_command (pfile
, 0, same_file
);
6219 cpp_reader_init (pfile
)
6222 bzero ((char *) pfile
, sizeof (cpp_reader
));
6223 pfile
->get_token
= cpp_get_token
;
6225 pfile
->token_buffer_size
= 200;
6226 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6227 CPP_SET_WRITTEN (pfile
, 0);
6229 pfile
->system_include_depth
= 0;
6230 pfile
->dont_repeat_files
= 0;
6231 pfile
->all_include_files
= 0;
6232 pfile
->max_include_len
= 0;
6233 pfile
->timebuf
= NULL
;
6234 pfile
->only_seen_white
= 1;
6235 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6238 static struct cpp_pending
*
6239 nreverse_pending (list
)
6240 struct cpp_pending
*list
;
6243 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6244 for (pend
= list
; pend
; pend
= next
)
6254 push_pending (pfile
, cmd
, arg
)
6259 struct cpp_pending
*pend
6260 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6263 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6264 CPP_OPTIONS (pfile
)->pending
= pend
;
6267 /* Handle command-line options in (argc, argv).
6268 Can be called multiple times, to handle multiple sets of options.
6269 Returns if an unrecognized option is seen.
6270 Returns number of handled arguments. */
6273 cpp_handle_options (pfile
, argc
, argv
)
6279 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6280 for (i
= 0; i
< argc
; i
++) {
6281 if (argv
[i
][0] != '-') {
6282 if (opts
->out_fname
!= NULL
)
6284 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6287 else if (opts
->in_fname
!= NULL
)
6288 opts
->out_fname
= argv
[i
];
6290 opts
->in_fname
= argv
[i
];
6292 switch (argv
[i
][1]) {
6295 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6298 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6302 if (!strcmp (argv
[i
], "-include")
6303 || !strcmp (argv
[i
], "-imacros")) {
6305 goto missing_filename
;
6307 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6309 if (!strcmp (argv
[i
], "-iprefix")) {
6311 goto missing_filename
;
6313 opts
->include_prefix
= argv
[++i
];
6315 if (!strcmp (argv
[i
], "-ifoutput")) {
6316 opts
->output_conditionals
= 1;
6318 if (!strcmp (argv
[i
], "-isystem")) {
6319 struct file_name_list
*dirtmp
;
6322 goto missing_filename
;
6324 dirtmp
= (struct file_name_list
*)
6325 xmalloc (sizeof (struct file_name_list
));
6327 dirtmp
->control_macro
= 0;
6328 dirtmp
->c_system_include_path
= 1;
6329 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6330 strcpy (dirtmp
->fname
, argv
[++i
]);
6331 dirtmp
->got_name_map
= 0;
6333 if (opts
->before_system
== 0)
6334 opts
->before_system
= dirtmp
;
6336 opts
->last_before_system
->next
= dirtmp
;
6337 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6339 /* Add directory to end of path for includes,
6340 with the default prefix at the front of its name. */
6341 if (!strcmp (argv
[i
], "-iwithprefix")) {
6342 struct file_name_list
*dirtmp
;
6345 if (opts
->include_prefix
!= 0)
6346 prefix
= opts
->include_prefix
;
6348 prefix
= savestring (GCC_INCLUDE_DIR
);
6349 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6350 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6351 prefix
[strlen (prefix
) - 7] = 0;
6354 dirtmp
= (struct file_name_list
*)
6355 xmalloc (sizeof (struct file_name_list
));
6356 dirtmp
->next
= 0; /* New one goes on the end */
6357 dirtmp
->control_macro
= 0;
6358 dirtmp
->c_system_include_path
= 0;
6360 goto missing_dirname
;
6362 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6363 + strlen (prefix
) + 1);
6364 strcpy (dirtmp
->fname
, prefix
);
6365 strcat (dirtmp
->fname
, argv
[++i
]);
6366 dirtmp
->got_name_map
= 0;
6368 if (opts
->after_include
== 0)
6369 opts
->after_include
= dirtmp
;
6371 opts
->last_after_include
->next
= dirtmp
;
6372 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6374 /* Add directory to main path for includes,
6375 with the default prefix at the front of its name. */
6376 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6377 struct file_name_list
*dirtmp
;
6380 if (opts
->include_prefix
!= 0)
6381 prefix
= opts
->include_prefix
;
6383 prefix
= savestring (GCC_INCLUDE_DIR
);
6384 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6385 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6386 prefix
[strlen (prefix
) - 7] = 0;
6389 dirtmp
= (struct file_name_list
*)
6390 xmalloc (sizeof (struct file_name_list
));
6391 dirtmp
->next
= 0; /* New one goes on the end */
6392 dirtmp
->control_macro
= 0;
6393 dirtmp
->c_system_include_path
= 0;
6395 goto missing_dirname
;
6397 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6398 + strlen (prefix
) + 1);
6399 strcpy (dirtmp
->fname
, prefix
);
6400 strcat (dirtmp
->fname
, argv
[++i
]);
6401 dirtmp
->got_name_map
= 0;
6403 append_include_chain (pfile
, dirtmp
, dirtmp
);
6405 /* Add directory to end of path for includes. */
6406 if (!strcmp (argv
[i
], "-idirafter")) {
6407 struct file_name_list
*dirtmp
;
6409 dirtmp
= (struct file_name_list
*)
6410 xmalloc (sizeof (struct file_name_list
));
6411 dirtmp
->next
= 0; /* New one goes on the end */
6412 dirtmp
->control_macro
= 0;
6413 dirtmp
->c_system_include_path
= 0;
6415 goto missing_dirname
;
6417 dirtmp
->fname
= argv
[++i
];
6418 dirtmp
->got_name_map
= 0;
6420 if (opts
->after_include
== 0)
6421 opts
->after_include
= dirtmp
;
6423 opts
->last_after_include
->next
= dirtmp
;
6424 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6429 if (opts
->out_fname
!= NULL
)
6431 cpp_fatal (pfile
, "Output filename specified twice");
6435 goto missing_filename
;
6436 opts
->out_fname
= argv
[++i
];
6437 if (!strcmp (opts
->out_fname
, "-"))
6438 opts
->out_fname
= "";
6442 if (!strcmp (argv
[i
], "-pedantic"))
6443 CPP_PEDANTIC (pfile
) = 1;
6444 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6445 CPP_PEDANTIC (pfile
) = 1;
6446 opts
->pedantic_errors
= 1;
6449 else if (!strcmp (argv
[i
], "-pcp")) {
6450 char *pcp_fname
= argv
[++i
];
6451 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6452 ? fopen (pcp_fname
, "w")
6453 : fdopen (dup (fileno (stdout
)), "w"));
6454 if (pcp_outfile
== 0)
6455 cpp_pfatal_with_name (pfile
, pcp_fname
);
6462 if (!strcmp (argv
[i
], "-traditional")) {
6463 opts
->traditional
= 1;
6464 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6466 opts
->no_trigraphs
= 0;
6471 if (! strcmp (argv
[i
], "-lang-c"))
6472 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6474 if (! strcmp (argv
[i
], "-lang-c89"))
6475 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
6477 if (! strcmp (argv
[i
], "-lang-c++"))
6478 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6480 if (! strcmp (argv
[i
], "-lang-objc"))
6481 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6483 if (! strcmp (argv
[i
], "-lang-objc++"))
6484 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6486 if (! strcmp (argv
[i
], "-lang-asm"))
6488 if (! strcmp (argv
[i
], "-lint"))
6490 if (! strcmp (argv
[i
], "-lang-chill"))
6491 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6492 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6496 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6500 opts
->inhibit_warnings
= 1;
6504 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6505 opts
->warn_trigraphs
= 1;
6506 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6507 opts
->warn_trigraphs
= 0;
6508 else if (!strcmp (argv
[i
], "-Wcomment"))
6509 opts
->warn_comments
= 1;
6510 else if (!strcmp (argv
[i
], "-Wno-comment"))
6511 opts
->warn_comments
= 0;
6512 else if (!strcmp (argv
[i
], "-Wcomments"))
6513 opts
->warn_comments
= 1;
6514 else if (!strcmp (argv
[i
], "-Wno-comments"))
6515 opts
->warn_comments
= 0;
6516 else if (!strcmp (argv
[i
], "-Wtraditional"))
6517 opts
->warn_stringify
= 1;
6518 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6519 opts
->warn_stringify
= 0;
6520 else if (!strcmp (argv
[i
], "-Wundef"))
6521 opts
->warn_undef
= 1;
6522 else if (!strcmp (argv
[i
], "-Wno-undef"))
6523 opts
->warn_undef
= 0;
6524 else if (!strcmp (argv
[i
], "-Wimport"))
6525 opts
->warn_import
= 1;
6526 else if (!strcmp (argv
[i
], "-Wno-import"))
6527 opts
->warn_import
= 0;
6528 else if (!strcmp (argv
[i
], "-Werror"))
6529 opts
->warnings_are_errors
= 1;
6530 else if (!strcmp (argv
[i
], "-Wno-error"))
6531 opts
->warnings_are_errors
= 0;
6532 else if (!strcmp (argv
[i
], "-Wall"))
6534 opts
->warn_trigraphs
= 1;
6535 opts
->warn_comments
= 1;
6540 /* The style of the choices here is a bit mixed.
6541 The chosen scheme is a hybrid of keeping all options in one string
6542 and specifying each option in a separate argument:
6543 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6544 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6545 -M[M][G][D file]. This is awkward to handle in specs, and is not
6547 /* ??? -MG must be specified in addition to one of -M or -MM.
6548 This can be relaxed in the future without breaking anything.
6549 The converse isn't true. */
6551 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6552 if (!strcmp (argv
[i
], "-MG"))
6554 opts
->print_deps_missing_files
= 1;
6557 if (!strcmp (argv
[i
], "-M"))
6558 opts
->print_deps
= 2;
6559 else if (!strcmp (argv
[i
], "-MM"))
6560 opts
->print_deps
= 1;
6561 else if (!strcmp (argv
[i
], "-MD"))
6562 opts
->print_deps
= 2;
6563 else if (!strcmp (argv
[i
], "-MMD"))
6564 opts
->print_deps
= 1;
6565 /* For -MD and -MMD options, write deps on file named by next arg. */
6566 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6569 goto missing_filename
;
6570 opts
->deps_file
= argv
[++i
];
6574 /* For -M and -MM, write deps on standard output
6575 and suppress the usual output. */
6576 opts
->no_output
= 1;
6582 char *p
= argv
[i
] + 2;
6584 while ((c
= *p
++) != 0) {
6585 /* Arg to -d specifies what parts of macros to dump */
6588 opts
->dump_macros
= dump_only
;
6589 opts
->no_output
= 1;
6592 opts
->dump_macros
= dump_names
;
6595 opts
->dump_macros
= dump_definitions
;
6598 opts
->dump_includes
= 1;
6606 if (argv
[i
][2] == '3')
6607 opts
->debug_output
= 1;
6611 fprintf (stderr
, "GNU CPP version %s", version_string
);
6612 #ifdef TARGET_VERSION
6615 fprintf (stderr
, "\n");
6620 opts
->print_include_names
= 1;
6624 if (argv
[i
][2] != 0)
6625 push_pending (pfile
, "-D", argv
[i
] + 2);
6626 else if (i
+ 1 == argc
)
6628 cpp_fatal (pfile
, "Macro name missing after -D option");
6632 i
++, push_pending (pfile
, "-D", argv
[i
]);
6639 if (argv
[i
][2] != 0)
6641 else if (i
+ 1 == argc
)
6643 cpp_fatal (pfile
, "Assertion missing after -A option");
6649 if (!strcmp (p
, "-")) {
6650 struct cpp_pending
**ptr
;
6651 /* -A- eliminates all predefined macros and assertions.
6652 Let's include also any that were specified earlier
6653 on the command line. That way we can get rid of any
6654 that were passed automatically in from GCC. */
6655 opts
->inhibit_predefs
= 1;
6656 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6658 struct cpp_pending
*pend
= *ptr
;
6659 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6660 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6669 push_pending (pfile
, "-A", p
);
6674 case 'U': /* JF #undef something */
6675 if (argv
[i
][2] != 0)
6676 push_pending (pfile
, "-U", argv
[i
] + 2);
6677 else if (i
+ 1 == argc
)
6679 cpp_fatal (pfile
, "Macro name missing after -U option", NULL
);
6683 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6687 opts
->put_out_comments
= 1;
6690 case 'E': /* -E comes from cc -E; ignore it. */
6694 opts
->no_line_commands
= 1;
6697 case '$': /* Don't include $ in identifiers. */
6698 opts
->dollars_in_ident
= 0;
6701 case 'I': /* Add directory to path for includes. */
6703 struct file_name_list
*dirtmp
;
6705 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6706 && !strcmp (argv
[i
] + 2, "-")) {
6707 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6708 /* Don't use any preceding -I directories for #include <...>. */
6709 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6712 dirtmp
= (struct file_name_list
*)
6713 xmalloc (sizeof (struct file_name_list
));
6714 dirtmp
->next
= 0; /* New one goes on the end */
6715 dirtmp
->control_macro
= 0;
6716 dirtmp
->c_system_include_path
= 0;
6717 if (argv
[i
][2] != 0)
6718 dirtmp
->fname
= argv
[i
] + 2;
6719 else if (i
+ 1 == argc
)
6720 goto missing_dirname
;
6722 dirtmp
->fname
= argv
[++i
];
6723 dirtmp
->got_name_map
= 0;
6724 append_include_chain (pfile
, dirtmp
, dirtmp
);
6730 if (!strcmp (argv
[i
], "-nostdinc"))
6731 /* -nostdinc causes no default include directories.
6732 You must specify all include-file directories with -I. */
6733 opts
->no_standard_includes
= 1;
6734 else if (!strcmp (argv
[i
], "-nostdinc++"))
6735 /* -nostdinc++ causes no default C++-specific include directories. */
6736 opts
->no_standard_cplusplus_includes
= 1;
6738 else if (!strcmp (argv
[i
], "-noprecomp"))
6744 if (!strcmp (argv
[i
], "-remap"))
6749 /* Sun compiler passes undocumented switch "-undef".
6750 Let's assume it means to inhibit the predefined symbols. */
6751 opts
->inhibit_predefs
= 1;
6754 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6755 if (opts
->in_fname
== NULL
) {
6756 opts
->in_fname
= "";
6758 } else if (opts
->out_fname
== NULL
) {
6759 opts
->out_fname
= "";
6761 } /* else fall through into error */
6775 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6777 if (opts
->print_deps
)
6779 /* Stream on which to print the dependency information. */
6782 /* Don't actually write the deps file if compilation has failed. */
6783 if (pfile
->errors
== 0)
6785 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6786 if (opts
->deps_file
== 0)
6787 deps_stream
= stdout
;
6788 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6789 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6790 fputs (pfile
->deps_buffer
, deps_stream
);
6791 putc ('\n', deps_stream
);
6792 if (opts
->deps_file
)
6794 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6795 cpp_fatal (pfile
, "I/O error on output");
6801 /* Free resources used by PFILE.
6802 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6809 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6810 cpp_pop_buffer (pfile
);
6812 if (pfile
->token_buffer
)
6814 free (pfile
->token_buffer
);
6815 pfile
->token_buffer
= NULL
;
6818 if (pfile
->deps_buffer
)
6820 free (pfile
->deps_buffer
);
6821 pfile
->deps_buffer
= NULL
;
6822 pfile
->deps_allocated_size
= 0;
6825 while (pfile
->if_stack
)
6827 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6828 pfile
->if_stack
= temp
->next
;
6832 while (pfile
->dont_repeat_files
)
6834 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6835 pfile
->dont_repeat_files
= temp
->next
;
6840 while (pfile
->all_include_files
)
6842 struct file_name_list
*temp
= pfile
->all_include_files
;
6843 pfile
->all_include_files
= temp
->next
;
6848 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6850 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6853 struct import_file
*next
= imp
->next
;
6858 pfile
->import_hash_table
[i
] = 0;
6861 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6863 while (pfile
->assertion_hashtab
[i
])
6864 delete_assertion (pfile
->assertion_hashtab
[i
]);
6867 cpp_hash_cleanup (pfile
);
6871 do_assert (pfile
, keyword
, buf
, limit
)
6873 struct directive
*keyword
;
6874 U_CHAR
*buf
, *limit
;
6876 long symstart
; /* remember where symbol name starts */
6878 int sym_length
; /* and how long it is */
6879 struct arglist
*tokens
= NULL
;
6881 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6882 && !CPP_BUFFER (pfile
)->system_header_p
)
6883 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6885 cpp_skip_hspace (pfile
);
6886 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6887 parse_name (pfile
, GETC());
6888 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6891 cpp_skip_hspace (pfile
);
6892 if (PEEKC() != '(') {
6893 cpp_error (pfile
, "missing token-sequence in `#assert'");
6899 tokens
= read_token_list (pfile
, &error_flag
);
6903 cpp_error (pfile
, "empty token-sequence in `#assert'");
6906 cpp_skip_hspace (pfile
);
6908 if (c
!= EOF
&& c
!= '\n')
6909 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6910 skip_rest_of_line (pfile
);
6913 /* If this name isn't already an assertion name, make it one.
6914 Error if it was already in use in some other way. */
6917 ASSERTION_HASHNODE
*hp
;
6918 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6919 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6920 struct tokenlist_list
*value
6921 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6923 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6925 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6926 cpp_error (pfile
, "`defined' redefined as assertion");
6927 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6930 /* Add the spec'd token-sequence to the list of such. */
6931 value
->tokens
= tokens
;
6932 value
->next
= hp
->value
;
6935 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6938 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6939 skip_rest_of_line (pfile
);
6944 do_unassert (pfile
, keyword
, buf
, limit
)
6946 struct directive
*keyword
;
6947 U_CHAR
*buf
, *limit
;
6949 long symstart
; /* remember where symbol name starts */
6950 int sym_length
; /* and how long it is */
6953 struct arglist
*tokens
= NULL
;
6954 int tokens_specified
= 0;
6956 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6957 && !CPP_BUFFER (pfile
)->system_header_p
)
6958 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6960 cpp_skip_hspace (pfile
);
6962 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6963 parse_name (pfile
, GETC());
6964 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6967 cpp_skip_hspace (pfile
);
6968 if (PEEKC() == '(') {
6971 tokens
= read_token_list (pfile
, &error_flag
);
6975 cpp_error (pfile
, "empty token list in `#unassert'");
6979 tokens_specified
= 1;
6982 cpp_skip_hspace (pfile
);
6984 if (c
!= EOF
&& c
!= '\n')
6985 cpp_error (pfile
, "junk at end of `#unassert'");
6986 skip_rest_of_line (pfile
);
6989 ASSERTION_HASHNODE
*hp
;
6990 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6991 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6992 struct tokenlist_list
*tail
, *prev
;
6994 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6998 /* If no token list was specified, then eliminate this assertion
7000 if (! tokens_specified
)
7001 delete_assertion (hp
);
7003 /* If a list of tokens was given, then delete any matching list. */
7008 struct tokenlist_list
*next
= tail
->next
;
7009 if (compare_token_lists (tail
->tokens
, tokens
)) {
7013 hp
->value
= tail
->next
;
7014 free_token_list (tail
->tokens
);
7024 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
7027 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
7028 skip_rest_of_line (pfile
);
7032 /* Test whether there is an assertion named NAME
7033 and optionally whether it has an asserted token list TOKENS.
7034 NAME is not null terminated; its length is SYM_LENGTH.
7035 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7038 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
7042 int tokens_specified
;
7043 struct arglist
*tokens
;
7045 ASSERTION_HASHNODE
*hp
;
7046 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
7048 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
7049 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
7051 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
7053 /* It is not an assertion; just return false. */
7056 /* If no token list was specified, then value is 1. */
7057 if (! tokens_specified
)
7061 struct tokenlist_list
*tail
;
7065 /* If a list of tokens was given,
7066 then succeed if the assertion records a matching list. */
7069 if (compare_token_lists (tail
->tokens
, tokens
))
7074 /* Fail if the assertion has no matching list. */
7079 /* Compare two lists of tokens for equality including order of tokens. */
7082 compare_token_lists (l1
, l2
)
7083 struct arglist
*l1
, *l2
;
7086 if (l1
->length
!= l2
->length
)
7088 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7094 /* Succeed if both lists end at the same time. */
7099 reverse_token_list (tokens
)
7100 struct arglist
*tokens
;
7102 register struct arglist
*prev
= 0, *this, *next
;
7103 for (this = tokens
; this; this = next
)
7112 /* Read a space-separated list of tokens ending in a close parenthesis.
7113 Return a list of strings, in the order they were written.
7114 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7116 static struct arglist
*
7117 read_token_list (pfile
, error_flag
)
7121 struct arglist
*token_ptrs
= 0;
7126 FORWARD (1); /* Skip '(' */
7128 /* Loop over the assertion value tokens. */
7131 struct arglist
*temp
;
7132 long name_written
= CPP_WRITTEN (pfile
);
7135 cpp_skip_hspace (pfile
);
7139 /* Find the end of the token. */
7142 CPP_PUTC (pfile
, c
);
7150 CPP_PUTC (pfile
, c
);
7152 else if (c
== '"' || c
== '\'')
7155 cpp_get_token (pfile
);
7161 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7162 && c
!= '"' && c
!= '\'')
7164 CPP_PUTC (pfile
, c
);
7167 if (c
!= EOF
) FORWARD(-1);
7170 length
= CPP_WRITTEN (pfile
) - name_written
;
7171 temp
= (struct arglist
*)
7172 xmalloc (sizeof (struct arglist
) + length
+ 1);
7173 temp
->name
= (U_CHAR
*) (temp
+ 1);
7174 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7175 (char *) temp
->name
, length
);
7176 temp
->name
[length
] = 0;
7177 temp
->next
= token_ptrs
;
7179 temp
->length
= length
;
7181 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7183 if (c
== EOF
|| c
== '\n')
7186 "unterminated token sequence following `#' operator");
7191 /* We accumulated the names in reverse order.
7192 Now reverse them to get the proper order. */
7193 return reverse_token_list (token_ptrs
);
7197 free_token_list (tokens
)
7198 struct arglist
*tokens
;
7201 struct arglist
*next
= tokens
->next
;
7202 free (tokens
->name
);
7208 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7209 retrying if necessary. If MAX_READ_LEN is defined, read at most
7210 that bytes at a time. Return a negative value if an error occurs,
7211 otherwise return the actual number of bytes read,
7212 which must be LEN unless end-of-file was reached. */
7215 safe_read (desc
, ptr
, len
)
7220 int left
, rcount
, nchars
;
7226 if (rcount
> MAX_READ_LEN
)
7227 rcount
= MAX_READ_LEN
;
7229 nchars
= read (desc
, ptr
, rcount
);
7247 xcalloc (number
, size
)
7248 unsigned number
, size
;
7250 register unsigned total
= number
* size
;
7251 register char *ptr
= (char *) xmalloc (total
);
7260 unsigned size
= strlen (input
);
7261 char *output
= xmalloc (size
+ 1);
7262 strcpy (output
, input
);
7266 /* Initialize PMARK to remember the current position of PFILE. */
7269 parse_set_mark (pmark
, pfile
)
7270 struct parse_marker
*pmark
;
7273 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7274 pmark
->next
= pbuf
->marks
;
7275 pbuf
->marks
= pmark
;
7277 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7280 /* Cleanup PMARK - we no longer need it. */
7283 parse_clear_mark (pmark
)
7284 struct parse_marker
*pmark
;
7286 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7287 for (; ; pp
= &(*pp
)->next
) {
7288 if (*pp
== NULL
) abort ();
7289 if (*pp
== pmark
) break;
7294 /* Backup the current position of PFILE to that saved in PMARK. */
7297 parse_goto_mark (pmark
, pfile
)
7298 struct parse_marker
*pmark
;
7301 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7302 if (pbuf
!= pmark
->buf
)
7303 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7304 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7307 /* Reset PMARK to point to the current position of PFILE. (Same
7308 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7311 parse_move_mark (pmark
, pfile
)
7312 struct parse_marker
*pmark
;
7315 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7316 if (pbuf
!= pmark
->buf
)
7317 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7318 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7322 cpp_read_check_assertion (pfile
)
7325 int name_start
= CPP_WRITTEN (pfile
);
7326 int name_length
, name_written
;
7328 FORWARD (1); /* Skip '#' */
7329 cpp_skip_hspace (pfile
);
7330 parse_name (pfile
, GETC ());
7331 name_written
= CPP_WRITTEN (pfile
);
7332 name_length
= name_written
- name_start
;
7333 cpp_skip_hspace (pfile
);
7334 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7337 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7338 result
= check_assertion (pfile
,
7339 pfile
->token_buffer
+ name_start
, name_length
,
7343 result
= check_assertion (pfile
,
7344 pfile
->token_buffer
+ name_start
, name_length
,
7346 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7351 cpp_print_file_and_line (pfile
)
7354 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7359 cpp_buf_line_and_col (ip
, &line
, &col
);
7360 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
7361 line
, pfile
->show_column
? col
: -1);
7366 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7369 char *arg1
, *arg2
, *arg3
;
7371 cpp_print_containing_files (pfile
);
7372 cpp_print_file_and_line (pfile
);
7373 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7376 /* Print error message but don't count it. */
7379 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7382 char *arg1
, *arg2
, *arg3
;
7384 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7387 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7390 cpp_print_containing_files (pfile
);
7391 cpp_print_file_and_line (pfile
);
7392 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7395 /* Print an error message and maybe count it. */
7398 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7401 char *arg1
, *arg2
, *arg3
;
7403 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7404 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7406 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7410 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7414 char *arg1
, *arg2
, *arg3
;
7416 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7418 cpp_print_containing_files (pfile
);
7421 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7423 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7427 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7431 char *arg1
, *arg2
, *arg3
;
7435 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7438 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7441 cpp_print_containing_files (pfile
);
7443 ip
= cpp_file_buffer (pfile
);
7446 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7448 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7452 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7456 char *arg1
, *arg2
, *arg3
;
7458 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7459 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7461 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7464 /* Report a warning (or an error if pedantic_errors)
7465 giving specified file name and line number, not current. */
7468 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7473 char *arg1
, *arg2
, *arg3
;
7475 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7476 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7479 cpp_file_line_for_message (pfile
, file
, line
, -1);
7480 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7481 msg
, arg1
, arg2
, arg3
);
7484 /* This defines "errno" properly for VMS, and gives us EACCES. */
7491 #ifndef HAVE_STRERROR
7492 extern int sys_nerr
;
7493 extern char *sys_errlist
[];
7494 #else /* HAVE_STRERROR */
7498 char *strerror (int,...);
7501 /* my_strerror - return the descriptive text associated with an
7505 my_strerror (errnum
)
7511 #ifndef HAVE_STRERROR
7512 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7514 result
= strerror (errnum
);
7517 /* VAXCRTL's strerror() takes an optional second argument, which only
7518 matters when the first argument is EVMSERR. However, it's simplest
7519 just to pass it unconditionally. `vaxc$errno' is declared in
7520 <errno.h>, and maintained by the library in parallel with `errno'.
7521 We assume that caller's `errnum' either matches the last setting of
7522 `errno' by the library or else does not have the value `EVMSERR'. */
7524 result
= strerror (errnum
, vaxc$errno
);
7528 result
= "undocumented I/O error";
7533 /* Error including a message from `errno'. */
7536 cpp_error_from_errno (pfile
, name
)
7540 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7542 cpp_print_containing_files (pfile
);
7545 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
7547 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (errno
));
7551 cpp_perror_with_name (pfile
, name
)
7555 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7559 * No pre-compiled header file support.
7561 * Possibly different enum token codes for each C/C++ token.
7563 * Should clean up remaining directives to that do_XXX functions
7564 * only take two arguments and all have command_reads_line.
7566 * Find and cleanup remaining uses of static variables,
7568 * Support for trigraphs.
7570 * Support -dM flag (dump_all_macros).
7572 * Support for_lint flag.