2 Copyright (C) 1986, 87, 89, 92-6, 1997 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.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
27 #include "../src/config.h"
35 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
36 for the sake of machines with limited C compilers. */
39 #endif /* not EMACS */
41 #ifndef STANDARD_INCLUDE_DIR
42 #define STANDARD_INCLUDE_DIR "/usr/include"
45 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
47 #define PTR_INT_TYPE ptrdiff_t
49 #define PTR_INT_TYPE long
60 /* By default, colon separates directories in a path. */
61 #ifndef PATH_SEPARATOR
62 #define PATH_SEPARATOR ':'
74 #include <sys/time.h> /* for __DATE__ and __TIME__ */
75 #include <sys/resource.h>
77 #include <sys/times.h>
87 /* This defines "errno" properly for VMS, and gives us EACCES. */
90 extern char *index ();
91 extern char *rindex ();
92 extern char *update_path ();
100 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
101 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
103 /* Find the largest host integer type and set its size and type.
104 Watch out: on some crazy hosts `long' is shorter than `int'. */
106 #ifndef HOST_WIDE_INT
108 # include <inttypes.h>
109 # define HOST_WIDE_INT intmax_t
111 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
112 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
113 # define HOST_WIDE_INT int
115 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
116 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
117 # define HOST_WIDE_INT long
119 # define HOST_WIDE_INT long long
126 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
130 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
133 /* Define a generic NULL if one hasn't already been defined. */
140 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
141 #define GENERIC_PTR void *
143 #define GENERIC_PTR char *
148 #define NULL_PTR ((GENERIC_PTR) 0)
151 #ifndef INCLUDE_LEN_FUDGE
152 #define INCLUDE_LEN_FUDGE 0
155 /* Symbols to predefine. */
157 #ifdef CPP_PREDEFINES
158 static char *predefs
= CPP_PREDEFINES
;
160 static char *predefs
= "";
163 /* We let tm.h override the types used here, to handle trivial differences
164 such as the choice of unsigned int or long unsigned int for size_t.
165 When machines start needing nontrivial differences in the size type,
166 it would be best to do something here to figure out automatically
167 from other information what type to use. */
169 /* The string value for __SIZE_TYPE__. */
172 #define SIZE_TYPE "long unsigned int"
175 /* The string value for __PTRDIFF_TYPE__. */
178 #define PTRDIFF_TYPE "long int"
181 /* The string value for __WCHAR_TYPE__. */
184 #define WCHAR_TYPE "int"
186 #define CPP_WCHAR_TYPE(PFILE) \
187 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
189 /* The string value for __USER_LABEL_PREFIX__ */
191 #ifndef USER_LABEL_PREFIX
192 #define USER_LABEL_PREFIX ""
195 /* The string value for __REGISTER_PREFIX__ */
197 #ifndef REGISTER_PREFIX
198 #define REGISTER_PREFIX ""
201 /* In the definition of a #assert name, this structure forms
202 a list of the individual values asserted.
203 Each value is itself a list of "tokens".
204 These are strings that are compared by name. */
206 struct tokenlist_list
{
207 struct tokenlist_list
*next
;
208 struct arglist
*tokens
;
211 struct assertion_hashnode
{
212 struct assertion_hashnode
*next
; /* double links for easy deletion */
213 struct assertion_hashnode
*prev
;
214 /* also, a back pointer to this node's hash
215 chain is kept, in case the node is the head
216 of the chain and gets deleted. */
217 struct assertion_hashnode
**bucket_hdr
;
218 int length
; /* length of token, for quick comparison */
219 U_CHAR
*name
; /* the actual name */
220 /* List of token-sequences. */
221 struct tokenlist_list
*value
;
224 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
225 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
227 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
228 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
229 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
230 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
231 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
232 (Note that it is false while we're expanding marco *arguments*.) */
233 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
235 /* Move all backslash-newline pairs out of embarrassing places.
236 Exchange all such pairs following BP
237 with any potentially-embarrassing characters that follow them.
238 Potentially-embarrassing characters are / and *
239 (because a backslash-newline inside a comment delimiter
240 would cause it not to be recognized). */
242 #define NEWLINE_FIX \
243 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
245 /* Same, but assume we've already read the potential '\\' into C. */
246 #define NEWLINE_FIX1(C) do { \
247 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
251 struct cpp_pending
*next
;
256 /* Forward declarations. */
260 void cpp_file_line_for_message
PARAMS ((cpp_reader
*, char *, int, int));
261 void cpp_hash_cleanup
PARAMS ((cpp_reader
*));
263 void cpp_print_containing_files
PARAMS ((cpp_reader
*));
265 static void add_import ();
266 static void append_include_chain ();
267 static void make_assertion ();
268 static void path_include ();
269 static void initialize_builtins ();
270 static void initialize_char_syntax ();
271 extern void delete_macro ();
273 static void trigraph_pcp ();
275 static int finclude ();
276 static void validate_else ();
277 static int comp_def_part ();
279 extern void fancy_abort ();
281 static int lookup_import ();
282 static int redundant_include_p ();
283 static is_system_include ();
284 static struct file_name_map
*read_name_map ();
285 static char *read_filename_string ();
286 static int open_include_file ();
287 static int check_macro_name ();
288 static int compare_defs ();
289 static int compare_token_lists ();
290 static HOST_WIDE_INT
eval_if_expression ();
291 static int change_newlines ();
293 static int file_size_and_mode ();
294 static struct arglist
*read_token_list ();
295 static void free_token_list ();
296 static int safe_read ();
297 static void push_macro_expansion
PARAMS ((cpp_reader
*,
298 U_CHAR
*, int, HASHNODE
*));
299 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
300 extern char *xrealloc ();
301 static char *xcalloc ();
302 static char *savestring ();
304 static void conditional_skip ();
305 static void skip_if_group ();
307 /* Last arg to output_line_command. */
308 enum file_change_code
{same_file
, enter_file
, leave_file
};
310 /* External declarations. */
312 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
314 extern char *getenv ();
315 extern FILE *fdopen ();
316 extern char *version_string
;
317 extern struct tm
*localtime ();
319 /* These functions are declared to return int instead of void since they
320 are going to be placed in a table and some old compilers have trouble with
321 pointers to functions returning void. */
323 static int do_define ();
324 static int do_line ();
325 static int do_include ();
326 static int do_undef ();
327 static int do_error ();
328 static int do_pragma ();
329 static int do_ident ();
331 static int do_xifdef ();
332 static int do_else ();
333 static int do_elif ();
334 static int do_endif ();
335 static int do_sccs ();
336 static int do_once ();
337 static int do_assert ();
338 static int do_unassert ();
339 static int do_warning ();
341 struct file_name_list
343 struct file_name_list
*next
;
345 /* If the following is nonzero, it is a macro name.
346 Don't include the file again if that macro is defined. */
347 U_CHAR
*control_macro
;
348 /* If the following is nonzero, it is a C-language system include
350 int c_system_include_path
;
351 /* Mapping of file names for this directory. */
352 struct file_name_map
*name_map
;
353 /* Non-zero if name_map is valid. */
357 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
358 via the same directory as the file that #included it. */
359 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
361 /* #include "file" looks in source file dir, then stack. */
362 /* #include <file> just looks in the stack. */
363 /* -I directories are added to the end, then the defaults are added. */
365 static struct default_include
{
366 char *fname
; /* The name of the directory. */
367 char *component
; /* The component containing the directory */
368 int cplusplus
; /* Only look here if we're compiling C++. */
369 int cxx_aware
; /* Includes in this directory don't need to
370 be wrapped in extern "C" when compiling
372 } include_defaults_array
[]
373 #ifdef INCLUDE_DEFAULTS
377 /* Pick up GNU C++ specific include files. */
378 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
379 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 1 },
381 /* This is the dir for fixincludes. Put it just before
382 the files that we fix. */
383 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
384 /* For cross-compilation, this dir name is generated
385 automatically in Makefile.in. */
386 { CROSS_INCLUDE_DIR
, "GCC",0, 0 },
387 #ifdef TOOL_INCLUDE_DIR
388 /* This is another place that the target system's headers might be. */
389 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
391 #else /* not CROSS_COMPILE */
392 #ifdef LOCAL_INCLUDE_DIR
393 /* This should be /usr/local/include and should come before
394 the fixincludes-fixed header files. */
395 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
397 #ifdef TOOL_INCLUDE_DIR
398 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
399 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
400 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
402 /* This is the dir for fixincludes. Put it just before
403 the files that we fix. */
404 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
405 /* Some systems have an extra dir of include files. */
406 #ifdef SYSTEM_INCLUDE_DIR
407 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
409 #ifndef STANDARD_INCLUDE_COMPONENT
410 #define STANDARD_INCLUDE_COMPONENT 0
412 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
413 #endif /* not CROSS_COMPILE */
416 #endif /* no INCLUDE_DEFAULTS */
418 /* `struct directive' defines one #-directive, including how to handle it. */
421 int length
; /* Length of name */
422 int (*func
)(); /* Function to handle directive */
423 char *name
; /* Name of directive */
424 enum node_type type
; /* Code which describes which directive. */
425 char command_reads_line
; /* One if rest of line is read by func. */
428 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
430 /* Here is the actual list of #-directives, most-often-used first.
431 The initialize_builtins function assumes #define is the very first. */
433 static struct directive directive_table
[] = {
434 { 6, do_define
, "define", T_DEFINE
},
435 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
436 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
437 { 7, do_include
, "include", T_INCLUDE
, 1},
438 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
439 { 6, do_include
, "import", T_IMPORT
, 1},
440 { 5, do_endif
, "endif", T_ENDIF
, 1},
441 { 4, do_else
, "else", T_ELSE
, 1},
442 { 2, do_if
, "if", T_IF
, 1},
443 { 4, do_elif
, "elif", T_ELIF
, 1},
444 { 5, do_undef
, "undef", T_UNDEF
},
445 { 5, do_error
, "error", T_ERROR
},
446 { 7, do_warning
, "warning", T_WARNING
},
447 { 6, do_pragma
, "pragma", T_PRAGMA
},
448 { 4, do_line
, "line", T_LINE
, 1},
449 { 5, do_ident
, "ident", T_IDENT
, 1},
450 #ifdef SCCS_DIRECTIVE
451 { 4, do_sccs
, "sccs", T_SCCS
},
453 { 6, do_assert
, "assert", T_ASSERT
, 1},
454 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
455 { -1, 0, "", T_UNUSED
},
458 /* table to tell if char can be part of a C identifier. */
459 U_CHAR is_idchar
[256];
460 /* table to tell if char can be first char of a c identifier. */
461 U_CHAR is_idstart
[256];
462 /* table to tell if c is horizontal space. */
463 U_CHAR is_hor_space
[256];
464 /* table to tell if c is horizontal or vertical space. */
465 static U_CHAR is_space
[256];
467 /* Initialize syntactic classifications of characters. */
470 initialize_char_syntax (opts
)
471 struct cpp_options
*opts
;
476 * Set up is_idchar and is_idstart tables. These should be
477 * faster than saying (is_alpha (c) || c == '_'), etc.
478 * Set up these things before calling any routines tthat
481 for (i
= 'a'; i
<= 'z'; i
++) {
482 is_idchar
[i
- 'a' + 'A'] = 1;
484 is_idstart
[i
- 'a' + 'A'] = 1;
487 for (i
= '0'; i
<= '9'; i
++)
491 is_idchar
['$'] = opts
->dollars_in_ident
;
492 is_idstart
['$'] = opts
->dollars_in_ident
;
494 /* horizontal space table */
495 is_hor_space
[' '] = 1;
496 is_hor_space
['\t'] = 1;
497 is_hor_space
['\v'] = 1;
498 is_hor_space
['\f'] = 1;
499 is_hor_space
['\r'] = 1;
510 /* Place into PFILE a quoted string representing the string SRC.
511 Caller must reserve enough space in pfile->token_buffer. */
514 quote_string (pfile
, src
)
520 CPP_PUTC_Q (pfile
, '\"');
522 switch ((c
= *src
++))
526 CPP_PUTC_Q (pfile
, c
);
529 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
530 CPP_ADJUST_WRITTEN (pfile
, 4);
536 CPP_PUTC_Q (pfile
, '\\');
537 CPP_PUTC_Q (pfile
, c
);
541 CPP_PUTC_Q (pfile
, '\"');
542 CPP_NUL_TERMINATE_Q (pfile
);
547 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
550 cpp_grow_buffer (pfile
, n
)
554 long old_written
= CPP_WRITTEN (pfile
);
555 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
556 pfile
->token_buffer
= (U_CHAR
*)
557 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
558 CPP_SET_WRITTEN (pfile
, old_written
);
563 * process a given definition string, for initialization
564 * If STR is just an identifier, define it with value 1.
565 * If STR has anything after the identifier, then it should
566 * be identifier=definition.
570 cpp_define (pfile
, str
)
580 cpp_error (pfile
, "malformed option `-D %s'", str
);
583 while (is_idchar
[*++p
])
587 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
588 strcpy ((char *)buf
, str
);
589 strcat ((char *)buf
, " 1");
593 cpp_error (pfile
, "malformed option `-D %s'", str
);
599 /* Copy the entire option so we can modify it. */
600 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
601 strncpy (buf
, str
, p
- str
);
602 /* Change the = to a space. */
604 /* Scan for any backslash-newline and remove it. */
609 if (*p
== '\\' && p
[1] == '\n')
617 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
620 /* Process the string STR as if it appeared as the body of a #assert.
621 OPTION is the option name for which STR was the argument. */
624 make_assertion (pfile
, option
, str
)
629 struct directive
*kt
;
632 /* Copy the entire option so we can modify it. */
633 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
634 strcpy ((char *) buf
, str
);
635 /* Scan for any backslash-newline and remove it. */
639 if (*p
== '\\' && p
[1] == '\n')
648 if (!is_idstart
[*p
]) {
649 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
652 while (is_idchar
[*++p
])
654 while (*p
== ' ' || *p
== '\t') p
++;
655 if (! (*p
== 0 || *p
== '(')) {
656 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
660 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
662 do_assert (pfile
, NULL
, NULL
, NULL
);
663 cpp_pop_buffer (pfile
);
667 /* Append a chain of `struct file_name_list's
668 to the end of the main include chain.
669 FIRST is the beginning of the chain to append, and LAST is the end. */
672 append_include_chain (pfile
, first
, last
)
674 struct file_name_list
*first
, *last
;
676 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
677 struct file_name_list
*dir
;
682 if (opts
->include
== 0)
683 opts
->include
= first
;
685 opts
->last_include
->next
= first
;
687 if (opts
->first_bracket_include
== 0)
688 opts
->first_bracket_include
= first
;
690 for (dir
= first
; ; dir
= dir
->next
) {
691 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
692 if (len
> pfile
->max_include_len
)
693 pfile
->max_include_len
= len
;
699 opts
->last_include
= last
;
702 /* Add output to `deps_buffer' for the -M switch.
703 STRING points to the text to be output.
704 SPACER is ':' for targets, ' ' for dependencies, zero for text
705 to be inserted literally. */
708 deps_output (pfile
, string
, spacer
)
713 int size
= strlen (string
);
718 #ifndef MAX_OUTPUT_COLUMNS
719 #define MAX_OUTPUT_COLUMNS 72
722 && pfile
->deps_column
> 0
723 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
725 deps_output (pfile
, " \\\n ", 0);
726 pfile
->deps_column
= 0;
729 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
731 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
732 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
733 pfile
->deps_allocated_size
);
735 if (spacer
== ' ' && pfile
->deps_column
> 0)
736 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
737 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
738 pfile
->deps_size
+= size
;
739 pfile
->deps_column
+= size
;
741 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
742 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
745 /* Given a colon-separated list of file names PATH,
746 add all the names to the search path for include files. */
749 path_include (pfile
, path
)
761 struct file_name_list
*dirtmp
;
763 /* Find the end of this name. */
764 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
766 /* An empty name in the path stands for the current directory. */
767 name
= (char *) xmalloc (2);
771 /* Otherwise use the directory that is named. */
772 name
= (char *) xmalloc (q
- p
+ 1);
773 bcopy (p
, name
, q
- p
);
777 dirtmp
= (struct file_name_list
*)
778 xmalloc (sizeof (struct file_name_list
));
779 dirtmp
->next
= 0; /* New one goes on the end */
780 dirtmp
->control_macro
= 0;
781 dirtmp
->c_system_include_path
= 0;
782 dirtmp
->fname
= name
;
783 dirtmp
->got_name_map
= 0;
784 append_include_chain (pfile
, dirtmp
, dirtmp
);
786 /* Advance past this name. */
790 /* Skip the colon. */
796 cpp_options_init (opts
)
799 bzero ((char *) opts
, sizeof *opts
);
800 opts
->in_fname
= NULL
;
801 opts
->out_fname
= NULL
;
803 /* Initialize is_idchar to allow $. */
804 opts
->dollars_in_ident
= 1;
805 initialize_char_syntax (opts
);
807 opts
->no_line_commands
= 0;
808 opts
->no_trigraphs
= 1;
809 opts
->put_out_comments
= 0;
810 opts
->print_include_names
= 0;
811 opts
->dump_macros
= dump_none
;
814 opts
->cplusplus_comments
= 0;
821 opts
->pedantic_errors
= 0;
822 opts
->inhibit_warnings
= 0;
823 opts
->warn_comments
= 0;
824 opts
->warn_import
= 1;
825 opts
->warnings_are_errors
= 0;
829 null_underflow (pfile
)
836 null_cleanup (pbuf
, pfile
)
844 macro_cleanup (pbuf
, pfile
)
848 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
849 if (macro
->type
== T_DISABLED
)
850 macro
->type
= T_MACRO
;
851 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
857 file_cleanup (pbuf
, pfile
)
869 /* Assuming we have read '/'.
870 If this is the start of a comment (followed by '*' or '/'),
871 skip to the end of the comment, and return ' '.
872 Return EOF if we reached the end of file before the end of the comment.
873 If not the start of a comment, return '/'. */
876 skip_comment (pfile
, linep
)
881 while (PEEKC() == '\\' && PEEKN(1) == '\n')
896 while (c
== '\\' && PEEKC() == '\n')
900 FORWARD(1), c
= GETC();
902 if (prev_c
== '*' && c
== '/')
904 if (c
== '\n' && linep
)
908 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
915 return ' '; /* Allow // to be terminated by EOF. */
916 while (c
== '\\' && PEEKC() == '\n')
925 /* Don't consider final '\n' to be part of comment. */
935 /* Skip whitespace \-newline and comments. Does not macro-expand. */
938 cpp_skip_hspace (pfile
)
948 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
949 cpp_pedwarn (pfile
, "%s in preprocessing directive",
950 c
== '\f' ? "formfeed" : "vertical tab");
956 c
= skip_comment (pfile
, NULL
);
959 if (c
== EOF
|| c
== '/')
962 else if (c
== '\\' && PEEKN(1) == '\n') {
965 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
966 && is_hor_space
[PEEKN(1)])
972 /* Read the rest of the current line.
973 The line is appended to PFILE's output buffer. */
976 copy_rest_of_line (pfile
)
979 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
996 goto scan_directive_token
;
1000 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '/'))
1001 goto scan_directive_token
;
1005 if (CPP_PEDANTIC (pfile
))
1006 cpp_pedwarn (pfile
, "%s in preprocessing directive",
1007 c
== '\f' ? "formfeed" : "vertical tab");
1013 scan_directive_token
:
1015 cpp_get_token (pfile
);
1018 CPP_PUTC (pfile
, c
);
1021 CPP_NUL_TERMINATE (pfile
);
1025 skip_rest_of_line (pfile
)
1028 long old
= CPP_WRITTEN (pfile
);
1029 copy_rest_of_line (pfile
);
1030 CPP_SET_WRITTEN (pfile
, old
);
1033 /* Handle a possible # directive.
1034 '#' has already been read. */
1037 handle_directive (pfile
)
1040 register struct directive
*kt
;
1043 U_CHAR
*ident
, *line_end
;
1044 long old_written
= CPP_WRITTEN (pfile
);
1046 cpp_skip_hspace (pfile
);
1049 if (c
>= '0' && c
<= '9')
1051 /* Handle # followed by a line number. */
1052 if (CPP_PEDANTIC (pfile
))
1053 cpp_pedwarn (pfile
, "`#' followed by integer");
1054 do_line (pfile
, NULL
);
1055 goto done_a_directive
;
1058 /* Now find the directive name. */
1059 CPP_PUTC (pfile
, '#');
1060 parse_name (pfile
, GETC());
1061 ident
= pfile
->token_buffer
+ old_written
+ 1;
1062 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1063 if (ident_length
== 0 && PEEKC() == '\n')
1065 /* A line of just `#' becomes blank. */
1066 goto done_a_directive
;
1070 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1072 while (is_idchar
[*p
]) {
1073 if (*p
< '0' || *p
> '9')
1077 /* Avoid error for `###' and similar cases unless -pedantic. */
1079 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1081 if (pedantic
&& !lang_asm
)
1082 cpp_warning (pfile
, "invalid preprocessor directive");
1088 cpp_error (pfile
, "invalid preprocessor directive name");
1094 * Decode the keyword and call the appropriate expansion
1095 * routine, after moving the input pointer up to the next line.
1097 for (kt
= directive_table
; ; kt
++) {
1098 if (kt
->length
<= 0)
1099 goto not_a_directive
;
1100 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1104 if (kt
->command_reads_line
)
1108 /* Nonzero means do not delete comments within the directive.
1109 #define needs this when -traditional. */
1110 int comments
= CPP_TRADITIONAL (pfile
) && kt
->type
== T_DEFINE
;
1111 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1112 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1113 after_ident
= CPP_WRITTEN (pfile
);
1114 copy_rest_of_line (pfile
);
1115 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1118 /* We may want to pass through #define, #pragma, and #include.
1119 Other directives may create output, but we don't want the directive
1120 itself out, so we pop it now. For example conditionals may emit
1121 #failed ... #endfailed stuff. But note that popping the buffer
1122 means the parameters to kt->func may point after pfile->limit
1123 so these parameters are invalid as soon as something gets appended
1124 to the token_buffer. */
1126 line_end
= CPP_PWRITTEN (pfile
);
1127 if (! (kt
->type
== T_DEFINE
1128 || kt
->type
== T_PRAGMA
1129 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
1130 && CPP_OPTIONS (pfile
)->dump_includes
)))
1131 CPP_SET_WRITTEN (pfile
, old_written
);
1133 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1135 if (kt
->type
== T_DEFINE
)
1137 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1139 /* Skip "#define". */
1140 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
1142 SKIP_WHITE_SPACE (p
);
1143 while (is_idchar
[*p
]) p
++;
1145 CPP_PUTC (pfile
, '\n');
1147 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
1148 CPP_SET_WRITTEN (pfile
, old_written
);
1158 /* Pass a directive through to the output file.
1159 BUF points to the contents of the directive, as a contiguous string.
1160 LIMIT points to the first character past the end of the directive.
1161 KEYWORD is the keyword-table entry for the directive. */
1164 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1165 U_CHAR
*buf
, *limit
;
1167 struct directive
*keyword
;
1169 register unsigned keyword_length
= keyword
->length
;
1171 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1172 CPP_PUTC_Q (pfile
, '#');
1173 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1174 if (limit
!= buf
&& buf
[0] != ' ')
1175 CPP_PUTC_Q (pfile
, ' ');
1176 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1178 CPP_PUTS_Q (pfile
, '\n');
1179 /* Count the line we have just made in the output,
1180 to get in sync properly. */
1185 /* The arglist structure is built by do_define to tell
1186 collect_definition where the argument names begin. That
1187 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1188 would contain pointers to the strings x, y, and z.
1189 Collect_definition would then build a DEFINITION node,
1190 with reflist nodes pointing to the places x, y, and z had
1191 appeared. So the arglist is just convenience data passed
1192 between these two routines. It is not kept around after
1193 the current #define has been processed and entered into the
1197 struct arglist
*next
;
1204 /* Read a replacement list for a macro with parameters.
1205 Build the DEFINITION structure.
1206 Reads characters of text starting at BUF until END.
1207 ARGLIST specifies the formal parameters to look for
1208 in the text of the definition; NARGS is the number of args
1209 in that list, or -1 for a macro name that wants no argument list.
1210 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1211 and NAMELEN is its length in characters.
1213 Note that comments, backslash-newlines, and leading white space
1214 have already been deleted from the argument. */
1217 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1219 U_CHAR
*buf
, *limit
;
1221 struct arglist
*arglist
;
1224 register U_CHAR
*p
, *lastp
, *exp_p
;
1225 struct reflist
*endpat
= NULL
;
1226 /* Pointer to first nonspace after last ## seen. */
1228 /* Pointer to first nonspace after last single-# seen. */
1229 U_CHAR
*stringify
= 0;
1231 int expected_delimiter
= '\0';
1233 /* Scan thru the replacement list, ignoring comments and quoted
1234 strings, picking up on the macro calls. It does a linear search
1235 thru the arg list on every potential symbol. Profiling might say
1236 that something smarter should happen. */
1241 /* Find the beginning of the trailing whitespace. */
1243 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1245 /* Allocate space for the text in the macro definition.
1246 Leading and trailing whitespace chars need 2 bytes each.
1247 Each other input char may or may not need 1 byte,
1248 so this is an upper bound. The extra 5 are for invented
1249 leading and trailing newline-marker and final null. */
1250 maxsize
= (sizeof (DEFINITION
)
1252 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1256 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1258 defn
->nargs
= nargs
;
1259 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1264 /* Add one initial space escape-marker to prevent accidental
1265 token-pasting (often removed by macroexpand). */
1269 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1270 cpp_error (pfile
, "`##' at start of macro definition");
1274 /* Process the main body of the definition. */
1276 int skipped_arg
= 0;
1277 register U_CHAR c
= *p
++;
1281 if (!CPP_TRADITIONAL (pfile
)) {
1285 if (expected_delimiter
!= '\0') {
1286 if (c
== expected_delimiter
)
1287 expected_delimiter
= '\0';
1289 expected_delimiter
= c
;
1293 if (p
< limit
&& expected_delimiter
) {
1294 /* In a string, backslash goes through
1295 and makes next char ordinary. */
1301 /* An '@' in a string or character constant stands for itself,
1302 and does not need to be escaped. */
1303 if (!expected_delimiter
)
1308 /* # is ordinary inside a string. */
1309 if (expected_delimiter
)
1311 if (p
< limit
&& *p
== '#') {
1312 /* ##: concatenate preceding and following tokens. */
1313 /* Take out the first #, discard preceding whitespace. */
1315 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1317 /* Skip the second #. */
1319 /* Discard following whitespace. */
1320 SKIP_WHITE_SPACE (p
);
1323 cpp_error (pfile
, "`##' at end of macro definition");
1324 } else if (nargs
>= 0) {
1325 /* Single #: stringify following argument ref.
1326 Don't leave the # in the expansion. */
1328 SKIP_WHITE_SPACE (p
);
1329 if (p
== limit
|| ! is_idstart
[*p
]
1330 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' || p
[1] == '"')))
1332 "`#' operator is not followed by a macro argument name");
1339 /* In -traditional mode, recognize arguments inside strings and
1340 and character constants, and ignore special properties of #.
1341 Arguments inside strings are considered "stringified", but no
1342 extra quote marks are supplied. */
1346 if (expected_delimiter
!= '\0') {
1347 if (c
== expected_delimiter
)
1348 expected_delimiter
= '\0';
1350 expected_delimiter
= c
;
1354 /* Backslash quotes delimiters and itself, but not macro args. */
1355 if (expected_delimiter
!= 0 && p
< limit
1356 && (*p
== expected_delimiter
|| *p
== '\\')) {
1363 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1366 /* If we find a comment that wasn't removed by handle_directive,
1367 this must be -traditional. So replace the comment with
1371 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1374 /* Mark this as a concatenation-point, as if it had been ##. */
1382 /* Handle the start of a symbol. */
1383 if (is_idchar
[c
] && nargs
> 0) {
1384 U_CHAR
*id_beg
= p
- 1;
1388 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1389 id_len
= p
- id_beg
;
1392 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
1393 register struct arglist
*arg
;
1395 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1396 struct reflist
*tpat
;
1398 if (arg
->name
[0] == c
1399 && arg
->length
== id_len
1400 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1401 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1402 if (CPP_TRADITIONAL (pfile
)) {
1403 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1407 "macro arg `%.*s' would be stringified with -traditional.",
1411 /* If ANSI, don't actually substitute inside a string. */
1412 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1414 /* make a pat node for this arg and append it to the end of
1416 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1418 tpat
->raw_before
= concat
== id_beg
;
1419 tpat
->raw_after
= 0;
1420 tpat
->rest_args
= arg
->rest_args
;
1421 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1422 ? expected_delimiter
!= '\0'
1423 : stringify
== id_beg
);
1426 defn
->pattern
= tpat
;
1428 endpat
->next
= tpat
;
1431 tpat
->argno
= arg
->argno
;
1432 tpat
->nchars
= exp_p
- lastp
;
1434 register U_CHAR
*p1
= p
;
1435 SKIP_WHITE_SPACE (p1
);
1436 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1437 tpat
->raw_after
= 1;
1439 lastp
= exp_p
; /* place to start copying from next time */
1446 /* If this was not a macro arg, copy it into the expansion. */
1447 if (! skipped_arg
) {
1448 register U_CHAR
*lim1
= p
;
1452 if (stringify
== id_beg
)
1454 "`#' operator should be followed by a macro argument name");
1459 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1461 /* If ANSI, put in a "@ " marker to prevent token pasting.
1462 But not if "inside a string" (which in ANSI mode
1463 happens only for -D option). */
1470 defn
->length
= exp_p
- defn
->expansion
;
1472 /* Crash now if we overrun the allocated size. */
1473 if (defn
->length
+ 1 > maxsize
)
1477 /* This isn't worth the time it takes. */
1478 /* give back excess storage */
1479 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1486 * special extension string that can be added to the last macro argument to
1487 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1488 * #define wow(a, b...) process (b, a, b)
1489 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1490 * { wow (one, two); } -> { process (two, one, two); }
1491 * if this "rest_arg" is used with the concat token '##' and if it is not
1492 * supplied then the token attached to with ## will not be outputted. Ex:
1493 * #define wow (a, b...) process (b ## , a, ## b)
1494 * { wow (1, 2); } -> { process (2, 1, 2); }
1495 * { wow (one); } -> { process (one); {
1497 static char rest_extension
[] = "...";
1498 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1500 /* Create a DEFINITION node from a #define directive. Arguments are
1501 as for do_define. */
1504 create_definition (buf
, limit
, pfile
, predefinition
)
1505 U_CHAR
*buf
, *limit
;
1509 U_CHAR
*bp
; /* temp ptr into input buffer */
1510 U_CHAR
*symname
; /* remember where symbol name starts */
1511 int sym_length
; /* and how long it is */
1514 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1516 int arglengths
= 0; /* Accumulate lengths of arg names
1517 plus number of args. */
1519 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1523 while (is_hor_space
[*bp
])
1526 symname
= bp
; /* remember where it starts */
1528 sym_length
= check_macro_name (pfile
, bp
, "macro");
1531 /* Lossage will occur if identifiers or control keywords are broken
1532 across lines using backslash. This is not the right place to take
1536 struct arglist
*arg_ptrs
= NULL
;
1539 bp
++; /* skip '(' */
1540 SKIP_WHITE_SPACE (bp
);
1542 /* Loop over macro argument names. */
1543 while (*bp
!= ')') {
1544 struct arglist
*temp
;
1546 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1548 temp
->next
= arg_ptrs
;
1549 temp
->argno
= argno
++;
1550 temp
->rest_args
= 0;
1554 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1556 if (!is_idstart
[*bp
])
1557 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1559 /* Find the end of the arg name. */
1560 while (is_idchar
[*bp
]) {
1562 /* do we have a "special" rest-args extension here? */
1563 if (limit
- bp
> REST_EXTENSION_LENGTH
1564 && strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1566 temp
->rest_args
= 1;
1570 temp
->length
= bp
- temp
->name
;
1572 bp
+= REST_EXTENSION_LENGTH
;
1573 arglengths
+= temp
->length
+ 2;
1574 SKIP_WHITE_SPACE (bp
);
1575 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1576 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1581 SKIP_WHITE_SPACE (bp
);
1584 cpp_error (pfile
, "unterminated parameter list in `#define'");
1588 struct arglist
*otemp
;
1590 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1591 if (temp
->length
== otemp
->length
1592 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1595 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1596 (void) strncpy (name
, temp
->name
, temp
->length
);
1597 name
[temp
->length
] = '\0';
1599 "duplicate argument name `%s' in `#define'", name
);
1605 ++bp
; /* skip paren */
1606 SKIP_WHITE_SPACE (bp
);
1607 /* now everything from bp before limit is the definition. */
1608 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1609 defn
->rest_args
= rest_args
;
1611 /* Now set defn->args.argnames to the result of concatenating
1612 the argument names in reverse order
1613 with comma-space between them. */
1614 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1616 struct arglist
*temp
;
1618 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1619 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1621 if (temp
->next
!= 0) {
1622 defn
->args
.argnames
[i
++] = ',';
1623 defn
->args
.argnames
[i
++] = ' ';
1626 defn
->args
.argnames
[i
] = 0;
1629 /* Simple expansion or empty definition. */
1633 if (is_hor_space
[*bp
]) {
1635 SKIP_WHITE_SPACE (bp
);
1638 case '!': case '"': case '#': case '%': case '&': case '\'':
1639 case ')': case '*': case '+': case ',': case '-': case '.':
1640 case '/': case ':': case ';': case '<': case '=': case '>':
1641 case '?': case '[': case '\\': case ']': case '^': case '{':
1642 case '|': case '}': case '~':
1643 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1644 sym_length
, symname
);
1648 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1649 sym_length
, symname
);
1654 /* now everything from bp before limit is the definition. */
1655 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1656 defn
->args
.argnames
= (U_CHAR
*) "";
1662 /* OP is null if this is a predefinition */
1663 defn
->predefined
= predefinition
;
1665 mdef
.symnam
= symname
;
1666 mdef
.symlen
= sym_length
;
1675 /* Check a purported macro name SYMNAME, and yield its length.
1676 USAGE is the kind of name this is intended for. */
1679 check_macro_name (pfile
, symname
, usage
)
1687 for (p
= symname
; is_idchar
[*p
]; p
++)
1689 sym_length
= p
- symname
;
1691 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
1692 cpp_error (pfile
, "invalid %s name", usage
);
1693 else if (!is_idstart
[*symname
]) {
1694 U_CHAR
*msg
; /* what pain... */
1695 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1696 bcopy (symname
, msg
, sym_length
);
1697 msg
[sym_length
] = 0;
1698 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1700 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1701 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1706 /* Return zero if two DEFINITIONs are isomorphic. */
1709 compare_defs (pfile
, d1
, d2
)
1711 DEFINITION
*d1
, *d2
;
1713 register struct reflist
*a1
, *a2
;
1714 register U_CHAR
*p1
= d1
->expansion
;
1715 register U_CHAR
*p2
= d2
->expansion
;
1718 if (d1
->nargs
!= d2
->nargs
)
1720 if (CPP_PEDANTIC (pfile
)
1721 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1723 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1724 a1
= a1
->next
, a2
= a2
->next
) {
1725 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1726 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1727 || a1
->argno
!= a2
->argno
1728 || a1
->stringify
!= a2
->stringify
1729 || a1
->raw_before
!= a2
->raw_before
1730 || a1
->raw_after
!= a2
->raw_after
)
1738 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1739 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1744 /* Return 1 if two parts of two macro definitions are effectively different.
1745 One of the parts starts at BEG1 and has LEN1 chars;
1746 the other has LEN2 chars at BEG2.
1747 Any sequence of whitespace matches any other sequence of whitespace.
1748 FIRST means these parts are the first of a macro definition;
1749 so ignore leading whitespace entirely.
1750 LAST means these parts are the last of a macro definition;
1751 so ignore trailing whitespace entirely. */
1754 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1756 U_CHAR
*beg1
, *beg2
;
1760 register U_CHAR
*end1
= beg1
+ len1
;
1761 register U_CHAR
*end2
= beg2
+ len2
;
1763 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1764 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1767 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1768 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1770 while (beg1
!= end1
&& beg2
!= end2
) {
1771 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1772 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1773 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1774 } else if (*beg1
== *beg2
) {
1778 return (beg1
!= end1
) || (beg2
!= end2
);
1781 /* Process a #define command.
1782 BUF points to the contents of the #define command, as a contiguous string.
1783 LIMIT points to the first character past the end of the definition.
1784 KEYWORD is the keyword-table entry for #define,
1785 or NULL for a "predefined" macro. */
1788 do_define (pfile
, keyword
, buf
, limit
)
1790 struct directive
*keyword
;
1791 U_CHAR
*buf
, *limit
;
1798 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1799 if (pcp_outfile
&& keyword
)
1800 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1803 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1807 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1809 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1812 /* Redefining a precompiled key is ok. */
1813 if (hp
->type
== T_PCSTRING
)
1815 /* Redefining a macro is ok if the definitions are the same. */
1816 else if (hp
->type
== T_MACRO
)
1817 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
1818 /* Redefining a constant is ok with -D. */
1819 else if (hp
->type
== T_CONST
)
1820 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1821 /* Print the warning if it's not ok. */
1824 U_CHAR
*msg
; /* what pain... */
1826 /* If we are passing through #define and #undef directives, do
1827 that for this re-definition now. */
1828 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1829 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1831 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1833 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1834 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1835 cpp_pedwarn (pfile
, msg
);
1836 if (hp
->type
== T_MACRO
)
1837 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1838 "this is the location of the previous definition");
1840 /* Replace the old definition. */
1842 hp
->value
.defn
= mdef
.defn
;
1846 /* If we are passing through #define and #undef directives, do
1847 that for this new definition now. */
1848 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1849 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1850 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1851 (char *) mdef
.defn
, hashcode
);
1861 /* This structure represents one parsed argument in a macro call.
1862 `raw' points to the argument text as written (`raw_length' is its length).
1863 `expanded' points to the argument's macro-expansion
1864 (its length is `expand_length').
1865 `stringified_length' is the length the argument would have
1867 `use_count' is the number of times this macro arg is substituted
1868 into the macro. If the actual use count exceeds 10,
1869 the value stored is 10. */
1871 /* raw and expanded are relative to ARG_BASE */
1872 #define ARG_BASE ((pfile)->token_buffer)
1875 /* Strings relative to pfile->token_buffer */
1876 long raw
, expanded
, stringified
;
1877 int raw_length
, expand_length
;
1878 int stringified_length
;
1883 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1884 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1885 as the new input buffer.
1886 Return the new buffer, or NULL on failure. */
1889 cpp_push_buffer (pfile
, buffer
, length
)
1894 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1895 if (buf
== pfile
->buffer_stack
)
1897 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1902 bzero ((char *) buf
, sizeof (cpp_buffer
));
1903 CPP_BUFFER (pfile
) = buf
;
1904 buf
->if_stack
= pfile
->if_stack
;
1905 buf
->cleanup
= null_cleanup
;
1906 buf
->underflow
= null_underflow
;
1907 buf
->buf
= buf
->cur
= buffer
;
1908 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1914 cpp_pop_buffer (pfile
)
1917 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1918 (*buf
->cleanup
) (buf
, pfile
);
1919 return ++CPP_BUFFER (pfile
);
1922 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1923 Pop the buffer when done. */
1926 cpp_scan_buffer (pfile
)
1929 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1932 enum cpp_token token
= cpp_get_token (pfile
);
1933 if (token
== CPP_EOF
) /* Should not happen ... */
1935 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1937 cpp_pop_buffer (pfile
);
1944 * Rescan a string (which may have escape marks) into pfile's buffer.
1945 * Place the result in pfile->token_buffer.
1947 * The input is copied before it is scanned, so it is safe to pass
1948 * it something from the token_buffer that will get overwritten
1949 * (because it follows CPP_WRITTEN). This is used by do_include.
1953 cpp_expand_to_buffer (pfile
, buf
, length
)
1958 register cpp_buffer
*ip
;
1960 U_CHAR
*limit
= buf
+ length
;
1963 int odepth
= indepth
;
1969 /* Set up the input on the input stack. */
1971 buf1
= (U_CHAR
*) alloca (length
+ 1);
1973 register U_CHAR
*p1
= buf
;
1974 register U_CHAR
*p2
= buf1
;
1981 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1984 ip
->has_escapes
= 1;
1986 ip
->lineno
= obuf
.lineno
= 1;
1989 /* Scan the input, create the output. */
1990 cpp_scan_buffer (pfile
);
1993 if (indepth
!= odepth
)
1997 CPP_NUL_TERMINATE (pfile
);
2002 adjust_position (buf
, limit
, linep
, colp
)
2012 (*linep
)++, (*colp
) = 1;
2018 /* Move line_base forward, updating lineno and colno. */
2021 update_position (pbuf
)
2022 register cpp_buffer
*pbuf
;
2024 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
2025 unsigned char *new_pos
= pbuf
->cur
;
2026 register struct parse_marker
*mark
;
2027 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
2029 if (pbuf
->buf
+ mark
->position
< new_pos
)
2030 new_pos
= pbuf
->buf
+ mark
->position
;
2032 pbuf
->line_base
+= new_pos
- old_pos
;
2033 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
2037 cpp_buf_line_and_col (pbuf
, linep
, colp
)
2038 register cpp_buffer
*pbuf
;
2046 *linep
= pbuf
->lineno
;
2047 *colp
= pbuf
->colno
;
2048 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2057 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2060 cpp_file_buffer (pfile
)
2063 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2065 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2066 if (ip
->fname
!= NULL
)
2072 count_newlines (buf
, limit
)
2073 register U_CHAR
*buf
;
2074 register U_CHAR
*limit
;
2076 register long count
= 0;
2087 * write out a #line command, for instance, after an #include file.
2088 * If CONDITIONAL is nonzero, we can omit the #line if it would
2089 * appear to be a no-op, and we can output a few newlines instead
2090 * if we want to increase the line number by a small amount.
2091 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2095 output_line_command (pfile
, conditional
, file_change
)
2098 enum file_change_code file_change
;
2101 char *line_cmd_buf
, *line_end
;
2103 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2105 if (ip
->fname
== NULL
)
2108 update_position (ip
);
2110 if (CPP_OPTIONS (pfile
)->no_line_commands
2111 || CPP_OPTIONS (pfile
)->no_output
)
2114 line
= CPP_BUFFER (pfile
)->lineno
;
2115 col
= CPP_BUFFER (pfile
)->colno
;
2116 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2118 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2122 if (line
== pfile
->lineno
)
2125 /* If the inherited line number is a little too small,
2126 output some newlines instead of a #line command. */
2127 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2128 CPP_RESERVE (pfile
, 20);
2129 while (line
> pfile
->lineno
) {
2130 CPP_PUTC_Q (pfile
, '\n');
2138 /* Don't output a line number of 0 if we can help it. */
2139 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2140 && *ip
->bufp
== '\n') {
2146 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2148 #ifdef OUTPUT_LINE_COMMANDS
2149 static char sharp_line
[] = "#line ";
2151 static char sharp_line
[] = "# ";
2153 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2156 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
2157 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2159 quote_string (pfile
, ip
->nominal_fname
);
2160 if (file_change
!= same_file
) {
2161 CPP_PUTC_Q (pfile
, ' ');
2162 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2164 /* Tell cc1 if following text comes from a system header file. */
2165 if (ip
->system_header_p
) {
2166 CPP_PUTC_Q (pfile
, ' ');
2167 CPP_PUTC_Q (pfile
, '3');
2169 #ifndef NO_IMPLICIT_EXTERN_C
2170 /* Tell cc1plus if following text should be treated as C. */
2171 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2172 CPP_PUTC_Q (pfile
, ' ');
2173 CPP_PUTC_Q (pfile
, '4');
2176 CPP_PUTC_Q (pfile
, '\n');
2177 pfile
->lineno
= line
;
2181 * Parse a macro argument and append the info on PFILE's token_buffer.
2182 * REST_ARGS means to absorb the rest of the args.
2183 * Return nonzero to indicate a syntax error.
2186 static enum cpp_token
2187 macarg (pfile
, rest_args
)
2192 enum cpp_token token
;
2193 long arg_start
= CPP_WRITTEN (pfile
);
2194 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2195 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2197 /* Try to parse as much of the argument as exists at this
2198 input stack level. */
2199 pfile
->no_macro_expand
++;
2202 token
= cpp_get_token (pfile
);
2208 /* If we've hit end of file, it's an error (reported by caller).
2209 Ditto if it's the end of cpp_expand_to_buffer text.
2210 If we've hit end of macro, just continue. */
2211 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2222 /* if we've returned to lowest level and
2223 we aren't absorbing all args */
2224 if (paren
== 0 && rest_args
== 0)
2228 /* Remove ',' or ')' from argument buffer. */
2229 CPP_ADJUST_WRITTEN (pfile
, -1);
2236 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2237 pfile
->no_macro_expand
--;
2242 /* Turn newlines to spaces in the string of length LENGTH at START,
2243 except inside of string constants.
2244 The string is copied into itself with its beginning staying fixed. */
2247 change_newlines (start
, length
)
2251 register U_CHAR
*ibp
;
2252 register U_CHAR
*obp
;
2253 register U_CHAR
*limit
;
2257 limit
= start
+ length
;
2260 while (ibp
< limit
) {
2261 *obp
++ = c
= *ibp
++;
2266 /* Notice and skip strings, so that we don't delete newlines in them. */
2269 while (ibp
< limit
) {
2270 *obp
++ = c
= *ibp
++;
2273 if (c
== '\n' && quotec
== '\'')
2289 if (!pfile
->timebuf
) {
2290 time_t t
= time ((time_t *) 0);
2291 pfile
->timebuf
= localtime (&t
);
2293 return pfile
->timebuf
;
2296 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2297 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2301 * expand things like __FILE__. Place the expansion into the output
2302 * buffer *without* rescanning.
2306 special_symbol (hp
, pfile
)
2313 cpp_buffer
*ip
= NULL
;
2316 int paren
= 0; /* For special `defined' keyword */
2319 if (pcp_outfile
&& pcp_inside_if
2320 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2322 "Predefined macro `%s' used inside `#if' during precompilation",
2326 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2328 if (ip
== CPP_NULL_BUFFER (pfile
))
2330 cpp_error (pfile
, "cccp error: not in any file?!");
2331 return; /* the show must go on */
2333 if (ip
->fname
!= NULL
)
2343 if (hp
->type
== T_BASE_FILE
)
2345 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2346 ip
= CPP_PREV_BUFFER (ip
);
2348 string
= ip
->nominal_fname
;
2352 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2353 quote_string (pfile
, string
);
2357 case T_INCLUDE_LEVEL
:
2359 ip
= CPP_BUFFER (pfile
);
2360 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2361 if (ip
->fname
!= NULL
)
2364 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2365 sprintf (buf
, "%d", true_indepth
- 1);
2369 buf
= (char *) alloca (3 + strlen (version_string
));
2370 sprintf (buf
, "\"%s\"", version_string
);
2373 #ifndef NO_BUILTIN_SIZE_TYPE
2379 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2380 case T_PTRDIFF_TYPE
:
2386 buf
= CPP_WCHAR_TYPE (pfile
);
2389 case T_USER_LABEL_PREFIX_TYPE
:
2390 buf
= USER_LABEL_PREFIX
;
2393 case T_REGISTER_PREFIX_TYPE
:
2394 buf
= REGISTER_PREFIX
;
2398 buf
= (char *) alloca (4 * sizeof (int));
2399 sprintf (buf
, "%d", hp
->value
.ival
);
2400 #ifdef STDC_0_IN_SYSTEM_HEADERS
2401 if (ip
->system_header_p
2402 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2403 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2407 if (pcp_inside_if
&& pcp_outfile
)
2408 /* Output a precondition for this macro use */
2409 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2415 long line
= ip
->lineno
;
2416 long col
= ip
->colno
;
2417 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2419 buf
= (char *) alloca (10);
2420 sprintf (buf
, "%ld", line
);
2426 buf
= (char *) alloca (20);
2427 timebuf
= timestamp (pfile
);
2428 if (hp
->type
== T_DATE
)
2429 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2430 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2432 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2436 case T_SPEC_DEFINED
:
2437 buf
= " 0 "; /* Assume symbol is not defined */
2438 ip
= CPP_BUFFER (pfile
);
2439 SKIP_WHITE_SPACE (ip
->cur
);
2440 if (*ip
->cur
== '(')
2443 ip
->cur
++; /* Skip over the paren */
2444 SKIP_WHITE_SPACE (ip
->cur
);
2447 if (!is_idstart
[*ip
->cur
])
2449 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2451 if (hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1))
2454 if (pcp_outfile
&& pcp_inside_if
2455 && (hp
->type
== T_CONST
2456 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2457 /* Output a precondition for this macro use. */
2458 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2464 if (pcp_outfile
&& pcp_inside_if
)
2466 /* Output a precondition for this macro use */
2467 U_CHAR
*cp
= ip
->bufp
;
2468 fprintf (pcp_outfile
, "#undef ");
2469 while (is_idchar
[*cp
]) /* Ick! */
2470 fputc (*cp
++, pcp_outfile
);
2471 putc ('\n', pcp_outfile
);
2474 while (is_idchar
[*ip
->cur
])
2476 SKIP_WHITE_SPACE (ip
->cur
);
2479 if (*ip
->cur
!= ')')
2487 cpp_error (pfile
, "`defined' without an identifier");
2491 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2495 CPP_RESERVE (pfile
, len
+ 1);
2496 CPP_PUTS_Q (pfile
, buf
, len
);
2497 CPP_NUL_TERMINATE_Q (pfile
);
2502 /* Write out a #define command for the special named MACRO_NAME
2503 to PFILE's token_buffer. */
2506 dump_special_to_buffer (pfile
, macro_name
)
2510 static char define_directive
[] = "#define ";
2511 int macro_name_length
= strlen (macro_name
);
2512 output_line_command (pfile
, 0, same_file
);
2513 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2514 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2515 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2516 CPP_PUTC_Q (pfile
, ' ');
2517 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2518 CPP_PUTC (pfile
, '\n');
2521 /* Initialize the built-in macros. */
2524 initialize_builtins (pfile
)
2527 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2528 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2529 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2530 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2531 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2532 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2533 #ifndef NO_BUILTIN_SIZE_TYPE
2534 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2536 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2537 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2539 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2540 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2541 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2542 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2543 if (!CPP_TRADITIONAL (pfile
))
2544 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2545 if (CPP_OPTIONS (pfile
)->objc
)
2546 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2547 /* This is supplied using a -D by the compiler driver
2548 so that it is present only when truly compiling with GNU C. */
2549 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2551 if (CPP_OPTIONS (pfile
)->debug_output
)
2553 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2554 dump_special_to_buffer (pfile
, "__VERSION__");
2555 #ifndef NO_BUILTIN_SIZE_TYPE
2556 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2558 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2559 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2561 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2562 dump_special_to_buffer (pfile
, "__DATE__");
2563 dump_special_to_buffer (pfile
, "__TIME__");
2564 if (!CPP_TRADITIONAL (pfile
))
2565 dump_special_to_buffer (pfile
, "__STDC__");
2566 if (CPP_OPTIONS (pfile
)->objc
)
2567 dump_special_to_buffer (pfile
, "__OBJC__");
2571 /* Return 1 iff a token ending in C1 followed directly by a token C2
2572 could cause mis-tokenization. */
2575 unsafe_chars (c1
, c2
)
2581 if (c2
== c1
|| c2
== '=')
2585 case '0': case '1': case '2': case '3': case '4':
2586 case '5': case '6': case '7': case '8': case '9':
2587 case 'e': case 'E': case 'p': case 'P':
2588 if (c2
== '-' || c2
== '+')
2589 return 1; /* could extend a pre-processing number */
2592 if (c2
== '\'' || c2
== '\"')
2593 return 1; /* Could turn into L"xxx" or L'xxx'. */
2597 case 'a': case 'b': case 'c': case 'd': case 'f':
2598 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2599 case 'm': case 'n': case 'o': case 'q': case 'r':
2600 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2602 case 'A': case 'B': case 'C': case 'D': case 'F':
2603 case 'G': case 'H': case 'I': case 'J': case 'K':
2604 case 'M': case 'N': case 'O': case 'Q': case 'R':
2605 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2607 /* We're in the middle of either a name or a pre-processing number. */
2608 return (is_idchar
[c2
] || c2
== '.');
2609 case '<': case '>': case '!': case '%': case '#': case ':':
2610 case '^': case '&': case '|': case '*': case '/': case '=':
2611 return (c2
== c1
|| c2
== '=');
2616 /* Expand a macro call.
2617 HP points to the symbol that is the macro being called.
2618 Put the result of expansion onto the input stack
2619 so that subsequent input by our caller will use it.
2621 If macro wants arguments, caller has already verified that
2622 an argument list follows; arguments come from the input stack. */
2625 macroexpand (pfile
, hp
)
2630 DEFINITION
*defn
= hp
->value
.defn
;
2631 register U_CHAR
*xbuf
;
2632 long start_line
, start_column
;
2634 struct argdata
*args
;
2635 long old_written
= CPP_WRITTEN (pfile
);
2637 int start_line
= instack
[indepth
].lineno
;
2639 int rest_args
, rest_zero
;
2643 CHECK_DEPTH (return;);
2647 /* This macro is being used inside a #if, which means it must be */
2648 /* recorded as a precondition. */
2649 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2650 dump_single_macro (hp
, pcp_outfile
);
2653 pfile
->output_escapes
++;
2654 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2656 nargs
= defn
->nargs
;
2660 enum cpp_token token
;
2662 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2664 for (i
= 0; i
< nargs
; i
++)
2666 args
[i
].raw
= args
[i
].expanded
= 0;
2667 args
[i
].raw_length
= 0;
2668 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2669 args
[i
].use_count
= 0;
2672 /* Parse all the macro args that are supplied. I counts them.
2673 The first NARGS args are stored in ARGS.
2674 The rest are discarded. If rest_args is set then we assume
2675 macarg absorbed the rest of the args. */
2679 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2684 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2686 /* if we are working on last arg which absorbs rest of args... */
2687 if (i
== nargs
- 1 && defn
->rest_args
)
2689 args
[i
].raw
= CPP_WRITTEN (pfile
);
2690 token
= macarg (pfile
, rest_args
);
2691 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2692 args
[i
].newlines
= 0; /* FIXME */
2695 token
= macarg (pfile
, 0);
2696 if (token
== CPP_EOF
|| token
== CPP_POP
)
2698 cpp_error_with_line (pfile
, start_line
, start_column
,
2699 "unterminated macro call");
2703 } while (token
== CPP_COMMA
);
2705 /* If we got one arg but it was just whitespace, call that 0 args. */
2708 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2709 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2710 /* cpp.texi says for foo ( ) we provide one argument.
2711 However, if foo wants just 0 arguments, treat this as 0. */
2713 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2718 /* Don't output an error message if we have already output one for
2719 a parse error above. */
2721 if (nargs
== 0 && i
> 0)
2723 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2727 /* traditional C allows foo() if foo wants one argument. */
2728 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2730 /* the rest args token is allowed to absorb 0 tokens */
2731 else if (i
== nargs
- 1 && defn
->rest_args
)
2734 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2736 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2738 cpp_error (pfile
, "macro `%s' used with only %d args",
2744 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2748 /* If macro wants zero args, we parsed the arglist for checking only.
2749 Read directly from the macro definition. */
2752 xbuf
= defn
->expansion
;
2753 xbuf_len
= defn
->length
;
2757 register U_CHAR
*exp
= defn
->expansion
;
2758 register int offset
; /* offset in expansion,
2759 copied a piece at a time */
2760 register int totlen
; /* total amount of exp buffer filled so far */
2762 register struct reflist
*ap
, *last_ap
;
2764 /* Macro really takes args. Compute the expansion of this call. */
2766 /* Compute length in characters of the macro's expansion.
2767 Also count number of times each arg is used. */
2768 xbuf_len
= defn
->length
;
2769 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2773 register struct argdata
*arg
= &args
[ap
->argno
];
2774 /* Stringify it it hasn't already been */
2775 if (arg
->stringified_length
< 0)
2777 int arglen
= arg
->raw_length
;
2781 /* Initially need_space is -1. Otherwise, 1 means the
2782 previous character was a space, but we suppressed it;
2783 0 means the previous character was a non-space. */
2784 int need_space
= -1;
2786 arg
->stringified
= CPP_WRITTEN (pfile
);
2787 if (!CPP_TRADITIONAL (pfile
))
2788 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2789 for (; i
< arglen
; i
++)
2791 c
= (ARG_BASE
+ arg
->raw
)[i
];
2795 /* Internal sequences of whitespace are replaced by
2796 one space except within an string or char token.*/
2799 if (CPP_WRITTEN (pfile
) > arg
->stringified
2800 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2802 /* "@ " escape markers are removed */
2803 CPP_ADJUST_WRITTEN (pfile
, -1);
2806 if (need_space
== 0)
2810 else if (need_space
> 0)
2811 CPP_PUTC (pfile
, ' ');
2826 else if (c
== '\"' || c
== '\'')
2830 /* Escape these chars */
2831 if (c
== '\"' || (in_string
&& c
== '\\'))
2832 CPP_PUTC (pfile
, '\\');
2834 CPP_PUTC (pfile
, c
);
2837 CPP_RESERVE (pfile
, 4);
2838 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2840 CPP_ADJUST_WRITTEN (pfile
, 4);
2843 if (!CPP_TRADITIONAL (pfile
))
2844 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2845 arg
->stringified_length
2846 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2848 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2850 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2851 /* Add 4 for two newline-space markers to prevent
2852 token concatenation. */
2853 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2856 /* We have an ordinary (expanded) occurrence of the arg.
2857 So compute its expansion, if we have not already. */
2858 if (args
[ap
->argno
].expand_length
< 0)
2860 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2861 cpp_expand_to_buffer (pfile
,
2862 ARG_BASE
+ args
[ap
->argno
].raw
,
2863 args
[ap
->argno
].raw_length
);
2865 args
[ap
->argno
].expand_length
2866 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2869 /* Add 4 for two newline-space markers to prevent
2870 token concatenation. */
2871 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2873 if (args
[ap
->argno
].use_count
< 10)
2874 args
[ap
->argno
].use_count
++;
2877 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2879 /* Generate in XBUF the complete expansion
2880 with arguments substituted in.
2881 TOTLEN is the total size generated so far.
2882 OFFSET is the index in the definition
2883 of where we are copying from. */
2884 offset
= totlen
= 0;
2885 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2886 last_ap
= ap
, ap
= ap
->next
)
2888 register struct argdata
*arg
= &args
[ap
->argno
];
2889 int count_before
= totlen
;
2891 /* Add chars to XBUF. */
2892 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2893 xbuf
[totlen
++] = exp
[offset
];
2895 /* If followed by an empty rest arg with concatenation,
2896 delete the last run of nonwhite chars. */
2897 if (rest_zero
&& totlen
> count_before
2898 && ((ap
->rest_args
&& ap
->raw_before
)
2899 || (last_ap
!= NULL
&& last_ap
->rest_args
2900 && last_ap
->raw_after
)))
2902 /* Delete final whitespace. */
2903 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2906 /* Delete the nonwhites before them. */
2907 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2911 if (ap
->stringify
!= 0)
2913 bcopy (ARG_BASE
+ arg
->stringified
,
2914 xbuf
+ totlen
, arg
->stringified_length
);
2915 totlen
+= arg
->stringified_length
;
2917 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2919 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2920 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2923 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2924 while (p1
!= l1
&& is_idchar
[*p1
])
2925 xbuf
[totlen
++] = *p1
++;
2926 /* Delete any no-reexpansion marker that follows
2927 an identifier at the beginning of the argument
2928 if the argument is concatenated with what precedes it. */
2929 if (p1
[0] == '@' && p1
[1] == '-')
2934 /* Arg is concatenated after: delete trailing whitespace,
2935 whitespace markers, and no-reexpansion markers. */
2938 if (is_space
[l1
[-1]]) l1
--;
2939 else if (l1
[-1] == '-')
2941 U_CHAR
*p2
= l1
- 1;
2942 /* If a `-' is preceded by an odd number of newlines then it
2943 and the last newline are a no-reexpansion marker. */
2944 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2945 if ((l1
- 1 - p2
) & 1) {
2954 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2959 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2960 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2961 && !CPP_TRADITIONAL(pfile
)
2962 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2964 xbuf
[totlen
++] = '@';
2965 xbuf
[totlen
++] = ' ';
2968 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2969 totlen
+= arg
->expand_length
;
2971 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2972 && !CPP_TRADITIONAL(pfile
)
2973 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2975 xbuf
[totlen
++] = '@';
2976 xbuf
[totlen
++] = ' ';
2979 /* If a macro argument with newlines is used multiple times,
2980 then only expand the newlines once. This avoids creating
2981 output lines which don't correspond to any input line,
2982 which confuses gdb and gcov. */
2983 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2985 /* Don't bother doing change_newlines for subsequent
2989 = change_newlines (expanded
, arg
->expand_length
);
2993 if (totlen
> xbuf_len
)
2997 /* if there is anything left of the definition
2998 after handling the arg list, copy that in too. */
3000 for (i
= offset
; i
< defn
->length
; i
++)
3002 /* if we've reached the end of the macro */
3005 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
3006 && last_ap
->raw_after
))
3007 xbuf
[totlen
++] = exp
[i
];
3015 pfile
->output_escapes
--;
3017 /* Now put the expansion on the input stack
3018 so our caller will commence reading from it. */
3019 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
3020 CPP_BUFFER (pfile
)->has_escapes
= 1;
3022 /* Pop the space we've used in the token_buffer for argument expansion. */
3023 CPP_SET_WRITTEN (pfile
, old_written
);
3025 /* Recursive macro use sometimes works traditionally.
3026 #define foo(x,y) bar (x (y,0), y)
3029 if (!CPP_TRADITIONAL (pfile
))
3030 hp
->type
= T_DISABLED
;
3034 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
3036 register U_CHAR
*xbuf
;
3040 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
3043 mbuf
->cleanup
= macro_cleanup
;
3046 /* The first chars of the expansion should be a "@ " added by
3047 collect_expansion. This is to prevent accidental token-pasting
3048 between the text preceding the macro invocation, and the macro
3051 We would like to avoid adding unneeded spaces (for the sake of
3052 tools that use cpp, such as imake). In some common cases we can
3053 tell that it is safe to omit the space.
3055 The character before the macro invocation cannot have been an
3056 idchar (or else it would have been pasted with the idchars of
3057 the macro name). Therefore, if the first non-space character
3058 of the expansion is an idchar, we do not need the extra space
3059 to prevent token pasting.
3061 Also, we don't need the extra space if the first char is '(',
3062 or some other (less common) characters. */
3064 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3065 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3066 || xbuf
[2] == '\"'))
3070 /* Like cpp_get_token, except that it does not read past end-of-line.
3071 Also, horizontal space is skipped, and macros are popped. */
3073 static enum cpp_token
3074 get_directive_token (pfile
)
3079 long old_written
= CPP_WRITTEN (pfile
);
3080 enum cpp_token token
;
3081 cpp_skip_hspace (pfile
);
3082 if (PEEKC () == '\n')
3084 token
= cpp_get_token (pfile
);
3088 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3090 /* ... else fall though ... */
3091 case CPP_HSPACE
: case CPP_COMMENT
:
3092 CPP_SET_WRITTEN (pfile
, old_written
);
3100 /* Handle #include and #import.
3101 This function expects to see "fname" or <fname> on the input.
3103 The input is normally in part of the output_buffer following
3104 CPP_WRITTEN, and will get overwritten by output_line_command.
3105 I.e. in input file specification has been popped by handle_directive.
3109 do_include (pfile
, keyword
, unused1
, unused2
)
3111 struct directive
*keyword
;
3112 U_CHAR
*unused1
, *unused2
;
3114 int importing
= (keyword
->type
== T_IMPORT
);
3115 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3116 char *fname
; /* Dynamically allocated fname buffer */
3119 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3120 enum cpp_token token
;
3122 /* Chain of dirs to search */
3123 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3124 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3125 struct file_name_list
*searchptr
= 0;
3126 long old_written
= CPP_WRITTEN (pfile
);
3130 int f
; /* file number */
3132 int retried
= 0; /* Have already tried macro
3133 expanding the include line */
3134 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3139 f
= -1; /* JF we iz paranoid! */
3141 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3144 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
3146 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
3149 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3150 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3151 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3153 pfile
->import_warning
= 1;
3154 cpp_warning (pfile
, "using `#import' is not recommended");
3155 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3156 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3157 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3158 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3159 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3160 fprintf (stderr
, " ... <real contents of file> ...\n");
3161 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3162 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3163 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3164 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3167 pfile
->parsing_include_directive
++;
3168 token
= get_directive_token (pfile
);
3169 pfile
->parsing_include_directive
--;
3171 if (token
== CPP_STRING
)
3173 /* FIXME - check no trailing garbage */
3174 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3175 fend
= CPP_PWRITTEN (pfile
) - 1;
3176 if (fbeg
[-1] == '<')
3179 /* If -I-, start with the first -I dir after the -I-. */
3180 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3181 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3183 /* If -I- was specified, don't search current dir, only spec'd ones. */
3184 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3186 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3187 /* We have "filename". Figure out directory this source
3188 file is coming from and put it on the front of the list. */
3190 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3195 if ((nam
= fp
->nominal_fname
) != NULL
)
3197 /* Found a named file. Figure out dir of the file,
3198 and put it in front of the search list. */
3199 dsp
[0].next
= search_start
;
3202 ep
= rindex (nam
, '/');
3204 ep
= rindex (nam
, ']');
3205 if (ep
== NULL
) ep
= rindex (nam
, '>');
3206 if (ep
== NULL
) ep
= rindex (nam
, ':');
3207 if (ep
!= NULL
) ep
++;
3212 dsp
[0].fname
= (char *) alloca (n
+ 1);
3213 strncpy (dsp
[0].fname
, nam
, n
);
3214 dsp
[0].fname
[n
] = '\0';
3215 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3216 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3220 dsp
[0].fname
= 0; /* Current directory */
3222 dsp
[0].got_name_map
= 0;
3229 else if (token
== CPP_NAME
)
3232 * Support '#include xyz' like VAX-C to allow for easy use of all the
3233 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3234 * code from case '<' is repeated here) and generates a warning.
3237 "VAX-C-style include specification found, use '#include <filename.h>' !");
3239 /* If -I-, start with the first -I dir after the -I-. */
3240 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3241 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3242 fbeg
= pfile
->token_buffer
+ old_written
;
3243 fend
= CPP_PWRITTEN (pfile
);
3249 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3250 CPP_SET_WRITTEN (pfile
, old_written
);
3251 skip_rest_of_line (pfile
);
3257 token
= get_directive_token (pfile
);
3258 if (token
!= CPP_VSPACE
)
3260 cpp_error (pfile
, "junk at end of `#include'");
3261 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3262 token
= get_directive_token (pfile
);
3265 /* For #include_next, skip in the search path
3266 past the dir in which the containing file was found. */
3269 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3270 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3271 if (fp
->fname
!= NULL
)
3273 /* fp->dir is null if the containing file was specified with
3274 an absolute file name. In that case, don't skip anything. */
3275 if (fp
->dir
== SELF_DIR_DUMMY
)
3276 search_start
= CPP_OPTIONS (pfile
)->include
;
3278 search_start
= fp
->dir
->next
;
3283 CPP_SET_WRITTEN (pfile
, old_written
);
3289 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3293 /* Allocate this permanently, because it gets stored in the definitions
3295 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3296 /* + 2 above for slash and terminating null. */
3297 /* + 2 added for '.h' on VMS (to support '#include filename') */
3299 /* If specified file name is absolute, just open it. */
3302 strncpy (fname
, fbeg
, flen
);
3304 if (redundant_include_p (pfile
, fname
))
3307 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3309 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3311 return 0; /* Already included this file */
3313 /* Search directory path, trying to open the file.
3314 Copy each filename tried into FNAME. */
3316 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3317 if (searchptr
->fname
) {
3318 /* The empty string in a search path is ignored.
3319 This makes it possible to turn off entirely
3320 a standard piece of the list. */
3321 if (searchptr
->fname
[0] == 0)
3323 strcpy (fname
, searchptr
->fname
);
3324 strcat (fname
, "/");
3325 fname
[strlen (fname
) + flen
] = 0;
3329 strncat (fname
, fbeg
, flen
);
3331 /* Change this 1/2 Unix 1/2 VMS file specification into a
3332 full VMS file specification */
3333 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3334 /* Fix up the filename */
3335 hack_vms_include_specification (fname
);
3337 /* This is a normal VMS filespec, so use it unchanged. */
3338 strncpy (fname
, fbeg
, flen
);
3340 /* if it's '#include filename', add the missing .h */
3341 if (index(fname
,'.')==NULL
) {
3342 strcat (fname
, ".h");
3346 /* ??? There are currently 3 separate mechanisms for avoiding processing
3347 of redundant include files: #import, #pragma once, and
3348 redundant_include_p. It would be nice if they were unified. */
3349 if (redundant_include_p (pfile
, fname
))
3352 f
= lookup_import (pfile
, fname
, searchptr
);
3354 f
= open_include_file (pfile
, fname
, searchptr
);
3356 return 0; /* Already included this file */
3358 else if (f
== -1 && errno
== EACCES
)
3359 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3369 /* A file that was not found. */
3370 strncpy (fname
, fbeg
, flen
);
3372 /* If generating dependencies and -MG was specified, we assume missing
3373 files are leaf files, living in the same directory as the source file
3374 or other similar place; these missing files may be generated from
3375 other files and may not exist yet (eg: y.tab.h). */
3377 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3378 && CPP_PRINT_DEPS (pfile
)
3379 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3381 /* If it was requested as a system header file,
3382 then assume it belongs in the first place to look for such. */
3385 for (searchptr
= search_start
; searchptr
;
3386 searchptr
= searchptr
->next
)
3388 if (searchptr
->fname
)
3392 if (searchptr
->fname
[0] == 0)
3394 p
= (char *) alloca (strlen (searchptr
->fname
)
3395 + strlen (fname
) + 2);
3396 strcpy (p
, searchptr
->fname
);
3399 deps_output (pfile
, p
, ' ');
3406 /* Otherwise, omit the directory, as if the file existed
3407 in the directory with the source. */
3408 deps_output (pfile
, fname
, ' ');
3411 /* If -M was specified, and this header file won't be added to the
3412 dependency list, then don't count this as an error, because we can
3413 still produce correct output. Otherwise, we can't produce correct
3414 output, because there may be dependencies we need inside the missing
3415 file, and we don't know what directory this missing file exists in.*/
3416 else if (CPP_PRINT_DEPS (pfile
)
3417 && (CPP_PRINT_DEPS (pfile
)
3418 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3419 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3420 else if (search_start
)
3421 cpp_error_from_errno (pfile
, fname
);
3423 cpp_error (pfile
, "No include path in which to find %s", fname
);
3426 /* Check to see if this include file is a once-only include file.
3429 struct file_name_list
*ptr
;
3431 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3432 if (!strcmp (ptr
->fname
, fname
)) {
3434 return 0; /* This file was once'd. */
3438 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3439 if (!strcmp (ptr
->fname
, fname
))
3440 break; /* This file was included before. */
3444 /* This is the first time for this file. */
3445 /* Add it to list of files included. */
3447 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3448 ptr
->control_macro
= 0;
3449 ptr
->c_system_include_path
= 0;
3450 ptr
->next
= pfile
->all_include_files
;
3451 pfile
->all_include_files
= ptr
;
3452 ptr
->fname
= savestring (fname
);
3453 ptr
->got_name_map
= 0;
3455 /* For -M, add this file to the dependencies. */
3456 if (CPP_PRINT_DEPS (pfile
)
3457 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3458 deps_output (pfile
, fname
, ' ');
3461 /* Handle -H option. */
3462 if (CPP_OPTIONS(pfile
)->print_include_names
)
3464 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3465 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3467 fprintf (stderr
, "%s\n", fname
);
3471 pfile
->system_include_depth
++;
3473 /* Actually process the file. */
3475 /* Record file on "seen" list for #import. */
3476 add_import (pfile
, f
, fname
);
3478 pcftry
= (char *) alloca (strlen (fname
) + 30);
3490 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3492 pcf
= open (pcftry
, O_RDONLY
, 0666);
3498 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3500 || stat_f
.st_dev
!= s
.st_dev
)
3502 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3503 /* Don't need it any more. */
3508 /* Don't need it at all. */
3513 } while (pcf
!= -1 && !pcfbuf
);
3517 /* Actually process the file */
3518 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3520 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3521 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3523 output_line_command (pfile
, 0, enter_file
);
3524 pfile
->only_seen_white
= 2;
3528 pfile
->system_include_depth
--;
3533 /* Return nonzero if there is no need to include file NAME
3534 because it has already been included and it contains a conditional
3535 to make a repeated include do nothing. */
3538 redundant_include_p (pfile
, name
)
3542 struct file_name_list
*l
= pfile
->all_include_files
;
3543 for (; l
; l
= l
->next
)
3544 if (! strcmp (name
, l
->fname
)
3546 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3551 /* Return nonzero if the given FILENAME is an absolute pathname which
3552 designates a file within one of the known "system" include file
3553 directories. We assume here that if the given FILENAME looks like
3554 it is the name of a file which resides either directly in a "system"
3555 include file directory, or within any subdirectory thereof, then the
3556 given file must be a "system" include file. This function tells us
3557 if we should suppress pedantic errors/warnings for the given FILENAME.
3559 The value is 2 if the file is a C-language system header file
3560 for which C++ should (on most systems) assume `extern "C"'. */
3563 is_system_include (pfile
, filename
)
3565 register char *filename
;
3567 struct file_name_list
*searchptr
;
3569 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3570 searchptr
= searchptr
->next
)
3571 if (searchptr
->fname
) {
3572 register char *sys_dir
= searchptr
->fname
;
3573 register unsigned length
= strlen (sys_dir
);
3575 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3577 if (searchptr
->c_system_include_path
)
3588 * Install a name in the assertion hash table.
3590 * If LEN is >= 0, it is the length of the name.
3591 * Otherwise, compute the length by scanning the entire name.
3593 * If HASH is >= 0, it is the precomputed hash code.
3594 * Otherwise, compute the hash code.
3597 static ASSERTION_HASHNODE
*
3598 assertion_install (pfile
, name
, len
, hash
)
3604 register ASSERTION_HASHNODE
*hp
;
3605 register int i
, bucket
;
3606 register U_CHAR
*p
, *q
;
3608 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3609 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3611 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3612 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3613 pfile
->assertion_hashtab
[bucket
] = hp
;
3615 if (hp
->next
!= NULL
)
3616 hp
->next
->prev
= hp
;
3619 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3622 for (i
= 0; i
< len
; i
++)
3628 * find the most recent hash node for name name (ending with first
3629 * non-identifier char) installed by install
3631 * If LEN is >= 0, it is the length of the name.
3632 * Otherwise, compute the length by scanning the entire name.
3634 * If HASH is >= 0, it is the precomputed hash code.
3635 * Otherwise, compute the hash code.
3638 static ASSERTION_HASHNODE
*
3639 assertion_lookup (pfile
, name
, len
, hash
)
3645 register ASSERTION_HASHNODE
*bucket
;
3647 bucket
= pfile
->assertion_hashtab
[hash
];
3649 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3651 bucket
= bucket
->next
;
3657 delete_assertion (hp
)
3658 ASSERTION_HASHNODE
*hp
;
3660 struct tokenlist_list
*tail
;
3661 if (hp
->prev
!= NULL
)
3662 hp
->prev
->next
= hp
->next
;
3663 if (hp
->next
!= NULL
)
3664 hp
->next
->prev
= hp
->prev
;
3666 for (tail
= hp
->value
; tail
; )
3668 struct tokenlist_list
*next
= tail
->next
;
3669 free_token_list (tail
->tokens
);
3674 /* Make sure that the bucket chain header that
3675 the deleted guy was on points to the right thing afterwards. */
3676 if (hp
== *hp
->bucket_hdr
)
3677 *hp
->bucket_hdr
= hp
->next
;
3682 /* Convert a character string literal into a nul-terminated string.
3683 The input string is [IN ... LIMIT).
3684 The result is placed in RESULT. RESULT can be the same as IN.
3685 The value returned in the end of the string written to RESULT,
3686 or NULL on error. */
3689 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3691 register U_CHAR
*result
, *in
, *limit
;
3711 char *bpc
= (char *) in
;
3712 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3713 in
= (U_CHAR
*) bpc
;
3715 *result
++ = (U_CHAR
)c
;
3718 /* else fall through */
3728 * interpret #line command. Remembers previously seen fnames
3729 * in its very own hash table.
3731 #define FNAME_HASHSIZE 37
3734 do_line (pfile
, keyword
)
3736 struct directive
*keyword
;
3738 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3740 long old_written
= CPP_WRITTEN (pfile
);
3741 enum file_change_code file_change
= same_file
;
3742 enum cpp_token token
;
3745 token
= get_directive_token (pfile
);
3747 if (token
!= CPP_NUMBER
3748 || !isdigit(pfile
->token_buffer
[old_written
]))
3750 cpp_error (pfile
, "invalid format `#line' command");
3751 goto bad_line_directive
;
3754 /* The Newline at the end of this line remains to be processed.
3755 To put the next line at the specified line number,
3756 we must store a line number now that is one less. */
3757 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3758 CPP_SET_WRITTEN (pfile
, old_written
);
3760 /* NEW_LINENO is one less than the actual line number here. */
3761 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3762 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3764 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3765 if (PEEKC() && !is_space
[PEEKC()]) {
3766 cpp_error (pfile
, "invalid format `#line' command");
3767 goto bad_line_directive
;
3771 token
= get_directive_token (pfile
);
3773 if (token
== CPP_STRING
) {
3774 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3776 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3777 HASHNODE
*hp
, **hash_bucket
;
3782 /* Turn the file name, which is a character string literal,
3783 into a null-terminated string. Do this in place. */
3784 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3785 if (end_name
== NULL
)
3787 cpp_error (pfile
, "invalid format `#line' command");
3788 goto bad_line_directive
;
3791 fname_length
= end_name
- fname
;
3793 num_start
= CPP_WRITTEN (pfile
);
3794 token
= get_directive_token (pfile
);
3795 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3796 p
= pfile
->token_buffer
+ num_start
;
3797 if (CPP_PEDANTIC (pfile
))
3798 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3800 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3802 cpp_error (pfile
, "invalid format `#line' command");
3803 goto bad_line_directive
;
3806 file_change
= enter_file
;
3808 file_change
= leave_file
;
3810 ip
->system_header_p
= 1;
3811 else /* if (*p == 4) */
3812 ip
->system_header_p
= 2;
3814 CPP_SET_WRITTEN (pfile
, num_start
);
3815 token
= get_directive_token (pfile
);
3816 p
= pfile
->token_buffer
+ num_start
;
3817 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3818 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3819 token
= get_directive_token (pfile
);
3821 if (token
!= CPP_VSPACE
) {
3822 cpp_error (pfile
, "invalid format `#line' command");
3823 goto bad_line_directive
;
3827 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3828 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3829 if (hp
->length
== fname_length
3830 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3831 ip
->nominal_fname
= hp
->value
.cpval
;
3835 /* Didn't find it; cons up a new one. */
3836 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3837 hp
->next
= *hash_bucket
;
3840 hp
->length
= fname_length
;
3841 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3842 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3845 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3846 cpp_error (pfile
, "invalid format `#line' command");
3847 goto bad_line_directive
;
3850 ip
->lineno
= new_lineno
;
3852 skip_rest_of_line (pfile
);
3853 CPP_SET_WRITTEN (pfile
, old_written
);
3854 output_line_command (pfile
, 0, file_change
);
3859 * remove the definition of a symbol from the symbol table.
3860 * according to un*x /lib/cpp, it is not an error to undef
3861 * something that has no definitions, so it isn't one here either.
3865 do_undef (pfile
, keyword
, buf
, limit
)
3867 struct directive
*keyword
;
3868 U_CHAR
*buf
, *limit
;
3872 U_CHAR
*orig_buf
= buf
;
3875 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3876 if (pcp_outfile
&& keyword
)
3877 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3880 SKIP_WHITE_SPACE (buf
);
3881 sym_length
= check_macro_name (pfile
, buf
, "macro");
3883 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3885 /* If we are generating additional info for debugging (with -g) we
3886 need to pass through all effective #undef commands. */
3887 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3888 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3889 if (hp
->type
!= T_MACRO
)
3890 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3894 if (CPP_PEDANTIC (pfile
)) {
3896 SKIP_WHITE_SPACE (buf
);
3898 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3904 * Report an error detected by the program we are processing.
3905 * Use the text of the line in the error message.
3906 * (We use error because it prints the filename & line#.)
3910 do_error (pfile
, keyword
, buf
, limit
)
3912 struct directive
*keyword
;
3913 U_CHAR
*buf
, *limit
;
3915 int length
= limit
- buf
;
3916 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3917 bcopy (buf
, copy
, length
);
3919 SKIP_WHITE_SPACE (copy
);
3920 cpp_error (pfile
, "#error %s", copy
);
3925 * Report a warning detected by the program we are processing.
3926 * Use the text of the line in the warning message, then continue.
3927 * (We use error because it prints the filename & line#.)
3931 do_warning (pfile
, keyword
, buf
, limit
)
3933 struct directive
*keyword
;
3934 U_CHAR
*buf
, *limit
;
3936 int length
= limit
- buf
;
3937 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3938 bcopy (buf
, copy
, length
);
3940 SKIP_WHITE_SPACE (copy
);
3941 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3942 if -pedantic-errors is given, #warning should cause an error. */
3943 cpp_pedwarn (pfile
, "#warning %s", copy
);
3947 /* Remember the name of the current file being read from so that we can
3948 avoid ever including it again. */
3954 cpp_buffer
*ip
= NULL
;
3955 struct file_name_list
*new;
3957 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3959 if (ip
== CPP_NULL_BUFFER (pfile
))
3961 if (ip
->fname
!= NULL
)
3966 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3967 new->next
= pfile
->dont_repeat_files
;
3968 pfile
->dont_repeat_files
= new;
3969 new->fname
= savestring (ip
->fname
);
3970 new->control_macro
= 0;
3971 new->got_name_map
= 0;
3972 new->c_system_include_path
= 0;
3977 /* Report program identification. */
3980 do_ident (pfile
, keyword
, buf
, limit
)
3982 struct directive
*keyword
;
3983 U_CHAR
*buf
, *limit
;
3985 /* long old_written = CPP_WRITTEN (pfile);*/
3988 /* Allow #ident in system headers, since that's not user's fault. */
3989 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3990 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3992 /* Leave rest of line to be read by later calls to cpp_get_token. */
3997 /* #pragma and its argument line have already been copied to the output file.
3998 Just check for some recognized pragmas that need validation here. */
4001 do_pragma (pfile
, keyword
, buf
, limit
)
4003 struct directive
*keyword
;
4004 U_CHAR
*buf
, *limit
;
4006 while (*buf
== ' ' || *buf
== '\t')
4008 if (!strncmp (buf
, "once", 4)) {
4009 /* Allow #pragma once in system headers, since that's not the user's
4011 if (!CPP_BUFFER (pfile
)->system_header_p
)
4012 cpp_warning (pfile
, "`#pragma once' is obsolete");
4016 if (!strncmp (buf
, "implementation", 14)) {
4017 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4018 been included yet. */
4019 struct file_name_list
*ptr
;
4020 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
4022 SKIP_WHITE_SPACE (p
);
4023 if (*p
== '\n' || *p
!= '\"')
4027 p
= (U_CHAR
*) index (fname
, '\"');
4028 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
4030 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
4031 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
4032 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
4033 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
4035 "`#pragma implementation' for `%s' appears after file is included",
4044 /* This was a fun hack, but #pragma seems to start to be useful.
4045 By failing to recognize it, we pass it through unchanged to cc1. */
4048 * the behavior of the #pragma directive is implementation defined.
4049 * this implementation defines it as follows.
4056 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
4059 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4061 execl ("/usr/games/hack", "#pragma", 0);
4062 execl ("/usr/games/rogue", "#pragma", 0);
4063 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4064 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4066 fatal ("You are in a maze of twisty compiler features, all different");
4070 /* Just ignore #sccs, on systems where we define it at all. */
4073 do_sccs (pfile
, keyword
, buf
, limit
)
4075 struct directive
*keyword
;
4076 U_CHAR
*buf
, *limit
;
4078 if (CPP_PEDANTIC (pfile
))
4079 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4084 * handle #if command by
4085 * 1) inserting special `defined' keyword into the hash table
4086 * that gets turned into 0 or 1 by special_symbol (thus,
4087 * if the luser has a symbol called `defined' already, it won't
4088 * work inside the #if command)
4089 * 2) rescan the input into a temporary output buffer
4090 * 3) pass the output buffer to the yacc parser and collect a value
4091 * 4) clean up the mess left from steps 1 and 2.
4092 * 5) call conditional_skip to skip til the next #endif (etc.),
4093 * or not, depending on the value from step 3.
4097 do_if (pfile
, keyword
, buf
, limit
)
4099 struct directive
*keyword
;
4100 U_CHAR
*buf
, *limit
;
4102 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4103 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4108 * handle a #elif directive by not changing if_stack either.
4109 * see the comment above do_else.
4113 do_elif (pfile
, keyword
, buf
, limit
)
4115 struct directive
*keyword
;
4116 U_CHAR
*buf
, *limit
;
4118 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4119 cpp_error (pfile
, "`#elif' not within a conditional");
4122 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4123 cpp_error (pfile
, "`#elif' after `#else'");
4125 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4127 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4128 && strcmp (pfile
->if_stack
->fname
,
4129 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4130 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4131 fprintf (stderr
, ")\n");
4133 pfile
->if_stack
->type
= T_ELIF
;
4136 if (pfile
->if_stack
->if_succeeded
)
4137 skip_if_group (pfile
, 0);
4139 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4141 skip_if_group (pfile
, 0);
4143 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4144 output_line_command (pfile
, 1, same_file
);
4151 * evaluate a #if expression in BUF, of length LENGTH,
4152 * then parse the result as a C expression and return the value as an int.
4155 static HOST_WIDE_INT
4156 eval_if_expression (pfile
, buf
, length
)
4161 HASHNODE
*save_defined
;
4162 HOST_WIDE_INT value
;
4163 long old_written
= CPP_WRITTEN (pfile
);
4165 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4166 pfile
->pcp_inside_if
= 1;
4168 value
= cpp_parse_expr (pfile
);
4169 pfile
->pcp_inside_if
= 0;
4170 delete_macro (save_defined
); /* clean up special symbol */
4172 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4178 * routine to handle ifdef/ifndef. Try to look up the symbol,
4179 * then do or don't skip to the #endif/#else/#elif depending
4180 * on what directive is actually being processed.
4184 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4186 struct directive
*keyword
;
4187 U_CHAR
*unused1
, *unused2
;
4190 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4193 enum cpp_token token
;
4194 int start_of_file
= 0;
4195 U_CHAR
*control_macro
= 0;
4196 int old_written
= CPP_WRITTEN (pfile
);
4198 /* Detect a #ifndef at start of file (not counting comments). */
4199 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4200 start_of_file
= pfile
->only_seen_white
== 2;
4202 pfile
->no_macro_expand
++;
4203 token
= get_directive_token (pfile
);
4204 pfile
->no_macro_expand
--;
4206 ident
= pfile
->token_buffer
+ old_written
;
4207 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4208 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4210 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4212 skip
= (keyword
->type
== T_IFDEF
);
4213 if (! CPP_TRADITIONAL (pfile
))
4214 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4216 else if (token
== CPP_NAME
)
4218 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4219 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4220 if (start_of_file
&& !skip
)
4222 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4223 bcopy (ident
, control_macro
, ident_length
+ 1);
4228 skip
= (keyword
->type
== T_IFDEF
);
4229 if (! CPP_TRADITIONAL (pfile
))
4230 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4233 if (!CPP_TRADITIONAL (pfile
))
4235 cpp_skip_hspace (pfile
);
4237 if (c
!= EOF
&& c
!= '\n')
4238 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4240 skip_rest_of_line (pfile
);
4244 /* Output a precondition for this macro. */
4245 if (hp
&& hp
->value
.defn
->predefined
)
4246 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4249 fprintf (pcp_outfile
, "#undef ");
4250 while (is_idchar
[*cp
]) /* Ick! */
4251 fputc (*cp
++, pcp_outfile
);
4252 putc ('\n', pcp_outfile
);
4256 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4260 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4261 If this is a #ifndef starting at the beginning of a file,
4262 CONTROL_MACRO is the macro name tested by the #ifndef.
4263 Otherwise, CONTROL_MACRO is 0. */
4266 conditional_skip (pfile
, skip
, type
, control_macro
)
4269 enum node_type type
;
4270 U_CHAR
*control_macro
;
4272 IF_STACK_FRAME
*temp
;
4274 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4275 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4277 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4279 temp
->next
= pfile
->if_stack
;
4280 temp
->control_macro
= control_macro
;
4281 pfile
->if_stack
= temp
;
4283 pfile
->if_stack
->type
= type
;
4286 skip_if_group (pfile
, 0);
4289 ++pfile
->if_stack
->if_succeeded
;
4290 output_line_command (pfile
, 1, same_file
);
4295 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4296 * leaves input ptr at the sharp sign found.
4297 * If ANY is nonzero, return at next directive of any sort.
4301 skip_if_group (pfile
, any
)
4306 int at_beg_of_line
= 1;
4307 struct directive
*kt
;
4308 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4310 U_CHAR
*beg_of_line
= bp
;
4312 register int ident_length
;
4313 U_CHAR
*ident
, *after_ident
;
4314 struct parse_marker line_start_mark
;
4316 parse_set_mark (&line_start_mark
, pfile
);
4318 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4319 static char failed
[] = "#failed\n";
4320 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4322 output_line_command (pfile
, 1, same_file
);
4326 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4328 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4329 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4330 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4332 parse_move_mark (&line_start_mark
, pfile
);
4333 if (!CPP_TRADITIONAL (pfile
))
4334 cpp_skip_hspace (pfile
);
4338 int old_written
= CPP_WRITTEN (pfile
);
4339 cpp_skip_hspace (pfile
);
4341 parse_name (pfile
, GETC());
4342 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4343 ident
= pfile
->token_buffer
+ old_written
;
4344 pfile
->limit
= ident
;
4346 if (ident_length
== 0)
4347 goto not_a_directive
;
4349 /* Handle # followed by a line number. */
4351 /* Avoid error for `###' and similar cases unless -pedantic. */
4354 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4356 IF_STACK_FRAME
*temp
;
4357 if (ident_length
== kt
->length
4358 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4360 /* If we are asked to return on next directive, do so now. */
4370 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4371 temp
->next
= pfile
->if_stack
;
4372 pfile
->if_stack
= temp
;
4374 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4376 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4377 temp
->type
= kt
->type
;
4381 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4382 validate_else (pfile
,
4383 kt
->type
== T_ELSE
? "#else" : "#endif");
4385 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4388 "`#%s' not within a conditional", kt
->name
);
4391 else if (pfile
->if_stack
== save_if_stack
)
4392 goto done
; /* found what we came for */
4394 if (kt
->type
!= T_ENDIF
)
4396 if (pfile
->if_stack
->type
== T_ELSE
)
4397 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4398 pfile
->if_stack
->type
= kt
->type
;
4402 temp
= pfile
->if_stack
;
4403 pfile
->if_stack
= temp
->next
;
4410 /* Don't let erroneous code go by. */
4411 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4412 && CPP_PEDANTIC (pfile
))
4413 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4417 /* We're in the middle of a line. Skip the rest of it. */
4424 case '/': /* possible comment */
4425 c
= skip_comment (pfile
, NULL
);
4432 old
= CPP_WRITTEN (pfile
);
4433 cpp_get_token (pfile
);
4434 CPP_SET_WRITTEN (pfile
, old
);
4437 /* Char after backslash loses its special meaning. */
4438 if (PEEKC() == '\n')
4448 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4449 static char end_failed
[] = "#endfailed\n";
4450 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4453 pfile
->only_seen_white
= 1;
4454 parse_goto_mark (&line_start_mark
, pfile
);
4455 parse_clear_mark (&line_start_mark
);
4459 * handle a #else directive. Do this by just continuing processing
4460 * without changing if_stack ; this is so that the error message
4461 * for missing #endif's etc. will point to the original #if. It
4462 * is possible that something different would be better.
4466 do_else (pfile
, keyword
, buf
, limit
)
4468 struct directive
*keyword
;
4469 U_CHAR
*buf
, *limit
;
4471 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4473 if (CPP_PEDANTIC (pfile
))
4474 validate_else (pfile
, "#else");
4475 skip_rest_of_line (pfile
);
4477 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4478 cpp_error (pfile
, "`#else' not within a conditional");
4481 /* #ifndef can't have its special treatment for containing the whole file
4482 if it has a #else clause. */
4483 pfile
->if_stack
->control_macro
= 0;
4485 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4486 cpp_error (pfile
, "`#else' after `#else'");
4487 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4488 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4489 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4490 fprintf (stderr
, ")\n");
4492 pfile
->if_stack
->type
= T_ELSE
;
4495 if (pfile
->if_stack
->if_succeeded
)
4496 skip_if_group (pfile
, 0);
4498 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4499 output_line_command (pfile
, 1, same_file
);
4505 * unstack after #endif command
4509 do_endif (pfile
, keyword
, buf
, limit
)
4511 struct directive
*keyword
;
4512 U_CHAR
*buf
, *limit
;
4514 if (CPP_PEDANTIC (pfile
))
4515 validate_else (pfile
, "#endif");
4516 skip_rest_of_line (pfile
);
4518 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4519 cpp_error (pfile
, "unbalanced `#endif'");
4522 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4523 pfile
->if_stack
= temp
->next
;
4524 if (temp
->control_macro
!= 0)
4526 /* This #endif matched a #ifndef at the start of the file.
4527 See if it is at the end of the file. */
4528 struct parse_marker start_mark
;
4531 parse_set_mark (&start_mark
, pfile
);
4535 cpp_skip_hspace (pfile
);
4540 parse_goto_mark (&start_mark
, pfile
);
4541 parse_clear_mark (&start_mark
);
4545 /* If we get here, this #endif ends a #ifndef
4546 that contains all of the file (aside from whitespace).
4547 Arrange not to include the file again
4548 if the macro that was tested is defined.
4550 Do not do this for the top-level file in a -include or any
4551 file in a -imacros. */
4555 && ! (indepth
== 1 && pfile
->no_record_file
)
4556 && ! (pfile
->no_record_file
&& no_output
))
4559 struct file_name_list
*ifile
= pfile
->all_include_files
;
4561 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4563 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4565 ifile
->control_macro
= temp
->control_macro
;
4573 output_line_command (pfile
, 1, same_file
);
4578 /* When an #else or #endif is found while skipping failed conditional,
4579 if -pedantic was specified, this is called to warn about text after
4580 the command name. P points to the first char after the command name. */
4583 validate_else (pfile
, directive
)
4588 cpp_skip_hspace (pfile
);
4590 if (c
!= EOF
&& c
!= '\n')
4592 "text following `%s' violates ANSI standard", directive
);
4595 /* Get the next token, and add it to the text in pfile->token_buffer.
4596 Return the kind of token we got. */
4599 cpp_get_token (pfile
)
4602 register int c
, c2
, c3
;
4604 long start_line
, start_column
;
4605 enum cpp_token token
;
4606 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4607 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4613 if (CPP_BUFFER (pfile
)->seen_eof
)
4615 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4622 cpp_buffer
*next_buf
4623 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4624 CPP_BUFFER (pfile
)->seen_eof
= 1;
4625 if (CPP_BUFFER (pfile
)->nominal_fname
4626 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4628 /* We're about to return from an #include file.
4629 Emit #line information now (as part of the CPP_POP) result.
4630 But the #line refers to the file we will pop to. */
4631 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4632 CPP_BUFFER (pfile
) = next_buf
;
4633 pfile
->input_stack_listing_current
= 0;
4634 output_line_command (pfile
, 0, leave_file
);
4635 CPP_BUFFER (pfile
) = cur_buffer
;
4645 struct parse_marker start_mark
;
4647 if (PEEKC () == '=')
4649 if (opts
->put_out_comments
)
4650 parse_set_mark (&start_mark
, pfile
);
4652 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4653 &start_line
, &start_column
);
4654 c
= skip_comment (pfile
, &newlines
);
4655 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4656 parse_clear_mark (&start_mark
);
4661 cpp_error_with_line (pfile
, start_line
, start_column
,
4662 "unterminated comment");
4665 c
= '/'; /* Initial letter of comment. */
4667 /* Comments are equivalent to spaces.
4668 For -traditional, a comment is equivalent to nothing. */
4669 if (opts
->put_out_comments
)
4671 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4673 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4674 int len
= pbuf
->cur
- start
;
4675 CPP_RESERVE(pfile
, 1 + len
);
4676 CPP_PUTC_Q (pfile
, c
);
4677 CPP_PUTS_Q (pfile
, start
, len
);
4678 pfile
->lineno
+= newlines
;
4679 parse_clear_mark (&start_mark
);
4682 else if (CPP_TRADITIONAL (pfile
))
4689 /* This may not work if cpp_get_token is called recursively,
4690 since many places look for horizontal space. */
4693 /* Copy the newlines into the output buffer, in order to
4694 avoid the pain of a #line every time a multiline comment
4696 CPP_RESERVE(pfile
, newlines
);
4697 while (--newlines
>= 0)
4699 CPP_PUTC_Q (pfile
, '\n');
4705 CPP_RESERVE(pfile
, 1);
4706 CPP_PUTC_Q (pfile
, ' ');
4710 if (opts
->for_lint
) {
4713 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4715 if (lintcmd
!= NULL
) {
4716 /* I believe it is always safe to emit this newline: */
4718 bcopy ("#pragma lint ", (char *) obp
, 13);
4720 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4725 bcopy (argbp
, (char *) obp
, arglen
);
4729 /* OK, now bring us back to the state we were in before we entered
4730 this branch. We need #line b/c the newline for the pragma
4731 could fuck things up. */
4732 output_line_command (pfile
, 0, same_file
);
4733 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4741 /* If this is expanding a macro definition, don't recognize
4742 preprocessor directives. */
4745 /* If this is expand_into_temp_buffer, recognize them
4746 only after an actual newline at this level,
4747 not at the beginning of the input level. */
4748 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4754 if (!pfile
->only_seen_white
)
4756 if (handle_directive (pfile
))
4757 return CPP_DIRECTIVE
;
4758 pfile
->only_seen_white
= 0;
4763 /* A single quoted string is treated like a double -- some
4764 programs (e.g., troff) are perverse this way */
4765 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4766 &start_line
, &start_column
);
4767 old_written
= CPP_WRITTEN (pfile
);
4769 CPP_PUTC (pfile
, c
);
4775 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4777 /* try harder: this string crosses a macro expansion
4778 boundary. This can happen naturally if -traditional.
4779 Otherwise, only -D can make a macro with an unmatched
4781 cpp_buffer
*next_buf
4782 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4783 (*CPP_BUFFER (pfile
)->cleanup
)
4784 (CPP_BUFFER (pfile
), pfile
);
4785 CPP_BUFFER (pfile
) = next_buf
;
4788 if (!CPP_TRADITIONAL (pfile
))
4790 cpp_error_with_line (pfile
, start_line
, start_column
,
4791 "unterminated string or character constant");
4792 if (pfile
->multiline_string_line
!= start_line
4793 && pfile
->multiline_string_line
!= 0)
4794 cpp_error_with_line (pfile
,
4795 pfile
->multiline_string_line
, -1,
4796 "possible real start of unterminated constant");
4797 pfile
->multiline_string_line
= 0;
4801 CPP_PUTC (pfile
, cc
);
4805 /* Traditionally, end of line ends a string constant with
4806 no error. So exit the loop and record the new line. */
4807 if (CPP_TRADITIONAL (pfile
))
4811 cpp_error_with_line (pfile
, start_line
, start_column
,
4812 "unterminated character constant");
4815 if (CPP_PEDANTIC (pfile
)
4816 && pfile
->multiline_string_line
== 0)
4818 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4819 "string constant runs past end of line");
4821 if (pfile
->multiline_string_line
== 0)
4822 pfile
->multiline_string_line
= start_line
;
4829 /* Backslash newline is replaced by nothing at all. */
4830 CPP_ADJUST_WRITTEN (pfile
, -1);
4835 /* ANSI stupidly requires that in \\ the second \
4836 is *not* prevented from combining with a newline. */
4839 CPP_PUTC (pfile
, cc
);
4851 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4852 CPP_PWRITTEN (pfile
));
4853 pfile
->only_seen_white
= 0;
4854 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4857 if (!opts
->dollars_in_ident
)
4862 if (opts
->cplusplus
&& PEEKC () == ':')
4871 if (c2
== c
|| c2
== '=')
4881 if (PEEKC () == '=')
4888 if (c2
== '-' && opts
->chill
)
4890 /* Chill style comment */
4891 if (opts
->put_out_comments
)
4892 parse_set_mark (&start_mark
, pfile
);
4893 FORWARD(1); /* Skip second '-'. */
4901 /* Don't consider final '\n' to be part of comment. */
4907 goto return_comment
;
4909 if (c2
== '-' || c2
== '=' || c2
== '>')
4914 if (pfile
->parsing_include_directive
)
4918 CPP_PUTC (pfile
, c
);
4923 if (c
== '\n' || c
== EOF
)
4926 "missing '>' in `#include <FILENAME>'");
4932 /* else fall through */
4941 CPP_RESERVE (pfile
, 4);
4942 CPP_PUTC (pfile
, c
);
4943 CPP_PUTC (pfile
, c2
);
4947 CPP_PUTC_Q (pfile
, GETC ());
4948 CPP_NUL_TERMINATE_Q (pfile
);
4949 pfile
->only_seen_white
= 0;
4953 if (CPP_BUFFER (pfile
)->has_escapes
)
4958 if (pfile
->output_escapes
)
4959 CPP_PUTS (pfile
, "@-", 2);
4960 parse_name (pfile
, GETC ());
4963 else if (is_space
[c
])
4965 CPP_RESERVE (pfile
, 2);
4966 if (pfile
->output_escapes
)
4967 CPP_PUTC_Q (pfile
, '@');
4968 CPP_PUTC_Q (pfile
, c
);
4972 if (pfile
->output_escapes
)
4974 CPP_PUTS (pfile
, "@@", 2);
4984 CPP_RESERVE(pfile
, 2);
4985 CPP_PUTC_Q (pfile
, '.');
4989 /* FIXME - misses the case "..\\\n." */
4990 if (c2
== '.' && PEEKN(1) == '.')
4992 CPP_RESERVE(pfile
, 4);
4993 CPP_PUTC_Q (pfile
, '.');
4994 CPP_PUTC_Q (pfile
, '.');
4995 CPP_PUTC_Q (pfile
, '.');
4997 CPP_NUL_TERMINATE_Q (pfile
);
4998 pfile
->only_seen_white
= 0;
5005 pfile
->only_seen_white
= 0;
5007 CPP_RESERVE(pfile
, 3);
5008 CPP_PUTC_Q (pfile
, c
);
5009 CPP_PUTC_Q (pfile
, GETC ());
5010 CPP_NUL_TERMINATE_Q (pfile
);
5016 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
5018 CPP_PUTC (pfile
, c
);
5024 case '0': case '1': case '2': case '3': case '4':
5025 case '5': case '6': case '7': case '8': case '9':
5030 CPP_RESERVE (pfile
, 2);
5031 CPP_PUTC_Q (pfile
, c
);
5036 if (!is_idchar
[c
] && c
!= '.'
5037 && ((c2
!= 'e' && c2
!= 'E'
5038 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
5039 || (c
!= '+' && c
!= '-')))
5044 CPP_NUL_TERMINATE_Q (pfile
);
5045 pfile
->only_seen_white
= 0;
5047 case 'b': case 'c': case 'd': case 'h': case 'o':
5048 case 'B': case 'C': case 'D': case 'H': case 'O':
5049 if (opts
->chill
&& PEEKC () == '\'')
5051 pfile
->only_seen_white
= 0;
5052 CPP_RESERVE (pfile
, 2);
5053 CPP_PUTC_Q (pfile
, c
);
5054 CPP_PUTC_Q (pfile
, '\'');
5060 goto chill_number_eof
;
5063 if (c
== '\\' && PEEKC() == '\n')
5070 CPP_PUTC (pfile
, c
);
5074 CPP_RESERVE (pfile
, 2);
5075 CPP_PUTC_Q (pfile
, c
);
5076 CPP_NUL_TERMINATE_Q (pfile
);
5083 CPP_NUL_TERMINATE (pfile
);
5090 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5091 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5092 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5093 case 'x': case 'y': case 'z':
5094 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5095 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5096 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5101 unsigned char *ident
;
5102 int before_name_written
= CPP_WRITTEN (pfile
);
5104 parse_name (pfile
, c
);
5105 pfile
->only_seen_white
= 0;
5106 if (pfile
->no_macro_expand
)
5108 ident
= pfile
->token_buffer
+ before_name_written
;
5109 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5110 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5113 if (hp
->type
== T_DISABLED
)
5115 if (pfile
->output_escapes
)
5116 { /* Return "@-IDENT", followed by '\0'. */
5118 CPP_RESERVE (pfile
, 3);
5119 ident
= pfile
->token_buffer
+ before_name_written
;
5120 CPP_ADJUST_WRITTEN (pfile
, 2);
5121 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5128 /* If macro wants an arglist, verify that a '(' follows.
5129 first skip all whitespace, copying it to the output
5130 after the macro name. Then, if there is no '(',
5131 decide this is not a macro call and leave things that way. */
5132 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5134 struct parse_marker macro_mark
;
5136 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5138 cpp_buffer
*next_buf
;
5139 cpp_skip_hspace (pfile
);
5140 if (PEEKC () != EOF
)
5142 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5143 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5144 CPP_BUFFER (pfile
) = next_buf
;
5146 parse_set_mark (¯o_mark
, pfile
);
5149 cpp_skip_hspace (pfile
);
5151 is_macro_call
= c
== '(';
5157 parse_goto_mark (¯o_mark
, pfile
);
5158 parse_clear_mark (¯o_mark
);
5162 /* This is now known to be a macro call. */
5164 /* it might not actually be a macro. */
5165 if (hp
->type
!= T_MACRO
) {
5166 int xbuf_len
; U_CHAR
*xbuf
;
5167 CPP_SET_WRITTEN (pfile
, before_name_written
);
5168 special_symbol (hp
, pfile
);
5169 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5170 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5171 CPP_SET_WRITTEN (pfile
, before_name_written
);
5172 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5173 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5177 /* Expand the macro, reading arguments as needed,
5178 and push the expansion on the input stack. */
5179 macroexpand (pfile
, hp
);
5180 CPP_SET_WRITTEN (pfile
, before_name_written
);
5183 /* An extra "@ " is added to the end of a macro expansion
5184 to prevent accidental token pasting. We prefer to avoid
5185 unneeded extra spaces (for the sake of cpp-using tools like
5186 imake). Here we remove the space if it is safe to do so. */
5187 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5188 && pfile
->buffer
->rlimit
[-2] == '@'
5189 && pfile
->buffer
->rlimit
[-1] == ' ')
5191 int c1
= pfile
->buffer
->rlimit
[-3];
5192 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5193 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5194 pfile
->buffer
->rlimit
-= 2;
5199 case ' ': case '\t': case '\v': case '\r':
5202 CPP_PUTC (pfile
, c
);
5204 if (c
== EOF
|| !is_hor_space
[c
])
5218 CPP_PUTC (pfile
, c
);
5219 if (pfile
->only_seen_white
== 0)
5220 pfile
->only_seen_white
= 1;
5222 output_line_command (pfile
, 1, same_file
);
5225 case '(': token
= CPP_LPAREN
; goto char1
;
5226 case ')': token
= CPP_RPAREN
; goto char1
;
5227 case '{': token
= CPP_LBRACE
; goto char1
;
5228 case '}': token
= CPP_RBRACE
; goto char1
;
5229 case ',': token
= CPP_COMMA
; goto char1
;
5230 case ';': token
= CPP_SEMICOLON
; goto char1
;
5236 pfile
->only_seen_white
= 0;
5237 CPP_PUTC (pfile
, c
);
5243 /* Like cpp_get_token, but skip spaces and comments. */
5246 cpp_get_non_space_token (pfile
)
5249 int old_written
= CPP_WRITTEN (pfile
);
5252 enum cpp_token token
= cpp_get_token (pfile
);
5253 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5254 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5256 CPP_SET_WRITTEN (pfile
, old_written
);
5260 /* Parse an identifier starting with C. */
5263 parse_name (pfile
, c
)
5264 cpp_reader
*pfile
; int c
;
5270 if (c
== '\\' && PEEKC() == '\n')
5279 if (c
== '$' && CPP_PEDANTIC (pfile
))
5280 cpp_pedwarn ("`$' in identifier");
5282 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5283 CPP_PUTC_Q (pfile
, c
);
5288 CPP_NUL_TERMINATE_Q (pfile
);
5293 /* Maintain and search list of included files, for #import. */
5295 /* Hash a file name for import_hash_table. */
5303 while (*f
) val
+= *f
++;
5304 return (val
%IMPORT_HASH_SIZE
);
5307 /* Search for file FILENAME in import_hash_table.
5308 Return -2 if found, either a matching name or a matching inode.
5309 Otherwise, open the file and return a file descriptor if successful
5310 or -1 if unsuccessful. */
5313 lookup_import (pfile
, filename
, searchptr
)
5316 struct file_name_list
*searchptr
;
5318 struct import_file
*i
;
5324 hashval
= import_hash (filename
);
5326 /* Attempt to find file in list of already included files */
5327 i
= pfile
->import_hash_table
[hashval
];
5330 if (!strcmp (filename
, i
->name
))
5331 return -2; /* return found */
5334 /* Open it and try a match on inode/dev */
5335 fd
= open_include_file (pfile
, filename
, searchptr
);
5339 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5340 i
= pfile
->import_hash_table
[h
];
5342 /* Compare the inode and the device.
5343 Supposedly on some systems the inode is not a scalar. */
5344 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5345 && i
->dev
== sb
.st_dev
) {
5347 return -2; /* return found */
5352 return fd
; /* Not found, return open file */
5355 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5358 add_import (pfile
, fd
, fname
)
5363 struct import_file
*i
;
5367 hashval
= import_hash (fname
);
5369 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5370 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5371 strcpy (i
->name
, fname
);
5372 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5374 i
->next
= pfile
->import_hash_table
[hashval
];
5375 pfile
->import_hash_table
[hashval
] = i
;
5378 /* The file_name_map structure holds a mapping of file names for a
5379 particular directory. This mapping is read from the file named
5380 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5381 map filenames on a file system with severe filename restrictions,
5382 such as DOS. The format of the file name map file is just a series
5383 of lines with two tokens on each line. The first token is the name
5384 to map, and the second token is the actual name to use. */
5386 struct file_name_map
5388 struct file_name_map
*map_next
;
5393 #define FILE_NAME_MAP_FILE "header.gcc"
5395 /* Read a space delimited string of unlimited length from a stdio
5399 read_filename_string (ch
, f
)
5407 set
= alloc
= xmalloc (len
+ 1);
5411 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5413 if (set
- alloc
== len
)
5416 alloc
= xrealloc (alloc
, len
+ 1);
5417 set
= alloc
+ len
/ 2;
5427 /* This structure holds a linked list of file name maps, one per directory. */
5429 struct file_name_map_list
5431 struct file_name_map_list
*map_list_next
;
5432 char *map_list_name
;
5433 struct file_name_map
*map_list_map
;
5436 /* Read the file name map file for DIRNAME. */
5438 static struct file_name_map
*
5439 read_name_map (pfile
, dirname
)
5443 register struct file_name_map_list
*map_list_ptr
;
5447 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5448 map_list_ptr
= map_list_ptr
->map_list_next
)
5449 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5450 return map_list_ptr
->map_list_map
;
5452 map_list_ptr
= ((struct file_name_map_list
*)
5453 xmalloc (sizeof (struct file_name_map_list
)));
5454 map_list_ptr
->map_list_name
= savestring (dirname
);
5455 map_list_ptr
->map_list_map
= NULL
;
5457 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5458 strcpy (name
, dirname
);
5461 strcat (name
, FILE_NAME_MAP_FILE
);
5462 f
= fopen (name
, "r");
5464 map_list_ptr
->map_list_map
= NULL
;
5468 int dirlen
= strlen (dirname
);
5470 while ((ch
= getc (f
)) != EOF
)
5473 struct file_name_map
*ptr
;
5477 from
= read_filename_string (ch
, f
);
5478 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5480 to
= read_filename_string (ch
, f
);
5482 ptr
= ((struct file_name_map
*)
5483 xmalloc (sizeof (struct file_name_map
)));
5484 ptr
->map_from
= from
;
5486 /* Make the real filename absolute. */
5491 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5492 strcpy (ptr
->map_to
, dirname
);
5493 ptr
->map_to
[dirlen
] = '/';
5494 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5498 ptr
->map_next
= map_list_ptr
->map_list_map
;
5499 map_list_ptr
->map_list_map
= ptr
;
5501 while ((ch
= getc (f
)) != '\n')
5508 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5509 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5511 return map_list_ptr
->map_list_map
;
5514 /* Try to open include file FILENAME. SEARCHPTR is the directory
5515 being tried from the include file search path. This function maps
5516 filenames on file systems based on information read by
5520 open_include_file (pfile
, filename
, searchptr
)
5523 struct file_name_list
*searchptr
;
5525 register struct file_name_map
*map
;
5526 register char *from
;
5529 if (searchptr
&& ! searchptr
->got_name_map
)
5531 searchptr
->name_map
= read_name_map (pfile
,
5533 ? searchptr
->fname
: ".");
5534 searchptr
->got_name_map
= 1;
5537 /* First check the mapping for the directory we are using. */
5538 if (searchptr
&& searchptr
->name_map
)
5541 if (searchptr
->fname
)
5542 from
+= strlen (searchptr
->fname
) + 1;
5543 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5545 if (! strcmp (map
->map_from
, from
))
5547 /* Found a match. */
5548 return open (map
->map_to
, O_RDONLY
, 0666);
5553 /* Try to find a mapping file for the particular directory we are
5554 looking in. Thus #include <sys/types.h> will look up sys/types.h
5555 in /usr/include/header.gcc and look up types.h in
5556 /usr/include/sys/header.gcc. */
5557 p
= rindex (filename
, '/');
5562 && strlen (searchptr
->fname
) == p
- filename
5563 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5565 /* FILENAME is in SEARCHPTR, which we've already checked. */
5566 return open (filename
, O_RDONLY
, 0666);
5576 dir
= (char *) alloca (p
- filename
+ 1);
5577 bcopy (filename
, dir
, p
- filename
);
5578 dir
[p
- filename
] = '\0';
5581 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5582 if (! strcmp (map
->map_from
, from
))
5583 return open (map
->map_to
, O_RDONLY
, 0666);
5585 return open (filename
, O_RDONLY
, 0666);
5588 /* Process the contents of include file FNAME, already open on descriptor F,
5590 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5591 "system" include directories (as decided by the `is_system_include'
5593 DIRPTR is the link in the dir path through which this file was found,
5594 or 0 if the file name was absolute or via the current directory.
5595 Return 1 on success, 0 on failure.
5597 The caller is responsible for the cpp_push_buffer. */
5600 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5604 int system_header_p
;
5605 struct file_name_list
*dirptr
;
5611 cpp_buffer
*fp
; /* For input stack frame */
5612 int missing_newline
= 0;
5614 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
5616 cpp_perror_with_name (pfile
, fname
);
5618 cpp_pop_buffer (pfile
);
5622 fp
= CPP_BUFFER (pfile
);
5623 fp
->nominal_fname
= fp
->fname
= fname
;
5628 fp
->system_header_p
= system_header_p
;
5631 fp
->cleanup
= file_cleanup
;
5633 if (S_ISREG (st_mode
)) {
5634 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5635 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5638 /* Read the file contents, knowing that st_size is an upper bound
5639 on the number of bytes we can read. */
5640 length
= safe_read (f
, fp
->buf
, st_size
);
5641 fp
->rlimit
= fp
->buf
+ length
;
5642 if (length
< 0) goto nope
;
5644 else if (S_ISDIR (st_mode
)) {
5645 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5649 /* Cannot count its file size before reading.
5650 First read the entire file into heap and
5651 copy them into buffer on stack. */
5656 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5659 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5661 goto nope
; /* error! */
5663 if (st_size
!= bsize
)
5664 break; /* End of file */
5666 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5672 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5673 /* Backslash-newline at end is not good enough. */
5674 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5675 fp
->buf
[length
++] = '\n';
5677 missing_newline
= 1;
5680 fp
->buf
[length
] = '\0';
5681 fp
->rlimit
= fp
->buf
+ length
;
5683 /* Close descriptor now, so nesting does not use lots of descriptors. */
5686 /* Must do this before calling trigraph_pcp, so that the correct file name
5687 will be printed in warning messages. */
5689 pfile
->input_stack_listing_current
= 0;
5699 if (missing_newline
)
5702 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5703 pedwarn ("file does not end in newline");
5706 input_file_stack_tick
++;
5713 cpp_perror_with_name (pfile
, fname
);
5719 /* This is called after options have been processed.
5720 * Check options for consistency, and setup for processing input
5721 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5722 * Return 1 on succes, 0 on failure.
5726 cpp_start_read (pfile
, fname
)
5730 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5731 struct cpp_pending
*pend
;
5736 /* The code looks at the defaults through this pointer, rather than through
5737 the constant structure above. This pointer gets changed if an environment
5738 variable specifies other defaults. */
5739 struct default_include
*include_defaults
= include_defaults_array
;
5741 /* Add dirs from CPATH after dirs from -I. */
5742 /* There seems to be confusion about what CPATH should do,
5743 so for the moment it is not documented. */
5744 /* Some people say that CPATH should replace the standard include dirs,
5745 but that seems pointless: it comes before them, so it overrides them
5747 p
= (char *) getenv ("CPATH");
5748 if (p
!= 0 && ! opts
->no_standard_includes
)
5749 path_include (pfile
, p
);
5751 /* Now that dollars_in_ident is known, initialize is_idchar. */
5752 initialize_char_syntax (opts
);
5754 /* Do partial setup of input buffer for the sake of generating
5755 early #line directives (when -g is in effect). */
5756 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5759 if (opts
->in_fname
== NULL
)
5760 opts
->in_fname
= "";
5761 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5764 /* Install __LINE__, etc. Must follow initialize_char_syntax
5765 and option processing. */
5766 initialize_builtins (pfile
);
5768 /* Do standard #defines and assertions
5769 that identify system and machine type. */
5771 if (!opts
->inhibit_predefs
) {
5772 char *p
= (char *) alloca (strlen (predefs
) + 1);
5773 strcpy (p
, predefs
);
5776 while (*p
== ' ' || *p
== '\t')
5778 /* Handle -D options. */
5779 if (p
[0] == '-' && p
[1] == 'D') {
5781 while (*p
&& *p
!= ' ' && *p
!= '\t')
5785 if (opts
->debug_output
)
5786 output_line_command (pfile
, 0, same_file
);
5787 cpp_define (pfile
, q
);
5788 while (*p
== ' ' || *p
== '\t')
5790 } else if (p
[0] == '-' && p
[1] == 'A') {
5791 /* Handle -A options (assertions). */
5800 past_name
= assertion
;
5801 /* Locate end of name. */
5802 while (*past_name
&& *past_name
!= ' '
5803 && *past_name
!= '\t' && *past_name
!= '(')
5805 /* Locate `(' at start of value. */
5807 while (*value
&& (*value
== ' ' || *value
== '\t'))
5809 if (*value
++ != '(')
5811 while (*value
&& (*value
== ' ' || *value
== '\t'))
5814 /* Locate end of value. */
5815 while (*past_value
&& *past_value
!= ' '
5816 && *past_value
!= '\t' && *past_value
!= ')')
5818 termination
= past_value
;
5819 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5821 if (*termination
++ != ')')
5823 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5825 /* Temporarily null-terminate the value. */
5826 save_char
= *termination
;
5827 *termination
= '\0';
5828 /* Install the assertion. */
5829 make_assertion (pfile
, "-A", assertion
);
5830 *termination
= (char) save_char
;
5832 while (*p
== ' ' || *p
== '\t')
5840 /* Now handle the command line options. */
5842 /* Do -U's, -D's and -A's in the order they were seen. */
5843 /* First reverse the list. */
5844 opts
->pending
= nreverse_pending (opts
->pending
);
5846 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5848 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5850 switch (pend
->cmd
[1])
5853 if (opts
->debug_output
)
5854 output_line_command (pfile
, 0, same_file
);
5855 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5858 if (opts
->debug_output
)
5859 output_line_command (pfile
, 0, same_file
);
5860 cpp_define (pfile
, pend
->arg
);
5863 make_assertion (pfile
, "-A", pend
->arg
);
5869 opts
->done_initializing
= 1;
5871 { /* Read the appropriate environment variable and if it exists
5872 replace include_defaults with the listed path. */
5874 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5877 epath
= getenv ("C_INCLUDE_PATH");
5880 epath
= getenv ("CPLUS_INCLUDE_PATH");
5883 epath
= getenv ("OBJC_INCLUDE_PATH");
5886 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
5889 /* If the environment var for this language is set,
5890 add to the default list of include directories. */
5892 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5894 char *startp
, *endp
;
5896 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5897 if (*startp
== PATH_SEPARATOR
)
5900 = (struct default_include
*) xmalloc ((num_dirs
5901 * sizeof (struct default_include
))
5902 + sizeof (include_defaults_array
));
5903 startp
= endp
= epath
;
5906 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5907 if ((*endp
== PATH_SEPARATOR
)
5909 strncpy (nstore
, startp
, endp
-startp
);
5911 strcpy (nstore
, ".");
5913 nstore
[endp
-startp
] = '\0';
5915 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5916 include_defaults
[num_dirs
].component
= 0;
5917 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5918 include_defaults
[num_dirs
].cxx_aware
= 1;
5922 endp
= startp
= endp
+ 1;
5926 /* Put the usual defaults back in at the end. */
5927 bcopy ((char *) include_defaults_array
,
5928 (char *) &include_defaults
[num_dirs
],
5929 sizeof (include_defaults_array
));
5933 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5934 opts
->first_system_include
= opts
->before_system
;
5936 /* Unless -fnostdinc,
5937 tack on the standard include file dirs to the specified list */
5938 if (!opts
->no_standard_includes
) {
5939 struct default_include
*p
= include_defaults
;
5940 char *specd_prefix
= opts
->include_prefix
;
5941 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5942 int default_len
= 0;
5943 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5944 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5945 default_len
= strlen (default_prefix
) - 7;
5946 default_prefix
[default_len
] = 0;
5948 /* Search "translated" versions of GNU directories.
5949 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5950 if (specd_prefix
!= 0 && default_len
!= 0)
5951 for (p
= include_defaults
; p
->fname
; p
++) {
5952 /* Some standard dirs are only for C++. */
5954 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5955 /* Does this dir start with the prefix? */
5956 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5957 /* Yes; change prefix and add to search list. */
5958 struct file_name_list
*new
5959 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5960 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5961 char *str
= (char *) xmalloc (this_len
+ 1);
5962 strcpy (str
, specd_prefix
);
5963 strcat (str
, p
->fname
+ default_len
);
5965 new->control_macro
= 0;
5966 new->c_system_include_path
= !p
->cxx_aware
;
5967 new->got_name_map
= 0;
5968 append_include_chain (pfile
, new, new);
5969 if (opts
->first_system_include
== 0)
5970 opts
->first_system_include
= new;
5974 /* Search ordinary names for GNU include directories. */
5975 for (p
= include_defaults
; p
->fname
; p
++) {
5976 /* Some standard dirs are only for C++. */
5978 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5979 struct file_name_list
*new
5980 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5981 new->control_macro
= 0;
5982 new->c_system_include_path
= !p
->cxx_aware
;
5983 new->fname
= update_path (p
->fname
, p
->component
);
5984 new->got_name_map
= 0;
5985 append_include_chain (pfile
, new, new);
5986 if (opts
->first_system_include
== 0)
5987 opts
->first_system_include
= new;
5992 /* Tack the after_include chain at the end of the include chain. */
5993 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5994 if (opts
->first_system_include
== 0)
5995 opts
->first_system_include
= opts
->after_include
;
5997 /* With -v, print the list of dirs to search. */
5998 if (opts
->verbose
) {
5999 struct file_name_list
*p
;
6000 fprintf (stderr
, "#include \"...\" search starts here:\n");
6001 for (p
= opts
->include
; p
; p
= p
->next
) {
6002 if (p
== opts
->first_bracket_include
)
6003 fprintf (stderr
, "#include <...> search starts here:\n");
6004 fprintf (stderr
, " %s\n", p
->fname
);
6006 fprintf (stderr
, "End of search list.\n");
6009 /* Scan the -imacros files before the main input.
6010 Much like #including them, but with no_output set
6011 so that only their macro definitions matter. */
6013 opts
->no_output
++; pfile
->no_record_file
++;
6014 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6016 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
6018 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6021 cpp_perror_with_name (pfile
, pend
->arg
);
6024 if (!cpp_push_buffer (pfile
, NULL
, 0))
6026 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6027 cpp_scan_buffer (pfile
);
6030 opts
->no_output
--; pfile
->no_record_file
--;
6032 /* Copy the entire contents of the main input file into
6033 the stacked input buffer previously allocated for it. */
6034 if (fname
== NULL
|| *fname
== 0) {
6037 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
6038 cpp_pfatal_with_name (pfile
, fname
);
6040 /* -MG doesn't select the form of output and must be specified with one of
6041 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6042 inhibit compilation. */
6043 if (opts
->print_deps_missing_files
6044 && (opts
->print_deps
== 0 || !opts
->no_output
))
6046 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
6050 /* Either of two environment variables can specify output of deps.
6051 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6052 where OUTPUT_FILE is the file to write deps info to
6053 and DEPS_TARGET is the target to mention in the deps. */
6055 if (opts
->print_deps
== 0
6056 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6057 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6058 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6064 spec
= getenv ("SUNPRO_DEPENDENCIES");
6065 opts
->print_deps
= 2;
6068 opts
->print_deps
= 1;
6071 /* Find the space before the DEPS_TARGET, if there is one. */
6072 /* This should use index. (mrs) */
6073 while (*s
!= 0 && *s
!= ' ') s
++;
6076 opts
->deps_target
= s
+ 1;
6077 output_file
= (char *) xmalloc (s
- spec
+ 1);
6078 bcopy (spec
, output_file
, s
- spec
);
6079 output_file
[s
- spec
] = 0;
6083 opts
->deps_target
= 0;
6087 opts
->deps_file
= output_file
;
6088 opts
->print_deps_append
= 1;
6091 /* For -M, print the expected object file name
6092 as the target of this Make-rule. */
6093 if (opts
->print_deps
)
6095 pfile
->deps_allocated_size
= 200;
6096 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6097 pfile
->deps_buffer
[0] = 0;
6098 pfile
->deps_size
= 0;
6099 pfile
->deps_column
= 0;
6101 if (opts
->deps_target
)
6102 deps_output (pfile
, opts
->deps_target
, ':');
6103 else if (*opts
->in_fname
== 0)
6104 deps_output (pfile
, "-", ':');
6109 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
6110 ".cc", ".cxx", ".cpp", ".cp",
6114 /* Discard all directory prefixes from filename. */
6115 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6116 #ifdef DIR_SEPARATOR
6117 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6124 /* Copy remainder to mungable area. */
6125 p
= (char *) alloca (strlen(q
) + 8);
6128 /* Output P, but remove known suffixes. */
6131 /* Point to the filename suffix. */
6132 r
= rindex (p
, '.');
6133 /* Compare against the known suffixes. */
6135 while (known_suffixes
[x
] != 0)
6137 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
6139 /* Make q point to the bit we're going to overwrite
6140 with an object suffix. */
6147 /* Supply our own suffix. */
6154 deps_output (pfile
, p
, ':');
6155 deps_output (pfile
, opts
->in_fname
, ' ');
6160 /* Make sure data ends with a newline. And put a null after it. */
6162 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6163 /* Backslash-newline at end is not good enough. */
6164 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6165 fp
->buf
[fp
->length
++] = '\n';
6166 missing_newline
= 1;
6168 fp
->buf
[fp
->length
] = '\0';
6170 /* Unless inhibited, convert trigraphs in the input. */
6176 /* Scan the -include files before the main input.
6177 We push these in reverse order, so that the first one is handled first. */
6179 pfile
->no_record_file
++;
6180 opts
->pending
= nreverse_pending (opts
->pending
);
6181 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6183 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6185 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6188 cpp_perror_with_name (pfile
, pend
->arg
);
6191 if (!cpp_push_buffer (pfile
, NULL
, 0))
6193 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6196 pfile
->no_record_file
--;
6198 /* Free the pending list. */
6199 for (pend
= opts
->pending
; pend
; )
6201 struct cpp_pending
*next
= pend
->next
;
6205 opts
->pending
= NULL
;
6208 /* Scan the input, processing macros and directives. */
6210 rescan (&outbuf
, 0);
6212 if (missing_newline
)
6215 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6216 pedwarn ("file does not end in newline");
6219 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6220 output_line_command (pfile
, 0, same_file
);
6225 cpp_reader_init (pfile
)
6228 bzero ((char *) pfile
, sizeof (cpp_reader
));
6229 pfile
->get_token
= cpp_get_token
;
6231 pfile
->token_buffer_size
= 200;
6232 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6233 CPP_SET_WRITTEN (pfile
, 0);
6235 pfile
->system_include_depth
= 0;
6236 pfile
->dont_repeat_files
= 0;
6237 pfile
->all_include_files
= 0;
6238 pfile
->max_include_len
= 0;
6239 pfile
->timebuf
= NULL
;
6240 pfile
->only_seen_white
= 1;
6241 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6244 static struct cpp_pending
*
6245 nreverse_pending (list
)
6246 struct cpp_pending
*list
;
6249 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6250 for (pend
= list
; pend
; pend
= next
)
6260 push_pending (pfile
, cmd
, arg
)
6265 struct cpp_pending
*pend
6266 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6269 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6270 CPP_OPTIONS (pfile
)->pending
= pend
;
6273 /* Handle command-line options in (argc, argv).
6274 Can be called multiple times, to handle multiple sets of options.
6275 Returns if an unrecognized option is seen.
6276 Returns number of handled arguments. */
6279 cpp_handle_options (pfile
, argc
, argv
)
6285 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6286 for (i
= 0; i
< argc
; i
++) {
6287 if (argv
[i
][0] != '-') {
6288 if (opts
->out_fname
!= NULL
)
6290 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6293 else if (opts
->in_fname
!= NULL
)
6294 opts
->out_fname
= argv
[i
];
6296 opts
->in_fname
= argv
[i
];
6298 switch (argv
[i
][1]) {
6301 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6304 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6308 if (!strcmp (argv
[i
], "-include")
6309 || !strcmp (argv
[i
], "-imacros")) {
6311 goto missing_filename
;
6313 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6315 if (!strcmp (argv
[i
], "-iprefix")) {
6317 goto missing_filename
;
6319 opts
->include_prefix
= argv
[++i
];
6321 if (!strcmp (argv
[i
], "-ifoutput")) {
6322 opts
->output_conditionals
= 1;
6324 if (!strcmp (argv
[i
], "-isystem")) {
6325 struct file_name_list
*dirtmp
;
6328 goto missing_filename
;
6330 dirtmp
= (struct file_name_list
*)
6331 xmalloc (sizeof (struct file_name_list
));
6333 dirtmp
->control_macro
= 0;
6334 dirtmp
->c_system_include_path
= 1;
6335 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6336 strcpy (dirtmp
->fname
, argv
[++i
]);
6337 dirtmp
->got_name_map
= 0;
6339 if (opts
->before_system
== 0)
6340 opts
->before_system
= dirtmp
;
6342 opts
->last_before_system
->next
= dirtmp
;
6343 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6345 /* Add directory to end of path for includes,
6346 with the default prefix at the front of its name. */
6347 if (!strcmp (argv
[i
], "-iwithprefix")) {
6348 struct file_name_list
*dirtmp
;
6351 if (opts
->include_prefix
!= 0)
6352 prefix
= opts
->include_prefix
;
6354 prefix
= savestring (GCC_INCLUDE_DIR
);
6355 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6356 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6357 prefix
[strlen (prefix
) - 7] = 0;
6360 dirtmp
= (struct file_name_list
*)
6361 xmalloc (sizeof (struct file_name_list
));
6362 dirtmp
->next
= 0; /* New one goes on the end */
6363 dirtmp
->control_macro
= 0;
6364 dirtmp
->c_system_include_path
= 0;
6366 goto missing_dirname
;
6368 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6369 + strlen (prefix
) + 1);
6370 strcpy (dirtmp
->fname
, prefix
);
6371 strcat (dirtmp
->fname
, argv
[++i
]);
6372 dirtmp
->got_name_map
= 0;
6374 if (opts
->after_include
== 0)
6375 opts
->after_include
= dirtmp
;
6377 opts
->last_after_include
->next
= dirtmp
;
6378 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6380 /* Add directory to main path for includes,
6381 with the default prefix at the front of its name. */
6382 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6383 struct file_name_list
*dirtmp
;
6386 if (opts
->include_prefix
!= 0)
6387 prefix
= opts
->include_prefix
;
6389 prefix
= savestring (GCC_INCLUDE_DIR
);
6390 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6391 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6392 prefix
[strlen (prefix
) - 7] = 0;
6395 dirtmp
= (struct file_name_list
*)
6396 xmalloc (sizeof (struct file_name_list
));
6397 dirtmp
->next
= 0; /* New one goes on the end */
6398 dirtmp
->control_macro
= 0;
6399 dirtmp
->c_system_include_path
= 0;
6401 goto missing_dirname
;
6403 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6404 + strlen (prefix
) + 1);
6405 strcpy (dirtmp
->fname
, prefix
);
6406 strcat (dirtmp
->fname
, argv
[++i
]);
6407 dirtmp
->got_name_map
= 0;
6409 append_include_chain (pfile
, dirtmp
, dirtmp
);
6411 /* Add directory to end of path for includes. */
6412 if (!strcmp (argv
[i
], "-idirafter")) {
6413 struct file_name_list
*dirtmp
;
6415 dirtmp
= (struct file_name_list
*)
6416 xmalloc (sizeof (struct file_name_list
));
6417 dirtmp
->next
= 0; /* New one goes on the end */
6418 dirtmp
->control_macro
= 0;
6419 dirtmp
->c_system_include_path
= 0;
6421 goto missing_dirname
;
6423 dirtmp
->fname
= argv
[++i
];
6424 dirtmp
->got_name_map
= 0;
6426 if (opts
->after_include
== 0)
6427 opts
->after_include
= dirtmp
;
6429 opts
->last_after_include
->next
= dirtmp
;
6430 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6435 if (opts
->out_fname
!= NULL
)
6437 cpp_fatal (pfile
, "Output filename specified twice");
6441 goto missing_filename
;
6442 opts
->out_fname
= argv
[++i
];
6443 if (!strcmp (opts
->out_fname
, "-"))
6444 opts
->out_fname
= "";
6448 if (!strcmp (argv
[i
], "-pedantic"))
6449 CPP_PEDANTIC (pfile
) = 1;
6450 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6451 CPP_PEDANTIC (pfile
) = 1;
6452 opts
->pedantic_errors
= 1;
6455 else if (!strcmp (argv
[i
], "-pcp")) {
6456 char *pcp_fname
= argv
[++i
];
6457 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6458 ? fopen (pcp_fname
, "w")
6459 : fdopen (dup (fileno (stdout
)), "w"));
6460 if (pcp_outfile
== 0)
6461 cpp_pfatal_with_name (pfile
, pcp_fname
);
6468 if (!strcmp (argv
[i
], "-traditional")) {
6469 opts
->traditional
= 1;
6470 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6472 opts
->no_trigraphs
= 0;
6477 if (! strcmp (argv
[i
], "-lang-c"))
6478 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6480 if (! strcmp (argv
[i
], "-lang-c89"))
6481 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
6483 if (! strcmp (argv
[i
], "-lang-c++"))
6484 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6486 if (! strcmp (argv
[i
], "-lang-objc"))
6487 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6489 if (! strcmp (argv
[i
], "-lang-objc++"))
6490 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6492 if (! strcmp (argv
[i
], "-lang-asm"))
6494 if (! strcmp (argv
[i
], "-lint"))
6496 if (! strcmp (argv
[i
], "-lang-chill"))
6497 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6498 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6502 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6506 opts
->inhibit_warnings
= 1;
6510 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6511 opts
->warn_trigraphs
= 1;
6512 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6513 opts
->warn_trigraphs
= 0;
6514 else if (!strcmp (argv
[i
], "-Wcomment"))
6515 opts
->warn_comments
= 1;
6516 else if (!strcmp (argv
[i
], "-Wno-comment"))
6517 opts
->warn_comments
= 0;
6518 else if (!strcmp (argv
[i
], "-Wcomments"))
6519 opts
->warn_comments
= 1;
6520 else if (!strcmp (argv
[i
], "-Wno-comments"))
6521 opts
->warn_comments
= 0;
6522 else if (!strcmp (argv
[i
], "-Wtraditional"))
6523 opts
->warn_stringify
= 1;
6524 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6525 opts
->warn_stringify
= 0;
6526 else if (!strcmp (argv
[i
], "-Wundef"))
6527 opts
->warn_undef
= 1;
6528 else if (!strcmp (argv
[i
], "-Wno-undef"))
6529 opts
->warn_undef
= 0;
6530 else if (!strcmp (argv
[i
], "-Wimport"))
6531 opts
->warn_import
= 1;
6532 else if (!strcmp (argv
[i
], "-Wno-import"))
6533 opts
->warn_import
= 0;
6534 else if (!strcmp (argv
[i
], "-Werror"))
6535 opts
->warnings_are_errors
= 1;
6536 else if (!strcmp (argv
[i
], "-Wno-error"))
6537 opts
->warnings_are_errors
= 0;
6538 else if (!strcmp (argv
[i
], "-Wall"))
6540 opts
->warn_trigraphs
= 1;
6541 opts
->warn_comments
= 1;
6546 /* The style of the choices here is a bit mixed.
6547 The chosen scheme is a hybrid of keeping all options in one string
6548 and specifying each option in a separate argument:
6549 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6550 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6551 -M[M][G][D file]. This is awkward to handle in specs, and is not
6553 /* ??? -MG must be specified in addition to one of -M or -MM.
6554 This can be relaxed in the future without breaking anything.
6555 The converse isn't true. */
6557 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6558 if (!strcmp (argv
[i
], "-MG"))
6560 opts
->print_deps_missing_files
= 1;
6563 if (!strcmp (argv
[i
], "-M"))
6564 opts
->print_deps
= 2;
6565 else if (!strcmp (argv
[i
], "-MM"))
6566 opts
->print_deps
= 1;
6567 else if (!strcmp (argv
[i
], "-MD"))
6568 opts
->print_deps
= 2;
6569 else if (!strcmp (argv
[i
], "-MMD"))
6570 opts
->print_deps
= 1;
6571 /* For -MD and -MMD options, write deps on file named by next arg. */
6572 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6575 goto missing_filename
;
6576 opts
->deps_file
= argv
[++i
];
6580 /* For -M and -MM, write deps on standard output
6581 and suppress the usual output. */
6582 opts
->no_output
= 1;
6588 char *p
= argv
[i
] + 2;
6590 while ((c
= *p
++) != 0) {
6591 /* Arg to -d specifies what parts of macros to dump */
6594 opts
->dump_macros
= dump_only
;
6595 opts
->no_output
= 1;
6598 opts
->dump_macros
= dump_names
;
6601 opts
->dump_macros
= dump_definitions
;
6604 opts
->dump_includes
= 1;
6612 if (argv
[i
][2] == '3')
6613 opts
->debug_output
= 1;
6617 fprintf (stderr
, "GNU CPP version %s", version_string
);
6618 #ifdef TARGET_VERSION
6621 fprintf (stderr
, "\n");
6626 opts
->print_include_names
= 1;
6630 if (argv
[i
][2] != 0)
6631 push_pending (pfile
, "-D", argv
[i
] + 2);
6632 else if (i
+ 1 == argc
)
6634 cpp_fatal (pfile
, "Macro name missing after -D option");
6638 i
++, push_pending (pfile
, "-D", argv
[i
]);
6645 if (argv
[i
][2] != 0)
6647 else if (i
+ 1 == argc
)
6649 cpp_fatal (pfile
, "Assertion missing after -A option");
6655 if (!strcmp (p
, "-")) {
6656 struct cpp_pending
**ptr
;
6657 /* -A- eliminates all predefined macros and assertions.
6658 Let's include also any that were specified earlier
6659 on the command line. That way we can get rid of any
6660 that were passed automatically in from GCC. */
6662 opts
->inhibit_predefs
= 1;
6663 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6665 struct cpp_pending
*pend
= *ptr
;
6666 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6667 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6676 push_pending (pfile
, "-A", p
);
6681 case 'U': /* JF #undef something */
6682 if (argv
[i
][2] != 0)
6683 push_pending (pfile
, "-U", argv
[i
] + 2);
6684 else if (i
+ 1 == argc
)
6686 cpp_fatal (pfile
, "Macro name missing after -U option", NULL
);
6690 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6694 opts
->put_out_comments
= 1;
6697 case 'E': /* -E comes from cc -E; ignore it. */
6701 opts
->no_line_commands
= 1;
6704 case '$': /* Don't include $ in identifiers. */
6705 opts
->dollars_in_ident
= 0;
6708 case 'I': /* Add directory to path for includes. */
6710 struct file_name_list
*dirtmp
;
6712 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6713 && !strcmp (argv
[i
] + 2, "-")) {
6714 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6715 /* Don't use any preceding -I directories for #include <...>. */
6716 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6719 dirtmp
= (struct file_name_list
*)
6720 xmalloc (sizeof (struct file_name_list
));
6721 dirtmp
->next
= 0; /* New one goes on the end */
6722 dirtmp
->control_macro
= 0;
6723 dirtmp
->c_system_include_path
= 0;
6724 if (argv
[i
][2] != 0)
6725 dirtmp
->fname
= argv
[i
] + 2;
6726 else if (i
+ 1 == argc
)
6727 goto missing_dirname
;
6729 dirtmp
->fname
= argv
[++i
];
6730 dirtmp
->got_name_map
= 0;
6731 append_include_chain (pfile
, dirtmp
, dirtmp
);
6737 if (!strcmp (argv
[i
], "-nostdinc"))
6738 /* -nostdinc causes no default include directories.
6739 You must specify all include-file directories with -I. */
6740 opts
->no_standard_includes
= 1;
6741 else if (!strcmp (argv
[i
], "-nostdinc++"))
6742 /* -nostdinc++ causes no default C++-specific include directories. */
6743 opts
->no_standard_cplusplus_includes
= 1;
6745 else if (!strcmp (argv
[i
], "-noprecomp"))
6751 /* Sun compiler passes undocumented switch "-undef".
6752 Let's assume it means to inhibit the predefined symbols. */
6753 opts
->inhibit_predefs
= 1;
6756 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6757 if (opts
->in_fname
== NULL
) {
6758 opts
->in_fname
= "";
6760 } else if (opts
->out_fname
== NULL
) {
6761 opts
->out_fname
= "";
6763 } /* else fall through into error */
6777 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6779 if (opts
->print_deps
)
6781 /* Stream on which to print the dependency information. */
6784 /* Don't actually write the deps file if compilation has failed. */
6785 if (pfile
->errors
== 0)
6787 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6788 if (opts
->deps_file
== 0)
6789 deps_stream
= stdout
;
6790 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6791 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6792 fputs (pfile
->deps_buffer
, deps_stream
);
6793 putc ('\n', deps_stream
);
6794 if (opts
->deps_file
)
6796 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6797 cpp_fatal (pfile
, "I/O error on output");
6803 /* Free resources used by PFILE.
6804 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6811 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6812 cpp_pop_buffer (pfile
);
6814 if (pfile
->token_buffer
)
6816 free (pfile
->token_buffer
);
6817 pfile
->token_buffer
= NULL
;
6820 if (pfile
->deps_buffer
)
6822 free (pfile
->deps_buffer
);
6823 pfile
->deps_buffer
= NULL
;
6824 pfile
->deps_allocated_size
= 0;
6827 while (pfile
->if_stack
)
6829 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6830 pfile
->if_stack
= temp
->next
;
6834 while (pfile
->dont_repeat_files
)
6836 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6837 pfile
->dont_repeat_files
= temp
->next
;
6842 while (pfile
->all_include_files
)
6844 struct file_name_list
*temp
= pfile
->all_include_files
;
6845 pfile
->all_include_files
= temp
->next
;
6850 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6852 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6855 struct import_file
*next
= imp
->next
;
6860 pfile
->import_hash_table
[i
] = 0;
6863 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6865 while (pfile
->assertion_hashtab
[i
])
6866 delete_assertion (pfile
->assertion_hashtab
[i
]);
6869 cpp_hash_cleanup (pfile
);
6873 do_assert (pfile
, keyword
, buf
, limit
)
6875 struct directive
*keyword
;
6876 U_CHAR
*buf
, *limit
;
6878 long symstart
; /* remember where symbol name starts */
6880 int sym_length
; /* and how long it is */
6881 struct arglist
*tokens
= NULL
;
6883 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6884 && !CPP_BUFFER (pfile
)->system_header_p
)
6885 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6887 cpp_skip_hspace (pfile
);
6888 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6889 parse_name (pfile
, GETC());
6890 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6893 cpp_skip_hspace (pfile
);
6894 if (PEEKC() != '(') {
6895 cpp_error (pfile
, "missing token-sequence in `#assert'");
6901 tokens
= read_token_list (pfile
, &error_flag
);
6905 cpp_error (pfile
, "empty token-sequence in `#assert'");
6908 cpp_skip_hspace (pfile
);
6910 if (c
!= EOF
&& c
!= '\n')
6911 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6912 skip_rest_of_line (pfile
);
6915 /* If this name isn't already an assertion name, make it one.
6916 Error if it was already in use in some other way. */
6919 ASSERTION_HASHNODE
*hp
;
6920 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6921 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6922 struct tokenlist_list
*value
6923 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6925 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6927 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6928 cpp_error (pfile
, "`defined' redefined as assertion");
6929 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6932 /* Add the spec'd token-sequence to the list of such. */
6933 value
->tokens
= tokens
;
6934 value
->next
= hp
->value
;
6937 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6940 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6941 skip_rest_of_line (pfile
);
6946 do_unassert (pfile
, keyword
, buf
, limit
)
6948 struct directive
*keyword
;
6949 U_CHAR
*buf
, *limit
;
6951 long symstart
; /* remember where symbol name starts */
6952 int sym_length
; /* and how long it is */
6955 struct arglist
*tokens
= NULL
;
6956 int tokens_specified
= 0;
6958 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6959 && !CPP_BUFFER (pfile
)->system_header_p
)
6960 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6962 cpp_skip_hspace (pfile
);
6964 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6965 parse_name (pfile
, GETC());
6966 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6969 cpp_skip_hspace (pfile
);
6970 if (PEEKC() == '(') {
6973 tokens
= read_token_list (pfile
, &error_flag
);
6977 cpp_error (pfile
, "empty token list in `#unassert'");
6981 tokens_specified
= 1;
6984 cpp_skip_hspace (pfile
);
6986 if (c
!= EOF
&& c
!= '\n')
6987 cpp_error (pfile
, "junk at end of `#unassert'");
6988 skip_rest_of_line (pfile
);
6991 ASSERTION_HASHNODE
*hp
;
6992 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6993 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6994 struct tokenlist_list
*tail
, *prev
;
6996 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
7000 /* If no token list was specified, then eliminate this assertion
7002 if (! tokens_specified
)
7003 delete_assertion (hp
);
7005 /* If a list of tokens was given, then delete any matching list. */
7010 struct tokenlist_list
*next
= tail
->next
;
7011 if (compare_token_lists (tail
->tokens
, tokens
)) {
7015 hp
->value
= tail
->next
;
7016 free_token_list (tail
->tokens
);
7026 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
7029 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
7030 skip_rest_of_line (pfile
);
7034 /* Test whether there is an assertion named NAME
7035 and optionally whether it has an asserted token list TOKENS.
7036 NAME is not null terminated; its length is SYM_LENGTH.
7037 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7040 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
7044 int tokens_specified
;
7045 struct arglist
*tokens
;
7047 ASSERTION_HASHNODE
*hp
;
7048 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
7050 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
7051 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
7053 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
7055 /* It is not an assertion; just return false. */
7058 /* If no token list was specified, then value is 1. */
7059 if (! tokens_specified
)
7063 struct tokenlist_list
*tail
;
7067 /* If a list of tokens was given,
7068 then succeed if the assertion records a matching list. */
7071 if (compare_token_lists (tail
->tokens
, tokens
))
7076 /* Fail if the assertion has no matching list. */
7081 /* Compare two lists of tokens for equality including order of tokens. */
7084 compare_token_lists (l1
, l2
)
7085 struct arglist
*l1
, *l2
;
7088 if (l1
->length
!= l2
->length
)
7090 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7096 /* Succeed if both lists end at the same time. */
7101 reverse_token_list (tokens
)
7102 struct arglist
*tokens
;
7104 register struct arglist
*prev
= 0, *this, *next
;
7105 for (this = tokens
; this; this = next
)
7114 /* Read a space-separated list of tokens ending in a close parenthesis.
7115 Return a list of strings, in the order they were written.
7116 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7118 static struct arglist
*
7119 read_token_list (pfile
, error_flag
)
7123 struct arglist
*token_ptrs
= 0;
7128 FORWARD (1); /* Skip '(' */
7130 /* Loop over the assertion value tokens. */
7133 struct arglist
*temp
;
7134 long name_written
= CPP_WRITTEN (pfile
);
7135 int eofp
= 0; int c
;
7137 cpp_skip_hspace (pfile
);
7141 /* Find the end of the token. */
7144 CPP_PUTC (pfile
, c
);
7152 CPP_PUTC (pfile
, c
);
7154 else if (c
== '"' || c
== '\'')
7157 cpp_get_token (pfile
);
7163 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7164 && c
!= '"' && c
!= '\'')
7166 CPP_PUTC (pfile
, c
);
7169 if (c
!= EOF
) FORWARD(-1);
7172 length
= CPP_WRITTEN (pfile
) - name_written
;
7173 temp
= (struct arglist
*)
7174 xmalloc (sizeof (struct arglist
) + length
+ 1);
7175 temp
->name
= (U_CHAR
*) (temp
+ 1);
7176 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7177 (char *) temp
->name
, length
);
7178 temp
->name
[length
] = 0;
7179 temp
->next
= token_ptrs
;
7181 temp
->length
= length
;
7183 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7185 if (c
== EOF
|| c
== '\n')
7188 "unterminated token sequence following `#' operator");
7193 /* We accumulated the names in reverse order.
7194 Now reverse them to get the proper order. */
7195 return reverse_token_list (token_ptrs
);
7199 free_token_list (tokens
)
7200 struct arglist
*tokens
;
7203 struct arglist
*next
= tokens
->next
;
7204 free (tokens
->name
);
7210 /* Get the file-mode and data size of the file open on FD
7211 and store them in *MODE_POINTER and *SIZE_POINTER. */
7214 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
7217 long int *size_pointer
;
7221 if (fstat (fd
, &sbuf
) < 0) return (-1);
7222 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
7223 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
7227 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7228 retrying if necessary. If MAX_READ_LEN is defined, read at most
7229 that bytes at a time. Return a negative value if an error occurs,
7230 otherwise return the actual number of bytes read,
7231 which must be LEN unless end-of-file was reached. */
7234 safe_read (desc
, ptr
, len
)
7239 int left
, rcount
, nchars
;
7245 if (rcount
> MAX_READ_LEN
)
7246 rcount
= MAX_READ_LEN
;
7248 nchars
= read (desc
, ptr
, rcount
);
7266 xcalloc (number
, size
)
7267 unsigned number
, size
;
7269 register unsigned total
= number
* size
;
7270 register char *ptr
= (char *) xmalloc (total
);
7279 unsigned size
= strlen (input
);
7280 char *output
= xmalloc (size
+ 1);
7281 strcpy (output
, input
);
7285 /* Initialize PMARK to remember the current position of PFILE. */
7288 parse_set_mark (pmark
, pfile
)
7289 struct parse_marker
*pmark
;
7292 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7293 pmark
->next
= pbuf
->marks
;
7294 pbuf
->marks
= pmark
;
7296 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7299 /* Cleanup PMARK - we no longer need it. */
7302 parse_clear_mark (pmark
)
7303 struct parse_marker
*pmark
;
7305 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7306 for (; ; pp
= &(*pp
)->next
) {
7307 if (*pp
== NULL
) abort ();
7308 if (*pp
== pmark
) break;
7313 /* Backup the current position of PFILE to that saved in PMARK. */
7316 parse_goto_mark (pmark
, pfile
)
7317 struct parse_marker
*pmark
;
7320 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7321 if (pbuf
!= pmark
->buf
)
7322 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7323 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7326 /* Reset PMARK to point to the current position of PFILE. (Same
7327 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7330 parse_move_mark (pmark
, pfile
)
7331 struct parse_marker
*pmark
;
7334 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7335 if (pbuf
!= pmark
->buf
)
7336 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7337 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7341 cpp_read_check_assertion (pfile
)
7344 int name_start
= CPP_WRITTEN (pfile
);
7345 int name_length
, name_written
;
7347 FORWARD (1); /* Skip '#' */
7348 cpp_skip_hspace (pfile
);
7349 parse_name (pfile
, GETC ());
7350 name_written
= CPP_WRITTEN (pfile
);
7351 name_length
= name_written
- name_start
;
7352 cpp_skip_hspace (pfile
);
7353 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7356 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7357 result
= check_assertion (pfile
,
7358 pfile
->token_buffer
+ name_start
, name_length
,
7362 result
= check_assertion (pfile
,
7363 pfile
->token_buffer
+ name_start
, name_length
,
7365 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7370 cpp_print_file_and_line (pfile
)
7373 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7378 cpp_buf_line_and_col (ip
, &line
, &col
);
7379 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
7380 line
, pfile
->show_column
? col
: -1);
7385 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7388 char *arg1
, *arg2
, *arg3
;
7390 cpp_print_containing_files (pfile
);
7391 cpp_print_file_and_line (pfile
);
7392 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7395 /* Print error message but don't count it. */
7398 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7401 char *arg1
, *arg2
, *arg3
;
7403 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7406 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7409 cpp_print_containing_files (pfile
);
7410 cpp_print_file_and_line (pfile
);
7411 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7414 /* Print an error message and maybe count it. */
7417 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7420 char *arg1
, *arg2
, *arg3
;
7422 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7423 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7425 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7429 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7433 char *arg1
, *arg2
, *arg3
;
7436 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7438 cpp_print_containing_files (pfile
);
7441 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7443 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7447 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7451 char *arg1
, *arg2
, *arg3
;
7456 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7459 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7462 cpp_print_containing_files (pfile
);
7464 ip
= cpp_file_buffer (pfile
);
7467 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7469 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7473 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7477 char *arg1
, *arg2
, *arg3
;
7479 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7480 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7482 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7485 /* Report a warning (or an error if pedantic_errors)
7486 giving specified file name and line number, not current. */
7489 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7494 char *arg1
, *arg2
, *arg3
;
7496 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7497 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7500 cpp_file_line_for_message (pfile
, file
, line
, -1);
7501 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7502 msg
, arg1
, arg2
, arg3
);
7505 /* This defines "errno" properly for VMS, and gives us EACCES. */
7512 #ifndef HAVE_STRERROR
7513 extern int sys_nerr
;
7514 extern char *sys_errlist
[];
7515 #else /* HAVE_STRERROR */
7519 char *strerror (int,...);
7522 /* my_strerror - return the descriptive text associated with an
7526 my_strerror (errnum
)
7532 #ifndef HAVE_STRERROR
7533 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7535 result
= strerror (errnum
);
7538 /* VAXCRTL's strerror() takes an optional second argument, which only
7539 matters when the first argument is EVMSERR. However, it's simplest
7540 just to pass it unconditionally. `vaxc$errno' is declared in
7541 <errno.h>, and maintained by the library in parallel with `errno'.
7542 We assume that caller's `errnum' either matches the last setting of
7543 `errno' by the library or else does not have the value `EVMSERR'. */
7545 result
= strerror (errnum
, vaxc$errno
);
7549 result
= "undocumented I/O error";
7554 /* Error including a message from `errno'. */
7557 cpp_error_from_errno (pfile
, name
)
7562 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7564 cpp_print_containing_files (pfile
);
7567 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
7569 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (errno
));
7573 cpp_perror_with_name (pfile
, name
)
7577 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7581 * No pre-compiled header file support.
7583 * Possibly different enum token codes for each C/C++ token.
7585 * Should clean up remaining directives to that do_XXX functions
7586 * only take two arguments and all have command_reads_line.
7588 * Find and cleanup remaining uses of static variables,
7590 * Support for trigraphs.
7592 * Support -dM flag (dump_all_macros).
7594 * Support for_lint flag.