2 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
3 Written 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>
83 /* This defines "errno" properly for VMS, and gives us EACCES. */
86 extern char *index ();
87 extern char *rindex ();
95 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
96 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
98 /* Find the largest host integer type and set its size and type. */
100 #ifndef HOST_BITS_PER_WIDE_INT
102 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
103 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
104 #define HOST_WIDE_INT long
106 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
107 #define HOST_WIDE_INT int
113 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
117 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
120 /* Define a generic NULL if one hasn't already been defined. */
127 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
128 #define GENERIC_PTR void *
130 #define GENERIC_PTR char *
135 #define NULL_PTR ((GENERIC_PTR) 0)
138 #ifndef INCLUDE_LEN_FUDGE
139 #define INCLUDE_LEN_FUDGE 0
142 /* Symbols to predefine. */
144 #ifdef CPP_PREDEFINES
145 static char *predefs
= CPP_PREDEFINES
;
147 static char *predefs
= "";
150 /* We let tm.h override the types used here, to handle trivial differences
151 such as the choice of unsigned int or long unsigned int for size_t.
152 When machines start needing nontrivial differences in the size type,
153 it would be best to do something here to figure out automatically
154 from other information what type to use. */
156 /* The string value for __SIZE_TYPE__. */
159 #define SIZE_TYPE "long unsigned int"
162 /* The string value for __PTRDIFF_TYPE__. */
165 #define PTRDIFF_TYPE "long int"
168 /* The string value for __WCHAR_TYPE__. */
171 #define WCHAR_TYPE "int"
173 #define CPP_WCHAR_TYPE(PFILE) \
174 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
176 /* The string value for __USER_LABEL_PREFIX__ */
178 #ifndef USER_LABEL_PREFIX
179 #define USER_LABEL_PREFIX ""
182 /* The string value for __REGISTER_PREFIX__ */
184 #ifndef REGISTER_PREFIX
185 #define REGISTER_PREFIX ""
188 /* In the definition of a #assert name, this structure forms
189 a list of the individual values asserted.
190 Each value is itself a list of "tokens".
191 These are strings that are compared by name. */
193 struct tokenlist_list
{
194 struct tokenlist_list
*next
;
195 struct arglist
*tokens
;
198 struct assertion_hashnode
{
199 struct assertion_hashnode
*next
; /* double links for easy deletion */
200 struct assertion_hashnode
*prev
;
201 /* also, a back pointer to this node's hash
202 chain is kept, in case the node is the head
203 of the chain and gets deleted. */
204 struct assertion_hashnode
**bucket_hdr
;
205 int length
; /* length of token, for quick comparison */
206 U_CHAR
*name
; /* the actual name */
207 /* List of token-sequences. */
208 struct tokenlist_list
*value
;
211 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
212 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
214 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
215 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
216 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
217 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
218 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
219 (Note that it is false while we're expanding marco *arguments*.) */
220 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
222 /* Move all backslash-newline pairs out of embarrassing places.
223 Exchange all such pairs following BP
224 with any potentially-embarrassing characters that follow them.
225 Potentially-embarrassing characters are / and *
226 (because a backslash-newline inside a comment delimiter
227 would cause it not to be recognized). */
229 #define NEWLINE_FIX \
230 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
232 /* Same, but assume we've already read the potential '\\' into C. */
233 #define NEWLINE_FIX1(C) do { \
234 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
238 struct cpp_pending
*next
;
243 /* Forward declarations. */
245 extern char *xmalloc ();
247 static void add_import ();
248 static void append_include_chain ();
249 static void make_undef ();
250 static void make_assertion ();
251 static void path_include ();
252 static void initialize_builtins ();
253 static void initialize_char_syntax ();
254 static void dump_arg_n ();
255 static void dump_defn_1 ();
256 extern void delete_macro ();
257 static void trigraph_pcp ();
258 static int finclude ();
259 static void validate_else ();
260 static int comp_def_part ();
262 extern void fancy_abort ();
264 static void pipe_closed ();
265 static void print_containing_files ();
266 static int lookup_import ();
267 static int redundant_include_p ();
268 static is_system_include ();
269 static struct file_name_map
*read_name_map ();
270 static char *read_filename_string ();
271 static int open_include_file ();
272 static int check_preconditions ();
273 static void pcfinclude ();
274 static void pcstring_used ();
275 static int check_macro_name ();
276 static int compare_defs ();
277 static int compare_token_lists ();
278 static HOST_WIDE_INT
eval_if_expression ();
279 static int change_newlines ();
281 static int file_size_and_mode ();
282 static struct arglist
*read_token_list ();
283 static void free_token_list ();
284 static int safe_read ();
285 static void push_macro_expansion
PARAMS ((cpp_reader
*,
286 U_CHAR
*, int, HASHNODE
*));
287 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
288 extern char *xrealloc ();
289 static char *xcalloc ();
290 static char *savestring ();
292 static void conditional_skip ();
293 static void skip_if_group ();
295 /* Last arg to output_line_command. */
296 enum file_change_code
{same_file
, enter_file
, leave_file
};
298 /* External declarations. */
300 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
302 extern char *getenv ();
303 extern FILE *fdopen ();
304 extern char *version_string
;
305 extern struct tm
*localtime ();
307 /* These functions are declared to return int instead of void since they
308 are going to be placed in a table and some old compilers have trouble with
309 pointers to functions returning void. */
311 static int do_define ();
312 static int do_line ();
313 static int do_include ();
314 static int do_undef ();
315 static int do_error ();
316 static int do_pragma ();
317 static int do_ident ();
319 static int do_xifdef ();
320 static int do_else ();
321 static int do_elif ();
322 static int do_endif ();
323 static int do_sccs ();
324 static int do_once ();
325 static int do_assert ();
326 static int do_unassert ();
327 static int do_warning ();
329 struct file_name_list
331 struct file_name_list
*next
;
333 /* If the following is nonzero, it is a macro name.
334 Don't include the file again if that macro is defined. */
335 U_CHAR
*control_macro
;
336 /* If the following is nonzero, it is a C-language system include
338 int c_system_include_path
;
339 /* Mapping of file names for this directory. */
340 struct file_name_map
*name_map
;
341 /* Non-zero if name_map is valid. */
345 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
346 via the same directory as the file that #included it. */
347 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
349 /* #include "file" looks in source file dir, then stack. */
350 /* #include <file> just looks in the stack. */
351 /* -I directories are added to the end, then the defaults are added. */
353 static struct default_include
{
354 char *fname
; /* The name of the directory. */
355 int cplusplus
; /* Only look here if we're compiling C++. */
356 int cxx_aware
; /* Includes in this directory don't need to
357 be wrapped in extern "C" when compiling
359 } include_defaults_array
[]
360 #ifdef INCLUDE_DEFAULTS
364 /* Pick up GNU C++ specific include files. */
365 { GPLUSPLUS_INCLUDE_DIR
, 1, 1 },
366 { OLD_GPLUSPLUS_INCLUDE_DIR
, 1, 1 },
368 /* This is the dir for fixincludes. Put it just before
369 the files that we fix. */
370 { GCC_INCLUDE_DIR
, 0, 0 },
371 /* For cross-compilation, this dir name is generated
372 automatically in Makefile.in. */
373 { CROSS_INCLUDE_DIR
, 0, 0 },
374 #ifdef TOOL_INCLUDE_DIR
375 /* This is another place that the target system's headers might be. */
376 { TOOL_INCLUDE_DIR
, 0, 1 },
378 #else /* not CROSS_COMPILE */
379 #ifdef LOCAL_INCLUDE_DIR
380 /* This should be /usr/local/include and should come before
381 the fixincludes-fixed header files. */
382 { LOCAL_INCLUDE_DIR
, 0, 1 },
384 #ifdef TOOL_INCLUDE_DIR
385 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
386 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
387 { TOOL_INCLUDE_DIR
, 0, 1 },
389 /* This is the dir for fixincludes. Put it just before
390 the files that we fix. */
391 { GCC_INCLUDE_DIR
, 0, 0 },
392 /* Some systems have an extra dir of include files. */
393 #ifdef SYSTEM_INCLUDE_DIR
394 { SYSTEM_INCLUDE_DIR
, 0, 0 },
396 { STANDARD_INCLUDE_DIR
, 0, 0 },
397 #endif /* not CROSS_COMPILE */
400 #endif /* no INCLUDE_DEFAULTS */
402 /* `struct directive' defines one #-directive, including how to handle it. */
405 int length
; /* Length of name */
406 int (*func
)(); /* Function to handle directive */
407 char *name
; /* Name of directive */
408 enum node_type type
; /* Code which describes which directive. */
409 char command_reads_line
; /* One if rest of line is read by func. */
410 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
411 char pass_thru
; /* Copy preprocessed directive to output file.*/
414 /* Here is the actual list of #-directives, most-often-used first.
415 The initialize_builtins function assumes #define is the very first. */
417 static struct directive directive_table
[] = {
418 { 6, do_define
, "define", T_DEFINE
, 0, 1},
419 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
420 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
421 { 7, do_include
, "include", T_INCLUDE
, 1},
422 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
423 { 6, do_include
, "import", T_IMPORT
, 1},
424 { 5, do_endif
, "endif", T_ENDIF
, 1},
425 { 4, do_else
, "else", T_ELSE
, 1},
426 { 2, do_if
, "if", T_IF
, 1},
427 { 4, do_elif
, "elif", T_ELIF
, 1},
428 { 5, do_undef
, "undef", T_UNDEF
},
429 { 5, do_error
, "error", T_ERROR
},
430 { 7, do_warning
, "warning", T_WARNING
},
431 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
432 { 4, do_line
, "line", T_LINE
, 1},
433 { 5, do_ident
, "ident", T_IDENT
, 1, 0, 1},
434 #ifdef SCCS_DIRECTIVE
435 { 4, do_sccs
, "sccs", T_SCCS
},
437 { 6, do_assert
, "assert", T_ASSERT
, 1},
438 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
439 { -1, 0, "", T_UNUSED
},
442 /* table to tell if char can be part of a C identifier. */
443 U_CHAR is_idchar
[256];
444 /* table to tell if char can be first char of a c identifier. */
445 U_CHAR is_idstart
[256];
446 /* table to tell if c is horizontal space. */
447 U_CHAR is_hor_space
[256];
448 /* table to tell if c is horizontal or vertical space. */
449 static U_CHAR is_space
[256];
451 /* Initialize syntactic classifications of characters. */
454 initialize_char_syntax (opts
)
455 struct cpp_options
*opts
;
460 * Set up is_idchar and is_idstart tables. These should be
461 * faster than saying (is_alpha (c) || c == '_'), etc.
462 * Set up these things before calling any routines tthat
465 for (i
= 'a'; i
<= 'z'; i
++) {
466 is_idchar
[i
- 'a' + 'A'] = 1;
468 is_idstart
[i
- 'a' + 'A'] = 1;
471 for (i
= '0'; i
<= '9'; i
++)
475 is_idchar
['$'] = opts
->dollars_in_ident
;
476 is_idstart
['$'] = opts
->dollars_in_ident
;
478 /* horizontal space table */
479 is_hor_space
[' '] = 1;
480 is_hor_space
['\t'] = 1;
481 is_hor_space
['\v'] = 1;
482 is_hor_space
['\f'] = 1;
483 is_hor_space
['\r'] = 1;
494 /* Place into PFILE a quoted string representing the string SRC.
495 Caller must reserve enough space in pfile->token_buffer. */
498 quote_string (pfile
, src
)
504 CPP_PUTC_Q (pfile
, '\"');
506 switch ((c
= *src
++))
510 CPP_PUTC_Q (pfile
, c
);
513 sprintf (CPP_PWRITTEN (pfile
), "\\%03o", c
);
514 CPP_ADJUST_WRITTEN (pfile
, 4);
520 CPP_PUTC_Q (pfile
, '\\');
521 CPP_PUTC_Q (pfile
, c
);
525 CPP_PUTC_Q (pfile
, '\"');
526 CPP_NUL_TERMINATE_Q (pfile
);
531 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
534 cpp_grow_buffer (pfile
, n
)
538 long old_written
= CPP_WRITTEN (pfile
);
539 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
540 pfile
->token_buffer
= (U_CHAR
*)
541 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
542 CPP_SET_WRITTEN (pfile
, old_written
);
547 * process a given definition string, for initialization
548 * If STR is just an identifier, define it with value 1.
549 * If STR has anything after the identifier, then it should
550 * be identifier=definition.
554 cpp_define (pfile
, str
)
564 cpp_error (pfile
, "malformed option `-D %s'", str
);
567 while (is_idchar
[*++p
])
571 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
572 strcpy ((char *)buf
, str
);
573 strcat ((char *)buf
, " 1");
577 cpp_error (pfile
, "malformed option `-D %s'", str
);
583 /* Copy the entire option so we can modify it. */
584 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
585 strncpy (buf
, str
, p
- str
);
586 /* Change the = to a space. */
588 /* Scan for any backslash-newline and remove it. */
593 if (*p
== '\\' && p
[1] == '\n')
601 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
604 /* Process the string STR as if it appeared as the body of a #assert.
605 OPTION is the option name for which STR was the argument. */
608 make_assertion (pfile
, option
, str
)
613 struct directive
*kt
;
616 /* Copy the entire option so we can modify it. */
617 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
618 strcpy ((char *) buf
, str
);
619 /* Scan for any backslash-newline and remove it. */
623 if (*p
== '\\' && p
[1] == '\n')
632 if (!is_idstart
[*p
]) {
633 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
636 while (is_idchar
[*++p
])
638 while (*p
== ' ' || *p
== '\t') p
++;
639 if (! (*p
== 0 || *p
== '(')) {
640 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
644 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
646 do_assert (pfile
, NULL
, NULL
, NULL
);
647 cpp_pop_buffer (pfile
);
651 /* Append a chain of `struct file_name_list's
652 to the end of the main include chain.
653 FIRST is the beginning of the chain to append, and LAST is the end. */
656 append_include_chain (pfile
, first
, last
)
658 struct file_name_list
*first
, *last
;
660 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
661 struct file_name_list
*dir
;
666 if (opts
->include
== 0)
667 opts
->include
= first
;
669 opts
->last_include
->next
= first
;
671 if (opts
->first_bracket_include
== 0)
672 opts
->first_bracket_include
= first
;
674 for (dir
= first
; ; dir
= dir
->next
) {
675 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
676 if (len
> pfile
->max_include_len
)
677 pfile
->max_include_len
= len
;
683 opts
->last_include
= last
;
686 /* Add output to `deps_buffer' for the -M switch.
687 STRING points to the text to be output.
688 SPACER is ':' for targets, ' ' for dependencies, zero for text
689 to be inserted literally. */
692 deps_output (pfile
, string
, spacer
)
697 int size
= strlen (string
);
702 #ifndef MAX_OUTPUT_COLUMNS
703 #define MAX_OUTPUT_COLUMNS 72
706 && pfile
->deps_column
> 0
707 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
709 deps_output (pfile
, " \\\n ", 0);
710 pfile
->deps_column
= 0;
713 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
715 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
716 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
717 pfile
->deps_allocated_size
);
719 if (spacer
== ' ' && pfile
->deps_column
> 0)
720 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
721 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
722 pfile
->deps_size
+= size
;
723 pfile
->deps_column
+= size
;
725 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
726 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
729 /* Given a colon-separated list of file names PATH,
730 add all the names to the search path for include files. */
733 path_include (pfile
, path
)
745 struct file_name_list
*dirtmp
;
747 /* Find the end of this name. */
748 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
750 /* An empty name in the path stands for the current directory. */
751 name
= (char *) xmalloc (2);
755 /* Otherwise use the directory that is named. */
756 name
= (char *) xmalloc (q
- p
+ 1);
757 bcopy (p
, name
, q
- p
);
761 dirtmp
= (struct file_name_list
*)
762 xmalloc (sizeof (struct file_name_list
));
763 dirtmp
->next
= 0; /* New one goes on the end */
764 dirtmp
->control_macro
= 0;
765 dirtmp
->c_system_include_path
= 0;
766 dirtmp
->fname
= name
;
767 dirtmp
->got_name_map
= 0;
768 append_include_chain (pfile
, dirtmp
, dirtmp
);
770 /* Advance past this name. */
774 /* Skip the colon. */
780 cpp_options_init (opts
)
783 bzero ((char *) opts
, sizeof *opts
);
784 opts
->in_fname
= NULL
;
785 opts
->out_fname
= NULL
;
787 /* Initialize is_idchar to allow $. */
788 opts
->dollars_in_ident
= 1;
789 initialize_char_syntax (opts
);
791 opts
->no_line_commands
= 0;
792 opts
->no_trigraphs
= 1;
793 opts
->put_out_comments
= 0;
794 opts
->print_include_names
= 0;
795 opts
->dump_macros
= dump_none
;
798 opts
->cplusplus_comments
= 0;
805 opts
->pedantic_errors
= 0;
806 opts
->inhibit_warnings
= 0;
807 opts
->warn_comments
= 0;
808 opts
->warn_import
= 1;
809 opts
->warnings_are_errors
= 0;
813 null_underflow (pfile
)
820 null_cleanup (pbuf
, pfile
)
828 macro_cleanup (pbuf
, pfile
)
832 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
833 if (macro
->type
== T_DISABLED
)
834 macro
->type
= T_MACRO
;
835 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
841 file_cleanup (pbuf
, pfile
)
853 /* Assuming we have read '/'.
854 If this is the start of a comment (followed by '*' or '/'),
855 skip to the end of the comment, and return ' '.
856 Return EOF if we reached the end of file before the end of the comment.
857 If not the start of a comment, return '/'. */
860 skip_comment (pfile
, linep
)
865 while (PEEKC() == '\\' && PEEKN(1) == '\n')
880 while (c
== '\\' && PEEKC() == '\n')
884 FORWARD(1), c
= GETC();
886 if (prev_c
== '*' && c
== '/')
888 if (c
== '\n' && linep
)
892 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
899 return ' '; /* Allow // to be terminated by EOF. */
900 while (c
== '\\' && PEEKC() == '\n')
909 /* Don't consider final '\n' to be part of comment. */
919 /* Skip whitespace \-newline and comments. Does not macro-expand. */
922 cpp_skip_hspace (pfile
)
932 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
933 cpp_pedwarn (pfile
, "%s in preprocessing directive",
934 c
== '\f' ? "formfeed" : "vertical tab");
940 c
= skip_comment (pfile
, NULL
);
943 if (c
== EOF
|| c
== '/')
946 else if (c
== '\\' && PEEKN(1) == '\n') {
949 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
950 && is_hor_space
[PEEKN(1)])
956 /* Read the rest of the current line.
957 The line is appended to PFILE's output buffer. */
960 copy_rest_of_line (pfile
)
963 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
980 goto scan_directive_token
;
984 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '*'))
985 goto scan_directive_token
;
989 if (CPP_PEDANTIC (pfile
))
990 cpp_pedwarn (pfile
, "%s in preprocessing directive",
991 c
== '\f' ? "formfeed" : "vertical tab");
997 scan_directive_token
:
999 cpp_get_token (pfile
);
1002 CPP_PUTC (pfile
, c
);
1005 CPP_NUL_TERMINATE (pfile
);
1009 skip_rest_of_line (pfile
)
1012 long old
= CPP_WRITTEN (pfile
);
1013 copy_rest_of_line (pfile
);
1014 CPP_SET_WRITTEN (pfile
, old
);
1017 /* Handle a possible # directive.
1018 '#' has already been read. */
1021 handle_directive (pfile
)
1024 register struct directive
*kt
;
1027 U_CHAR
*ident
, *line_end
;
1028 long old_written
= CPP_WRITTEN (pfile
);
1030 cpp_skip_hspace (pfile
);
1033 if (c
>= '0' && c
<= '9')
1035 /* Handle # followed by a line number. */
1036 if (CPP_PEDANTIC (pfile
))
1037 cpp_pedwarn (pfile
, "`#' followed by integer");
1038 do_line (pfile
, NULL
);
1039 goto done_a_directive
;
1042 /* Now find the directive name. */
1043 CPP_PUTC (pfile
, '#');
1044 parse_name (pfile
, GETC());
1045 ident
= pfile
->token_buffer
+ old_written
+ 1;
1046 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1047 if (ident_length
== 0 && PEEKC() == '\n')
1049 /* A line of just `#' becomes blank. */
1050 goto done_a_directive
;
1054 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1056 while (is_idchar
[*p
]) {
1057 if (*p
< '0' || *p
> '9')
1061 /* Avoid error for `###' and similar cases unless -pedantic. */
1063 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1065 if (pedantic
&& !lang_asm
)
1066 cpp_warning (pfile
, "invalid preprocessor directive");
1072 cpp_error (pfile
, "invalid preprocessor directive name");
1078 * Decode the keyword and call the appropriate expansion
1079 * routine, after moving the input pointer up to the next line.
1081 for (kt
= directive_table
; ; kt
++) {
1082 if (kt
->length
<= 0)
1083 goto not_a_directive
;
1084 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1088 if (kt
->command_reads_line
)
1092 /* Nonzero means do not delete comments within the directive.
1093 #define needs this when -traditional. */
1094 int comments
= CPP_TRADITIONAL (pfile
) && kt
->traditional_comments
;
1095 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1096 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1097 after_ident
= CPP_WRITTEN (pfile
);
1098 copy_rest_of_line (pfile
);
1099 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1102 /* For #pragma and #define, we may want to pass through the directive.
1103 Other directives may create output, but we don't want the directive
1104 itself out, so we pop it now. For example #include may write a #line
1105 command (see comment in do_include), and conditionals may emit
1106 #failed ... #endfailed stuff. But note that popping the buffer
1107 means the parameters to kt->func may point after pfile->limit
1108 so these parameters are invalid as soon as something gets appended
1109 to the token_buffer. */
1111 line_end
= CPP_PWRITTEN (pfile
);
1112 if (!kt
->pass_thru
&& kt
->type
!= T_DEFINE
)
1113 CPP_SET_WRITTEN (pfile
, old_written
);
1115 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1117 || (kt
->type
== T_DEFINE
1118 && CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
))
1120 /* Just leave the entire #define in the output stack. */
1122 else if (kt
->type
== T_DEFINE
1123 && CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1125 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7; /* Skip "#define". */
1126 SKIP_WHITE_SPACE (p
);
1127 while (is_idchar
[*p
]) p
++;
1129 CPP_PUTC (pfile
, '\n');
1131 else if (kt
->type
== T_DEFINE
)
1132 CPP_SET_WRITTEN (pfile
, old_written
);
1140 /* Pass a directive through to the output file.
1141 BUF points to the contents of the directive, as a contiguous string.
1142 LIMIT points to the first character past the end of the directive.
1143 KEYWORD is the keyword-table entry for the directive. */
1146 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1147 U_CHAR
*buf
, *limit
;
1149 struct directive
*keyword
;
1151 register unsigned keyword_length
= keyword
->length
;
1153 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1154 CPP_PUTC_Q (pfile
, '#');
1155 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1156 if (limit
!= buf
&& buf
[0] != ' ')
1157 CPP_PUTC_Q (pfile
, ' ');
1158 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1160 CPP_PUTS_Q (pfile
, '\n');
1161 /* Count the line we have just made in the output,
1162 to get in sync properly. */
1167 /* The arglist structure is built by do_define to tell
1168 collect_definition where the argument names begin. That
1169 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1170 would contain pointers to the strings x, y, and z.
1171 Collect_definition would then build a DEFINITION node,
1172 with reflist nodes pointing to the places x, y, and z had
1173 appeared. So the arglist is just convenience data passed
1174 between these two routines. It is not kept around after
1175 the current #define has been processed and entered into the
1179 struct arglist
*next
;
1186 /* Read a replacement list for a macro with parameters.
1187 Build the DEFINITION structure.
1188 Reads characters of text starting at BUF until END.
1189 ARGLIST specifies the formal parameters to look for
1190 in the text of the definition; NARGS is the number of args
1191 in that list, or -1 for a macro name that wants no argument list.
1192 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1193 and NAMELEN is its length in characters.
1195 Note that comments, backslash-newlines, and leading white space
1196 have already been deleted from the argument. */
1199 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1201 U_CHAR
*buf
, *limit
;
1203 struct arglist
*arglist
;
1206 register U_CHAR
*p
, *lastp
, *exp_p
;
1207 struct reflist
*endpat
= NULL
;
1208 /* Pointer to first nonspace after last ## seen. */
1210 /* Pointer to first nonspace after last single-# seen. */
1211 U_CHAR
*stringify
= 0;
1213 int expected_delimiter
= '\0';
1215 /* Scan thru the replacement list, ignoring comments and quoted
1216 strings, picking up on the macro calls. It does a linear search
1217 thru the arg list on every potential symbol. Profiling might say
1218 that something smarter should happen. */
1223 /* Find the beginning of the trailing whitespace. */
1225 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1227 /* Allocate space for the text in the macro definition.
1228 Leading and trailing whitespace chars need 2 bytes each.
1229 Each other input char may or may not need 1 byte,
1230 so this is an upper bound. The extra 5 are for invented
1231 leading and trailing newline-marker and final null. */
1232 maxsize
= (sizeof (DEFINITION
)
1234 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1238 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1240 defn
->nargs
= nargs
;
1241 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1246 /* Add one initial space escape-marker to prevent accidental
1247 token-pasting (often removed by macroexpand). */
1251 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1252 cpp_error (pfile
, "`##' at start of macro definition");
1256 /* Process the main body of the definition. */
1258 int skipped_arg
= 0;
1259 register U_CHAR c
= *p
++;
1263 if (!CPP_TRADITIONAL (pfile
)) {
1267 if (expected_delimiter
!= '\0') {
1268 if (c
== expected_delimiter
)
1269 expected_delimiter
= '\0';
1271 expected_delimiter
= c
;
1275 if (p
< limit
&& expected_delimiter
) {
1276 /* In a string, backslash goes through
1277 and makes next char ordinary. */
1283 /* An '@' in a string or character constant stands for itself,
1284 and does not need to be escaped. */
1285 if (!expected_delimiter
)
1290 /* # is ordinary inside a string. */
1291 if (expected_delimiter
)
1293 if (p
< limit
&& *p
== '#') {
1294 /* ##: concatenate preceding and following tokens. */
1295 /* Take out the first #, discard preceding whitespace. */
1297 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1299 /* Skip the second #. */
1301 /* Discard following whitespace. */
1302 SKIP_WHITE_SPACE (p
);
1305 cpp_error (pfile
, "`##' at end of macro definition");
1306 } else if (nargs
>= 0) {
1307 /* Single #: stringify following argument ref.
1308 Don't leave the # in the expansion. */
1310 SKIP_WHITE_SPACE (p
);
1311 if (p
== limit
|| ! is_idstart
[*p
])
1313 "`#' operator is not followed by a macro argument name");
1320 /* In -traditional mode, recognize arguments inside strings and
1321 and character constants, and ignore special properties of #.
1322 Arguments inside strings are considered "stringified", but no
1323 extra quote marks are supplied. */
1327 if (expected_delimiter
!= '\0') {
1328 if (c
== expected_delimiter
)
1329 expected_delimiter
= '\0';
1331 expected_delimiter
= c
;
1335 /* Backslash quotes delimiters and itself, but not macro args. */
1336 if (expected_delimiter
!= 0 && p
< limit
1337 && (*p
== expected_delimiter
|| *p
== '\\')) {
1344 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1347 /* If we find a comment that wasn't removed by handle_directive,
1348 this must be -traditional. So replace the comment with
1352 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1355 /* Mark this as a concatenation-point, as if it had been ##. */
1363 /* Handle the start of a symbol. */
1364 if (is_idchar
[c
] && nargs
> 0) {
1365 U_CHAR
*id_beg
= p
- 1;
1369 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1370 id_len
= p
- id_beg
;
1372 if (is_idstart
[c
]) {
1373 register struct arglist
*arg
;
1375 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1376 struct reflist
*tpat
;
1378 if (arg
->name
[0] == c
1379 && arg
->length
== id_len
1380 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1381 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1382 if (CPP_TRADITIONAL (pfile
)) {
1383 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1387 "macro arg `%.*s' would be stringified with -traditional.",
1391 /* If ANSI, don't actually substitute inside a string. */
1392 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1394 /* make a pat node for this arg and append it to the end of
1396 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1398 tpat
->raw_before
= concat
== id_beg
;
1399 tpat
->raw_after
= 0;
1400 tpat
->rest_args
= arg
->rest_args
;
1401 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1402 ? expected_delimiter
!= '\0'
1403 : stringify
== id_beg
);
1406 defn
->pattern
= tpat
;
1408 endpat
->next
= tpat
;
1411 tpat
->argno
= arg
->argno
;
1412 tpat
->nchars
= exp_p
- lastp
;
1414 register U_CHAR
*p1
= p
;
1415 SKIP_WHITE_SPACE (p1
);
1416 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1417 tpat
->raw_after
= 1;
1419 lastp
= exp_p
; /* place to start copying from next time */
1426 /* If this was not a macro arg, copy it into the expansion. */
1427 if (! skipped_arg
) {
1428 register U_CHAR
*lim1
= p
;
1432 if (stringify
== id_beg
)
1434 "`#' operator should be followed by a macro argument name");
1439 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1441 /* If ANSI, put in a "@ " marker to prevent token pasting.
1442 But not if "inside a string" (which in ANSI mode
1443 happens only for -D option). */
1450 defn
->length
= exp_p
- defn
->expansion
;
1452 /* Crash now if we overrun the allocated size. */
1453 if (defn
->length
+ 1 > maxsize
)
1457 /* This isn't worth the time it takes. */
1458 /* give back excess storage */
1459 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1466 * special extension string that can be added to the last macro argument to
1467 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1468 * #define wow(a, b...) process (b, a, b)
1469 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1470 * { wow (one, two); } -> { process (two, one, two); }
1471 * if this "rest_arg" is used with the concat token '##' and if it is not
1472 * supplied then the token attached to with ## will not be outputted. Ex:
1473 * #define wow (a, b...) process (b ## , a, ## b)
1474 * { wow (1, 2); } -> { process (2, 1, 2); }
1475 * { wow (one); } -> { process (one); {
1477 static char rest_extension
[] = "...";
1478 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1480 /* Create a DEFINITION node from a #define directive. Arguments are
1481 as for do_define. */
1484 create_definition (buf
, limit
, pfile
, predefinition
)
1485 U_CHAR
*buf
, *limit
;
1489 U_CHAR
*bp
; /* temp ptr into input buffer */
1490 U_CHAR
*symname
; /* remember where symbol name starts */
1491 int sym_length
; /* and how long it is */
1494 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1496 int arglengths
= 0; /* Accumulate lengths of arg names
1497 plus number of args. */
1499 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1503 while (is_hor_space
[*bp
])
1506 symname
= bp
; /* remember where it starts */
1508 sym_length
= check_macro_name (pfile
, bp
, "macro");
1511 /* Lossage will occur if identifiers or control keywords are broken
1512 across lines using backslash. This is not the right place to take
1516 struct arglist
*arg_ptrs
= NULL
;
1519 bp
++; /* skip '(' */
1520 SKIP_WHITE_SPACE (bp
);
1522 /* Loop over macro argument names. */
1523 while (*bp
!= ')') {
1524 struct arglist
*temp
;
1526 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1528 temp
->next
= arg_ptrs
;
1529 temp
->argno
= argno
++;
1530 temp
->rest_args
= 0;
1534 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1536 if (!is_idstart
[*bp
])
1537 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1539 /* Find the end of the arg name. */
1540 while (is_idchar
[*bp
]) {
1542 /* do we have a "special" rest-args extension here? */
1543 if (limit
- bp
> REST_EXTENSION_LENGTH
&&
1544 strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1546 temp
->rest_args
= 1;
1550 temp
->length
= bp
- temp
->name
;
1552 bp
+= REST_EXTENSION_LENGTH
;
1553 arglengths
+= temp
->length
+ 2;
1554 SKIP_WHITE_SPACE (bp
);
1555 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1556 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1561 SKIP_WHITE_SPACE (bp
);
1564 cpp_error (pfile
, "unterminated parameter list in `#define'");
1568 struct arglist
*otemp
;
1570 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1571 if (temp
->length
== otemp
->length
&&
1572 strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1575 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1576 (void) strncpy (name
, temp
->name
, temp
->length
);
1577 name
[temp
->length
] = '\0';
1579 "duplicate argument name `%s' in `#define'", name
);
1585 ++bp
; /* skip paren */
1586 SKIP_WHITE_SPACE (bp
);
1587 /* now everything from bp before limit is the definition. */
1588 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1589 defn
->rest_args
= rest_args
;
1591 /* Now set defn->args.argnames to the result of concatenating
1592 the argument names in reverse order
1593 with comma-space between them. */
1594 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1596 struct arglist
*temp
;
1598 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1599 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1601 if (temp
->next
!= 0) {
1602 defn
->args
.argnames
[i
++] = ',';
1603 defn
->args
.argnames
[i
++] = ' ';
1606 defn
->args
.argnames
[i
] = 0;
1609 /* Simple expansion or empty definition. */
1613 if (is_hor_space
[*bp
]) {
1615 SKIP_WHITE_SPACE (bp
);
1618 case '!': case '"': case '#': case '%': case '&': case '\'':
1619 case ')': case '*': case '+': case ',': case '-': case '.':
1620 case '/': case ':': case ';': case '<': case '=': case '>':
1621 case '?': case '[': case '\\': case ']': case '^': case '{':
1622 case '|': case '}': case '~':
1623 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1624 sym_length
, symname
);
1628 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1629 sym_length
, symname
);
1634 /* now everything from bp before limit is the definition. */
1635 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1636 defn
->args
.argnames
= (U_CHAR
*) "";
1642 /* OP is null if this is a predefinition */
1643 defn
->predefined
= predefinition
;
1645 mdef
.symnam
= symname
;
1646 mdef
.symlen
= sym_length
;
1655 /* Check a purported macro name SYMNAME, and yield its length.
1656 USAGE is the kind of name this is intended for. */
1659 check_macro_name (pfile
, symname
, usage
)
1667 for (p
= symname
; is_idchar
[*p
]; p
++)
1669 sym_length
= p
- symname
;
1670 if (sym_length
== 0)
1671 cpp_error (pfile
, "invalid %s name", usage
);
1672 else if (!is_idstart
[*symname
]) {
1673 U_CHAR
*msg
; /* what pain... */
1674 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1675 bcopy (symname
, msg
, sym_length
);
1676 msg
[sym_length
] = 0;
1677 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1679 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1680 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1685 /* Return zero if two DEFINITIONs are isomorphic. */
1688 compare_defs (d1
, d2
)
1689 DEFINITION
*d1
, *d2
;
1691 register struct reflist
*a1
, *a2
;
1692 register U_CHAR
*p1
= d1
->expansion
;
1693 register U_CHAR
*p2
= d2
->expansion
;
1696 if (d1
->nargs
!= d2
->nargs
)
1698 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1700 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1701 a1
= a1
->next
, a2
= a2
->next
) {
1702 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1703 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1704 || a1
->argno
!= a2
->argno
1705 || a1
->stringify
!= a2
->stringify
1706 || a1
->raw_before
!= a2
->raw_before
1707 || a1
->raw_after
!= a2
->raw_after
)
1715 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1716 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1721 /* Return 1 if two parts of two macro definitions are effectively different.
1722 One of the parts starts at BEG1 and has LEN1 chars;
1723 the other has LEN2 chars at BEG2.
1724 Any sequence of whitespace matches any other sequence of whitespace.
1725 FIRST means these parts are the first of a macro definition;
1726 so ignore leading whitespace entirely.
1727 LAST means these parts are the last of a macro definition;
1728 so ignore trailing whitespace entirely. */
1731 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1733 U_CHAR
*beg1
, *beg2
;
1737 register U_CHAR
*end1
= beg1
+ len1
;
1738 register U_CHAR
*end2
= beg2
+ len2
;
1740 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1741 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1744 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1745 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1747 while (beg1
!= end1
&& beg2
!= end2
) {
1748 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1749 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1750 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1751 } else if (*beg1
== *beg2
) {
1755 return (beg1
!= end1
) || (beg2
!= end2
);
1758 /* Process a #define command.
1759 BUF points to the contents of the #define command, as a contiguous string.
1760 LIMIT points to the first character past the end of the definition.
1761 KEYWORD is the keyword-table entry for #define,
1762 or NULL for a "predefined" macro. */
1765 do_define (pfile
, keyword
, buf
, limit
)
1767 struct directive
*keyword
;
1768 U_CHAR
*buf
, *limit
;
1775 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1776 if (pcp_outfile
&& keyword
)
1777 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1780 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1784 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1786 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1789 /* Redefining a precompiled key is ok. */
1790 if (hp
->type
== T_PCSTRING
)
1792 /* Redefining a macro is ok if the definitions are the same. */
1793 else if (hp
->type
== T_MACRO
)
1794 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
1795 /* Redefining a constant is ok with -D. */
1796 else if (hp
->type
== T_CONST
)
1797 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1798 /* Print the warning if it's not ok. */
1801 U_CHAR
*msg
; /* what pain... */
1803 /* If we are passing through #define and #undef directives, do
1804 that for this re-definition now. */
1805 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1806 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1808 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1810 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1811 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1812 cpp_pedwarn (pfile
, msg
);
1813 if (hp
->type
== T_MACRO
)
1814 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1815 "this is the location of the previous definition");
1817 /* Replace the old definition. */
1819 hp
->value
.defn
= mdef
.defn
;
1823 /* If we are passing through #define and #undef directives, do
1824 that for this new definition now. */
1825 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1826 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1827 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1828 (char *) mdef
.defn
, hashcode
);
1838 /* This structure represents one parsed argument in a macro call.
1839 `raw' points to the argument text as written (`raw_length' is its length).
1840 `expanded' points to the argument's macro-expansion
1841 (its length is `expand_length').
1842 `stringified_length' is the length the argument would have
1844 `use_count' is the number of times this macro arg is substituted
1845 into the macro. If the actual use count exceeds 10,
1846 the value stored is 10. */
1848 /* raw and expanded are relative to ARG_BASE */
1849 #define ARG_BASE ((pfile)->token_buffer)
1852 /* Strings relative to pfile->token_buffer */
1853 long raw
, expanded
, stringified
;
1854 int raw_length
, expand_length
;
1855 int stringified_length
;
1860 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1861 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1862 as the new input buffer.
1863 Return the new buffer, or NULL on failure. */
1866 cpp_push_buffer (pfile
, buffer
, length
)
1871 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1872 if (buf
== pfile
->buffer_stack
)
1874 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1879 bzero ((char *) buf
, sizeof (cpp_buffer
));
1880 CPP_BUFFER (pfile
) = buf
;
1881 buf
->if_stack
= pfile
->if_stack
;
1882 buf
->cleanup
= null_cleanup
;
1883 buf
->underflow
= null_underflow
;
1884 buf
->buf
= buf
->cur
= buffer
;
1885 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1891 cpp_pop_buffer (pfile
)
1894 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1895 (*buf
->cleanup
) (buf
, pfile
);
1896 return ++CPP_BUFFER (pfile
);
1899 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1900 Pop the buffer when done. */
1903 cpp_scan_buffer (pfile
)
1906 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1909 enum cpp_token token
= cpp_get_token (pfile
);
1910 if (token
== CPP_EOF
) /* Should not happen ... */
1912 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1914 cpp_pop_buffer (pfile
);
1921 * Rescan a string (which may have escape marks) into pfile's buffer.
1922 * Place the result in pfile->token_buffer.
1924 * The input is copied before it is scanned, so it is safe to pass
1925 * it something from the token_buffer that will get overwritten
1926 * (because it follows CPP_WRITTEN). This is used by do_include.
1930 cpp_expand_to_buffer (pfile
, buf
, length
)
1935 register cpp_buffer
*ip
;
1937 U_CHAR
*limit
= buf
+ length
;
1940 int odepth
= indepth
;
1946 /* Set up the input on the input stack. */
1948 buf1
= (U_CHAR
*) alloca (length
+ 1);
1950 register U_CHAR
*p1
= buf
;
1951 register U_CHAR
*p2
= buf1
;
1958 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1961 ip
->has_escapes
= 1;
1963 ip
->lineno
= obuf
.lineno
= 1;
1966 /* Scan the input, create the output. */
1967 cpp_scan_buffer (pfile
);
1970 if (indepth
!= odepth
)
1974 CPP_NUL_TERMINATE (pfile
);
1979 adjust_position (buf
, limit
, linep
, colp
)
1989 (*linep
)++, (*colp
) = 1;
1995 /* Move line_base forward, updating lineno and colno. */
1998 update_position (pbuf
)
1999 register cpp_buffer
*pbuf
;
2001 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
2002 unsigned char *new_pos
= pbuf
->cur
;
2003 register struct parse_marker
*mark
;
2004 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
2006 if (pbuf
->buf
+ mark
->position
< new_pos
)
2007 new_pos
= pbuf
->buf
+ mark
->position
;
2009 pbuf
->line_base
+= new_pos
- old_pos
;
2010 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
2014 cpp_buf_line_and_col (pbuf
, linep
, colp
)
2015 register cpp_buffer
*pbuf
;
2023 *linep
= pbuf
->lineno
;
2024 *colp
= pbuf
->colno
;
2025 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2034 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2037 cpp_file_buffer (pfile
)
2040 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2042 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2043 if (ip
->fname
!= NULL
)
2049 count_newlines (buf
, limit
)
2050 register U_CHAR
*buf
;
2051 register U_CHAR
*limit
;
2053 register long count
= 0;
2064 * write out a #line command, for instance, after an #include file.
2065 * If CONDITIONAL is nonzero, we can omit the #line if it would
2066 * appear to be a no-op, and we can output a few newlines instead
2067 * if we want to increase the line number by a small amount.
2068 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2072 output_line_command (pfile
, conditional
, file_change
)
2075 enum file_change_code file_change
;
2078 char *line_cmd_buf
, *line_end
;
2080 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2082 if (ip
->fname
== NULL
)
2085 update_position (ip
);
2087 if (CPP_OPTIONS (pfile
)->no_line_commands
2088 || CPP_OPTIONS (pfile
)->no_output
)
2091 line
= CPP_BUFFER (pfile
)->lineno
;
2092 col
= CPP_BUFFER (pfile
)->colno
;
2093 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2095 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2099 if (line
== pfile
->lineno
)
2102 /* If the inherited line number is a little too small,
2103 output some newlines instead of a #line command. */
2104 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2105 CPP_RESERVE (pfile
, 20);
2106 while (line
> pfile
->lineno
) {
2107 CPP_PUTC_Q (pfile
, '\n');
2115 /* Don't output a line number of 0 if we can help it. */
2116 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2117 && *ip
->bufp
== '\n') {
2123 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2125 #ifdef OUTPUT_LINE_COMMANDS
2126 static char sharp_line
[] = "#line ";
2128 static char sharp_line
[] = "# ";
2130 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2133 sprintf (CPP_PWRITTEN (pfile
), "%d ", line
);
2134 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2136 quote_string (pfile
, ip
->nominal_fname
);
2137 if (file_change
!= same_file
) {
2138 CPP_PUTC_Q (pfile
, ' ');
2139 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2141 /* Tell cc1 if following text comes from a system header file. */
2142 if (ip
->system_header_p
) {
2143 CPP_PUTC_Q (pfile
, ' ');
2144 CPP_PUTC_Q (pfile
, '3');
2146 #ifndef NO_IMPLICIT_EXTERN_C
2147 /* Tell cc1plus if following text should be treated as C. */
2148 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2149 CPP_PUTC_Q (pfile
, ' ');
2150 CPP_PUTC_Q (pfile
, '4');
2153 CPP_PUTC_Q (pfile
, '\n');
2154 pfile
->lineno
= line
;
2158 * Parse a macro argument and append the info on PFILE's token_buffer.
2159 * REST_ARGS means to absorb the rest of the args.
2160 * Return nonzero to indicate a syntax error.
2163 static enum cpp_token
2164 macarg (pfile
, rest_args
)
2169 enum cpp_token token
;
2170 long arg_start
= CPP_WRITTEN (pfile
);
2171 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2172 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2174 /* Try to parse as much of the argument as exists at this
2175 input stack level. */
2176 pfile
->no_macro_expand
++;
2179 token
= cpp_get_token (pfile
);
2185 /* If we've hit end of file, it's an error (reported by caller).
2186 Ditto if it's the end of cpp_expand_to_buffer text.
2187 If we've hit end of macro, just continue. */
2188 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2199 /* if we've returned to lowest level and
2200 we aren't absorbing all args */
2201 if (paren
== 0 && rest_args
== 0)
2205 /* Remove ',' or ')' from argument buffer. */
2206 CPP_ADJUST_WRITTEN (pfile
, -1);
2213 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2214 pfile
->no_macro_expand
--;
2219 /* Turn newlines to spaces in the string of length LENGTH at START,
2220 except inside of string constants.
2221 The string is copied into itself with its beginning staying fixed. */
2224 change_newlines (start
, length
)
2228 register U_CHAR
*ibp
;
2229 register U_CHAR
*obp
;
2230 register U_CHAR
*limit
;
2234 limit
= start
+ length
;
2237 while (ibp
< limit
) {
2238 *obp
++ = c
= *ibp
++;
2243 /* Notice and skip strings, so that we don't delete newlines in them. */
2246 while (ibp
< limit
) {
2247 *obp
++ = c
= *ibp
++;
2250 if (c
== '\n' && quotec
== '\'')
2266 if (!pfile
->timebuf
) {
2267 time_t t
= time ((time_t *) 0);
2268 pfile
->timebuf
= localtime (&t
);
2270 return pfile
->timebuf
;
2273 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2274 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2278 * expand things like __FILE__. Place the expansion into the output
2279 * buffer *without* rescanning.
2283 special_symbol (hp
, pfile
)
2290 cpp_buffer
*ip
= NULL
;
2293 int paren
= 0; /* For special `defined' keyword */
2296 if (pcp_outfile
&& pcp_inside_if
2297 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2299 "Predefined macro `%s' used inside `#if' during precompilation",
2303 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2305 if (ip
== CPP_NULL_BUFFER (pfile
))
2307 cpp_error (pfile
, "cccp error: not in any file?!");
2308 return; /* the show must go on */
2310 if (ip
->fname
!= NULL
)
2320 if (hp
->type
== T_BASE_FILE
)
2322 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2323 ip
= CPP_PREV_BUFFER (ip
);
2325 string
= ip
->nominal_fname
;
2329 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2330 quote_string (pfile
, string
);
2334 case T_INCLUDE_LEVEL
:
2336 ip
= CPP_BUFFER (pfile
);
2337 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2338 if (ip
->fname
!= NULL
)
2341 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2342 sprintf (buf
, "%d", true_indepth
- 1);
2346 buf
= (char *) alloca (3 + strlen (version_string
));
2347 sprintf (buf
, "\"%s\"", version_string
);
2350 #ifndef NO_BUILTIN_SIZE_TYPE
2356 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2357 case T_PTRDIFF_TYPE
:
2363 buf
= CPP_WCHAR_TYPE (pfile
);
2366 case T_USER_LABEL_PREFIX_TYPE
:
2367 buf
= USER_LABEL_PREFIX
;
2370 case T_REGISTER_PREFIX_TYPE
:
2371 buf
= REGISTER_PREFIX
;
2375 buf
= (char *) alloca (4 * sizeof (int));
2376 sprintf (buf
, "%d", hp
->value
.ival
);
2378 if (pcp_inside_if
&& pcp_outfile
)
2379 /* Output a precondition for this macro use */
2380 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2386 long line
= ip
->lineno
;
2387 long col
= ip
->colno
;
2388 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2390 buf
= (char *) alloca (10);
2391 sprintf (buf
, "%d", line
);
2397 buf
= (char *) alloca (20);
2398 timebuf
= timestamp (pfile
);
2399 if (hp
->type
== T_DATE
)
2400 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2401 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2403 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2407 case T_SPEC_DEFINED
:
2408 buf
= " 0 "; /* Assume symbol is not defined */
2409 ip
= CPP_BUFFER (pfile
);
2410 SKIP_WHITE_SPACE (ip
->cur
);
2411 if (*ip
->cur
== '(')
2414 ip
->cur
++; /* Skip over the paren */
2415 SKIP_WHITE_SPACE (ip
->cur
);
2418 if (!is_idstart
[*ip
->cur
])
2420 if (hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1))
2423 if (pcp_outfile
&& pcp_inside_if
2424 && (hp
->type
== T_CONST
2425 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2426 /* Output a precondition for this macro use. */
2427 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2433 if (pcp_outfile
&& pcp_inside_if
)
2435 /* Output a precondition for this macro use */
2436 U_CHAR
*cp
= ip
->bufp
;
2437 fprintf (pcp_outfile
, "#undef ");
2438 while (is_idchar
[*cp
]) /* Ick! */
2439 fputc (*cp
++, pcp_outfile
);
2440 putc ('\n', pcp_outfile
);
2443 while (is_idchar
[*ip
->cur
])
2445 SKIP_WHITE_SPACE (ip
->cur
);
2448 if (*ip
->cur
!= ')')
2456 cpp_error (pfile
, "`defined' without an identifier");
2460 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2464 CPP_RESERVE (pfile
, len
+ 1);
2465 CPP_PUTS_Q (pfile
, buf
, len
);
2466 CPP_NUL_TERMINATE_Q (pfile
);
2471 /* Write out a #define command for the special named MACRO_NAME
2472 to PFILE's token_buffer. */
2475 dump_special_to_buffer (pfile
, macro_name
)
2479 static char define_directive
[] = "#define ";
2480 int macro_name_length
= strlen (macro_name
);
2481 output_line_command (pfile
, 0, same_file
);
2482 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2483 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2484 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2485 CPP_PUTC_Q (pfile
, ' ');
2486 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2487 CPP_PUTC (pfile
, '\n');
2490 /* Initialize the built-in macros. */
2493 initialize_builtins (pfile
)
2496 install ("__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2497 install ("__DATE__", -1, T_DATE
, 0, 0, -1);
2498 install ("__FILE__", -1, T_FILE
, 0, 0, -1);
2499 install ("__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2500 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2501 install ("__VERSION__", -1, T_VERSION
, 0, 0, -1);
2502 #ifndef NO_BUILTIN_SIZE_TYPE
2503 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2505 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2506 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2508 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2509 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2510 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2511 install ("__TIME__", -1, T_TIME
, 0, 0, -1);
2512 if (!CPP_TRADITIONAL (pfile
))
2513 install ("__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2514 if (CPP_OPTIONS (pfile
)->objc
)
2515 install ("__OBJC__", -1, T_CONST
, 1, 0, -1);
2516 /* This is supplied using a -D by the compiler driver
2517 so that it is present only when truly compiling with GNU C. */
2518 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2520 if (CPP_OPTIONS (pfile
)->debug_output
)
2522 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2523 dump_special_to_buffer (pfile
, "__VERSION__");
2524 #ifndef NO_BUILTIN_SIZE_TYPE
2525 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2527 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2528 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2530 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2531 dump_special_to_buffer (pfile
, "__DATE__");
2532 dump_special_to_buffer (pfile
, "__TIME__");
2533 if (!CPP_TRADITIONAL (pfile
))
2534 dump_special_to_buffer (pfile
, "__STDC__");
2535 if (CPP_OPTIONS (pfile
)->objc
)
2536 dump_special_to_buffer (pfile
, "__OBJC__");
2540 /* Return 1 iff a token ending in C1 followed directly by a token C2
2541 could cause mis-tokenization. */
2544 unsafe_chars (c1
, c2
)
2550 if (c2
== c1
|| c2
== '=')
2554 case '0': case '1': case '2': case '3': case '4':
2555 case '5': case '6': case '7': case '8': case '9':
2557 if (c2
== '-' || c2
== '+')
2558 return 1; /* could extend a pre-processing number */
2561 if (c2
== '\'' || c2
== '\"')
2562 return 1; /* Could turn into L"xxx" or L'xxx'. */
2566 case 'a': case 'b': case 'c': case 'd': case 'f':
2567 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2568 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2569 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2571 case 'A': case 'B': case 'C': case 'D': case 'F':
2572 case 'G': case 'H': case 'I': case 'J': case 'K':
2573 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2574 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2576 /* We're in the middle of either a name or a pre-processing number. */
2577 return (is_idchar
[c2
] || c2
== '.');
2578 case '<': case '>': case '!': case '%': case '#': case ':':
2579 case '^': case '&': case '|': case '*': case '/': case '=':
2580 return (c2
== c1
|| c2
== '=');
2585 /* Expand a macro call.
2586 HP points to the symbol that is the macro being called.
2587 Put the result of expansion onto the input stack
2588 so that subsequent input by our caller will use it.
2590 If macro wants arguments, caller has already verified that
2591 an argument list follows; arguments come from the input stack. */
2594 macroexpand (pfile
, hp
)
2599 DEFINITION
*defn
= hp
->value
.defn
;
2600 register U_CHAR
*xbuf
;
2601 long start_line
, start_column
;
2603 struct argdata
*args
;
2604 long old_written
= CPP_WRITTEN (pfile
);
2606 int start_line
= instack
[indepth
].lineno
;
2608 int rest_args
, rest_zero
;
2612 CHECK_DEPTH (return;);
2616 /* This macro is being used inside a #if, which means it must be */
2617 /* recorded as a precondition. */
2618 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2619 dump_single_macro (hp
, pcp_outfile
);
2622 pfile
->output_escapes
++;
2623 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2625 nargs
= defn
->nargs
;
2629 enum cpp_token token
;
2631 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2633 for (i
= 0; i
< nargs
; i
++)
2635 args
[i
].raw
= args
[i
].expanded
= 0;
2636 args
[i
].raw_length
= 0;
2637 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2638 args
[i
].use_count
= 0;
2641 /* Parse all the macro args that are supplied. I counts them.
2642 The first NARGS args are stored in ARGS.
2643 The rest are discarded. If rest_args is set then we assume
2644 macarg absorbed the rest of the args. */
2648 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2653 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2655 /* if we are working on last arg which absorbs rest of args... */
2656 if (i
== nargs
- 1 && defn
->rest_args
)
2658 args
[i
].raw
= CPP_WRITTEN (pfile
);
2659 token
= macarg (pfile
, rest_args
);
2660 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2661 args
[i
].newlines
= 0; /* FIXME */
2664 token
= macarg (pfile
, 0);
2665 if (token
== CPP_EOF
|| token
== CPP_POP
)
2667 cpp_error_with_line (pfile
, start_line
, start_column
,
2668 "unterminated macro call");
2672 } while (token
== CPP_COMMA
);
2674 /* If we got one arg but it was just whitespace, call that 0 args. */
2677 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2678 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2679 /* cpp.texi says for foo ( ) we provide one argument.
2680 However, if foo wants just 0 arguments, treat this as 0. */
2682 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2687 /* Don't output an error message if we have already output one for
2688 a parse error above. */
2690 if (nargs
== 0 && i
> 0)
2692 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2696 /* traditional C allows foo() if foo wants one argument. */
2697 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2699 /* the rest args token is allowed to absorb 0 tokens */
2700 else if (i
== nargs
- 1 && defn
->rest_args
)
2703 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2705 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2707 cpp_error (pfile
, "macro `%s' used with only %d args",
2713 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2717 /* If macro wants zero args, we parsed the arglist for checking only.
2718 Read directly from the macro definition. */
2721 xbuf
= defn
->expansion
;
2722 xbuf_len
= defn
->length
;
2726 register U_CHAR
*exp
= defn
->expansion
;
2727 register int offset
; /* offset in expansion,
2728 copied a piece at a time */
2729 register int totlen
; /* total amount of exp buffer filled so far */
2731 register struct reflist
*ap
, *last_ap
;
2733 /* Macro really takes args. Compute the expansion of this call. */
2735 /* Compute length in characters of the macro's expansion.
2736 Also count number of times each arg is used. */
2737 xbuf_len
= defn
->length
;
2738 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2742 register struct argdata
*arg
= &args
[ap
->argno
];
2743 /* Stringify it it hasn't already been */
2744 if (arg
->stringified_length
< 0)
2746 int arglen
= arg
->raw_length
;
2750 /* Initially need_space is -1. Otherwise, 1 means the
2751 previous character was a space, but we suppressed it;
2752 0 means the previous character was a non-space. */
2753 int need_space
= -1;
2755 arg
->stringified
= CPP_WRITTEN (pfile
);
2756 if (!CPP_TRADITIONAL (pfile
))
2757 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2758 for (; i
< arglen
; i
++)
2760 c
= (ARG_BASE
+ arg
->raw
)[i
];
2764 /* Internal sequences of whitespace are replaced by
2765 one space except within an string or char token.*/
2768 if (CPP_WRITTEN (pfile
) > arg
->stringified
2769 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2771 /* "@ " escape markers are removed */
2772 CPP_ADJUST_WRITTEN (pfile
, -1);
2775 if (need_space
== 0)
2779 else if (need_space
> 0)
2780 CPP_PUTC (pfile
, ' ');
2795 else if (c
== '\"' || c
== '\'')
2799 /* Escape these chars */
2800 if (c
== '\"' || (in_string
&& c
== '\\'))
2801 CPP_PUTC (pfile
, '\\');
2803 CPP_PUTC (pfile
, c
);
2806 CPP_RESERVE (pfile
, 4);
2807 sprintf (CPP_PWRITTEN (pfile
), "\\%03o",
2809 CPP_ADJUST_WRITTEN (pfile
, 4);
2812 if (!CPP_TRADITIONAL (pfile
))
2813 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2814 arg
->stringified_length
2815 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2817 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2819 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2820 /* Add 4 for two newline-space markers to prevent
2821 token concatenation. */
2822 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2825 /* We have an ordinary (expanded) occurrence of the arg.
2826 So compute its expansion, if we have not already. */
2827 if (args
[ap
->argno
].expand_length
< 0)
2829 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2830 cpp_expand_to_buffer (pfile
,
2831 ARG_BASE
+ args
[ap
->argno
].raw
,
2832 args
[ap
->argno
].raw_length
);
2834 args
[ap
->argno
].expand_length
2835 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2838 /* Add 4 for two newline-space markers to prevent
2839 token concatenation. */
2840 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2842 if (args
[ap
->argno
].use_count
< 10)
2843 args
[ap
->argno
].use_count
++;
2846 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2848 /* Generate in XBUF the complete expansion
2849 with arguments substituted in.
2850 TOTLEN is the total size generated so far.
2851 OFFSET is the index in the definition
2852 of where we are copying from. */
2853 offset
= totlen
= 0;
2854 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2855 last_ap
= ap
, ap
= ap
->next
)
2857 register struct argdata
*arg
= &args
[ap
->argno
];
2858 int count_before
= totlen
;
2860 /* Add chars to XBUF. */
2861 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2862 xbuf
[totlen
++] = exp
[offset
];
2864 /* If followed by an empty rest arg with concatenation,
2865 delete the last run of nonwhite chars. */
2866 if (rest_zero
&& totlen
> count_before
2867 && ((ap
->rest_args
&& ap
->raw_before
)
2868 || (last_ap
!= NULL
&& last_ap
->rest_args
2869 && last_ap
->raw_after
)))
2871 /* Delete final whitespace. */
2872 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2875 /* Delete the nonwhites before them. */
2876 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2880 if (ap
->stringify
!= 0)
2882 bcopy (ARG_BASE
+ arg
->stringified
,
2883 xbuf
+ totlen
, arg
->stringified_length
);
2884 totlen
+= arg
->stringified_length
;
2886 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2888 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2889 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2892 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2893 while (p1
!= l1
&& is_idchar
[*p1
])
2894 xbuf
[totlen
++] = *p1
++;
2895 /* Delete any no-reexpansion marker that follows
2896 an identifier at the beginning of the argument
2897 if the argument is concatenated with what precedes it. */
2898 if (p1
[0] == '@' && p1
[1] == '-')
2903 /* Arg is concatenated after: delete trailing whitespace,
2904 whitespace markers, and no-reexpansion markers. */
2907 if (is_space
[l1
[-1]]) l1
--;
2908 else if (l1
[-1] == '-')
2910 U_CHAR
*p2
= l1
- 1;
2911 /* If a `-' is preceded by an odd number of newlines then it
2912 and the last newline are a no-reexpansion marker. */
2913 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2914 if ((l1
- 1 - p2
) & 1) {
2923 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2928 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2929 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2930 && !CPP_TRADITIONAL(pfile
)
2931 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2933 xbuf
[totlen
++] = '@';
2934 xbuf
[totlen
++] = ' ';
2937 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2938 totlen
+= arg
->expand_length
;
2940 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2941 && !CPP_TRADITIONAL(pfile
)
2942 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2944 xbuf
[totlen
++] = '@';
2945 xbuf
[totlen
++] = ' ';
2948 /* If a macro argument with newlines is used multiple times,
2949 then only expand the newlines once. This avoids creating
2950 output lines which don't correspond to any input line,
2951 which confuses gdb and gcov. */
2952 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2954 /* Don't bother doing change_newlines for subsequent
2958 = change_newlines (expanded
, arg
->expand_length
);
2962 if (totlen
> xbuf_len
)
2966 /* if there is anything left of the definition
2967 after handling the arg list, copy that in too. */
2969 for (i
= offset
; i
< defn
->length
; i
++)
2971 /* if we've reached the end of the macro */
2974 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2975 && last_ap
->raw_after
))
2976 xbuf
[totlen
++] = exp
[i
];
2984 pfile
->output_escapes
--;
2986 /* Now put the expansion on the input stack
2987 so our caller will commence reading from it. */
2988 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2989 CPP_BUFFER (pfile
)->has_escapes
= 1;
2991 /* Pop the space we've used in the token_buffer for argument expansion. */
2992 CPP_SET_WRITTEN (pfile
, old_written
);
2994 /* Recursive macro use sometimes works traditionally.
2995 #define foo(x,y) bar (x (y,0), y)
2998 if (!CPP_TRADITIONAL (pfile
))
2999 hp
->type
= T_DISABLED
;
3003 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
3005 register U_CHAR
*xbuf
;
3009 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
3012 mbuf
->cleanup
= macro_cleanup
;
3015 /* The first chars of the expansion should be a "@ " added by
3016 collect_expansion. This is to prevent accidental token-pasting
3017 between the text preceding the macro invocation, and the macro
3020 We would like to avoid adding unneeded spaces (for the sake of
3021 tools that use cpp, such as imake). In some common cases we can
3022 tell that it is safe to omit the space.
3024 The character before the macro invocation cannot have been an
3025 idchar (or else it would have been pasted with the idchars of
3026 the macro name). Therefore, if the first non-space character
3027 of the expansion is an idchar, we do not need the extra space
3028 to prevent token pasting.
3030 Also, we don't need the extra space if the first char is '(',
3031 or some other (less common) characters. */
3033 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3034 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3035 || xbuf
[2] == '\"'))
3039 /* Like cpp_get_token, except that it does not read past end-of-line.
3040 Also, horizontal space is skipped, and macros are popped. */
3042 static enum cpp_token
3043 get_directive_token (pfile
)
3048 long old_written
= CPP_WRITTEN (pfile
);
3049 enum cpp_token token
;
3050 cpp_skip_hspace (pfile
);
3051 if (PEEKC () == '\n')
3053 token
= cpp_get_token (pfile
);
3057 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3059 /* ... else fall though ... */
3060 case CPP_HSPACE
: case CPP_COMMENT
:
3061 CPP_SET_WRITTEN (pfile
, old_written
);
3069 /* Handle #include and #import.
3070 This function expects to see "fname" or <fname> on the input.
3072 The input is normally in part of the output_buffer following
3073 CPP_WRITTEN, and will get overwritten by output_line_command.
3074 I.e. in input file specification has been popped by handle_directive.
3078 do_include (pfile
, keyword
, unused1
, unused2
)
3080 struct directive
*keyword
;
3081 U_CHAR
*unused1
, *unused2
;
3083 int importing
= (keyword
->type
== T_IMPORT
);
3084 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3085 char *fname
; /* Dynamically allocated fname buffer */
3088 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3089 enum cpp_token token
;
3091 /* Chain of dirs to search */
3092 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3093 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3094 struct file_name_list
*searchptr
= 0;
3095 long old_written
= CPP_WRITTEN (pfile
);
3099 int f
; /* file number */
3101 int retried
= 0; /* Have already tried macro
3102 expanding the include line */
3103 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3108 f
= -1; /* JF we iz paranoid! */
3110 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3111 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3112 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3114 pfile
->import_warning
= 1;
3115 cpp_warning (pfile
, "using `#import' is not recommended");
3116 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3117 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3118 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3119 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3120 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3121 fprintf (stderr
, " ... <real contents of file> ...\n");
3122 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3123 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3124 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3125 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3128 pfile
->parsing_include_directive
++;
3129 token
= get_directive_token (pfile
);
3130 pfile
->parsing_include_directive
--;
3132 if (token
== CPP_STRING
)
3134 /* FIXME - check no trailing garbage */
3135 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3136 fend
= CPP_PWRITTEN (pfile
) - 1;
3137 if (fbeg
[-1] == '<')
3140 /* If -I-, start with the first -I dir after the -I-. */
3141 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3142 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3144 /* If -I- was specified, don't search current dir, only spec'd ones. */
3145 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3147 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3148 /* We have "filename". Figure out directory this source
3149 file is coming from and put it on the front of the list. */
3151 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3156 if ((nam
= fp
->nominal_fname
) != NULL
)
3158 /* Found a named file. Figure out dir of the file,
3159 and put it in front of the search list. */
3160 dsp
[0].next
= search_start
;
3163 ep
= rindex (nam
, '/');
3165 ep
= rindex (nam
, ']');
3166 if (ep
== NULL
) ep
= rindex (nam
, '>');
3167 if (ep
== NULL
) ep
= rindex (nam
, ':');
3168 if (ep
!= NULL
) ep
++;
3173 dsp
[0].fname
= (char *) alloca (n
+ 1);
3174 strncpy (dsp
[0].fname
, nam
, n
);
3175 dsp
[0].fname
[n
] = '\0';
3176 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3177 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3181 dsp
[0].fname
= 0; /* Current directory */
3183 dsp
[0].got_name_map
= 0;
3190 else if (token
== CPP_NAME
)
3193 * Support '#include xyz' like VAX-C to allow for easy use of all the
3194 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3195 * code from case '<' is repeated here) and generates a warning.
3198 "VAX-C-style include specification found, use '#include <filename.h>' !");
3200 /* If -I-, start with the first -I dir after the -I-. */
3201 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3202 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3203 fbeg
= pfile
->token_buffer
+ old_written
;
3204 fend
= CPP_PWRITTEN (pfile
);
3210 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3211 CPP_SET_WRITTEN (pfile
, old_written
);
3212 skip_rest_of_line (pfile
);
3218 token
= get_directive_token (pfile
);
3219 if (token
!= CPP_VSPACE
)
3221 cpp_error (pfile
, "junk at end of `#include'");
3222 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3223 token
= get_directive_token (pfile
);
3226 /* For #include_next, skip in the search path
3227 past the dir in which the containing file was found. */
3230 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3231 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3232 if (fp
->fname
!= NULL
)
3234 /* fp->dir is null if the containing file was specified with
3235 an absolute file name. In that case, don't skip anything. */
3236 if (fp
->dir
== SELF_DIR_DUMMY
)
3237 search_start
= CPP_OPTIONS (pfile
)->include
;
3239 search_start
= fp
->dir
->next
;
3244 CPP_SET_WRITTEN (pfile
, old_written
);
3250 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3254 /* Allocate this permanently, because it gets stored in the definitions
3256 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3257 /* + 2 above for slash and terminating null. */
3258 /* + 2 added for '.h' on VMS (to support '#include filename') */
3260 /* If specified file name is absolute, just open it. */
3263 strncpy (fname
, fbeg
, flen
);
3265 if (redundant_include_p (pfile
, fname
))
3268 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3270 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3272 return 0; /* Already included this file */
3274 /* Search directory path, trying to open the file.
3275 Copy each filename tried into FNAME. */
3277 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3278 if (searchptr
->fname
) {
3279 /* The empty string in a search path is ignored.
3280 This makes it possible to turn off entirely
3281 a standard piece of the list. */
3282 if (searchptr
->fname
[0] == 0)
3284 strcpy (fname
, searchptr
->fname
);
3285 strcat (fname
, "/");
3286 fname
[strlen (fname
) + flen
] = 0;
3290 strncat (fname
, fbeg
, flen
);
3292 /* Change this 1/2 Unix 1/2 VMS file specification into a
3293 full VMS file specification */
3294 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3295 /* Fix up the filename */
3296 hack_vms_include_specification (fname
);
3298 /* This is a normal VMS filespec, so use it unchanged. */
3299 strncpy (fname
, fbeg
, flen
);
3301 /* if it's '#include filename', add the missing .h */
3302 if (index(fname
,'.')==NULL
) {
3303 strcat (fname
, ".h");
3307 /* ??? There are currently 3 separate mechanisms for avoiding processing
3308 of redundant include files: #import, #pragma once, and
3309 redundant_include_p. It would be nice if they were unified. */
3310 if (redundant_include_p (pfile
, fname
))
3313 f
= lookup_import (pfile
, fname
, searchptr
);
3315 f
= open_include_file (pfile
, fname
, searchptr
);
3317 return 0; /* Already included this file */
3319 else if (f
== -1 && errno
== EACCES
)
3320 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3330 /* A file that was not found. */
3331 strncpy (fname
, fbeg
, flen
);
3333 /* If generating dependencies and -MG was specified, we assume missing
3334 files are leaf files, living in the same directory as the source file
3335 or other similar place; these missing files may be generated from
3336 other files and may not exist yet (eg: y.tab.h). */
3338 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3339 && CPP_PRINT_DEPS (pfile
)
3340 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3342 /* If it was requested as a system header file,
3343 then assume it belongs in the first place to look for such. */
3346 for (searchptr
= search_start
; searchptr
;
3347 searchptr
= searchptr
->next
)
3349 if (searchptr
->fname
)
3353 if (searchptr
->fname
[0] == 0)
3355 p
= (char *) alloca (strlen (searchptr
->fname
)
3356 + strlen (fname
) + 2);
3357 strcpy (p
, searchptr
->fname
);
3360 deps_output (pfile
, p
, ' ');
3367 /* Otherwise, omit the directory, as if the file existed
3368 in the directory with the source. */
3369 deps_output (pfile
, fname
, ' ');
3372 /* If -M was specified, and this header file won't be added to the
3373 dependency list, then don't count this as an error, because we can
3374 still produce correct output. Otherwise, we can't produce correct
3375 output, because there may be dependencies we need inside the missing
3376 file, and we don't know what directory this missing file exists in.*/
3377 else if (CPP_PRINT_DEPS (pfile
)
3378 && (CPP_PRINT_DEPS (pfile
)
3379 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3380 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3381 else if (search_start
)
3382 cpp_error_from_errno (pfile
, fname
);
3384 cpp_error (pfile
, "No include path in which to find %s", fname
);
3387 /* Check to see if this include file is a once-only include file.
3390 struct file_name_list
*ptr
;
3392 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3393 if (!strcmp (ptr
->fname
, fname
)) {
3395 return 0; /* This file was once'd. */
3399 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3400 if (!strcmp (ptr
->fname
, fname
))
3401 break; /* This file was included before. */
3405 /* This is the first time for this file. */
3406 /* Add it to list of files included. */
3408 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3409 ptr
->control_macro
= 0;
3410 ptr
->c_system_include_path
= 0;
3411 ptr
->next
= pfile
->all_include_files
;
3412 pfile
->all_include_files
= ptr
;
3413 ptr
->fname
= savestring (fname
);
3414 ptr
->got_name_map
= 0;
3416 /* For -M, add this file to the dependencies. */
3417 if (CPP_PRINT_DEPS (pfile
)
3418 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3419 deps_output (pfile
, fname
, ' ');
3422 /* Handle -H option. */
3423 if (CPP_OPTIONS(pfile
)->print_include_names
)
3425 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3426 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3428 fprintf (stderr
, "%s\n", fname
);
3432 pfile
->system_include_depth
++;
3434 /* Actually process the file. */
3436 /* Record file on "seen" list for #import. */
3437 add_import (pfile
, f
, fname
);
3439 pcftry
= (char *) alloca (strlen (fname
) + 30);
3451 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3453 pcf
= open (pcftry
, O_RDONLY
, 0666);
3459 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3461 || stat_f
.st_dev
!= s
.st_dev
)
3463 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3464 /* Don't need it any more. */
3469 /* Don't need it at all. */
3474 } while (pcf
!= -1 && !pcfbuf
);
3478 /* Actually process the file */
3479 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3481 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3482 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3484 output_line_command (pfile
, 0, enter_file
);
3485 pfile
->only_seen_white
= 2;
3489 pfile
->system_include_depth
--;
3494 /* Return nonzero if there is no need to include file NAME
3495 because it has already been included and it contains a conditional
3496 to make a repeated include do nothing. */
3499 redundant_include_p (pfile
, name
)
3503 struct file_name_list
*l
= pfile
->all_include_files
;
3504 for (; l
; l
= l
->next
)
3505 if (! strcmp (name
, l
->fname
)
3507 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3512 /* Return nonzero if the given FILENAME is an absolute pathname which
3513 designates a file within one of the known "system" include file
3514 directories. We assume here that if the given FILENAME looks like
3515 it is the name of a file which resides either directly in a "system"
3516 include file directory, or within any subdirectory thereof, then the
3517 given file must be a "system" include file. This function tells us
3518 if we should suppress pedantic errors/warnings for the given FILENAME.
3520 The value is 2 if the file is a C-language system header file
3521 for which C++ should (on most systems) assume `extern "C"'. */
3524 is_system_include (pfile
, filename
)
3526 register char *filename
;
3528 struct file_name_list
*searchptr
;
3530 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3531 searchptr
= searchptr
->next
)
3532 if (searchptr
->fname
) {
3533 register char *sys_dir
= searchptr
->fname
;
3534 register unsigned length
= strlen (sys_dir
);
3536 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3538 if (searchptr
->c_system_include_path
)
3549 * Install a name in the assertion hash table.
3551 * If LEN is >= 0, it is the length of the name.
3552 * Otherwise, compute the length by scanning the entire name.
3554 * If HASH is >= 0, it is the precomputed hash code.
3555 * Otherwise, compute the hash code.
3558 static ASSERTION_HASHNODE
*
3559 assertion_install (pfile
, name
, len
, hash
)
3565 register ASSERTION_HASHNODE
*hp
;
3566 register int i
, bucket
;
3567 register U_CHAR
*p
, *q
;
3569 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3570 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3572 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3573 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3574 pfile
->assertion_hashtab
[bucket
] = hp
;
3576 if (hp
->next
!= NULL
)
3577 hp
->next
->prev
= hp
;
3580 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3583 for (i
= 0; i
< len
; i
++)
3589 * find the most recent hash node for name name (ending with first
3590 * non-identifier char) installed by install
3592 * If LEN is >= 0, it is the length of the name.
3593 * Otherwise, compute the length by scanning the entire name.
3595 * If HASH is >= 0, it is the precomputed hash code.
3596 * Otherwise, compute the hash code.
3599 static ASSERTION_HASHNODE
*
3600 assertion_lookup (pfile
, name
, len
, hash
)
3606 register ASSERTION_HASHNODE
*bucket
;
3608 bucket
= pfile
->assertion_hashtab
[hash
];
3610 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3612 bucket
= bucket
->next
;
3618 delete_assertion (hp
)
3619 ASSERTION_HASHNODE
*hp
;
3621 struct tokenlist_list
*tail
;
3622 if (hp
->prev
!= NULL
)
3623 hp
->prev
->next
= hp
->next
;
3624 if (hp
->next
!= NULL
)
3625 hp
->next
->prev
= hp
->prev
;
3627 for (tail
= hp
->value
; tail
; )
3629 struct tokenlist_list
*next
= tail
->next
;
3630 free_token_list (tail
->tokens
);
3635 /* Make sure that the bucket chain header that
3636 the deleted guy was on points to the right thing afterwards. */
3637 if (hp
== *hp
->bucket_hdr
)
3638 *hp
->bucket_hdr
= hp
->next
;
3643 /* Convert a character string literal into a nul-terminated string.
3644 The input string is [IN ... LIMIT).
3645 The result is placed in RESULT. RESULT can be the same as IN.
3646 The value returned in the end of the string written to RESULT,
3647 or NULL on error. */
3650 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3652 register U_CHAR
*result
, *in
, *limit
;
3672 char *bpc
= (char *) in
;
3673 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3674 in
= (U_CHAR
*) bpc
;
3676 *result
++ = (U_CHAR
)c
;
3679 /* else fall through */
3689 * interpret #line command. Remembers previously seen fnames
3690 * in its very own hash table.
3692 #define FNAME_HASHSIZE 37
3695 do_line (pfile
, keyword
)
3697 struct directive
*keyword
;
3699 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3701 long old_written
= CPP_WRITTEN (pfile
);
3702 enum file_change_code file_change
= same_file
;
3703 enum cpp_token token
;
3706 token
= get_directive_token (pfile
);
3708 if (token
!= CPP_NUMBER
3709 || !isdigit(pfile
->token_buffer
[old_written
]))
3711 cpp_error (pfile
, "invalid format `#line' command");
3712 goto bad_line_directive
;
3715 /* The Newline at the end of this line remains to be processed.
3716 To put the next line at the specified line number,
3717 we must store a line number now that is one less. */
3718 new_lineno
= atoi (pfile
->token_buffer
+ old_written
) - 1;
3719 CPP_SET_WRITTEN (pfile
, old_written
);
3721 /* NEW_LINENO is one less than the actual line number here. */
3722 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3723 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3725 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3726 if (PEEKC() && !is_space
[PEEKC()]) {
3727 cpp_error (pfile
, "invalid format `#line' command");
3728 goto bad_line_directive
;
3732 token
= get_directive_token (pfile
);
3734 if (token
== CPP_STRING
) {
3735 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3737 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3738 HASHNODE
*hp
, **hash_bucket
;
3743 /* Turn the file name, which is a character string literal,
3744 into a null-terminated string. Do this in place. */
3745 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3746 if (end_name
== NULL
)
3748 cpp_error (pfile
, "invalid format `#line' command");
3749 goto bad_line_directive
;
3752 fname_length
= end_name
- fname
;
3754 num_start
= CPP_WRITTEN (pfile
);
3755 token
= get_directive_token (pfile
);
3756 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3757 p
= pfile
->token_buffer
+ num_start
;
3758 if (CPP_PEDANTIC (pfile
))
3759 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3761 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3763 cpp_error (pfile
, "invalid format `#line' command");
3764 goto bad_line_directive
;
3767 file_change
= enter_file
;
3769 file_change
= leave_file
;
3771 ip
->system_header_p
= 1;
3772 else /* if (*p == 4) */
3773 ip
->system_header_p
= 2;
3775 CPP_SET_WRITTEN (pfile
, num_start
);
3776 token
= get_directive_token (pfile
);
3777 p
= pfile
->token_buffer
+ num_start
;
3778 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3779 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3780 token
= get_directive_token (pfile
);
3782 if (token
!= CPP_VSPACE
) {
3783 cpp_error (pfile
, "invalid format `#line' command");
3784 goto bad_line_directive
;
3789 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3790 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3791 if (hp
->length
== fname_length
&&
3792 strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3793 ip
->nominal_fname
= hp
->value
.cpval
;
3797 /* Didn't find it; cons up a new one. */
3798 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3799 hp
->next
= *hash_bucket
;
3802 hp
->length
= fname_length
;
3803 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3804 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3807 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3808 cpp_error (pfile
, "invalid format `#line' command");
3809 goto bad_line_directive
;
3812 ip
->lineno
= new_lineno
;
3814 skip_rest_of_line (pfile
);
3815 CPP_SET_WRITTEN (pfile
, old_written
);
3816 output_line_command (pfile
, 0, file_change
);
3821 * remove the definition of a symbol from the symbol table.
3822 * according to un*x /lib/cpp, it is not an error to undef
3823 * something that has no definitions, so it isn't one here either.
3827 do_undef (pfile
, keyword
, buf
, limit
)
3829 struct directive
*keyword
;
3830 U_CHAR
*buf
, *limit
;
3834 U_CHAR
*orig_buf
= buf
;
3837 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3838 if (pcp_outfile
&& keyword
)
3839 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3842 SKIP_WHITE_SPACE (buf
);
3843 sym_length
= check_macro_name (pfile
, buf
, "macro");
3845 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3847 /* If we are generating additional info for debugging (with -g) we
3848 need to pass through all effective #undef commands. */
3849 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3850 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3851 if (hp
->type
!= T_MACRO
)
3852 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3856 if (CPP_PEDANTIC (pfile
)) {
3858 SKIP_WHITE_SPACE (buf
);
3860 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3866 * Report an error detected by the program we are processing.
3867 * Use the text of the line in the error message.
3868 * (We use error because it prints the filename & line#.)
3872 do_error (pfile
, keyword
, buf
, limit
)
3874 struct directive
*keyword
;
3875 U_CHAR
*buf
, *limit
;
3877 int length
= limit
- buf
;
3878 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
3879 bcopy (buf
, copy
, length
);
3881 SKIP_WHITE_SPACE (copy
);
3882 cpp_error (pfile
, "#error %s", copy
);
3887 * Report a warning detected by the program we are processing.
3888 * Use the text of the line in the warning message, then continue.
3889 * (We use error because it prints the filename & line#.)
3893 do_warning (pfile
, keyword
, buf
, limit
)
3895 struct directive
*keyword
;
3896 U_CHAR
*buf
, *limit
;
3898 int length
= limit
- buf
;
3899 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
3900 bcopy (buf
, copy
, length
);
3902 SKIP_WHITE_SPACE (copy
);
3903 cpp_warning (pfile
, "#warning %s", copy
);
3907 /* Remember the name of the current file being read from so that we can
3908 avoid ever including it again. */
3914 cpp_buffer
*ip
= NULL
;
3915 struct file_name_list
*new;
3917 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3919 if (ip
== CPP_NULL_BUFFER (pfile
))
3921 if (ip
->fname
!= NULL
)
3926 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3927 new->next
= pfile
->dont_repeat_files
;
3928 pfile
->dont_repeat_files
= new;
3929 new->fname
= savestring (ip
->fname
);
3930 new->control_macro
= 0;
3931 new->got_name_map
= 0;
3932 new->c_system_include_path
= 0;
3937 /* #ident has already been copied to the output file, so just ignore it. */
3940 do_ident (pfile
, keyword
, buf
, limit
)
3942 struct directive
*keyword
;
3943 U_CHAR
*buf
, *limit
;
3945 /* long old_written = CPP_WRITTEN (pfile);*/
3948 /* Allow #ident in system headers, since that's not user's fault. */
3949 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3950 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3952 /* Leave rest of line to be read by later calls to cpp_get_token. */
3957 /* #pragma and its argument line have already been copied to the output file.
3958 Just check for some recognized pragmas that need validation here. */
3961 do_pragma (pfile
, keyword
, buf
, limit
)
3963 struct directive
*keyword
;
3964 U_CHAR
*buf
, *limit
;
3966 while (*buf
== ' ' || *buf
== '\t')
3968 if (!strncmp (buf
, "once", 4)) {
3969 /* Allow #pragma once in system headers, since that's not the user's
3971 if (!CPP_BUFFER (pfile
)->system_header_p
)
3972 cpp_warning (pfile
, "`#pragma once' is obsolete");
3976 if (!strncmp (buf
, "implementation", 14)) {
3977 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3978 been included yet. */
3979 struct file_name_list
*ptr
;
3980 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
3982 SKIP_WHITE_SPACE (p
);
3983 if (*p
== '\n' || *p
!= '\"')
3987 p
= (U_CHAR
*) index (fname
, '\"');
3988 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
3990 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3991 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
3992 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
3993 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
3995 "`#pragma implementation' for `%s' appears after file is included",
4004 /* This was a fun hack, but #pragma seems to start to be useful.
4005 By failing to recognize it, we pass it through unchanged to cc1. */
4008 * the behavior of the #pragma directive is implementation defined.
4009 * this implementation defines it as follows.
4016 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
4019 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4021 execl ("/usr/games/hack", "#pragma", 0);
4022 execl ("/usr/games/rogue", "#pragma", 0);
4023 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4024 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4026 fatal ("You are in a maze of twisty compiler features, all different");
4030 /* Just ignore #sccs, on systems where we define it at all. */
4033 do_sccs (pfile
, keyword
, buf
, limit
)
4035 struct directive
*keyword
;
4036 U_CHAR
*buf
, *limit
;
4038 if (CPP_PEDANTIC (pfile
))
4039 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4044 * handle #if command by
4045 * 1) inserting special `defined' keyword into the hash table
4046 * that gets turned into 0 or 1 by special_symbol (thus,
4047 * if the luser has a symbol called `defined' already, it won't
4048 * work inside the #if command)
4049 * 2) rescan the input into a temporary output buffer
4050 * 3) pass the output buffer to the yacc parser and collect a value
4051 * 4) clean up the mess left from steps 1 and 2.
4052 * 5) call conditional_skip to skip til the next #endif (etc.),
4053 * or not, depending on the value from step 3.
4057 do_if (pfile
, keyword
, buf
, limit
)
4059 struct directive
*keyword
;
4060 U_CHAR
*buf
, *limit
;
4062 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4063 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4068 * handle a #elif directive by not changing if_stack either.
4069 * see the comment above do_else.
4073 do_elif (pfile
, keyword
, buf
, limit
)
4075 struct directive
*keyword
;
4076 U_CHAR
*buf
, *limit
;
4078 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4079 cpp_error (pfile
, "`#elif' not within a conditional");
4082 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4083 cpp_error (pfile
, "`#elif' after `#else'");
4085 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4087 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4088 && strcmp (pfile
->if_stack
->fname
,
4089 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4090 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4091 fprintf (stderr
, ")\n");
4093 pfile
->if_stack
->type
= T_ELIF
;
4096 if (pfile
->if_stack
->if_succeeded
)
4097 skip_if_group (pfile
, 0);
4099 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4101 skip_if_group (pfile
, 0);
4103 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4104 output_line_command (pfile
, 1, same_file
);
4111 * evaluate a #if expression in BUF, of length LENGTH,
4112 * then parse the result as a C expression and return the value as an int.
4115 static HOST_WIDE_INT
4116 eval_if_expression (pfile
, buf
, length
)
4121 HASHNODE
*save_defined
;
4122 HOST_WIDE_INT value
;
4123 long old_written
= CPP_WRITTEN (pfile
);
4125 save_defined
= install ("defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4126 pfile
->pcp_inside_if
= 1;
4128 value
= cpp_parse_expr (pfile
);
4129 pfile
->pcp_inside_if
= 0;
4130 delete_macro (save_defined
); /* clean up special symbol */
4132 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4138 * routine to handle ifdef/ifndef. Try to look up the symbol,
4139 * then do or don't skip to the #endif/#else/#elif depending
4140 * on what directive is actually being processed.
4144 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4146 struct directive
*keyword
;
4147 U_CHAR
*unused1
, *unused2
;
4150 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4153 enum cpp_token token
;
4154 int start_of_file
= 0;
4155 U_CHAR
*control_macro
= 0;
4156 int old_written
= CPP_WRITTEN (pfile
);
4158 /* Detect a #ifndef at start of file (not counting comments). */
4159 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4160 start_of_file
= pfile
->only_seen_white
== 2;
4162 pfile
->no_macro_expand
++;
4163 token
= get_directive_token (pfile
);
4164 pfile
->no_macro_expand
--;
4166 ident
= pfile
->token_buffer
+ old_written
;
4167 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4168 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4170 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4172 skip
= (keyword
->type
== T_IFDEF
);
4173 if (! CPP_TRADITIONAL (pfile
))
4174 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4176 else if (token
== CPP_NAME
)
4178 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4179 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4180 if (start_of_file
&& !skip
)
4182 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4183 bcopy (ident
, control_macro
, ident_length
+ 1);
4188 skip
= (keyword
->type
== T_IFDEF
);
4189 if (! CPP_TRADITIONAL (pfile
))
4190 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4193 if (!CPP_TRADITIONAL (pfile
))
4195 cpp_skip_hspace (pfile
);
4197 if (c
!= EOF
&& c
!= '\n')
4198 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4200 skip_rest_of_line (pfile
);
4204 /* Output a precondition for this macro. */
4205 if (hp
&& hp
->value
.defn
->predefined
)
4206 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4209 fprintf (pcp_outfile
, "#undef ");
4210 while (is_idchar
[*cp
]) /* Ick! */
4211 fputc (*cp
++, pcp_outfile
);
4212 putc ('\n', pcp_outfile
);
4216 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4220 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4221 If this is a #ifndef starting at the beginning of a file,
4222 CONTROL_MACRO is the macro name tested by the #ifndef.
4223 Otherwise, CONTROL_MACRO is 0. */
4226 conditional_skip (pfile
, skip
, type
, control_macro
)
4229 enum node_type type
;
4230 U_CHAR
*control_macro
;
4232 IF_STACK_FRAME
*temp
;
4234 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4235 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4237 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4239 temp
->next
= pfile
->if_stack
;
4240 temp
->control_macro
= control_macro
;
4241 pfile
->if_stack
= temp
;
4243 pfile
->if_stack
->type
= type
;
4246 skip_if_group (pfile
, 0);
4249 ++pfile
->if_stack
->if_succeeded
;
4250 output_line_command (pfile
, 1, same_file
);
4255 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4256 * leaves input ptr at the sharp sign found.
4257 * If ANY is nonzero, return at next directive of any sort.
4261 skip_if_group (pfile
, any
)
4266 int at_beg_of_line
= 1;
4267 struct directive
*kt
;
4268 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4270 U_CHAR
*beg_of_line
= bp
;
4272 register int ident_length
;
4273 U_CHAR
*ident
, *after_ident
;
4274 struct parse_marker line_start_mark
;
4276 parse_set_mark (&line_start_mark
, pfile
);
4278 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4279 static char failed
[] = "#failed\n";
4280 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4282 output_line_command (pfile
, 1, same_file
);
4286 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4288 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4289 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4290 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4292 parse_move_mark (&line_start_mark
, pfile
);
4293 if (!CPP_TRADITIONAL (pfile
))
4294 cpp_skip_hspace (pfile
);
4298 int old_written
= CPP_WRITTEN (pfile
);
4299 cpp_skip_hspace (pfile
);
4301 parse_name (pfile
, GETC());
4302 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4303 ident
= pfile
->token_buffer
+ old_written
;
4304 pfile
->limit
= ident
;
4306 if (ident_length
== 0)
4307 goto not_a_directive
;
4309 /* Handle # followed by a line number. */
4311 /* Avoid error for `###' and similar cases unless -pedantic. */
4314 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4316 IF_STACK_FRAME
*temp
;
4317 if (ident_length
== kt
->length
4318 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4320 /* If we are asked to return on next directive, do so now. */
4330 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4331 temp
->next
= pfile
->if_stack
;
4332 pfile
->if_stack
= temp
;
4334 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4336 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4337 temp
->type
= kt
->type
;
4341 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4342 validate_else (pfile
,
4343 kt
->type
== T_ELSE
? "#else" : "#endif");
4345 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4348 "`#%s' not within a conditional", kt
->name
);
4351 else if (pfile
->if_stack
== save_if_stack
)
4352 goto done
; /* found what we came for */
4354 if (kt
->type
!= T_ENDIF
)
4356 if (pfile
->if_stack
->type
== T_ELSE
)
4357 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4358 pfile
->if_stack
->type
= kt
->type
;
4362 temp
= pfile
->if_stack
;
4363 pfile
->if_stack
= temp
->next
;
4370 /* Don't let erroneous code go by. */
4371 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4372 && CPP_PEDANTIC (pfile
))
4373 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4377 /* We're in the middle of a line. Skip the rest of it. */
4384 case '/': /* possible comment */
4385 c
= skip_comment (pfile
, NULL
);
4392 old
= CPP_WRITTEN (pfile
);
4393 cpp_get_token (pfile
);
4394 CPP_SET_WRITTEN (pfile
, old
);
4397 /* Char after backslash loses its special meaning. */
4398 if (PEEKC() == '\n')
4408 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4409 static char end_failed
[] = "#endfailed\n";
4410 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4413 pfile
->only_seen_white
= 1;
4414 parse_goto_mark (&line_start_mark
, pfile
);
4415 parse_clear_mark (&line_start_mark
);
4419 * handle a #else directive. Do this by just continuing processing
4420 * without changing if_stack ; this is so that the error message
4421 * for missing #endif's etc. will point to the original #if. It
4422 * is possible that something different would be better.
4426 do_else (pfile
, keyword
, buf
, limit
)
4428 struct directive
*keyword
;
4429 U_CHAR
*buf
, *limit
;
4431 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4433 if (CPP_PEDANTIC (pfile
))
4434 validate_else (pfile
, "#else");
4435 skip_rest_of_line (pfile
);
4437 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4438 cpp_error (pfile
, "`#else' not within a conditional");
4441 /* #ifndef can't have its special treatment for containing the whole file
4442 if it has a #else clause. */
4443 pfile
->if_stack
->control_macro
= 0;
4445 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4446 cpp_error (pfile
, "`#else' after `#else'");
4447 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4448 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4449 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4450 fprintf (stderr
, ")\n");
4452 pfile
->if_stack
->type
= T_ELSE
;
4455 if (pfile
->if_stack
->if_succeeded
)
4456 skip_if_group (pfile
, 0);
4458 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4459 output_line_command (pfile
, 1, same_file
);
4465 * unstack after #endif command
4469 do_endif (pfile
, keyword
, buf
, limit
)
4471 struct directive
*keyword
;
4472 U_CHAR
*buf
, *limit
;
4474 if (CPP_PEDANTIC (pfile
))
4475 validate_else (pfile
, "#endif");
4476 skip_rest_of_line (pfile
);
4478 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4479 cpp_error (pfile
, "unbalanced `#endif'");
4482 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4483 pfile
->if_stack
= temp
->next
;
4484 if (temp
->control_macro
!= 0)
4486 /* This #endif matched a #ifndef at the start of the file.
4487 See if it is at the end of the file. */
4488 struct parse_marker start_mark
;
4491 parse_set_mark (&start_mark
, pfile
);
4495 cpp_skip_hspace (pfile
);
4500 parse_goto_mark (&start_mark
, pfile
);
4501 parse_clear_mark (&start_mark
);
4505 /* If we get here, this #endif ends a #ifndef
4506 that contains all of the file (aside from whitespace).
4507 Arrange not to include the file again
4508 if the macro that was tested is defined.
4510 Do not do this for the top-level file in a -include or any
4511 file in a -imacros. */
4515 && ! (indepth
== 1 && pfile
->no_record_file
)
4516 && ! (pfile
->no_record_file
&& no_output
))
4519 struct file_name_list
*ifile
= pfile
->all_include_files
;
4521 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4523 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4525 ifile
->control_macro
= temp
->control_macro
;
4533 output_line_command (pfile
, 1, same_file
);
4538 /* When an #else or #endif is found while skipping failed conditional,
4539 if -pedantic was specified, this is called to warn about text after
4540 the command name. P points to the first char after the command name. */
4543 validate_else (pfile
, directive
)
4548 cpp_skip_hspace (pfile
);
4550 if (c
!= EOF
&& c
!= '\n')
4552 "text following `%s' violates ANSI standard", directive
);
4555 /* Get the next token, and add it to the text in pfile->token_buffer.
4556 Return the kind of token we got. */
4559 cpp_get_token (pfile
)
4562 register int c
, c2
, c3
;
4564 long start_line
, start_column
;
4565 enum cpp_token token
;
4566 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4567 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4573 if (CPP_BUFFER (pfile
)->seen_eof
)
4575 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4582 cpp_buffer
*next_buf
4583 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4584 CPP_BUFFER (pfile
)->seen_eof
= 1;
4585 if (CPP_BUFFER (pfile
)->nominal_fname
4586 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4588 /* We're about to return from an #include file.
4589 Emit #line information now (as part of the CPP_POP) result.
4590 But the #line refers to the file we will pop to. */
4591 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4592 CPP_BUFFER (pfile
) = next_buf
;
4593 pfile
->input_stack_listing_current
= 0;
4594 output_line_command (pfile
, 0, leave_file
);
4595 CPP_BUFFER (pfile
) = cur_buffer
;
4605 struct parse_marker start_mark
;
4607 if (PEEKC () == '=')
4609 if (opts
->put_out_comments
)
4610 parse_set_mark (&start_mark
, pfile
);
4612 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4613 &start_line
, &start_column
);
4614 c
= skip_comment (pfile
, &newlines
);
4615 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4616 parse_clear_mark (&start_mark
);
4621 cpp_error_with_line (pfile
, start_line
, start_column
,
4622 "unterminated comment");
4625 c
= '/'; /* Initial letter of comment. */
4627 /* Comments are equivalent to spaces.
4628 For -traditional, a comment is equivalent to nothing. */
4629 if (opts
->put_out_comments
)
4631 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4633 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4634 int len
= pbuf
->cur
- start
;
4635 CPP_RESERVE(pfile
, 1 + len
);
4636 CPP_PUTC_Q (pfile
, c
);
4637 CPP_PUTS_Q (pfile
, start
, len
);
4638 pfile
->lineno
+= newlines
;
4639 parse_clear_mark (&start_mark
);
4642 else if (CPP_TRADITIONAL (pfile
))
4649 /* This may not work if cpp_get_token is called recursively,
4650 since many places look for horizontal space. */
4653 /* Copy the newlines into the output buffer, in order to
4654 avoid the pain of a #line every time a multiline comment
4656 CPP_RESERVE(pfile
, newlines
);
4657 while (--newlines
>= 0)
4659 CPP_PUTC_Q (pfile
, '\n');
4665 CPP_RESERVE(pfile
, 1);
4666 CPP_PUTC_Q (pfile
, ' ');
4670 if (opts
->for_lint
) {
4673 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4675 if (lintcmd
!= NULL
) {
4676 /* I believe it is always safe to emit this newline: */
4678 bcopy ("#pragma lint ", (char *) obp
, 13);
4680 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4685 bcopy (argbp
, (char *) obp
, arglen
);
4689 /* OK, now bring us back to the state we were in before we entered
4690 this branch. We need #line b/c the newline for the pragma
4691 could fuck things up. */
4692 output_line_command (pfile
, 0, same_file
);
4693 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4701 /* If this is expanding a macro definition, don't recognize
4702 preprocessor directives. */
4705 /* If this is expand_into_temp_buffer, recognize them
4706 only after an actual newline at this level,
4707 not at the beginning of the input level. */
4708 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4714 if (!pfile
->only_seen_white
)
4716 if (handle_directive (pfile
))
4717 return CPP_DIRECTIVE
;
4718 pfile
->only_seen_white
= 0;
4723 /* A single quoted string is treated like a double -- some
4724 programs (e.g., troff) are perverse this way */
4725 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4726 &start_line
, &start_column
);
4727 old_written
= CPP_WRITTEN (pfile
);
4729 CPP_PUTC (pfile
, c
);
4735 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4737 /* try harder: this string crosses a macro expansion
4738 boundary. This can happen naturally if -traditional.
4739 Otherwise, only -D can make a macro with an unmatched
4741 cpp_buffer
*next_buf
4742 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4743 (*CPP_BUFFER (pfile
)->cleanup
)
4744 (CPP_BUFFER (pfile
), pfile
);
4745 CPP_BUFFER (pfile
) = next_buf
;
4748 if (!CPP_TRADITIONAL (pfile
))
4750 cpp_error_with_line (pfile
, start_line
, start_column
,
4751 "unterminated string or character constant");
4752 if (pfile
->multiline_string_line
!= start_line
4753 && pfile
->multiline_string_line
!= 0)
4754 cpp_error_with_line (pfile
,
4755 pfile
->multiline_string_line
, -1,
4756 "possible real start of unterminated constant");
4757 pfile
->multiline_string_line
= 0;
4761 CPP_PUTC (pfile
, cc
);
4765 /* Traditionally, end of line ends a string constant with
4766 no error. So exit the loop and record the new line. */
4767 if (CPP_TRADITIONAL (pfile
))
4771 cpp_error_with_line (pfile
, start_line
, start_column
,
4772 "unterminated character constant");
4775 if (CPP_PEDANTIC (pfile
)
4776 && pfile
->multiline_string_line
== 0)
4778 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4779 "string constant runs past end of line");
4781 if (pfile
->multiline_string_line
== 0)
4782 pfile
->multiline_string_line
= start_line
;
4789 /* Backslash newline is replaced by nothing at all. */
4790 CPP_ADJUST_WRITTEN (pfile
, -1);
4795 /* ANSI stupidly requires that in \\ the second \
4796 is *not* prevented from combining with a newline. */
4799 CPP_PUTC (pfile
, cc
);
4811 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4812 CPP_PWRITTEN (pfile
));
4813 pfile
->only_seen_white
= 0;
4814 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4817 if (!opts
->dollars_in_ident
)
4822 if (opts
->cplusplus
&& PEEKC () == ':')
4831 if (c2
== c
|| c2
== '=')
4841 if (PEEKC () == '=')
4848 if (c2
== '-' && opts
->chill
)
4850 /* Chill style comment */
4851 if (opts
->put_out_comments
)
4852 parse_set_mark (&start_mark
, pfile
);
4853 FORWARD(1); /* Skip second '-'. */
4861 /* Don't consider final '\n' to be part of comment. */
4867 goto return_comment
;
4869 if (c2
== '-' || c2
== '=' || c2
== '>')
4874 if (pfile
->parsing_include_directive
)
4878 CPP_PUTC (pfile
, c
);
4883 if (c
== '\n' || c
== EOF
)
4886 "missing '>' in `#include <FILENAME>'");
4892 /* else fall through */
4901 CPP_RESERVE (pfile
, 4);
4902 CPP_PUTC (pfile
, c
);
4903 CPP_PUTC (pfile
, c2
);
4907 CPP_PUTC_Q (pfile
, GETC ());
4908 CPP_NUL_TERMINATE_Q (pfile
);
4909 pfile
->only_seen_white
= 0;
4913 if (CPP_BUFFER (pfile
)->has_escapes
)
4918 if (pfile
->output_escapes
)
4919 CPP_PUTS (pfile
, "@-", 2);
4920 parse_name (pfile
, GETC ());
4923 else if (is_space
[c
])
4925 CPP_RESERVE (pfile
, 2);
4926 if (pfile
->output_escapes
)
4927 CPP_PUTC_Q (pfile
, '@');
4928 CPP_PUTC_Q (pfile
, c
);
4932 if (pfile
->output_escapes
)
4934 CPP_PUTS (pfile
, "@@", 2);
4944 CPP_RESERVE(pfile
, 2);
4945 CPP_PUTC_Q (pfile
, '.');
4949 /* FIXME - misses the case "..\\\n." */
4950 if (c2
== '.' && PEEKN(1) == '.')
4952 CPP_RESERVE(pfile
, 4);
4953 CPP_PUTC_Q (pfile
, '.');
4954 CPP_PUTC_Q (pfile
, '.');
4955 CPP_PUTC_Q (pfile
, '.');
4957 CPP_NUL_TERMINATE_Q (pfile
);
4958 pfile
->only_seen_white
= 0;
4965 pfile
->only_seen_white
= 0;
4967 CPP_RESERVE(pfile
, 3);
4968 CPP_PUTC_Q (pfile
, c
);
4969 CPP_PUTC_Q (pfile
, GETC ());
4970 CPP_NUL_TERMINATE_Q (pfile
);
4976 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4978 CPP_PUTC (pfile
, c
);
4984 case '0': case '1': case '2': case '3': case '4':
4985 case '5': case '6': case '7': case '8': case '9':
4990 CPP_RESERVE (pfile
, 2);
4991 CPP_PUTC_Q (pfile
, c
);
4996 if (!is_idchar
[c
] && c
!= '.'
4997 && ((c2
!= 'e' && c2
!= 'E') || (c
!= '+' && c
!= '-')))
5002 CPP_NUL_TERMINATE_Q (pfile
);
5003 pfile
->only_seen_white
= 0;
5005 case 'b': case 'c': case 'd': case 'h': case 'o':
5006 case 'B': case 'C': case 'D': case 'H': case 'O':
5007 if (opts
->chill
&& PEEKC () == '\'')
5009 pfile
->only_seen_white
= 0;
5010 CPP_RESERVE (pfile
, 2);
5011 CPP_PUTC_Q (pfile
, c
);
5012 CPP_PUTC_Q (pfile
, '\'');
5018 goto chill_number_eof
;
5021 if (c
== '\\' && PEEKC() == '\n')
5028 CPP_PUTC (pfile
, c
);
5032 CPP_RESERVE (pfile
, 2);
5033 CPP_PUTC_Q (pfile
, c
);
5034 CPP_NUL_TERMINATE_Q (pfile
);
5041 CPP_NUL_TERMINATE (pfile
);
5048 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5049 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5050 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5051 case 'x': case 'y': case 'z':
5052 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5053 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5054 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5059 unsigned char *ident
;
5060 int before_name_written
= CPP_WRITTEN (pfile
);
5062 parse_name (pfile
, c
);
5063 pfile
->only_seen_white
= 0;
5064 if (pfile
->no_macro_expand
)
5066 ident
= pfile
->token_buffer
+ before_name_written
;
5067 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5068 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5071 if (hp
->type
== T_DISABLED
)
5073 if (pfile
->output_escapes
)
5074 { /* Return "@-IDENT", followed by '\0'. */
5076 CPP_RESERVE (pfile
, 3);
5077 ident
= pfile
->token_buffer
+ before_name_written
;
5078 CPP_ADJUST_WRITTEN (pfile
, 2);
5079 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5086 /* If macro wants an arglist, verify that a '(' follows.
5087 first skip all whitespace, copying it to the output
5088 after the macro name. Then, if there is no '(',
5089 decide this is not a macro call and leave things that way. */
5090 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5092 struct parse_marker macro_mark
;
5094 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5096 cpp_buffer
*next_buf
;
5097 cpp_skip_hspace (pfile
);
5098 if (PEEKC () != EOF
)
5100 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5101 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5102 CPP_BUFFER (pfile
) = next_buf
;
5104 parse_set_mark (¯o_mark
, pfile
);
5107 cpp_skip_hspace (pfile
);
5109 is_macro_call
= c
== '(';
5115 parse_goto_mark (¯o_mark
, pfile
);
5116 parse_clear_mark (¯o_mark
);
5120 /* This is now known to be a macro call. */
5122 /* it might not actually be a macro. */
5123 if (hp
->type
!= T_MACRO
) {
5124 int xbuf_len
; U_CHAR
*xbuf
;
5125 CPP_SET_WRITTEN (pfile
, before_name_written
);
5126 special_symbol (hp
, pfile
);
5127 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5128 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5129 CPP_SET_WRITTEN (pfile
, before_name_written
);
5130 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5131 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5135 /* Expand the macro, reading arguments as needed,
5136 and push the expansion on the input stack. */
5137 macroexpand (pfile
, hp
);
5138 CPP_SET_WRITTEN (pfile
, before_name_written
);
5141 /* An extra "@ " is added to the end of a macro expansion
5142 to prevent accidental token pasting. We prefer to avoid
5143 unneeded extra spaces (for the sake of cpp-using tools like
5144 imake). Here we remove the space if it is safe to do so. */
5145 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5146 && pfile
->buffer
->rlimit
[-2] == '@'
5147 && pfile
->buffer
->rlimit
[-1] == ' ')
5149 int c1
= pfile
->buffer
->rlimit
[-3];
5150 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5151 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5152 pfile
->buffer
->rlimit
-= 2;
5157 case ' ': case '\t': case '\v': case '\r':
5160 CPP_PUTC (pfile
, c
);
5162 if (c
== EOF
|| !is_hor_space
[c
])
5176 CPP_PUTC (pfile
, c
);
5177 if (pfile
->only_seen_white
== 0)
5178 pfile
->only_seen_white
= 1;
5180 output_line_command (pfile
, 1, same_file
);
5183 case '(': token
= CPP_LPAREN
; goto char1
;
5184 case ')': token
= CPP_RPAREN
; goto char1
;
5185 case '{': token
= CPP_LBRACE
; goto char1
;
5186 case '}': token
= CPP_RBRACE
; goto char1
;
5187 case ',': token
= CPP_COMMA
; goto char1
;
5188 case ';': token
= CPP_SEMICOLON
; goto char1
;
5194 pfile
->only_seen_white
= 0;
5195 CPP_PUTC (pfile
, c
);
5201 /* Like cpp_get_token, but skip spaces and comments. */
5204 cpp_get_non_space_token (pfile
)
5207 int old_written
= CPP_WRITTEN (pfile
);
5210 enum cpp_token token
= cpp_get_token (pfile
);
5211 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5212 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5214 CPP_SET_WRITTEN (pfile
, old_written
);
5218 /* Parse an identifier starting with C. */
5221 parse_name (pfile
, c
)
5222 cpp_reader
*pfile
; int c
;
5228 if (c
== '\\' && PEEKC() == '\n')
5237 if (c
== '$' && CPP_PEDANTIC (pfile
))
5238 cpp_pedwarn ("`$' in identifier");
5240 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5241 CPP_PUTC_Q (pfile
, c
);
5246 CPP_NUL_TERMINATE_Q (pfile
);
5251 /* Maintain and search list of included files, for #import. */
5253 /* Hash a file name for import_hash_table. */
5261 while (*f
) val
+= *f
++;
5262 return (val
%IMPORT_HASH_SIZE
);
5265 /* Search for file FILENAME in import_hash_table.
5266 Return -2 if found, either a matching name or a matching inode.
5267 Otherwise, open the file and return a file descriptor if successful
5268 or -1 if unsuccessful. */
5271 lookup_import (pfile
, filename
, searchptr
)
5274 struct file_name_list
*searchptr
;
5276 struct import_file
*i
;
5282 hashval
= import_hash (filename
);
5284 /* Attempt to find file in list of already included files */
5285 i
= pfile
->import_hash_table
[hashval
];
5288 if (!strcmp (filename
, i
->name
))
5289 return -2; /* return found */
5292 /* Open it and try a match on inode/dev */
5293 fd
= open_include_file (pfile
, filename
, searchptr
);
5297 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5298 i
= pfile
->import_hash_table
[h
];
5300 /* Compare the inode and the device.
5301 Supposedly on some systems the inode is not a scalar. */
5302 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5303 && i
->dev
== sb
.st_dev
) {
5305 return -2; /* return found */
5310 return fd
; /* Not found, return open file */
5313 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5316 add_import (pfile
, fd
, fname
)
5321 struct import_file
*i
;
5325 hashval
= import_hash (fname
);
5327 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5328 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5329 strcpy (i
->name
, fname
);
5330 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5332 i
->next
= pfile
->import_hash_table
[hashval
];
5333 pfile
->import_hash_table
[hashval
] = i
;
5336 /* The file_name_map structure holds a mapping of file names for a
5337 particular directory. This mapping is read from the file named
5338 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5339 map filenames on a file system with severe filename restrictions,
5340 such as DOS. The format of the file name map file is just a series
5341 of lines with two tokens on each line. The first token is the name
5342 to map, and the second token is the actual name to use. */
5344 struct file_name_map
5346 struct file_name_map
*map_next
;
5351 #define FILE_NAME_MAP_FILE "header.gcc"
5353 /* Read a space delimited string of unlimited length from a stdio
5357 read_filename_string (ch
, f
)
5365 set
= alloc
= xmalloc (len
+ 1);
5369 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5371 if (set
- alloc
== len
)
5374 alloc
= xrealloc (alloc
, len
+ 1);
5375 set
= alloc
+ len
/ 2;
5385 /* This structure holds a linked list of file name maps, one per directory. */
5387 struct file_name_map_list
5389 struct file_name_map_list
*map_list_next
;
5390 char *map_list_name
;
5391 struct file_name_map
*map_list_map
;
5394 /* Read the file name map file for DIRNAME. */
5396 static struct file_name_map
*
5397 read_name_map (pfile
, dirname
)
5401 register struct file_name_map_list
*map_list_ptr
;
5405 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5406 map_list_ptr
= map_list_ptr
->map_list_next
)
5407 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5408 return map_list_ptr
->map_list_map
;
5410 map_list_ptr
= ((struct file_name_map_list
*)
5411 xmalloc (sizeof (struct file_name_map_list
)));
5412 map_list_ptr
->map_list_name
= savestring (dirname
);
5413 map_list_ptr
->map_list_map
= NULL
;
5415 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5416 strcpy (name
, dirname
);
5419 strcat (name
, FILE_NAME_MAP_FILE
);
5420 f
= fopen (name
, "r");
5422 map_list_ptr
->map_list_map
= NULL
;
5426 int dirlen
= strlen (dirname
);
5428 while ((ch
= getc (f
)) != EOF
)
5431 struct file_name_map
*ptr
;
5435 from
= read_filename_string (ch
, f
);
5436 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5438 to
= read_filename_string (ch
, f
);
5440 ptr
= ((struct file_name_map
*)
5441 xmalloc (sizeof (struct file_name_map
)));
5442 ptr
->map_from
= from
;
5444 /* Make the real filename absolute. */
5449 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5450 strcpy (ptr
->map_to
, dirname
);
5451 ptr
->map_to
[dirlen
] = '/';
5452 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5456 ptr
->map_next
= map_list_ptr
->map_list_map
;
5457 map_list_ptr
->map_list_map
= ptr
;
5459 while ((ch
= getc (f
)) != '\n')
5466 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5467 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5469 return map_list_ptr
->map_list_map
;
5472 /* Try to open include file FILENAME. SEARCHPTR is the directory
5473 being tried from the include file search path. This function maps
5474 filenames on file systems based on information read by
5478 open_include_file (pfile
, filename
, searchptr
)
5481 struct file_name_list
*searchptr
;
5483 register struct file_name_map
*map
;
5484 register char *from
;
5487 if (searchptr
&& ! searchptr
->got_name_map
)
5489 searchptr
->name_map
= read_name_map (pfile
,
5491 ? searchptr
->fname
: ".");
5492 searchptr
->got_name_map
= 1;
5495 /* First check the mapping for the directory we are using. */
5496 if (searchptr
&& searchptr
->name_map
)
5499 if (searchptr
->fname
)
5500 from
+= strlen (searchptr
->fname
) + 1;
5501 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5503 if (! strcmp (map
->map_from
, from
))
5505 /* Found a match. */
5506 return open (map
->map_to
, O_RDONLY
, 0666);
5511 /* Try to find a mapping file for the particular directory we are
5512 looking in. Thus #include <sys/types.h> will look up sys/types.h
5513 in /usr/include/header.gcc and look up types.h in
5514 /usr/include/sys/header.gcc. */
5515 p
= rindex (filename
, '/');
5520 && strlen (searchptr
->fname
) == p
- filename
5521 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5523 /* FILENAME is in SEARCHPTR, which we've already checked. */
5524 return open (filename
, O_RDONLY
, 0666);
5534 dir
= (char *) alloca (p
- filename
+ 1);
5535 bcopy (filename
, dir
, p
- filename
);
5536 dir
[p
- filename
] = '\0';
5539 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5540 if (! strcmp (map
->map_from
, from
))
5541 return open (map
->map_to
, O_RDONLY
, 0666);
5543 return open (filename
, O_RDONLY
, 0666);
5546 /* Process the contents of include file FNAME, already open on descriptor F,
5548 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5549 "system" include directories (as decided by the `is_system_include'
5551 DIRPTR is the link in the dir path through which this file was found,
5552 or 0 if the file name was absolute or via the current directory.
5553 Return 1 on success, 0 on failure.
5555 The caller is responsible for the cpp_push_buffer. */
5558 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5562 int system_header_p
;
5563 struct file_name_list
*dirptr
;
5569 cpp_buffer
*fp
; /* For input stack frame */
5570 int missing_newline
= 0;
5572 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
5574 cpp_perror_with_name (pfile
, fname
);
5576 cpp_pop_buffer (pfile
);
5580 fp
= CPP_BUFFER (pfile
);
5581 fp
->nominal_fname
= fp
->fname
= fname
;
5586 fp
->system_header_p
= system_header_p
;
5589 fp
->cleanup
= file_cleanup
;
5591 if (S_ISREG (st_mode
)) {
5592 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5593 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5596 /* Read the file contents, knowing that st_size is an upper bound
5597 on the number of bytes we can read. */
5598 length
= safe_read (f
, fp
->buf
, st_size
);
5599 fp
->rlimit
= fp
->buf
+ length
;
5600 if (length
< 0) goto nope
;
5602 else if (S_ISDIR (st_mode
)) {
5603 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5607 /* Cannot count its file size before reading.
5608 First read the entire file into heap and
5609 copy them into buffer on stack. */
5614 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5617 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5619 goto nope
; /* error! */
5621 if (st_size
!= bsize
)
5622 break; /* End of file */
5624 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5630 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5631 /* Backslash-newline at end is not good enough. */
5632 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5633 fp
->buf
[length
++] = '\n';
5635 missing_newline
= 1;
5638 fp
->buf
[length
] = '\0';
5639 fp
->rlimit
= fp
->buf
+ length
;
5641 /* Close descriptor now, so nesting does not use lots of descriptors. */
5644 /* Must do this before calling trigraph_pcp, so that the correct file name
5645 will be printed in warning messages. */
5647 pfile
->input_stack_listing_current
= 0;
5657 if (missing_newline
)
5660 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5661 pedwarn ("file does not end in newline");
5664 input_file_stack_tick
++;
5671 cpp_perror_with_name (pfile
, fname
);
5677 /* This is called after options have been processed.
5678 * Check options for consistency, and setup for processing input
5679 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5680 * Return 1 on succes, 0 on failure.
5684 cpp_start_read (pfile
, fname
)
5688 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5689 struct cpp_pending
*pend
;
5694 /* The code looks at the defaults through this pointer, rather than through
5695 the constant structure above. This pointer gets changed if an environment
5696 variable specifies other defaults. */
5697 struct default_include
*include_defaults
= include_defaults_array
;
5699 /* Add dirs from CPATH after dirs from -I. */
5700 /* There seems to be confusion about what CPATH should do,
5701 so for the moment it is not documented. */
5702 /* Some people say that CPATH should replace the standard include dirs,
5703 but that seems pointless: it comes before them, so it overrides them
5705 p
= (char *) getenv ("CPATH");
5706 if (p
!= 0 && ! opts
->no_standard_includes
)
5707 path_include (pfile
, p
);
5709 /* Now that dollars_in_ident is known, initialize is_idchar. */
5710 initialize_char_syntax (opts
);
5712 /* Do partial setup of input buffer for the sake of generating
5713 early #line directives (when -g is in effect). */
5714 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5717 if (opts
->in_fname
== NULL
)
5718 opts
->in_fname
= "";
5719 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5722 /* Install __LINE__, etc. Must follow initialize_char_syntax
5723 and option processing. */
5724 initialize_builtins (pfile
);
5726 /* Do standard #defines and assertions
5727 that identify system and machine type. */
5729 if (!opts
->inhibit_predefs
) {
5730 char *p
= (char *) alloca (strlen (predefs
) + 1);
5731 strcpy (p
, predefs
);
5734 while (*p
== ' ' || *p
== '\t')
5736 /* Handle -D options. */
5737 if (p
[0] == '-' && p
[1] == 'D') {
5739 while (*p
&& *p
!= ' ' && *p
!= '\t')
5743 if (opts
->debug_output
)
5744 output_line_command (pfile
, 0, same_file
);
5745 cpp_define (pfile
, q
);
5746 while (*p
== ' ' || *p
== '\t')
5748 } else if (p
[0] == '-' && p
[1] == 'A') {
5749 /* Handle -A options (assertions). */
5758 past_name
= assertion
;
5759 /* Locate end of name. */
5760 while (*past_name
&& *past_name
!= ' '
5761 && *past_name
!= '\t' && *past_name
!= '(')
5763 /* Locate `(' at start of value. */
5765 while (*value
&& (*value
== ' ' || *value
== '\t'))
5767 if (*value
++ != '(')
5769 while (*value
&& (*value
== ' ' || *value
== '\t'))
5772 /* Locate end of value. */
5773 while (*past_value
&& *past_value
!= ' '
5774 && *past_value
!= '\t' && *past_value
!= ')')
5776 termination
= past_value
;
5777 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5779 if (*termination
++ != ')')
5781 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5783 /* Temporarily null-terminate the value. */
5784 save_char
= *termination
;
5785 *termination
= '\0';
5786 /* Install the assertion. */
5787 make_assertion (pfile
, "-A", assertion
);
5788 *termination
= (char) save_char
;
5790 while (*p
== ' ' || *p
== '\t')
5798 /* Now handle the command line options. */
5800 /* Do -U's, -D's and -A's in the order they were seen. */
5801 /* First reverse the list. */
5802 opts
->pending
= nreverse_pending (opts
->pending
);
5804 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5806 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5808 switch (pend
->cmd
[1])
5811 if (opts
->debug_output
)
5812 output_line_command (pfile
, 0, same_file
);
5813 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5816 if (opts
->debug_output
)
5817 output_line_command (pfile
, 0, same_file
);
5818 cpp_define (pfile
, pend
->arg
);
5821 make_assertion (pfile
, "-A", pend
->arg
);
5827 opts
->done_initializing
= 1;
5829 { /* Read the appropriate environment variable and if it exists
5830 replace include_defaults with the listed path. */
5832 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5835 epath
= getenv ("C_INCLUDE_PATH");
5838 epath
= getenv ("CPLUS_INCLUDE_PATH");
5841 epath
= getenv ("OBJC_INCLUDE_PATH");
5844 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
5847 /* If the environment var for this language is set,
5848 add to the default list of include directories. */
5850 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5852 char *startp
, *endp
;
5854 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5855 if (*startp
== PATH_SEPARATOR
)
5858 = (struct default_include
*) xmalloc ((num_dirs
5859 * sizeof (struct default_include
))
5860 + sizeof (include_defaults_array
));
5861 startp
= endp
= epath
;
5864 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5865 if ((*endp
== PATH_SEPARATOR
)
5867 strncpy (nstore
, startp
, endp
-startp
);
5869 strcpy (nstore
, ".");
5871 nstore
[endp
-startp
] = '\0';
5873 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5874 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5875 include_defaults
[num_dirs
].cxx_aware
= 1;
5879 endp
= startp
= endp
+ 1;
5883 /* Put the usual defaults back in at the end. */
5884 bcopy ((char *) include_defaults_array
,
5885 (char *) &include_defaults
[num_dirs
],
5886 sizeof (include_defaults_array
));
5890 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5891 opts
->first_system_include
= opts
->before_system
;
5893 /* Unless -fnostdinc,
5894 tack on the standard include file dirs to the specified list */
5895 if (!opts
->no_standard_includes
) {
5896 struct default_include
*p
= include_defaults
;
5897 char *specd_prefix
= opts
->include_prefix
;
5898 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5899 int default_len
= 0;
5900 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5901 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5902 default_len
= strlen (default_prefix
) - 7;
5903 default_prefix
[default_len
] = 0;
5905 /* Search "translated" versions of GNU directories.
5906 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5907 if (specd_prefix
!= 0 && default_len
!= 0)
5908 for (p
= include_defaults
; p
->fname
; p
++) {
5909 /* Some standard dirs are only for C++. */
5911 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5912 /* Does this dir start with the prefix? */
5913 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5914 /* Yes; change prefix and add to search list. */
5915 struct file_name_list
*new
5916 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5917 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5918 char *str
= (char *) xmalloc (this_len
+ 1);
5919 strcpy (str
, specd_prefix
);
5920 strcat (str
, p
->fname
+ default_len
);
5922 new->control_macro
= 0;
5923 new->c_system_include_path
= !p
->cxx_aware
;
5924 new->got_name_map
= 0;
5925 append_include_chain (pfile
, new, new);
5926 if (opts
->first_system_include
== 0)
5927 opts
->first_system_include
= new;
5931 /* Search ordinary names for GNU include directories. */
5932 for (p
= include_defaults
; p
->fname
; p
++) {
5933 /* Some standard dirs are only for C++. */
5935 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5936 struct file_name_list
*new
5937 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5938 new->control_macro
= 0;
5939 new->c_system_include_path
= !p
->cxx_aware
;
5940 new->fname
= p
->fname
;
5941 new->got_name_map
= 0;
5942 append_include_chain (pfile
, new, new);
5943 if (opts
->first_system_include
== 0)
5944 opts
->first_system_include
= new;
5949 /* Tack the after_include chain at the end of the include chain. */
5950 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5951 if (opts
->first_system_include
== 0)
5952 opts
->first_system_include
= opts
->after_include
;
5954 /* With -v, print the list of dirs to search. */
5955 if (opts
->verbose
) {
5956 struct file_name_list
*p
;
5957 fprintf (stderr
, "#include \"...\" search starts here:\n");
5958 for (p
= opts
->include
; p
; p
= p
->next
) {
5959 if (p
== opts
->first_bracket_include
)
5960 fprintf (stderr
, "#include <...> search starts here:\n");
5961 fprintf (stderr
, " %s\n", p
->fname
);
5963 fprintf (stderr
, "End of search list.\n");
5966 /* Scan the -imacros files before the main input.
5967 Much like #including them, but with no_output set
5968 so that only their macro definitions matter. */
5970 opts
->no_output
++; pfile
->no_record_file
++;
5971 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5973 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
5975 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5978 cpp_perror_with_name (pfile
, pend
->arg
);
5981 if (!cpp_push_buffer (pfile
, NULL
, 0))
5983 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
5984 cpp_scan_buffer (pfile
);
5987 opts
->no_output
--; pfile
->no_record_file
--;
5989 /* Copy the entire contents of the main input file into
5990 the stacked input buffer previously allocated for it. */
5991 if (fname
== NULL
|| *fname
== 0) {
5994 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
5995 cpp_pfatal_with_name (pfile
, fname
);
5997 /* -MG doesn't select the form of output and must be specified with one of
5998 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5999 inhibit compilation. */
6000 if (opts
->print_deps_missing_files
6001 && (opts
->print_deps
== 0 || !opts
->no_output
))
6003 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
6007 /* Either of two environment variables can specify output of deps.
6008 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6009 where OUTPUT_FILE is the file to write deps info to
6010 and DEPS_TARGET is the target to mention in the deps. */
6012 if (opts
->print_deps
== 0
6013 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6014 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6015 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6021 spec
= getenv ("SUNPRO_DEPENDENCIES");
6022 opts
->print_deps
= 2;
6025 opts
->print_deps
= 1;
6028 /* Find the space before the DEPS_TARGET, if there is one. */
6029 /* This should use index. (mrs) */
6030 while (*s
!= 0 && *s
!= ' ') s
++;
6033 opts
->deps_target
= s
+ 1;
6034 output_file
= (char *) xmalloc (s
- spec
+ 1);
6035 bcopy (spec
, output_file
, s
- spec
);
6036 output_file
[s
- spec
] = 0;
6040 opts
->deps_target
= 0;
6044 opts
->deps_file
= output_file
;
6045 opts
->print_deps_append
= 1;
6048 /* For -M, print the expected object file name
6049 as the target of this Make-rule. */
6050 if (opts
->print_deps
)
6052 pfile
->deps_allocated_size
= 200;
6053 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6054 pfile
->deps_buffer
[0] = 0;
6055 pfile
->deps_size
= 0;
6056 pfile
->deps_column
= 0;
6058 if (opts
->deps_target
)
6059 deps_output (pfile
, opts
->deps_target
, ':');
6060 else if (*opts
->in_fname
== 0)
6061 deps_output (pfile
, "-", ':');
6067 /* Discard all directory prefixes from filename. */
6068 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6069 #ifdef DIR_SEPARATOR
6070 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6077 /* Copy remainder to mungable area. */
6078 p
= (char *) alloca (strlen(q
) + 8);
6081 /* Output P, but remove known suffixes. */
6085 && p
[len
- 2] == '.'
6086 && index("cCsSm", p
[len
- 1]))
6089 && p
[len
- 3] == '.'
6090 && p
[len
- 2] == 'c'
6091 && p
[len
- 1] == 'c')
6094 && p
[len
- 4] == '.'
6095 && p
[len
- 3] == 'c'
6096 && p
[len
- 2] == 'x'
6097 && p
[len
- 1] == 'x')
6100 && p
[len
- 4] == '.'
6101 && p
[len
- 3] == 'c'
6102 && p
[len
- 2] == 'p'
6103 && p
[len
- 1] == 'p')
6106 /* Supply our own suffix. */
6113 deps_output (pfile
, p
, ':');
6114 deps_output (pfile
, opts
->in_fname
, ' ');
6119 /* Make sure data ends with a newline. And put a null after it. */
6121 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6122 /* Backslash-newline at end is not good enough. */
6123 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6124 fp
->buf
[fp
->length
++] = '\n';
6125 missing_newline
= 1;
6127 fp
->buf
[fp
->length
] = '\0';
6129 /* Unless inhibited, convert trigraphs in the input. */
6135 /* Scan the -include files before the main input.
6136 We push these in reverse order, so that the first one is handled first. */
6138 pfile
->no_record_file
++;
6139 opts
->pending
= nreverse_pending (opts
->pending
);
6140 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6142 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6144 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6147 cpp_perror_with_name (pfile
, pend
->arg
);
6150 if (!cpp_push_buffer (pfile
, NULL
, 0))
6152 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6155 pfile
->no_record_file
--;
6157 /* Free the pending list. */
6158 for (pend
= opts
->pending
; pend
; )
6160 struct cpp_pending
*next
= pend
->next
;
6164 opts
->pending
= NULL
;
6167 /* Scan the input, processing macros and directives. */
6169 rescan (&outbuf
, 0);
6171 if (missing_newline
)
6174 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6175 pedwarn ("file does not end in newline");
6178 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6179 output_line_command (pfile
, 0, same_file
);
6184 cpp_reader_init (pfile
)
6187 bzero ((char *) pfile
, sizeof (cpp_reader
));
6188 pfile
->get_token
= cpp_get_token
;
6190 pfile
->token_buffer_size
= 200;
6191 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6192 CPP_SET_WRITTEN (pfile
, 0);
6194 pfile
->system_include_depth
= 0;
6195 pfile
->dont_repeat_files
= 0;
6196 pfile
->all_include_files
= 0;
6197 pfile
->max_include_len
= 0;
6198 pfile
->timebuf
= NULL
;
6199 pfile
->only_seen_white
= 1;
6200 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6203 static struct cpp_pending
*
6204 nreverse_pending (list
)
6205 struct cpp_pending
*list
;
6208 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6209 for (pend
= list
; pend
; pend
= next
)
6219 push_pending (pfile
, cmd
, arg
)
6224 struct cpp_pending
*pend
6225 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6228 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6229 CPP_OPTIONS (pfile
)->pending
= pend
;
6232 /* Handle command-line options in (argc, argv).
6233 Can be called multiple times, to handle multiple sets of options.
6234 Returns if an unrecognized option is seen.
6235 Returns number of handled arguments. */
6238 cpp_handle_options (pfile
, argc
, argv
)
6244 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6245 for (i
= 0; i
< argc
; i
++) {
6246 if (argv
[i
][0] != '-') {
6247 if (opts
->out_fname
!= NULL
)
6249 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6252 else if (opts
->in_fname
!= NULL
)
6253 opts
->out_fname
= argv
[i
];
6255 opts
->in_fname
= argv
[i
];
6257 switch (argv
[i
][1]) {
6260 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6263 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6267 if (!strcmp (argv
[i
], "-include")
6268 || !strcmp (argv
[i
], "-imacros")) {
6270 goto missing_filename
;
6272 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6274 if (!strcmp (argv
[i
], "-iprefix")) {
6276 goto missing_filename
;
6278 opts
->include_prefix
= argv
[++i
];
6280 if (!strcmp (argv
[i
], "-ifoutput")) {
6281 opts
->output_conditionals
= 1;
6283 if (!strcmp (argv
[i
], "-isystem")) {
6284 struct file_name_list
*dirtmp
;
6287 goto missing_filename
;
6289 dirtmp
= (struct file_name_list
*)
6290 xmalloc (sizeof (struct file_name_list
));
6292 dirtmp
->control_macro
= 0;
6293 dirtmp
->c_system_include_path
= 1;
6294 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6295 strcpy (dirtmp
->fname
, argv
[++i
]);
6296 dirtmp
->got_name_map
= 0;
6298 if (opts
->before_system
== 0)
6299 opts
->before_system
= dirtmp
;
6301 opts
->last_before_system
->next
= dirtmp
;
6302 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6304 /* Add directory to end of path for includes,
6305 with the default prefix at the front of its name. */
6306 if (!strcmp (argv
[i
], "-iwithprefix")) {
6307 struct file_name_list
*dirtmp
;
6310 if (opts
->include_prefix
!= 0)
6311 prefix
= opts
->include_prefix
;
6313 prefix
= savestring (GCC_INCLUDE_DIR
);
6314 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6315 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6316 prefix
[strlen (prefix
) - 7] = 0;
6319 dirtmp
= (struct file_name_list
*)
6320 xmalloc (sizeof (struct file_name_list
));
6321 dirtmp
->next
= 0; /* New one goes on the end */
6322 dirtmp
->control_macro
= 0;
6323 dirtmp
->c_system_include_path
= 0;
6325 goto missing_dirname
;
6327 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6328 + strlen (prefix
) + 1);
6329 strcpy (dirtmp
->fname
, prefix
);
6330 strcat (dirtmp
->fname
, argv
[++i
]);
6331 dirtmp
->got_name_map
= 0;
6333 if (opts
->after_include
== 0)
6334 opts
->after_include
= dirtmp
;
6336 opts
->last_after_include
->next
= dirtmp
;
6337 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6339 /* Add directory to main path for includes,
6340 with the default prefix at the front of its name. */
6341 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6342 struct file_name_list
*dirtmp
;
6345 if (opts
->include_prefix
!= 0)
6346 prefix
= opts
->include_prefix
;
6348 prefix
= savestring (GCC_INCLUDE_DIR
);
6349 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6350 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6351 prefix
[strlen (prefix
) - 7] = 0;
6354 dirtmp
= (struct file_name_list
*)
6355 xmalloc (sizeof (struct file_name_list
));
6356 dirtmp
->next
= 0; /* New one goes on the end */
6357 dirtmp
->control_macro
= 0;
6358 dirtmp
->c_system_include_path
= 0;
6360 goto missing_dirname
;
6362 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6363 + strlen (prefix
) + 1);
6364 strcpy (dirtmp
->fname
, prefix
);
6365 strcat (dirtmp
->fname
, argv
[++i
]);
6366 dirtmp
->got_name_map
= 0;
6368 append_include_chain (pfile
, dirtmp
, dirtmp
);
6370 /* Add directory to end of path for includes. */
6371 if (!strcmp (argv
[i
], "-idirafter")) {
6372 struct file_name_list
*dirtmp
;
6374 dirtmp
= (struct file_name_list
*)
6375 xmalloc (sizeof (struct file_name_list
));
6376 dirtmp
->next
= 0; /* New one goes on the end */
6377 dirtmp
->control_macro
= 0;
6378 dirtmp
->c_system_include_path
= 0;
6380 goto missing_dirname
;
6382 dirtmp
->fname
= argv
[++i
];
6383 dirtmp
->got_name_map
= 0;
6385 if (opts
->after_include
== 0)
6386 opts
->after_include
= dirtmp
;
6388 opts
->last_after_include
->next
= dirtmp
;
6389 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6394 if (opts
->out_fname
!= NULL
)
6396 cpp_fatal (pfile
, "Output filename specified twice");
6400 goto missing_filename
;
6401 opts
->out_fname
= argv
[++i
];
6402 if (!strcmp (opts
->out_fname
, "-"))
6403 opts
->out_fname
= "";
6407 if (!strcmp (argv
[i
], "-pedantic"))
6408 CPP_PEDANTIC (pfile
) = 1;
6409 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6410 CPP_PEDANTIC (pfile
) = 1;
6411 opts
->pedantic_errors
= 1;
6414 else if (!strcmp (argv
[i
], "-pcp")) {
6415 char *pcp_fname
= argv
[++i
];
6417 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6418 ? fopen (pcp_fname
, "w")
6419 : fdopen (dup (fileno (stdout
)), "w"));
6420 if (pcp_outfile
== 0)
6421 cpp_pfatal_with_name (pfile
, pcp_fname
);
6428 if (!strcmp (argv
[i
], "-traditional")) {
6429 opts
->traditional
= 1;
6430 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6432 opts
->no_trigraphs
= 0;
6437 if (! strcmp (argv
[i
], "-lang-c"))
6438 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->objc
= 0;
6439 if (! strcmp (argv
[i
], "-lang-c++"))
6440 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->objc
= 0;
6441 if (! strcmp (argv
[i
], "-lang-c-c++-comments"))
6442 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->objc
= 0;
6443 if (! strcmp (argv
[i
], "-lang-objc"))
6444 opts
->objc
= 1, opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1;
6445 if (! strcmp (argv
[i
], "-lang-objc++"))
6446 opts
->objc
= 1, opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6447 if (! strcmp (argv
[i
], "-lang-asm"))
6449 if (! strcmp (argv
[i
], "-lint"))
6451 if (! strcmp (argv
[i
], "-lang-chill"))
6452 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6453 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6457 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6461 opts
->inhibit_warnings
= 1;
6465 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6466 opts
->warn_trigraphs
= 1;
6467 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6468 opts
->warn_trigraphs
= 0;
6469 else if (!strcmp (argv
[i
], "-Wcomment"))
6470 opts
->warn_comments
= 1;
6471 else if (!strcmp (argv
[i
], "-Wno-comment"))
6472 opts
->warn_comments
= 0;
6473 else if (!strcmp (argv
[i
], "-Wcomments"))
6474 opts
->warn_comments
= 1;
6475 else if (!strcmp (argv
[i
], "-Wno-comments"))
6476 opts
->warn_comments
= 0;
6477 else if (!strcmp (argv
[i
], "-Wtraditional"))
6478 opts
->warn_stringify
= 1;
6479 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6480 opts
->warn_stringify
= 0;
6481 else if (!strcmp (argv
[i
], "-Wimport"))
6482 opts
->warn_import
= 1;
6483 else if (!strcmp (argv
[i
], "-Wno-import"))
6484 opts
->warn_import
= 0;
6485 else if (!strcmp (argv
[i
], "-Werror"))
6486 opts
->warnings_are_errors
= 1;
6487 else if (!strcmp (argv
[i
], "-Wno-error"))
6488 opts
->warnings_are_errors
= 0;
6489 else if (!strcmp (argv
[i
], "-Wall"))
6491 opts
->warn_trigraphs
= 1;
6492 opts
->warn_comments
= 1;
6497 /* The style of the choices here is a bit mixed.
6498 The chosen scheme is a hybrid of keeping all options in one string
6499 and specifying each option in a separate argument:
6500 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6501 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6502 -M[M][G][D file]. This is awkward to handle in specs, and is not
6504 /* ??? -MG must be specified in addition to one of -M or -MM.
6505 This can be relaxed in the future without breaking anything.
6506 The converse isn't true. */
6508 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6509 if (!strcmp (argv
[i
], "-MG"))
6511 opts
->print_deps_missing_files
= 1;
6514 if (!strcmp (argv
[i
], "-M"))
6515 opts
->print_deps
= 2;
6516 else if (!strcmp (argv
[i
], "-MM"))
6517 opts
->print_deps
= 1;
6518 else if (!strcmp (argv
[i
], "-MD"))
6519 opts
->print_deps
= 2;
6520 else if (!strcmp (argv
[i
], "-MMD"))
6521 opts
->print_deps
= 1;
6522 /* For -MD and -MMD options, write deps on file named by next arg. */
6523 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6526 goto missing_filename
;
6527 opts
->deps_file
= argv
[++i
];
6531 /* For -M and -MM, write deps on standard output
6532 and suppress the usual output. */
6533 opts
->no_output
= 1;
6539 char *p
= argv
[i
] + 2;
6541 while ((c
= *p
++) != 0) {
6542 /* Arg to -d specifies what parts of macros to dump */
6545 opts
->dump_macros
= dump_only
;
6546 opts
->no_output
= 1;
6549 opts
->dump_macros
= dump_names
;
6552 opts
->dump_macros
= dump_definitions
;
6560 if (argv
[i
][2] == '3')
6561 opts
->debug_output
= 1;
6565 fprintf (stderr
, "GNU CPP version %s", version_string
);
6566 #ifdef TARGET_VERSION
6569 fprintf (stderr
, "\n");
6574 opts
->print_include_names
= 1;
6578 if (argv
[i
][2] != 0)
6579 push_pending (pfile
, "-D", argv
[i
] + 2);
6580 else if (i
+ 1 == argc
)
6582 cpp_fatal (pfile
, "Macro name missing after -D option");
6586 i
++, push_pending (pfile
, "-D", argv
[i
]);
6593 if (argv
[i
][2] != 0)
6595 else if (i
+ 1 == argc
)
6597 cpp_fatal (pfile
, "Assertion missing after -A option");
6603 if (!strcmp (p
, "-")) {
6604 struct cpp_pending
**ptr
;
6605 /* -A- eliminates all predefined macros and assertions.
6606 Let's include also any that were specified earlier
6607 on the command line. That way we can get rid of any
6608 that were passed automatically in from GCC. */
6610 opts
->inhibit_predefs
= 1;
6611 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6613 struct cpp_pending
*pend
= *ptr
;
6614 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6615 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6624 push_pending (pfile
, "-A", p
);
6629 case 'U': /* JF #undef something */
6630 if (argv
[i
][2] != 0)
6631 push_pending (pfile
, "-U", argv
[i
] + 2);
6632 else if (i
+ 1 == argc
)
6634 cpp_fatal (pfile
, "Macro name missing after -U option", NULL
);
6638 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6642 opts
->put_out_comments
= 1;
6645 case 'E': /* -E comes from cc -E; ignore it. */
6649 opts
->no_line_commands
= 1;
6652 case '$': /* Don't include $ in identifiers. */
6653 opts
->dollars_in_ident
= 0;
6656 case 'I': /* Add directory to path for includes. */
6658 struct file_name_list
*dirtmp
;
6660 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6661 && !strcmp (argv
[i
] + 2, "-")) {
6662 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6663 /* Don't use any preceding -I directories for #include <...>. */
6664 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6667 dirtmp
= (struct file_name_list
*)
6668 xmalloc (sizeof (struct file_name_list
));
6669 dirtmp
->next
= 0; /* New one goes on the end */
6670 dirtmp
->control_macro
= 0;
6671 dirtmp
->c_system_include_path
= 0;
6672 if (argv
[i
][2] != 0)
6673 dirtmp
->fname
= argv
[i
] + 2;
6674 else if (i
+ 1 == argc
)
6675 goto missing_dirname
;
6677 dirtmp
->fname
= argv
[++i
];
6678 dirtmp
->got_name_map
= 0;
6679 append_include_chain (pfile
, dirtmp
, dirtmp
);
6685 if (!strcmp (argv
[i
], "-nostdinc"))
6686 /* -nostdinc causes no default include directories.
6687 You must specify all include-file directories with -I. */
6688 opts
->no_standard_includes
= 1;
6689 else if (!strcmp (argv
[i
], "-nostdinc++"))
6690 /* -nostdinc++ causes no default C++-specific include directories. */
6691 opts
->no_standard_cplusplus_includes
= 1;
6693 else if (!strcmp (argv
[i
], "-noprecomp"))
6699 /* Sun compiler passes undocumented switch "-undef".
6700 Let's assume it means to inhibit the predefined symbols. */
6701 opts
->inhibit_predefs
= 1;
6704 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6705 if (opts
->in_fname
== NULL
) {
6706 opts
->in_fname
= "";
6708 } else if (opts
->out_fname
== NULL
) {
6709 opts
->out_fname
= "";
6711 } /* else fall through into error */
6725 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6727 if (opts
->print_deps
)
6729 /* Stream on which to print the dependency information. */
6732 /* Don't actually write the deps file if compilation has failed. */
6733 if (pfile
->errors
== 0)
6735 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6736 if (opts
->deps_file
== 0)
6737 deps_stream
= stdout
;
6738 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6739 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6740 fputs (pfile
->deps_buffer
, deps_stream
);
6741 putc ('\n', deps_stream
);
6742 if (opts
->deps_file
)
6744 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6745 cpp_fatal (pfile
, "I/O error on output");
6751 /* Free resources used by PFILE.
6752 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6759 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6760 cpp_pop_buffer (pfile
);
6762 if (pfile
->token_buffer
)
6764 free (pfile
->token_buffer
);
6765 pfile
->token_buffer
= NULL
;
6768 if (pfile
->deps_buffer
)
6770 free (pfile
->deps_buffer
);
6771 pfile
->deps_buffer
= NULL
;
6772 pfile
->deps_allocated_size
= 0;
6775 while (pfile
->if_stack
)
6777 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6778 pfile
->if_stack
= temp
->next
;
6782 while (pfile
->dont_repeat_files
)
6784 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6785 pfile
->dont_repeat_files
= temp
->next
;
6790 while (pfile
->all_include_files
)
6792 struct file_name_list
*temp
= pfile
->all_include_files
;
6793 pfile
->all_include_files
= temp
->next
;
6798 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6800 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6803 struct import_file
*next
= imp
->next
;
6808 pfile
->import_hash_table
[i
] = 0;
6811 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6813 while (pfile
->assertion_hashtab
[i
])
6814 delete_assertion (pfile
->assertion_hashtab
[i
]);
6817 cpp_hash_cleanup (pfile
);
6821 do_assert (pfile
, keyword
, buf
, limit
)
6823 struct directive
*keyword
;
6824 U_CHAR
*buf
, *limit
;
6826 long symstart
; /* remember where symbol name starts */
6828 int sym_length
; /* and how long it is */
6829 struct arglist
*tokens
= NULL
;
6831 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6832 && !CPP_BUFFER (pfile
)->system_header_p
)
6833 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6835 cpp_skip_hspace (pfile
);
6836 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6837 parse_name (pfile
, GETC());
6838 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6841 cpp_skip_hspace (pfile
);
6842 if (PEEKC() != '(') {
6843 cpp_error (pfile
, "missing token-sequence in `#assert'");
6849 tokens
= read_token_list (pfile
, &error_flag
);
6853 cpp_error (pfile
, "empty token-sequence in `#assert'");
6856 cpp_skip_hspace (pfile
);
6858 if (c
!= EOF
&& c
!= '\n')
6859 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6860 skip_rest_of_line (pfile
);
6863 /* If this name isn't already an assertion name, make it one.
6864 Error if it was already in use in some other way. */
6867 ASSERTION_HASHNODE
*hp
;
6868 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6869 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6870 struct tokenlist_list
*value
6871 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6873 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6875 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6876 cpp_error (pfile
, "`defined' redefined as assertion");
6877 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6880 /* Add the spec'd token-sequence to the list of such. */
6881 value
->tokens
= tokens
;
6882 value
->next
= hp
->value
;
6885 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6888 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6889 skip_rest_of_line (pfile
);
6894 do_unassert (pfile
, keyword
, buf
, limit
)
6896 struct directive
*keyword
;
6897 U_CHAR
*buf
, *limit
;
6899 long symstart
; /* remember where symbol name starts */
6900 int sym_length
; /* and how long it is */
6903 struct arglist
*tokens
= NULL
;
6904 int tokens_specified
= 0;
6906 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6907 && !CPP_BUFFER (pfile
)->system_header_p
)
6908 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6910 cpp_skip_hspace (pfile
);
6912 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6913 parse_name (pfile
, GETC());
6914 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6917 cpp_skip_hspace (pfile
);
6918 if (PEEKC() == '(') {
6921 tokens
= read_token_list (pfile
, &error_flag
);
6925 cpp_error (pfile
, "empty token list in `#unassert'");
6929 tokens_specified
= 1;
6932 cpp_skip_hspace (pfile
);
6934 if (c
!= EOF
&& c
!= '\n')
6935 cpp_error (pfile
, "junk at end of `#unassert'");
6936 skip_rest_of_line (pfile
);
6939 ASSERTION_HASHNODE
*hp
;
6940 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6941 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6942 struct tokenlist_list
*tail
, *prev
;
6944 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6948 /* If no token list was specified, then eliminate this assertion
6950 if (! tokens_specified
)
6951 delete_assertion (hp
);
6953 /* If a list of tokens was given, then delete any matching list. */
6958 struct tokenlist_list
*next
= tail
->next
;
6959 if (compare_token_lists (tail
->tokens
, tokens
)) {
6963 hp
->value
= tail
->next
;
6964 free_token_list (tail
->tokens
);
6974 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6977 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6978 skip_rest_of_line (pfile
);
6982 /* Test whether there is an assertion named NAME
6983 and optionally whether it has an asserted token list TOKENS.
6984 NAME is not null terminated; its length is SYM_LENGTH.
6985 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6988 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
6992 int tokens_specified
;
6993 struct arglist
*tokens
;
6995 ASSERTION_HASHNODE
*hp
;
6996 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6998 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
6999 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
7001 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
7003 /* It is not an assertion; just return false. */
7006 /* If no token list was specified, then value is 1. */
7007 if (! tokens_specified
)
7011 struct tokenlist_list
*tail
;
7015 /* If a list of tokens was given,
7016 then succeed if the assertion records a matching list. */
7019 if (compare_token_lists (tail
->tokens
, tokens
))
7024 /* Fail if the assertion has no matching list. */
7029 /* Compare two lists of tokens for equality including order of tokens. */
7032 compare_token_lists (l1
, l2
)
7033 struct arglist
*l1
, *l2
;
7036 if (l1
->length
!= l2
->length
)
7038 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7044 /* Succeed if both lists end at the same time. */
7049 reverse_token_list (tokens
)
7050 struct arglist
*tokens
;
7052 register struct arglist
*prev
= 0, *this, *next
;
7053 for (this = tokens
; this; this = next
)
7062 /* Read a space-separated list of tokens ending in a close parenthesis.
7063 Return a list of strings, in the order they were written.
7064 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7066 static struct arglist
*
7067 read_token_list (pfile
, error_flag
)
7071 struct arglist
*token_ptrs
= 0;
7076 FORWARD (1); /* Skip '(' */
7078 /* Loop over the assertion value tokens. */
7081 struct arglist
*temp
;
7082 long name_written
= CPP_WRITTEN (pfile
);
7083 int eofp
= 0; int c
;
7085 cpp_skip_hspace (pfile
);
7089 /* Find the end of the token. */
7092 CPP_PUTC (pfile
, c
);
7100 CPP_PUTC (pfile
, c
);
7102 else if (c
== '"' || c
== '\'')
7105 cpp_get_token (pfile
);
7111 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7112 && c
!= '"' && c
!= '\'')
7114 CPP_PUTC (pfile
, c
);
7117 if (c
!= EOF
) FORWARD(-1);
7120 length
= CPP_WRITTEN (pfile
) - name_written
;
7121 temp
= (struct arglist
*)
7122 xmalloc (sizeof (struct arglist
) + length
+ 1);
7123 temp
->name
= (U_CHAR
*) (temp
+ 1);
7124 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7125 (char *) temp
->name
, length
);
7126 temp
->name
[length
] = 0;
7127 temp
->next
= token_ptrs
;
7129 temp
->length
= length
;
7131 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7133 if (c
== EOF
|| c
== '\n')
7136 "unterminated token sequence following `#' operator");
7141 /* We accumulated the names in reverse order.
7142 Now reverse them to get the proper order. */
7143 return reverse_token_list (token_ptrs
);
7147 free_token_list (tokens
)
7148 struct arglist
*tokens
;
7151 struct arglist
*next
= tokens
->next
;
7152 free (tokens
->name
);
7158 /* Get the file-mode and data size of the file open on FD
7159 and store them in *MODE_POINTER and *SIZE_POINTER. */
7162 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
7165 long int *size_pointer
;
7169 if (fstat (fd
, &sbuf
) < 0) return (-1);
7170 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
7171 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
7175 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7176 retrying if necessary. Return a negative value if an error occurs,
7177 otherwise return the actual number of bytes read,
7178 which must be LEN unless end-of-file was reached. */
7181 safe_read (desc
, ptr
, len
)
7188 int nchars
= read (desc
, ptr
, left
);
7206 xcalloc (number
, size
)
7207 unsigned number
, size
;
7209 register unsigned total
= number
* size
;
7210 register char *ptr
= (char *) xmalloc (total
);
7219 unsigned size
= strlen (input
);
7220 char *output
= xmalloc (size
+ 1);
7221 strcpy (output
, input
);
7225 /* Initialize PMARK to remember the current position of PFILE. */
7228 parse_set_mark (pmark
, pfile
)
7229 struct parse_marker
*pmark
;
7232 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7233 pmark
->next
= pbuf
->marks
;
7234 pbuf
->marks
= pmark
;
7236 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7239 /* Cleanup PMARK - we no longer need it. */
7242 parse_clear_mark (pmark
)
7243 struct parse_marker
*pmark
;
7245 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7246 for (; ; pp
= &(*pp
)->next
) {
7247 if (*pp
== NULL
) abort ();
7248 if (*pp
== pmark
) break;
7253 /* Backup the current position of PFILE to that saved in PMARK. */
7256 parse_goto_mark (pmark
, pfile
)
7257 struct parse_marker
*pmark
;
7260 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7261 if (pbuf
!= pmark
->buf
)
7262 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7263 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7266 /* Reset PMARK to point to the current position of PFILE. (Same
7267 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7270 parse_move_mark (pmark
, pfile
)
7271 struct parse_marker
*pmark
;
7274 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7275 if (pbuf
!= pmark
->buf
)
7276 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7277 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7281 cpp_read_check_assertion (pfile
)
7284 int name_start
= CPP_WRITTEN (pfile
);
7285 int name_length
, name_written
;
7287 FORWARD (1); /* Skip '#' */
7288 cpp_skip_hspace (pfile
);
7289 parse_name (pfile
, GETC ());
7290 name_written
= CPP_WRITTEN (pfile
);
7291 name_length
= name_written
- name_start
;
7292 cpp_skip_hspace (pfile
);
7293 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7296 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7297 result
= check_assertion (pfile
,
7298 pfile
->token_buffer
+ name_start
, name_length
,
7302 result
= check_assertion (pfile
,
7303 pfile
->token_buffer
+ name_start
, name_length
,
7305 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7310 cpp_print_file_and_line (pfile
)
7313 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7318 cpp_buf_line_and_col (ip
, &line
, &col
);
7319 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
7320 line
, pfile
->show_column
? col
: -1);
7325 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7328 char *arg1
, *arg2
, *arg3
;
7330 cpp_print_containing_files (pfile
);
7331 cpp_print_file_and_line (pfile
);
7332 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7335 /* Print error message but don't count it. */
7338 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7341 char *arg1
, *arg2
, *arg3
;
7343 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7346 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7349 cpp_print_containing_files (pfile
);
7350 cpp_print_file_and_line (pfile
);
7351 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7354 /* Print an error message and maybe count it. */
7357 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7360 char *arg1
, *arg2
, *arg3
;
7362 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7363 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7365 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7369 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7373 char *arg1
, *arg2
, *arg3
;
7376 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7378 cpp_print_containing_files (pfile
);
7381 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7383 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7387 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7391 char *arg1
, *arg2
, *arg3
;
7396 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7399 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7402 cpp_print_containing_files (pfile
);
7404 ip
= cpp_file_buffer (pfile
);
7407 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7409 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7413 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7417 char *arg1
, *arg2
, *arg3
;
7419 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7420 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7422 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7425 /* Report a warning (or an error if pedantic_errors)
7426 giving specified file name and line number, not current. */
7429 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7434 char *arg1
, *arg2
, *arg3
;
7436 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7437 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7440 cpp_file_line_for_message (pfile
, file
, line
, -1);
7441 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7442 msg
, arg1
, arg2
, arg3
);
7445 /* This defines "errno" properly for VMS, and gives us EACCES. */
7452 #ifndef HAVE_STRERROR
7453 extern int sys_nerr
;
7455 extern const char *const sys_errlist
[];
7457 extern char *sys_errlist
[];
7459 #else /* HAVE_STRERROR */
7463 char *strerror (int,...);
7466 /* my_strerror - return the descriptive text associated with an
7470 my_strerror (errnum
)
7476 #ifndef HAVE_STRERROR
7477 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7479 result
= strerror (errnum
);
7482 /* VAXCRTL's strerror() takes an optional second argument, which only
7483 matters when the first argument is EVMSERR. However, it's simplest
7484 just to pass it unconditionally. `vaxc$errno' is declared in
7485 <errno.h>, and maintained by the library in parallel with `errno'.
7486 We assume that caller's `errnum' either matches the last setting of
7487 `errno' by the library or else does not have the value `EVMSERR'. */
7489 result
= strerror (errnum
, vaxc$errno
);
7493 result
= "undocumented I/O error";
7498 /* Error including a message from `errno'. */
7501 cpp_error_from_errno (pfile
, name
)
7506 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7508 cpp_print_containing_files (pfile
);
7511 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
7513 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (errno
));
7517 cpp_perror_with_name (pfile
, name
)
7521 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7525 * No pre-compiled header file support.
7527 * Possibly different enum token codes for each C/C++ token.
7529 * Should clean up remaining directives to that do_XXX functions
7530 * only take two arguments and all have command_reads_line.
7532 * Find and cleanup remaining uses of static variables,
7534 * Support for trigraphs.
7536 * Support -dM flag (dump_all_macros).
7538 * Support for_lint flag.