2 Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
4 Based on CCCP program by 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 #ifndef LOCAL_INCLUDE_DIR
46 #define LOCAL_INCLUDE_DIR "/usr/local/include"
49 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
51 #define PTR_INT_TYPE ptrdiff_t
53 #define PTR_INT_TYPE long
64 /* By default, colon separates directories in a path. */
65 #ifndef PATH_SEPARATOR
66 #define PATH_SEPARATOR ':'
78 #include <sys/time.h> /* for __DATE__ and __TIME__ */
79 #include <sys/resource.h>
81 #include <sys/times.h>
87 /* This defines "errno" properly for VMS, and gives us EACCES. */
90 extern char *index ();
91 extern char *rindex ();
99 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
100 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
102 /* Find the largest host integer type and set its size and type. */
104 #ifndef HOST_BITS_PER_WIDE_INT
106 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
107 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
108 #define HOST_WIDE_INT long
110 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
111 #define HOST_WIDE_INT int
117 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
121 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
124 /* Define a generic NULL if one hasn't already been defined. */
131 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
132 #define GENERIC_PTR void *
134 #define GENERIC_PTR char *
139 #define NULL_PTR ((GENERIC_PTR)0)
142 #ifndef INCLUDE_LEN_FUDGE
143 #define INCLUDE_LEN_FUDGE 0
146 /* Symbols to predefine. */
148 #ifdef CPP_PREDEFINES
149 static char *predefs
= CPP_PREDEFINES
;
151 static char *predefs
= "";
154 /* We let tm.h override the types used here, to handle trivial differences
155 such as the choice of unsigned int or long unsigned int for size_t.
156 When machines start needing nontrivial differences in the size type,
157 it would be best to do something here to figure out automatically
158 from other information what type to use. */
160 /* The string value for __SIZE_TYPE__. */
163 #define SIZE_TYPE "long unsigned int"
166 /* The string value for __PTRDIFF_TYPE__. */
169 #define PTRDIFF_TYPE "long int"
172 /* The string value for __WCHAR_TYPE__. */
175 #define WCHAR_TYPE "int"
177 #define CPP_WCHAR_TYPE(PFILE) \
178 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
180 /* The string value for __USER_LABEL_PREFIX__ */
182 #ifndef USER_LABEL_PREFIX
183 #define USER_LABEL_PREFIX ""
186 /* The string value for __REGISTER_PREFIX__ */
188 #ifndef REGISTER_PREFIX
189 #define REGISTER_PREFIX ""
192 /* In the definition of a #assert name, this structure forms
193 a list of the individual values asserted.
194 Each value is itself a list of "tokens".
195 These are strings that are compared by name. */
197 struct tokenlist_list
{
198 struct tokenlist_list
*next
;
199 struct arglist
*tokens
;
202 struct assertion_hashnode
{
203 struct assertion_hashnode
*next
; /* double links for easy deletion */
204 struct assertion_hashnode
*prev
;
205 /* also, a back pointer to this node's hash
206 chain is kept, in case the node is the head
207 of the chain and gets deleted. */
208 struct assertion_hashnode
**bucket_hdr
;
209 int length
; /* length of token, for quick comparison */
210 U_CHAR
*name
; /* the actual name */
211 /* List of token-sequences. */
212 struct tokenlist_list
*value
;
215 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
216 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
218 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
219 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
220 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
221 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
222 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
223 (Note that it is false while we're expanding marco *arguments*.) */
224 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
226 /* Move all backslash-newline pairs out of embarrassing places.
227 Exchange all such pairs following BP
228 with any potentially-embarrassing characters that follow them.
229 Potentially-embarrassing characters are / and *
230 (because a backslash-newline inside a comment delimiter
231 would cause it not to be recognized). */
233 #define NEWLINE_FIX \
234 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
236 /* Same, but assume we've already read the potential '\\' into C. */
237 #define NEWLINE_FIX1(C) do { \
238 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
242 struct cpp_pending
*next
;
247 /* Forward declarations. */
249 extern char *xmalloc ();
251 static void add_import ();
252 static void append_include_chain ();
253 static void make_undef ();
254 static void make_assertion ();
255 static void path_include ();
256 static void initialize_builtins ();
257 static void initialize_char_syntax ();
258 static void dump_arg_n ();
259 static void dump_defn_1 ();
260 extern void delete_macro ();
261 static void trigraph_pcp ();
262 static int finclude ();
263 static void validate_else ();
264 static int comp_def_part ();
265 extern void fancy_abort ();
266 static void pipe_closed ();
267 static void print_containing_files ();
268 static int lookup_import ();
269 static int redundant_include_p ();
270 static is_system_include ();
271 static struct file_name_map
*read_name_map ();
272 static char *read_filename_string ();
273 static int open_include_file ();
274 static int check_preconditions ();
275 static void pcfinclude ();
276 static void pcstring_used ();
277 static int check_macro_name ();
278 static int compare_defs ();
279 static int compare_token_lists ();
280 static HOST_WIDE_INT
eval_if_expression ();
281 static int change_newlines ();
283 static int file_size_and_mode ();
284 static struct arglist
*read_token_list ();
285 static void free_token_list ();
286 static int safe_read ();
287 static void push_macro_expansion
PARAMS ((cpp_reader
*,
288 U_CHAR
*, int, HASHNODE
*));
289 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
290 extern char *xrealloc ();
291 extern char *xcalloc ();
292 static char *savestring ();
294 static void conditional_skip ();
295 static void skip_if_group ();
297 /* Last arg to output_line_command. */
298 enum file_change_code
{same_file
, enter_file
, leave_file
};
300 /* External declarations. */
302 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
304 extern char *getenv ();
305 extern FILE *fdopen ();
306 extern char *version_string
;
307 extern struct tm
*localtime ();
309 /* These functions are declared to return int instead of void since they
310 are going to be placed in a table and some old compilers have trouble with
311 pointers to functions returning void. */
313 static int do_define ();
314 static int do_line ();
315 static int do_include ();
316 static int do_undef ();
317 static int do_error ();
318 static int do_pragma ();
319 static int do_ident ();
321 static int do_xifdef ();
322 static int do_else ();
323 static int do_elif ();
324 static int do_endif ();
325 static int do_sccs ();
326 static int do_once ();
327 static int do_assert ();
328 static int do_unassert ();
329 static int do_warning ();
331 struct file_name_list
333 struct file_name_list
*next
;
335 /* If the following is nonzero, it is a macro name.
336 Don't include the file again if that macro is defined. */
337 U_CHAR
*control_macro
;
338 /* If the following is nonzero, it is a C-language system include
340 int c_system_include_path
;
341 /* Mapping of file names for this directory. */
342 struct file_name_map
*name_map
;
343 /* Non-zero if name_map is valid. */
347 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
348 via the same directory as the file that #included it. */
349 #define SELF_DIR_DUMMY ((struct file_name_list*)(~0))
351 /* #include "file" looks in source file dir, then stack. */
352 /* #include <file> just looks in the stack. */
353 /* -I directories are added to the end, then the defaults are added. */
355 static struct default_include
{
356 char *fname
; /* The name of the directory. */
357 int cplusplus
; /* Only look here if we're compiling C++. */
358 int cxx_aware
; /* Includes in this directory don't need to
359 be wrapped in extern "C" when compiling
361 } include_defaults_array
[]
362 #ifdef INCLUDE_DEFAULTS
366 /* Pick up GNU C++ specific include files. */
367 { GPLUSPLUS_INCLUDE_DIR
, 1, 1 },
369 /* This is the dir for fixincludes. Put it just before
370 the files that we fix. */
371 { GCC_INCLUDE_DIR
, 0, 0 },
372 /* For cross-compilation, this dir name is generated
373 automatically in Makefile.in. */
374 { CROSS_INCLUDE_DIR
, 0, 0 },
375 /* This is another place that the target system's headers might be. */
376 { TOOL_INCLUDE_DIR
, 0, 1 },
377 #else /* not CROSS_COMPILE */
378 /* This should be /usr/local/include and should come before
379 the fixincludes-fixed header files. */
380 { LOCAL_INCLUDE_DIR
, 0, 1 },
381 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
382 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
383 { TOOL_INCLUDE_DIR
, 0, 1 },
384 /* This is the dir for fixincludes. Put it just before
385 the files that we fix. */
386 { GCC_INCLUDE_DIR
, 0, 0 },
387 /* Some systems have an extra dir of include files. */
388 #ifdef SYSTEM_INCLUDE_DIR
389 { SYSTEM_INCLUDE_DIR
, 0, 0 },
391 { STANDARD_INCLUDE_DIR
, 0, 0 },
392 #endif /* not CROSS_COMPILE */
395 #endif /* no INCLUDE_DEFAULTS */
397 /* `struct directive' defines one #-directive, including how to handle it. */
400 int length
; /* Length of name */
401 int (*func
)(); /* Function to handle directive */
402 char *name
; /* Name of directive */
403 enum node_type type
; /* Code which describes which directive. */
404 char command_reads_line
; /* One if rest of line is read by func. */
405 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
406 char pass_thru
; /* Copy preprocessed directive to output file.*/
409 /* Here is the actual list of #-directives, most-often-used first.
410 The initialize_builtins function assumes #define is the very first. */
412 static struct directive directive_table
[] = {
413 { 6, do_define
, "define", T_DEFINE
, 0, 1},
414 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
415 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
416 { 7, do_include
, "include", T_INCLUDE
, 1},
417 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
418 { 6, do_include
, "import", T_IMPORT
, 1},
419 { 5, do_endif
, "endif", T_ENDIF
, 1},
420 { 4, do_else
, "else", T_ELSE
, 1},
421 { 2, do_if
, "if", T_IF
, 1},
422 { 4, do_elif
, "elif", T_ELIF
, 1},
423 { 5, do_undef
, "undef", T_UNDEF
},
424 { 5, do_error
, "error", T_ERROR
},
425 { 7, do_warning
, "warning", T_WARNING
},
426 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
427 { 4, do_line
, "line", T_LINE
, 1},
428 { 5, do_ident
, "ident", T_IDENT
, 1, 0, 1},
429 #ifdef SCCS_DIRECTIVE
430 { 4, do_sccs
, "sccs", T_SCCS
},
432 { 6, do_assert
, "assert", T_ASSERT
, 1},
433 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
434 { -1, 0, "", T_UNUSED
},
437 /* table to tell if char can be part of a C identifier. */
438 U_CHAR is_idchar
[256];
439 /* table to tell if char can be first char of a c identifier. */
440 U_CHAR is_idstart
[256];
441 /* table to tell if c is horizontal space. */
442 U_CHAR is_hor_space
[256];
443 /* table to tell if c is horizontal or vertical space. */
444 static U_CHAR is_space
[256];
446 /* Initialize syntactic classifications of characters. */
449 initialize_char_syntax (opts
)
450 struct cpp_options
*opts
;
455 * Set up is_idchar and is_idstart tables. These should be
456 * faster than saying (is_alpha (c) || c == '_'), etc.
457 * Set up these things before calling any routines tthat
460 for (i
= 'a'; i
<= 'z'; i
++) {
461 is_idchar
[i
- 'a' + 'A'] = 1;
463 is_idstart
[i
- 'a' + 'A'] = 1;
466 for (i
= '0'; i
<= '9'; i
++)
470 is_idchar
['$'] = opts
->dollars_in_ident
;
471 is_idstart
['$'] = opts
->dollars_in_ident
;
473 /* horizontal space table */
474 is_hor_space
[' '] = 1;
475 is_hor_space
['\t'] = 1;
476 is_hor_space
['\v'] = 1;
477 is_hor_space
['\f'] = 1;
478 is_hor_space
['\r'] = 1;
489 /* Place into PFILE a quoted string representing the string SRC.
490 Caller must reserve enough space in pfile->token_buffer. */
492 quote_string (pfile
, src
)
498 CPP_PUTC_Q (pfile
, '\"');
500 switch ((c
= *src
++))
504 CPP_PUTC_Q (pfile
, c
);
507 sprintf (CPP_PWRITTEN (pfile
), "\\%03o", c
);
508 CPP_ADJUST_WRITTEN (pfile
, 4);
514 CPP_PUTC_Q (pfile
, '\\');
515 CPP_PUTC_Q (pfile
, c
);
519 CPP_PUTC_Q (pfile
, '\"');
520 CPP_NUL_TERMINATE_Q (pfile
);
525 /* Make sure PFILE->token_buffer will hold at least N more chars. */
528 cpp_grow_buffer (pfile
, n
)
532 long old_written
= CPP_WRITTEN (pfile
);
533 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
534 pfile
->token_buffer
= (U_CHAR
*)
535 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
536 CPP_SET_WRITTEN (pfile
, old_written
);
541 * process a given definition string, for initialization
542 * If STR is just an identifier, define it with value 1.
543 * If STR has anything after the identifier, then it should
544 * be identifier=definition.
548 cpp_define (pfile
, str
)
558 cpp_error (pfile
, "malformed option `-D %s'", str
);
561 while (is_idchar
[*++p
])
565 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
566 strcpy ((char *)buf
, str
);
567 strcat ((char *)buf
, " 1");
571 cpp_error (pfile
, "malformed option `-D %s'", str
);
577 /* Copy the entire option so we can modify it. */
578 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
579 strncpy (buf
, str
, p
- str
);
580 /* Change the = to a space. */
582 /* Scan for any backslash-newline and remove it. */
587 if (*p
== '\\' && p
[1] == '\n')
595 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
598 /* Process the string STR as if it appeared as the body of a #assert.
599 OPTION is the option name for which STR was the argument. */
602 make_assertion (pfile
, option
, str
)
608 struct directive
*kt
;
611 /* Copy the entire option so we can modify it. */
612 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
613 strcpy ((char *) buf
, str
);
614 /* Scan for any backslash-newline and remove it. */
618 if (*p
== '\\' && p
[1] == '\n')
627 if (!is_idstart
[*p
]) {
628 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
631 while (is_idchar
[*++p
])
633 while (*p
== ' ' || *p
== '\t') p
++;
634 if (! (*p
== 0 || *p
== '(')) {
635 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
639 ip
= cpp_push_buffer (pfile
, buf
, strlen (buf
));
640 do_assert (pfile
, NULL
, NULL
, NULL
);
641 cpp_pop_buffer (pfile
);
644 /* Append a chain of `struct file_name_list's
645 to the end of the main include chain.
646 FIRST is the beginning of the chain to append, and LAST is the end. */
649 append_include_chain (pfile
, first
, last
)
651 struct file_name_list
*first
, *last
;
653 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
654 struct file_name_list
*dir
;
659 if (opts
->include
== 0)
660 opts
->include
= first
;
662 opts
->last_include
->next
= first
;
664 if (opts
->first_bracket_include
== 0)
665 opts
->first_bracket_include
= first
;
667 for (dir
= first
; ; dir
= dir
->next
) {
668 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
669 if (len
> pfile
->max_include_len
)
670 pfile
->max_include_len
= len
;
676 opts
->last_include
= last
;
679 /* Add output to `deps_buffer' for the -M switch.
680 STRING points to the text to be output.
681 SPACER is ':' for targets, ' ' for dependencies, zero for text
682 to be inserted literally. */
685 deps_output (pfile
, string
, spacer
)
690 int size
= strlen (string
);
695 #ifndef MAX_OUTPUT_COLUMNS
696 #define MAX_OUTPUT_COLUMNS 72
699 && pfile
->deps_column
> 0
700 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
702 deps_output (pfile
, " \\\n ", 0);
703 pfile
->deps_column
= 0;
706 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
708 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
709 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
710 pfile
->deps_allocated_size
);
712 if (spacer
== ' ' && pfile
->deps_column
> 0)
713 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
714 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
715 pfile
->deps_size
+= size
;
716 pfile
->deps_column
+= size
;
718 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
719 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
722 /* Given a colon-separated list of file names PATH,
723 add all the names to the search path for include files. */
726 path_include (pfile
, path
)
738 struct file_name_list
*dirtmp
;
740 /* Find the end of this name. */
741 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
743 /* An empty name in the path stands for the current directory. */
744 name
= (char *) xmalloc (2);
748 /* Otherwise use the directory that is named. */
749 name
= (char *) xmalloc (q
- p
+ 1);
750 bcopy (p
, name
, q
- p
);
754 dirtmp
= (struct file_name_list
*)
755 xmalloc (sizeof (struct file_name_list
));
756 dirtmp
->next
= 0; /* New one goes on the end */
757 dirtmp
->control_macro
= 0;
758 dirtmp
->c_system_include_path
= 0;
759 dirtmp
->fname
= name
;
760 dirtmp
->got_name_map
= 0;
761 append_include_chain (pfile
, dirtmp
, dirtmp
);
763 /* Advance past this name. */
767 /* Skip the colon. */
773 init_parse_options (opts
)
774 struct cpp_options
*opts
;
776 bzero ((char *) opts
, sizeof *opts
);
777 opts
->in_fname
= NULL
;
778 opts
->out_fname
= NULL
;
780 /* Initialize is_idchar to allow $. */
781 opts
->dollars_in_ident
= 1;
782 initialize_char_syntax (opts
);
783 opts
->dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
> 0;
785 opts
->no_line_commands
= 0;
786 opts
->no_trigraphs
= 1;
787 opts
->put_out_comments
= 0;
788 opts
->print_include_names
= 0;
789 opts
->dump_macros
= dump_none
;
792 opts
->cplusplus_comments
= 0;
799 opts
->pedantic_errors
= 0;
800 opts
->inhibit_warnings
= 0;
801 opts
->warn_comments
= 0;
802 opts
->warn_import
= 1;
803 opts
->warnings_are_errors
= 0;
807 null_underflow (pfile
)
814 null_cleanup (pbuf
, pfile
)
822 macro_cleanup (pbuf
, pfile
)
826 HASHNODE
*macro
= (HASHNODE
*)pbuf
->data
;
827 if (macro
->type
== T_DISABLED
)
828 macro
->type
= T_MACRO
;
829 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
835 file_cleanup (pbuf
, pfile
)
854 register U_CHAR
*p
= bp
;
856 /* First count the backslash-newline pairs here. */
858 while (p
[0] == '\\' && p
[1] == '\n')
861 /* What follows the backslash-newlines is not embarrassing. */
863 if (*p
!= '/' && *p
!= '*')
866 /* Copy all potentially embarrassing characters
867 that follow the backslash-newline pairs
868 down to where the pairs originally started. */
870 while (*p
== '*' || *p
== '/')
873 /* Now write the same number of pairs after the embarrassing chars. */
881 /* Assuming we have read '/'.
882 If this is the start of a comment (followed by '*' or '/'),
883 skip to the end of the comment, and return ' '.
884 Return EOF if we reached the end of file before the end of the comment.
885 If not the start of a comment, return '/'. */
888 skip_comment (pfile
, linep
)
893 while (PEEKC() == '\\' && PEEKN(1) == '\n')
908 while (c
== '\\' && PEEKC() == '\n')
912 FORWARD(1), c
= GETC();
914 if (prev_c
== '*' && c
== '/')
916 if (c
== '\n' && linep
)
920 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
927 return ' '; /* Allow // to be terminated by EOF. */
928 while (c
== '\\' && PEEKC() == '\n')
937 /* Don't consider final '\n' to be part of comment. */
947 /* Skip whitespace \-newline and comments. Does not macro-expand. */
949 cpp_skip_hspace (pfile
)
959 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
960 cpp_pedwarn (pfile
, "%s in preprocessing directive",
961 c
== '\f' ? "formfeed" : "vertical tab");
967 c
= skip_comment (pfile
, NULL
);
970 if (c
== EOF
|| c
== '/')
973 else if (c
== '\\' && PEEKN(1) == '\n') {
976 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
977 && is_hor_space
[PEEKN(1)])
983 /* Read the rest of the current line.
984 The line is appended to PFILE's output buffer. */
987 copy_rest_of_line (pfile
)
990 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
1000 if (PEEKC() == '\n')
1007 goto scan_directive_token
;
1011 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '*'))
1012 goto scan_directive_token
;
1016 if (CPP_PEDANTIC (pfile
))
1017 cpp_pedwarn (pfile
, "%s in preprocessing directive",
1018 c
== '\f' ? "formfeed" : "vertical tab");
1024 scan_directive_token
:
1026 cpp_get_token (pfile
);
1029 CPP_PUTC (pfile
, c
);
1032 CPP_NUL_TERMINATE (pfile
);
1036 skip_rest_of_line (pfile
)
1039 long old
= CPP_WRITTEN (pfile
);
1040 copy_rest_of_line (pfile
);
1041 CPP_SET_WRITTEN (pfile
, old
);
1044 /* Handle a possible # directive.
1045 '#' has already been read. */
1048 handle_directive (pfile
)
1051 register struct directive
*kt
;
1054 U_CHAR
*ident
, *line_end
;
1055 long old_written
= CPP_WRITTEN (pfile
);
1057 cpp_skip_hspace (pfile
);
1060 if (c
>= '0' && c
<= '9')
1062 /* Handle # followed by a line number. */
1063 if (CPP_PEDANTIC (pfile
))
1064 cpp_pedwarn (pfile
, "`#' followed by integer");
1065 do_line (pfile
, NULL
);
1066 goto done_a_directive
;
1069 /* Now find the directive name. */
1070 CPP_PUTC (pfile
, '#');
1071 parse_name (pfile
, GETC());
1072 ident
= pfile
->token_buffer
+ old_written
+ 1;
1073 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1074 if (ident_length
== 0 && PEEKC() == '\n')
1076 /* A line of just `#' becomes blank. */
1077 goto done_a_directive
;
1081 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1083 while (is_idchar
[*p
]) {
1084 if (*p
< '0' || *p
> '9')
1088 /* Avoid error for `###' and similar cases unless -pedantic. */
1090 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1092 if (pedantic
&& !lang_asm
)
1093 cpp_warning (pfile
, "invalid preprocessor directive");
1099 cpp_error (pfile
, "invalid preprocessor directive name");
1105 * Decode the keyword and call the appropriate expansion
1106 * routine, after moving the input pointer up to the next line.
1108 for (kt
= directive_table
; ; kt
++) {
1109 if (kt
->length
<= 0)
1110 goto not_a_directive
;
1111 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1115 if (! kt
->command_reads_line
)
1117 /* Nonzero means do not delete comments within the directive.
1118 #define needs this when -traditional. */
1119 int comments
= CPP_TRADITIONAL (pfile
) && kt
->traditional_comments
;
1120 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1121 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1122 after_ident
= CPP_WRITTEN (pfile
);
1123 copy_rest_of_line (pfile
);
1124 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1127 /* For #pragma and #define, we may want to pass through the directive.
1128 Other directives may create output, but we don't want the directive
1129 itself out, so we pop it now. For example #include may write a #line
1130 command (see comment in do_include), and conditionals may emit
1131 #failed ... #endfailed stuff. But note that popping the buffer
1132 means the parameters to kt->func may point after pfile->limit
1133 so these parameters are invalid as soon as something gets appended
1134 to the token_buffer. */
1136 line_end
= CPP_PWRITTEN (pfile
);
1137 if (!kt
->pass_thru
&& kt
->type
!= T_DEFINE
)
1138 CPP_SET_WRITTEN (pfile
, old_written
);
1140 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1142 || (kt
->type
== T_DEFINE
1143 && CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
))
1145 /* Just leave the entire #define in the output stack. */
1147 else if (kt
->type
== T_DEFINE
1148 && CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1150 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7; /* Skip "#define". */
1151 SKIP_WHITE_SPACE (p
);
1152 while (is_idchar
[*p
]) p
++;
1154 CPP_PUTC (pfile
, '\n');
1156 else if (kt
->type
== T_DEFINE
)
1157 CPP_SET_WRITTEN (pfile
, old_written
);
1165 /* Pass a directive through to the output file.
1166 BUF points to the contents of the directive, as a contiguous string.
1167 LIMIT points to the first character past the end of the directive.
1168 KEYWORD is the keyword-table entry for the directive. */
1171 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1172 U_CHAR
*buf
, *limit
;
1174 struct directive
*keyword
;
1176 register unsigned keyword_length
= keyword
->length
;
1178 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1179 CPP_PUTC_Q (pfile
, '#');
1180 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1181 if (limit
!= buf
&& buf
[0] != ' ')
1182 CPP_PUTC_Q (pfile
, ' ');
1183 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1185 CPP_PUTS_Q (pfile
, '\n');
1186 /* Count the line we have just made in the output,
1187 to get in sync properly. */
1192 /* The arglist structure is built by do_define to tell
1193 collect_definition where the argument names begin. That
1194 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1195 would contain pointers to the strings x, y, and z.
1196 Collect_definition would then build a DEFINITION node,
1197 with reflist nodes pointing to the places x, y, and z had
1198 appeared. So the arglist is just convenience data passed
1199 between these two routines. It is not kept around after
1200 the current #define has been processed and entered into the
1204 struct arglist
*next
;
1211 /* Read a replacement list for a macro with parameters.
1212 Build the DEFINITION structure.
1213 Reads characters of text starting at BUF until END.
1214 ARGLIST specifies the formal parameters to look for
1215 in the text of the definition; NARGS is the number of args
1216 in that list, or -1 for a macro name that wants no argument list.
1217 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1218 and NAMELEN is its length in characters.
1220 Note that comments, backslash-newlines, and leading white space
1221 have already been deleted from the argument. */
1224 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1226 U_CHAR
*buf
, *limit
;
1228 struct arglist
*arglist
;
1231 register U_CHAR
*p
, *lastp
, *exp_p
;
1232 struct reflist
*endpat
= NULL
;
1233 /* Pointer to first nonspace after last ## seen. */
1235 /* Pointer to first nonspace after last single-# seen. */
1236 U_CHAR
*stringify
= 0;
1238 int expected_delimiter
= '\0';
1240 /* Scan thru the replacement list, ignoring comments and quoted
1241 strings, picking up on the macro calls. It does a linear search
1242 thru the arg list on every potential symbol. Profiling might say
1243 that something smarter should happen. */
1248 /* Find the beginning of the trailing whitespace. */
1250 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1252 /* Allocate space for the text in the macro definition.
1253 Leading and trailing whitespace chars need 2 bytes each.
1254 Each other input char may or may not need 1 byte,
1255 so this is an upper bound. The extra 5 are for invented
1256 leading and trailing newline-marker and final null. */
1257 maxsize
= (sizeof (DEFINITION
)
1259 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1263 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1265 defn
->nargs
= nargs
;
1266 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1271 /* Add one initial space escape-marker to prevent accidental
1272 token-pasting (often removed by macroexpand). */
1276 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1277 cpp_error (pfile
, "`##' at start of macro definition");
1281 /* Process the main body of the definition. */
1283 int skipped_arg
= 0;
1284 register U_CHAR c
= *p
++;
1288 if (!CPP_TRADITIONAL (pfile
)) {
1292 if (expected_delimiter
!= '\0') {
1293 if (c
== expected_delimiter
)
1294 expected_delimiter
= '\0';
1296 expected_delimiter
= c
;
1300 if (p
< limit
&& expected_delimiter
) {
1301 /* In a string, backslash goes through
1302 and makes next char ordinary. */
1308 /* An '@' in a string or character constant stands for itself,
1309 and does not need to be escaped. */
1310 if (!expected_delimiter
)
1315 /* # is ordinary inside a string. */
1316 if (expected_delimiter
)
1318 if (p
< limit
&& *p
== '#') {
1319 /* ##: concatenate preceding and following tokens. */
1320 /* Take out the first #, discard preceding whitespace. */
1322 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1324 /* Skip the second #. */
1326 /* Discard following whitespace. */
1327 SKIP_WHITE_SPACE (p
);
1330 cpp_error (pfile
, "`##' at end of macro definition");
1331 } else if (nargs
>= 0) {
1332 /* Single #: stringify following argument ref.
1333 Don't leave the # in the expansion. */
1335 SKIP_WHITE_SPACE (p
);
1336 if (p
== limit
|| ! is_idstart
[*p
])
1338 "`#' operator is not followed by a macro argument name");
1345 /* In -traditional mode, recognize arguments inside strings and
1346 and character constants, and ignore special properties of #.
1347 Arguments inside strings are considered "stringified", but no
1348 extra quote marks are supplied. */
1352 if (expected_delimiter
!= '\0') {
1353 if (c
== expected_delimiter
)
1354 expected_delimiter
= '\0';
1356 expected_delimiter
= c
;
1360 /* Backslash quotes delimiters and itself, but not macro args. */
1361 if (expected_delimiter
!= 0 && p
< limit
1362 && (*p
== expected_delimiter
|| *p
== '\\')) {
1369 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1372 /* If we find a comment that wasn't removed by handle_directive,
1373 this must be -traditional. So replace the comment with
1377 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1380 /* Mark this as a concatenation-point, as if it had been ##. */
1388 /* Handle the start of a symbol. */
1389 if (is_idchar
[c
] && nargs
> 0) {
1390 U_CHAR
*id_beg
= p
- 1;
1394 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1395 id_len
= p
- id_beg
;
1397 if (is_idstart
[c
]) {
1398 register struct arglist
*arg
;
1400 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1401 struct reflist
*tpat
;
1403 if (arg
->name
[0] == c
1404 && arg
->length
== id_len
1405 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1406 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1407 if (CPP_TRADITIONAL (pfile
)) {
1408 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1412 "macro arg `%.*s' would be stringified with -traditional.",
1416 /* If ANSI, don't actually substitute inside a string. */
1417 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1419 /* make a pat node for this arg and append it to the end of
1421 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1423 tpat
->raw_before
= concat
== id_beg
;
1424 tpat
->raw_after
= 0;
1425 tpat
->rest_args
= arg
->rest_args
;
1426 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1427 ? expected_delimiter
!= '\0'
1428 : stringify
== id_beg
);
1431 defn
->pattern
= tpat
;
1433 endpat
->next
= tpat
;
1436 tpat
->argno
= arg
->argno
;
1437 tpat
->nchars
= exp_p
- lastp
;
1439 register U_CHAR
*p1
= p
;
1440 SKIP_WHITE_SPACE (p1
);
1441 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1442 tpat
->raw_after
= 1;
1444 lastp
= exp_p
; /* place to start copying from next time */
1451 /* If this was not a macro arg, copy it into the expansion. */
1452 if (! skipped_arg
) {
1453 register U_CHAR
*lim1
= p
;
1457 if (stringify
== id_beg
)
1459 "`#' operator should be followed by a macro argument name");
1464 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1466 /* If ANSI, put in a "@ " marker to prevent token pasting.
1467 But not if "inside a string" (which in ANSI mode
1468 happens only for -D option). */
1475 defn
->length
= exp_p
- defn
->expansion
;
1477 /* Crash now if we overrun the allocated size. */
1478 if (defn
->length
+ 1 > maxsize
)
1482 /* This isn't worth the time it takes. */
1483 /* give back excess storage */
1484 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1491 * special extension string that can be added to the last macro argument to
1492 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1493 * #define wow(a, b...) process (b, a, b)
1494 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1495 * { wow (one, two); } -> { process (two, one, two); }
1496 * if this "rest_arg" is used with the concat token '##' and if it is not
1497 * supplied then the token attached to with ## will not be outputted. Ex:
1498 * #define wow (a, b...) process (b ## , a, ## b)
1499 * { wow (1, 2); } -> { process (2, 1, 2); }
1500 * { wow (one); } -> { process (one); {
1502 static char rest_extension
[] = "...";
1503 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1505 /* Create a DEFINITION node from a #define directive. Arguments are
1506 as for do_define. */
1508 create_definition (buf
, limit
, pfile
, predefinition
)
1509 U_CHAR
*buf
, *limit
;
1513 U_CHAR
*bp
; /* temp ptr into input buffer */
1514 U_CHAR
*symname
; /* remember where symbol name starts */
1515 int sym_length
; /* and how long it is */
1518 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1520 int arglengths
= 0; /* Accumulate lengths of arg names
1521 plus number of args. */
1523 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1527 while (is_hor_space
[*bp
])
1530 symname
= bp
; /* remember where it starts */
1532 sym_length
= check_macro_name (pfile
, bp
, "macro");
1535 /* Lossage will occur if identifiers or control keywords are broken
1536 across lines using backslash. This is not the right place to take
1540 struct arglist
*arg_ptrs
= NULL
;
1543 bp
++; /* skip '(' */
1544 SKIP_WHITE_SPACE (bp
);
1546 /* Loop over macro argument names. */
1547 while (*bp
!= ')') {
1548 struct arglist
*temp
;
1550 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1552 temp
->next
= arg_ptrs
;
1553 temp
->argno
= argno
++;
1554 temp
->rest_args
= 0;
1558 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1560 if (!is_idstart
[*bp
])
1561 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1563 /* Find the end of the arg name. */
1564 while (is_idchar
[*bp
]) {
1566 /* do we have a "special" rest-args extension here? */
1567 if (limit
- bp
> REST_EXTENSION_LENGTH
&&
1568 strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1570 temp
->rest_args
= 1;
1574 temp
->length
= bp
- temp
->name
;
1576 bp
+= REST_EXTENSION_LENGTH
;
1577 arglengths
+= temp
->length
+ 2;
1578 SKIP_WHITE_SPACE (bp
);
1579 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1580 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1585 SKIP_WHITE_SPACE (bp
);
1588 cpp_error (pfile
, "unterminated parameter list in `#define'");
1592 struct arglist
*otemp
;
1594 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1595 if (temp
->length
== otemp
->length
&&
1596 strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1599 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1600 (void) strncpy (name
, temp
->name
, temp
->length
);
1601 name
[temp
->length
] = '\0';
1603 "duplicate argument name `%s' in `#define'", name
);
1609 ++bp
; /* skip paren */
1610 SKIP_WHITE_SPACE (bp
);
1611 /* now everything from bp before limit is the definition. */
1612 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1613 defn
->rest_args
= rest_args
;
1615 /* Now set defn->args.argnames to the result of concatenating
1616 the argument names in reverse order
1617 with comma-space between them. */
1618 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1620 struct arglist
*temp
;
1622 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1623 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1625 if (temp
->next
!= 0) {
1626 defn
->args
.argnames
[i
++] = ',';
1627 defn
->args
.argnames
[i
++] = ' ';
1630 defn
->args
.argnames
[i
] = 0;
1633 /* Simple expansion or empty definition. */
1637 if (is_hor_space
[*bp
]) {
1639 SKIP_WHITE_SPACE (bp
);
1642 case '!': case '"': case '#': case '%': case '&': case '\'':
1643 case ')': case '*': case '+': case ',': case '-': case '.':
1644 case '/': case ':': case ';': case '<': case '=': case '>':
1645 case '?': case '[': case '\\': case ']': case '^': case '{':
1646 case '|': case '}': case '~':
1647 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1648 sym_length
, symname
);
1652 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1653 sym_length
, symname
);
1658 /* now everything from bp before limit is the definition. */
1659 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1660 defn
->args
.argnames
= (U_CHAR
*) "";
1666 /* OP is null if this is a predefinition */
1667 defn
->predefined
= predefinition
;
1669 mdef
.symnam
= symname
;
1670 mdef
.symlen
= sym_length
;
1679 /* Check a purported macro name SYMNAME, and yield its length.
1680 USAGE is the kind of name this is intended for. */
1683 check_macro_name (pfile
, symname
, usage
)
1691 for (p
= symname
; is_idchar
[*p
]; p
++)
1693 sym_length
= p
- symname
;
1694 if (sym_length
== 0)
1695 cpp_error (pfile
, "invalid %s name", usage
);
1696 else if (!is_idstart
[*symname
]) {
1697 U_CHAR
*msg
; /* what pain... */
1698 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1699 bcopy (symname
, msg
, sym_length
);
1700 msg
[sym_length
] = 0;
1701 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1703 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1704 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1710 * return zero if two DEFINITIONs are isomorphic
1713 compare_defs (d1
, d2
)
1714 DEFINITION
*d1
, *d2
;
1716 register struct reflist
*a1
, *a2
;
1717 register U_CHAR
*p1
= d1
->expansion
;
1718 register U_CHAR
*p2
= d2
->expansion
;
1721 if (d1
->nargs
!= d2
->nargs
)
1723 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1725 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1726 a1
= a1
->next
, a2
= a2
->next
) {
1727 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1728 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1729 || a1
->argno
!= a2
->argno
1730 || a1
->stringify
!= a2
->stringify
1731 || a1
->raw_before
!= a2
->raw_before
1732 || a1
->raw_after
!= a2
->raw_after
)
1740 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1741 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1746 /* Return 1 if two parts of two macro definitions are effectively different.
1747 One of the parts starts at BEG1 and has LEN1 chars;
1748 the other has LEN2 chars at BEG2.
1749 Any sequence of whitespace matches any other sequence of whitespace.
1750 FIRST means these parts are the first of a macro definition;
1751 so ignore leading whitespace entirely.
1752 LAST means these parts are the last of a macro definition;
1753 so ignore trailing whitespace entirely. */
1756 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1758 U_CHAR
*beg1
, *beg2
;
1762 register U_CHAR
*end1
= beg1
+ len1
;
1763 register U_CHAR
*end2
= beg2
+ len2
;
1765 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1766 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1769 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1770 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1772 while (beg1
!= end1
&& beg2
!= end2
) {
1773 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1774 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1775 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1776 } else if (*beg1
== *beg2
) {
1780 return (beg1
!= end1
) || (beg2
!= end2
);
1783 /* Process a #define command.
1784 BUF points to the contents of the #define command, as a contiguous string.
1785 LIMIT points to the first character past the end of the definition.
1786 KEYWORD is the keyword-table entry for #define,
1787 or NULL for a "predefined" macro. */
1790 do_define (pfile
, keyword
, buf
, limit
)
1792 struct directive
*keyword
;
1793 U_CHAR
*buf
, *limit
;
1800 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1801 if (pcp_outfile
&& keyword
)
1802 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1805 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1809 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1811 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1814 /* Redefining a precompiled key is ok. */
1815 if (hp
->type
== T_PCSTRING
)
1817 /* Redefining a macro is ok if the definitions are the same. */
1818 else if (hp
->type
== T_MACRO
)
1819 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
1820 /* Redefining a constant is ok with -D. */
1821 else if (hp
->type
== T_CONST
)
1822 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1823 /* Print the warning if it's not ok. */
1826 U_CHAR
*msg
; /* what pain... */
1828 /* If we are passing through #define and #undef directives, do
1829 that for this re-definition now. */
1830 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1831 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1833 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1835 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1836 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1837 cpp_pedwarn (pfile
, msg
);
1838 if (hp
->type
== T_MACRO
)
1839 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1840 "this is the location of the previous definition");
1842 /* Replace the old definition. */
1844 hp
->value
.defn
= mdef
.defn
;
1848 /* If we are passing through #define and #undef directives, do
1849 that for this new definition now. */
1850 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1851 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1852 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1853 (char *) mdef
.defn
, hashcode
);
1863 /* This structure represents one parsed argument in a macro call.
1864 `raw' points to the argument text as written (`raw_length' is its length).
1865 `expanded' points to the argument's macro-expansion
1866 (its length is `expand_length').
1867 `stringified_length' is the length the argument would have
1869 `use_count' is the number of times this macro arg is substituted
1870 into the macro. If the actual use count exceeds 10,
1871 the value stored is 10. */
1873 /* raw and expanded are relative to ARG_BASE */
1874 #define ARG_BASE ((pfile)->token_buffer)
1877 /* Strings relative to pfile->token_buffer */
1878 long raw
, expanded
, stringified
;
1879 int raw_length
, expand_length
;
1880 int stringified_length
;
1887 cpp_push_buffer (pfile
, buffer
, length
)
1892 #ifdef STATIC_BUFFERS
1893 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1894 if (buf
== pfile
->buffer_stack
)
1895 fatal ("%s: macro or `#include' recursion too deep", buf
->fname
);
1897 bzero ((char *) buf
, sizeof (cpp_buffer
));
1898 CPP_BUFFER (pfile
) = buf
;
1900 register cpp_buffer
*buf
= (cpp_buffer
*) xmalloc (sizeof(cpp_buffer
));
1901 bzero ((char *) buf
, sizeof (cpp_buffer
));
1902 CPP_PREV_BUFFER (buf
) = CPP_BUFFER (pfile
);
1903 CPP_BUFFER (pfile
) = buf
;
1905 buf
->if_stack
= pfile
->if_stack
;
1906 buf
->cleanup
= null_cleanup
;
1907 buf
->underflow
= null_underflow
;
1908 buf
->buf
= buf
->cur
= buffer
;
1909 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1915 cpp_pop_buffer (pfile
)
1918 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1919 #ifdef STATIC_BUFFERS
1920 (*buf
->cleanup
) (buf
, pfile
);
1921 return ++CPP_BUFFER (pfile
);
1923 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (buf
);
1924 (*buf
->cleanup
) (buf
, pfile
);
1925 CPP_BUFFER (pfile
) = next_buf
;
1931 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1932 Pop the buffer when done. */
1935 cpp_scan_buffer (pfile
)
1938 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1941 enum cpp_token token
= cpp_get_token (pfile
);
1942 if (token
== CPP_EOF
) /* Should not happen ... */
1944 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1946 cpp_pop_buffer (pfile
);
1953 * Rescan a string (which may have escape marks) into pfile's buffer.
1954 * Place the result in pfile->token_buffer.
1956 * The input is copied before it is scanned, so it is safe to pass
1957 * it something from the token_buffer that will get overwritten
1958 * (because it follows CPP_WRITTEN). This is used by do_include.
1962 cpp_expand_to_buffer (pfile
, buf
, length
)
1967 register cpp_buffer
*ip
;
1969 U_CHAR
*limit
= buf
+ length
;
1972 int odepth
= indepth
;
1978 /* Set up the input on the input stack. */
1980 buf1
= (U_CHAR
*) alloca (length
+ 1);
1982 register U_CHAR
*p1
= buf
;
1983 register U_CHAR
*p2
= buf1
;
1990 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1991 ip
->has_escapes
= 1;
1993 ip
->lineno
= obuf
.lineno
= 1;
1996 /* Scan the input, create the output. */
1997 cpp_scan_buffer (pfile
);
2000 if (indepth
!= odepth
)
2004 CPP_NUL_TERMINATE (pfile
);
2009 adjust_position (buf
, limit
, linep
, colp
)
2019 (*linep
)++, (*colp
) = 1;
2025 /* Move line_base forward, updating lineno and colno. */
2028 update_position (pbuf
)
2029 register cpp_buffer
*pbuf
;
2031 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
2032 unsigned char *new_pos
= pbuf
->cur
;
2033 register struct parse_marker
*mark
;
2034 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
2036 if (pbuf
->buf
+ mark
->position
< new_pos
)
2037 new_pos
= pbuf
->buf
+ mark
->position
;
2039 pbuf
->line_base
+= new_pos
- old_pos
;
2040 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
2044 cpp_buf_line_and_col (pbuf
, linep
, colp
)
2045 register cpp_buffer
*pbuf
;
2053 *linep
= pbuf
->lineno
;
2054 *colp
= pbuf
->colno
;
2055 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2064 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2067 cpp_file_buffer (pfile
)
2070 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2072 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2073 if (ip
->fname
!= NULL
)
2079 count_newlines (buf
, limit
)
2080 register U_CHAR
*buf
;
2081 register U_CHAR
*limit
;
2083 register long count
= 0;
2094 * write out a #line command, for instance, after an #include file.
2095 * If CONDITIONAL is nonzero, we can omit the #line if it would
2096 * appear to be a no-op, and we can output a few newlines instead
2097 * if we want to increase the line number by a small amount.
2098 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2102 output_line_command (pfile
, conditional
, file_change
)
2105 enum file_change_code file_change
;
2108 char *line_cmd_buf
, *line_end
;
2110 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2112 if (ip
->fname
== NULL
|| CPP_OPTIONS (pfile
)->no_output
) {
2116 update_position (ip
);
2117 line
= CPP_BUFFER (pfile
)->lineno
;
2118 col
= CPP_BUFFER (pfile
)->colno
;
2119 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2121 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2125 if (line
== pfile
->lineno
)
2128 /* If the inherited line number is a little too small,
2129 output some newlines instead of a #line command. */
2130 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2131 CPP_RESERVE (pfile
, 20);
2132 while (line
> pfile
->lineno
) {
2133 CPP_PUTC_Q (pfile
, '\n');
2141 /* Don't output a line number of 0 if we can help it. */
2142 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2143 && *ip
->bufp
== '\n') {
2149 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2151 #ifdef OUTPUT_LINE_COMMANDS
2152 static char sharp_line
[] = "#line ";
2154 static char sharp_line
[] = "# ";
2156 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2159 sprintf (CPP_PWRITTEN (pfile
), "%d ", line
);
2160 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2162 quote_string (pfile
, ip
->nominal_fname
);
2163 if (file_change
!= same_file
) {
2164 CPP_PUTC_Q (pfile
, ' ');
2165 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2167 /* Tell cc1 if following text comes from a system header file. */
2168 if (ip
->system_header_p
) {
2169 CPP_PUTC_Q (pfile
, ' ');
2170 CPP_PUTC_Q (pfile
, '3');
2172 #ifndef NO_IMPLICIT_EXTERN_C
2173 /* Tell cc1plus if following text should be treated as C. */
2174 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2175 CPP_PUTC_Q (pfile
, ' ');
2176 CPP_PUTC_Q (pfile
, '4');
2179 CPP_PUTC_Q (pfile
, '\n');
2180 pfile
->lineno
= line
;
2184 * Parse a macro argument and append the info on PFILE's token_buffer.
2185 * REST_ARGS means to absorb the rest of the args.
2186 * Return nonzero to indicate a syntax error.
2189 static enum cpp_token
2190 macarg (pfile
, rest_args
)
2195 enum cpp_token token
;
2196 long arg_start
= CPP_WRITTEN (pfile
);
2197 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2198 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2200 /* Try to parse as much of the argument as exists at this
2201 input stack level. */
2202 pfile
->no_macro_expand
++;
2205 token
= cpp_get_token (pfile
);
2211 /* If we've hit end of file, it's an error (reported by caller).
2212 Ditto if it's the end of cpp_expand_to_buffer text.
2213 If we've hit end of macro, just continue. */
2214 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2225 /* if we've returned to lowest level and
2226 we aren't absorbing all args */
2227 if (paren
== 0 && rest_args
== 0)
2231 /* Remove ',' or ')' from argument buffer. */
2232 CPP_ADJUST_WRITTEN (pfile
, -1);
2239 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2240 pfile
->no_macro_expand
--;
2245 /* Turn newlines to spaces in the string of length LENGTH at START,
2246 except inside of string constants.
2247 The string is copied into itself with its beginning staying fixed. */
2250 change_newlines (start
, length
)
2254 register U_CHAR
*ibp
;
2255 register U_CHAR
*obp
;
2256 register U_CHAR
*limit
;
2260 limit
= start
+ length
;
2263 while (ibp
< limit
) {
2264 *obp
++ = c
= *ibp
++;
2269 /* Notice and skip strings, so that we don't delete newlines in them. */
2272 while (ibp
< limit
) {
2273 *obp
++ = c
= *ibp
++;
2276 if (c
== '\n' && quotec
== '\'')
2292 if (!pfile
->timebuf
) {
2293 time_t t
= time ((time_t *)0);
2294 pfile
->timebuf
= localtime (&t
);
2296 return pfile
->timebuf
;
2299 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2300 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2304 * expand things like __FILE__. Place the expansion into the output
2305 * buffer *without* rescanning.
2309 special_symbol (hp
, pfile
)
2316 cpp_buffer
*ip
= NULL
;
2319 int paren
= 0; /* For special `defined' keyword */
2322 if (pcp_outfile
&& pcp_inside_if
2323 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2325 "Predefined macro `%s' used inside `#if' during precompilation",
2329 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2333 cpp_error (pfile
, "cccp error: not in any file?!");
2334 return; /* the show must go on */
2336 if (ip
->fname
!= NULL
)
2346 if (hp
->type
== T_BASE_FILE
)
2348 while (CPP_PREV_BUFFER (ip
))
2349 ip
= CPP_PREV_BUFFER (ip
);
2351 string
= ip
->nominal_fname
;
2355 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2356 quote_string (pfile
, string
);
2360 case T_INCLUDE_LEVEL
:
2362 for (ip
= CPP_BUFFER (pfile
); ip
!= NULL
; ip
= CPP_PREV_BUFFER (ip
))
2363 if (ip
->fname
!= NULL
)
2366 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2367 sprintf (buf
, "%d", true_indepth
- 1);
2371 buf
= (char *) alloca (3 + strlen (version_string
));
2372 sprintf (buf
, "\"%s\"", version_string
);
2375 #ifndef NO_BUILTIN_SIZE_TYPE
2381 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2382 case T_PTRDIFF_TYPE
:
2388 buf
= CPP_WCHAR_TYPE (pfile
);
2391 case T_USER_LABEL_PREFIX_TYPE
:
2392 buf
= USER_LABEL_PREFIX
;
2395 case T_REGISTER_PREFIX_TYPE
:
2396 buf
= REGISTER_PREFIX
;
2400 buf
= (char *) alloca (4 * sizeof (int));
2401 sprintf (buf
, "%d", hp
->value
.ival
);
2403 if (pcp_inside_if
&& pcp_outfile
)
2404 /* Output a precondition for this macro use */
2405 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2411 long line
= ip
->lineno
;
2412 long col
= ip
->colno
;
2413 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2415 buf
= (char *) alloca (10);
2416 sprintf (buf
, "%d", line
);
2422 buf
= (char *) alloca (20);
2423 timebuf
= timestamp (pfile
);
2424 if (hp
->type
== T_DATE
)
2425 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2426 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2428 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2432 case T_SPEC_DEFINED
:
2433 buf
= " 0 "; /* Assume symbol is not defined */
2434 ip
= CPP_BUFFER (pfile
);
2435 SKIP_WHITE_SPACE (ip
->cur
);
2436 if (*ip
->cur
== '(')
2439 ip
->cur
++; /* Skip over the paren */
2440 SKIP_WHITE_SPACE (ip
->cur
);
2443 if (!is_idstart
[*ip
->cur
])
2445 if (hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1))
2448 if (pcp_outfile
&& pcp_inside_if
2449 && (hp
->type
== T_CONST
2450 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2451 /* Output a precondition for this macro use. */
2452 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2458 if (pcp_outfile
&& pcp_inside_if
)
2460 /* Output a precondition for this macro use */
2461 U_CHAR
*cp
= ip
->bufp
;
2462 fprintf (pcp_outfile
, "#undef ");
2463 while (is_idchar
[*cp
]) /* Ick! */
2464 fputc (*cp
++, pcp_outfile
);
2465 putc ('\n', pcp_outfile
);
2468 while (is_idchar
[*ip
->cur
])
2470 SKIP_WHITE_SPACE (ip
->cur
);
2473 if (*ip
->cur
!= ')')
2481 cpp_error (pfile
, "`defined' without an identifier");
2485 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2489 CPP_RESERVE (pfile
, len
+ 1);
2490 CPP_PUTS_Q (pfile
, buf
, len
);
2491 CPP_NUL_TERMINATE_Q (pfile
);
2496 /* Initialize the built-in macros. */
2499 initialize_builtins (pfile
)
2502 install ("__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2503 install ("__DATE__", -1, T_DATE
, 0, 0, -1);
2504 install ("__FILE__", -1, T_FILE
, 0, 0, -1);
2505 install ("__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2506 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2507 install ("__VERSION__", -1, T_VERSION
, 0, 0, -1);
2508 #ifndef NO_BUILTIN_SIZE_TYPE
2509 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2511 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2512 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2514 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2515 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2516 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2517 install ("__TIME__", -1, T_TIME
, 0, 0, -1);
2518 if (!CPP_TRADITIONAL (pfile
))
2519 install ("__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2520 if (CPP_OPTIONS (pfile
)->objc
)
2521 install ("__OBJC__", -1, T_CONST
, 1, 0, -1);
2522 /* This is supplied using a -D by the compiler driver
2523 so that it is present only when truly compiling with GNU C. */
2524 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2526 if (CPP_OPTIONS (pfile
)->debug_output
)
2528 char directive
[2048];
2529 register struct directive
*dp
= &directive_table
[0];
2530 struct tm
*timebuf
= timestamp (pfile
);
2531 cpp_buffer
*pbuffer
= CPP_BUFFER (pfile
);
2533 while (CPP_PREV_BUFFER (pbuffer
))
2534 pbuffer
= CPP_PREV_BUFFER (pbuffer
);
2535 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
2536 pbuffer
->nominal_fname
);
2537 output_line_command (pfile
, 0, same_file
);
2538 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2540 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
2541 output_line_command (pfile
, 0, same_file
);
2542 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2544 #ifndef NO_BUILTIN_SIZE_TYPE
2545 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
2546 output_line_command (pfile
, 0, same_file
);
2547 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2550 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2551 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
2552 output_line_command (pfile
, 0, same_file
);
2553 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2556 sprintf (directive
, " __WCHAR_TYPE__ %s\n", CPP_WCHAR_TYPE (pfile
));
2557 output_line_command (pfile
, 0, same_file
);
2558 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2560 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
2561 monthnames
[timebuf
->tm_mon
],
2562 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2563 output_line_command (pfile
, 0, same_file
);
2564 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2566 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
2567 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
2568 output_line_command (pfile
, 0, same_file
);
2569 pass_thru_directive (directive
, &directive
[strlen (directive
)], pfile
, dp
);
2571 if (!CPP_TRADITIONAL (pfile
))
2573 sprintf (directive
, " __STDC__ 1");
2574 output_line_command (pfile
, 0, same_file
);
2575 pass_thru_directive (directive
, &directive
[strlen (directive
)],
2578 if (CPP_OPTIONS (pfile
)->objc
)
2580 sprintf (directive
, " __OBJC__ 1");
2581 output_line_command (pfile
, 0, same_file
);
2582 pass_thru_directive (directive
, &directive
[strlen (directive
)],
2588 /* Return 1 iff a token ending in C1 followed directly by a token C2
2589 could cause mis-tokenization. */
2592 unsafe_chars (c1
, c2
)
2598 if (c2
== c1
|| c2
== '=')
2602 case '0': case '1': case '2': case '3': case '4':
2603 case '5': case '6': case '7': case '8': case '9':
2605 if (c2
== '-' || c2
== '+')
2606 return 1; /* could extend a pre-processing number */
2609 if (c2
== '\'' || c2
== '\"')
2610 return 1; /* Could turn into L"xxx" or L'xxx'. */
2614 case 'a': case 'b': case 'c': case 'd': case 'f':
2615 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2616 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2617 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2619 case 'A': case 'B': case 'C': case 'D': case 'F':
2620 case 'G': case 'H': case 'I': case 'J': case 'K':
2621 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2622 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2624 /* We're in the middle of either a name or a pre-processing number. */
2625 return (is_idchar
[c2
] || c2
== '.');
2626 case '<': case '>': case '!': case '%': case '#': case ':':
2627 case '^': case '&': case '|': case '*': case '/': case '=':
2628 return (c2
== c1
|| c2
== '=');
2633 /* Expand a macro call.
2634 HP points to the symbol that is the macro being called.
2635 Put the result of expansion onto the input stack
2636 so that subsequent input by our caller will use it.
2638 If macro wants arguments, caller has already verified that
2639 an argument list follows; arguments come from the input stack. */
2642 macroexpand (pfile
, hp
)
2647 DEFINITION
*defn
= hp
->value
.defn
;
2648 register U_CHAR
*xbuf
;
2649 long start_line
, start_column
;
2651 struct argdata
*args
;
2652 long old_written
= CPP_WRITTEN (pfile
);
2654 int start_line
= instack
[indepth
].lineno
;
2656 int rest_args
, rest_zero
;
2660 CHECK_DEPTH (return;);
2664 /* This macro is being used inside a #if, which means it must be */
2665 /* recorded as a precondition. */
2666 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2667 dump_single_macro (hp
, pcp_outfile
);
2670 pfile
->output_escapes
++;
2671 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2673 nargs
= defn
->nargs
;
2677 enum cpp_token token
;
2679 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2681 for (i
= 0; i
< nargs
; i
++)
2683 args
[i
].raw
= args
[i
].expanded
= 0;
2684 args
[i
].raw_length
= 0;
2685 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2686 args
[i
].use_count
= 0;
2689 /* Parse all the macro args that are supplied. I counts them.
2690 The first NARGS args are stored in ARGS.
2691 The rest are discarded. If rest_args is set then we assume
2692 macarg absorbed the rest of the args. */
2696 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2701 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2703 /* if we are working on last arg which absorbs rest of args... */
2704 if (i
== nargs
- 1 && defn
->rest_args
)
2706 args
[i
].raw
= CPP_WRITTEN (pfile
);
2707 token
= macarg (pfile
, rest_args
);
2708 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2709 args
[i
].newlines
= 0; /* FIXME */
2712 token
= macarg (pfile
, 0);
2713 if (token
== CPP_EOF
|| token
== CPP_POP
)
2715 cpp_error_with_line (pfile
, start_line
, start_column
,
2716 "unterminated macro call");
2720 } while (token
== CPP_COMMA
);
2722 /* If we got one arg but it was just whitespace, call that 0 args. */
2725 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2726 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2727 /* cpp.texi says for foo ( ) we provide one argument.
2728 However, if foo wants just 0 arguments, treat this as 0. */
2730 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2735 /* Don't output an error message if we have already output one for
2736 a parse error above. */
2738 if (nargs
== 0 && i
> 0)
2740 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2744 /* traditional C allows foo() if foo wants one argument. */
2745 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2747 /* the rest args token is allowed to absorb 0 tokens */
2748 else if (i
== nargs
- 1 && defn
->rest_args
)
2751 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2753 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2755 cpp_error (pfile
, "macro `%s' used with only %d args",
2761 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2765 /* If macro wants zero args, we parsed the arglist for checking only.
2766 Read directly from the macro definition. */
2769 xbuf
= defn
->expansion
;
2770 xbuf_len
= defn
->length
;
2774 register U_CHAR
*exp
= defn
->expansion
;
2775 register int offset
; /* offset in expansion,
2776 copied a piece at a time */
2777 register int totlen
; /* total amount of exp buffer filled so far */
2779 register struct reflist
*ap
, *last_ap
;
2781 /* Macro really takes args. Compute the expansion of this call. */
2783 /* Compute length in characters of the macro's expansion.
2784 Also count number of times each arg is used. */
2785 xbuf_len
= defn
->length
;
2786 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2790 register struct argdata
*arg
= &args
[ap
->argno
];
2791 /* Stringify it it hasn't already been */
2792 if (arg
->stringified_length
< 0)
2794 int arglen
= arg
->raw_length
;
2798 /* Initially need_space is -1. Otherwise, 1 means the
2799 previous character was a space, but we suppressed it;
2800 0 means the previous character was a non-space. */
2801 int need_space
= -1;
2803 arg
->stringified
= CPP_WRITTEN (pfile
);
2804 if (!CPP_TRADITIONAL (pfile
))
2805 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2806 for (; i
< arglen
; i
++)
2808 c
= (ARG_BASE
+ arg
->raw
)[i
];
2812 /* Internal sequences of whitespace are replaced by
2813 one space except within an string or char token.*/
2816 if (CPP_WRITTEN (pfile
) > arg
->stringified
2817 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2819 /* "@ " escape markers are removed */
2820 CPP_ADJUST_WRITTEN (pfile
, -1);
2823 if (need_space
== 0)
2827 else if (need_space
> 0)
2828 CPP_PUTC (pfile
, ' ');
2843 else if (c
== '\"' || c
== '\'')
2847 /* Escape these chars */
2848 if (c
== '\"' || (in_string
&& c
== '\\'))
2849 CPP_PUTC (pfile
, '\\');
2851 CPP_PUTC (pfile
, c
);
2854 CPP_RESERVE (pfile
, 4);
2855 sprintf (CPP_PWRITTEN (pfile
), "\\%03o",
2857 CPP_ADJUST_WRITTEN (pfile
, 4);
2860 if (!CPP_TRADITIONAL (pfile
))
2861 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2862 arg
->stringified_length
2863 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2865 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2867 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2868 /* Add 4 for two newline-space markers to prevent
2869 token concatenation. */
2870 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2873 /* We have an ordinary (expanded) occurrence of the arg.
2874 So compute its expansion, if we have not already. */
2875 if (args
[ap
->argno
].expand_length
< 0)
2877 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2878 cpp_expand_to_buffer (pfile
,
2879 ARG_BASE
+ args
[ap
->argno
].raw
,
2880 args
[ap
->argno
].raw_length
);
2882 args
[ap
->argno
].expand_length
2883 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2886 /* Add 4 for two newline-space markers to prevent
2887 token concatenation. */
2888 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2890 if (args
[ap
->argno
].use_count
< 10)
2891 args
[ap
->argno
].use_count
++;
2894 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2896 /* Generate in XBUF the complete expansion
2897 with arguments substituted in.
2898 TOTLEN is the total size generated so far.
2899 OFFSET is the index in the definition
2900 of where we are copying from. */
2901 offset
= totlen
= 0;
2902 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2903 last_ap
= ap
, ap
= ap
->next
)
2905 register struct argdata
*arg
= &args
[ap
->argno
];
2906 int count_before
= totlen
;
2908 /* Add chars to XBUF. */
2909 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2910 xbuf
[totlen
++] = exp
[offset
];
2912 /* If followed by an empty rest arg with concatenation,
2913 delete the last run of nonwhite chars. */
2914 if (rest_zero
&& totlen
> count_before
2915 && ((ap
->rest_args
&& ap
->raw_before
)
2916 || (last_ap
!= NULL
&& last_ap
->rest_args
2917 && last_ap
->raw_after
)))
2919 /* Delete final whitespace. */
2920 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2923 /* Delete the nonwhites before them. */
2924 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2928 if (ap
->stringify
!= 0)
2930 bcopy (ARG_BASE
+ arg
->stringified
,
2931 xbuf
+ totlen
, arg
->stringified_length
);
2932 totlen
+= arg
->stringified_length
;
2934 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2936 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2937 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2940 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2941 while (p1
!= l1
&& is_idchar
[*p1
])
2942 xbuf
[totlen
++] = *p1
++;
2946 /* Arg is concatenated after: delete trailing whitespace,
2947 whitespace markers, and no-reexpansion markers. */
2950 if (is_space
[l1
[-1]]) l1
--;
2951 else if (l1
[-1] == '-')
2953 U_CHAR
*p2
= l1
- 1;
2954 /* If a `-' is preceded by an odd number of newlines then it
2955 and the last newline are a no-reexpansion marker. */
2956 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2957 if ((l1
- 1 - p2
) & 1) {
2966 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2971 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2972 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2973 && !CPP_TRADITIONAL(pfile
)
2974 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2976 xbuf
[totlen
++] = '@';
2977 xbuf
[totlen
++] = ' ';
2980 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2981 totlen
+= arg
->expand_length
;
2983 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2984 && !CPP_TRADITIONAL(pfile
)
2985 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2987 xbuf
[totlen
++] = '@';
2988 xbuf
[totlen
++] = ' ';
2991 /* If a macro argument with newlines is used multiple times,
2992 then only expand the newlines once. This avoids creating
2993 output lines which don't correspond to any input line,
2994 which confuses gdb and gcov. */
2995 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2997 /* Don't bother doing change_newlines for subsequent
3001 = change_newlines (expanded
, arg
->expand_length
);
3005 if (totlen
> xbuf_len
)
3009 /* if there is anything left of the definition
3010 after handling the arg list, copy that in too. */
3012 for (i
= offset
; i
< defn
->length
; i
++)
3014 /* if we've reached the end of the macro */
3017 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
3018 && last_ap
->raw_after
))
3019 xbuf
[totlen
++] = exp
[i
];
3027 pfile
->output_escapes
--;
3029 /* Now put the expansion on the input stack
3030 so our caller will commence reading from it. */
3031 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
3032 CPP_BUFFER (pfile
)->has_escapes
= 1;
3034 /* Pop the space we've used in the token_buffer for argument expansion. */
3035 CPP_SET_WRITTEN (pfile
, old_written
);
3037 /* Recursive macro use sometimes works traditionally.
3038 #define foo(x,y) bar (x (y,0), y)
3041 if (!CPP_TRADITIONAL (pfile
))
3042 hp
->type
= T_DISABLED
;
3046 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
3048 register U_CHAR
*xbuf
;
3052 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
3053 mbuf
->cleanup
= macro_cleanup
;
3056 /* The first chars of the expansion should be a "@ " added by
3057 collect_expansion. This is to prevent accidental token-pasting
3058 between the text preceding the macro invocation, and the macro
3061 We would like to avoid adding unneeded spaces (for the sake of
3062 tools that use cpp, such as imake). In some common cases we can
3063 tell that it is safe to omit the space.
3065 The character before the macro invocation cannot have been an
3066 idchar (or else it would have been pasted with the idchars of
3067 the macro name). Therefore, if the first non-space character
3068 of the expansion is an idchar, we do not need the extra space
3069 to prevent token pasting.
3071 Also, we don't need the extra space if the first char is '(',
3072 or some other (less common) characters. */
3074 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3075 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3076 || xbuf
[2] == '\"'))
3080 /* Like cpp_get_token, except that it does not read past end-of-line.
3081 Also, horizontal space is skipped, and macros are popped. */
3083 static enum cpp_token
3084 get_directive_token (pfile
)
3089 long old_written
= CPP_WRITTEN (pfile
);
3090 enum cpp_token token
;
3091 cpp_skip_hspace (pfile
);
3092 if (PEEKC () == '\n')
3094 token
= cpp_get_token (pfile
);
3098 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3100 /* ... else fall though ... */
3101 case CPP_HSPACE
: case CPP_COMMENT
:
3102 CPP_SET_WRITTEN (pfile
, old_written
);
3110 /* Handle #include and #import.
3111 This function expects to see "fname" or <fname> on the input.
3113 The input is normally in part of the output_buffer following
3114 CPP_WRITTEN, and will get overwritten by output_line_command.
3115 I.e. in input file specification has been popped by handle_directive.
3119 do_include (pfile
, keyword
, unused1
, unused2
)
3121 struct directive
*keyword
;
3122 U_CHAR
*unused1
, *unused2
;
3124 int importing
= (keyword
->type
== T_IMPORT
);
3125 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3126 char *fname
; /* Dynamically allocated fname buffer */
3129 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3130 enum cpp_token token
;
3132 /* Chain of dirs to search */
3133 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3134 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3135 struct file_name_list
*searchptr
= 0;
3136 long old_written
= CPP_WRITTEN (pfile
);
3140 int f
; /* file number */
3142 int retried
= 0; /* Have already tried macro
3143 expanding the include line*/
3144 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3149 f
= -1; /* JF we iz paranoid! */
3151 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3152 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3153 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3155 pfile
->import_warning
= 1;
3156 cpp_warning (pfile
, "using `#import' is not recommended");
3157 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3158 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3159 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3160 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3161 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3162 fprintf (stderr
, " ... <real contents of file> ...\n");
3163 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3164 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3165 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3166 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3169 pfile
->parsing_include_directive
++;
3170 token
= get_directive_token (pfile
);
3171 pfile
->parsing_include_directive
--;
3173 if (token
== CPP_STRING
)
3175 /* FIXME - check no trailing garbage */
3176 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3177 fend
= CPP_PWRITTEN (pfile
) - 1;
3178 if (fbeg
[-1] == '<')
3181 /* If -I-, start with the first -I dir after the -I-. */
3182 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3183 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3185 /* If -I- was specified, don't search current dir, only spec'd ones. */
3186 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3189 /* We have "filename". Figure out directory this source
3190 file is coming from and put it on the front of the list. */
3192 for (fp
= CPP_BUFFER (pfile
); fp
!= NULL
; fp
= CPP_PREV_BUFFER (fp
))
3197 if ((nam
= fp
->nominal_fname
) != NULL
)
3199 /* Found a named file. Figure out dir of the file,
3200 and put it in front of the search list. */
3201 dsp
[0].next
= search_start
;
3204 ep
= rindex (nam
, '/');
3206 ep
= rindex (nam
, ']');
3207 if (ep
== NULL
) ep
= rindex (nam
, '>');
3208 if (ep
== NULL
) ep
= rindex (nam
, ':');
3209 if (ep
!= NULL
) ep
++;
3214 dsp
[0].fname
= (char *) alloca (n
+ 1);
3215 strncpy (dsp
[0].fname
, nam
, n
);
3216 dsp
[0].fname
[n
] = '\0';
3217 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3218 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3222 dsp
[0].fname
= 0; /* Current directory */
3224 dsp
[0].got_name_map
= 0;
3231 else if (token
== CPP_NAME
)
3234 * Support '#include xyz' like VAX-C to allow for easy use of all the
3235 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3236 * code from case '<' is repeated here) and generates a warning.
3239 "VAX-C-style include specification found, use '#include <filename.h>' !");
3241 /* If -I-, start with the first -I dir after the -I-. */
3242 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3243 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3244 fbeg
= pfile
->token_buffer
+ old_written
;
3245 fend
= CPP_PWRITTEN (pfile
);
3251 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3252 CPP_SET_WRITTEN (pfile
, old_written
);
3253 skip_rest_of_line (pfile
);
3259 token
= get_directive_token (pfile
);
3260 if (token
!= CPP_VSPACE
)
3262 cpp_error (pfile
, "junk at end of `#include'");
3263 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3264 token
= get_directive_token (pfile
);
3267 /* For #include_next, skip in the search path
3268 past the dir in which the containing file was found. */
3272 for (fp
= CPP_BUFFER (pfile
); fp
!= NULL
; fp
= CPP_PREV_BUFFER (fp
))
3273 if (fp
->fname
!= NULL
)
3275 /* fp->dir is null if the containing file was specified with
3276 an absolute file name. In that case, don't skip anything. */
3277 if (fp
->dir
== SELF_DIR_DUMMY
)
3278 search_start
= CPP_OPTIONS (pfile
)->include
;
3280 search_start
= fp
->dir
->next
;
3285 CPP_SET_WRITTEN (pfile
, old_written
);
3291 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3295 /* Allocate this permanently, because it gets stored in the definitions
3297 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3298 /* + 2 above for slash and terminating null. */
3299 /* + 2 added for '.h' on VMS (to support '#include filename') */
3301 /* If specified file name is absolute, just open it. */
3304 strncpy (fname
, fbeg
, flen
);
3306 if (redundant_include_p (pfile
, fname
))
3309 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3311 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3313 return 0; /* Already included this file */
3315 /* Search directory path, trying to open the file.
3316 Copy each filename tried into FNAME. */
3318 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3319 if (searchptr
->fname
) {
3320 /* The empty string in a search path is ignored.
3321 This makes it possible to turn off entirely
3322 a standard piece of the list. */
3323 if (searchptr
->fname
[0] == 0)
3325 strcpy (fname
, searchptr
->fname
);
3326 strcat (fname
, "/");
3327 fname
[strlen (fname
) + flen
] = 0;
3331 strncat (fname
, fbeg
, flen
);
3333 /* Change this 1/2 Unix 1/2 VMS file specification into a
3334 full VMS file specification */
3335 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3336 /* Fix up the filename */
3337 hack_vms_include_specification (fname
);
3339 /* This is a normal VMS filespec, so use it unchanged. */
3340 strncpy (fname
, fbeg
, flen
);
3342 /* if it's '#include filename', add the missing .h */
3343 if (index(fname
,'.')==NULL
) {
3344 strcat (fname
, ".h");
3348 /* ??? There are currently 3 separate mechanisms for avoiding processing
3349 of redundant include files: #import, #pragma once, and
3350 redundant_include_p. It would be nice if they were unified. */
3351 if (redundant_include_p (pfile
, fname
))
3354 f
= lookup_import (pfile
, fname
, searchptr
);
3356 f
= open_include_file (pfile
, fname
, searchptr
);
3358 return 0; /* Already included this file */
3360 else if (f
== -1 && errno
== EACCES
)
3361 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3371 /* A file that was not found. */
3372 strncpy (fname
, fbeg
, flen
);
3374 /* If generating dependencies and -MG was specified, we assume missing
3375 files are leaf files, living in the same directory as the source file
3376 or other similar place; these missing files may be generated from
3377 other files and may not exist yet (eg: y.tab.h). */
3379 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3380 && CPP_PRINT_DEPS (pfile
)
3381 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3383 /* If it was requested as a system header file,
3384 then assume it belongs in the first place to look for such. */
3387 for (searchptr
= search_start
; searchptr
;
3388 searchptr
= searchptr
->next
)
3390 if (searchptr
->fname
)
3394 if (searchptr
->fname
[0] == 0)
3396 p
= (char *) alloca (strlen (searchptr
->fname
)
3397 + strlen (fname
) + 2);
3398 strcpy (p
, searchptr
->fname
);
3401 deps_output (pfile
, p
, ' ');
3408 /* Otherwise, omit the directory, as if the file existed
3409 in the directory with the source. */
3410 deps_output (pfile
, fname
, ' ');
3413 /* If -M was specified, and this header file won't be added to the
3414 dependency list, then don't count this as an error, because we can
3415 still produce correct output. Otherwise, we can't produce correct
3416 output, because there may be dependencies we need inside the missing
3417 file, and we don't know what directory this missing file exists in.*/
3418 else if (CPP_PRINT_DEPS (pfile
)
3419 && (CPP_PRINT_DEPS (pfile
)
3420 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3421 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3422 else if (search_start
)
3423 cpp_error_from_errno (pfile
, fname
);
3425 cpp_error (pfile
, "No include path in which to find %s", fname
);
3428 /* Check to see if this include file is a once-only include file.
3431 struct file_name_list
* ptr
;
3433 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3434 if (!strcmp (ptr
->fname
, fname
)) {
3436 return 0; /* This file was once'd. */
3440 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3441 if (!strcmp (ptr
->fname
, fname
))
3442 break; /* This file was included before. */
3446 /* This is the first time for this file. */
3447 /* Add it to list of files included. */
3449 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3450 ptr
->control_macro
= 0;
3451 ptr
->c_system_include_path
= 0;
3452 ptr
->next
= pfile
->all_include_files
;
3453 pfile
->all_include_files
= ptr
;
3454 ptr
->fname
= savestring (fname
);
3455 ptr
->got_name_map
= 0;
3457 /* For -M, add this file to the dependencies. */
3458 if (CPP_PRINT_DEPS (pfile
)
3459 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3460 deps_output (pfile
, fname
, ' ');
3463 /* Handle -H option. */
3464 if (CPP_OPTIONS(pfile
)->print_include_names
)
3466 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3467 while ((buf
= CPP_PREV_BUFFER (buf
)) != NULL
)
3469 fprintf (stderr
, "%s\n", fname
);
3473 pfile
->system_include_depth
++;
3475 /* Actually process the file. */
3477 /* Record file on "seen" list for #import. */
3478 add_import (pfile
, f
, fname
);
3480 pcftry
= (char *) alloca (strlen (fname
) + 30);
3492 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3494 pcf
= open (pcftry
, O_RDONLY
, 0666);
3500 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3502 || stat_f
.st_dev
!= s
.st_dev
)
3504 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3505 /* Don't need it any more. */
3510 /* Don't need it at all. */
3515 } while (pcf
!= -1 && !pcfbuf
);
3519 /* Actually process the file */
3520 cpp_push_buffer (pfile
, NULL
, 0);
3521 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3522 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3524 output_line_command (pfile
, 0, enter_file
);
3525 pfile
->only_seen_white
= 2;
3529 pfile
->system_include_depth
--;
3534 /* Return nonzero if there is no need to include file NAME
3535 because it has already been included and it contains a conditional
3536 to make a repeated include do nothing. */
3539 redundant_include_p (pfile
, name
)
3543 struct file_name_list
*l
= pfile
->all_include_files
;
3544 for (; l
; l
= l
->next
)
3545 if (! strcmp (name
, l
->fname
)
3547 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3552 /* Return nonzero if the given FILENAME is an absolute pathname which
3553 designates a file within one of the known "system" include file
3554 directories. We assume here that if the given FILENAME looks like
3555 it is the name of a file which resides either directly in a "system"
3556 include file directory, or within any subdirectory thereof, then the
3557 given file must be a "system" include file. This function tells us
3558 if we should suppress pedantic errors/warnings for the given FILENAME.
3560 The value is 2 if the file is a C-language system header file
3561 for which C++ should (on most systems) assume `extern "C"'. */
3564 is_system_include (pfile
, filename
)
3566 register char *filename
;
3568 struct file_name_list
*searchptr
;
3570 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3571 searchptr
= searchptr
->next
)
3572 if (searchptr
->fname
) {
3573 register char *sys_dir
= searchptr
->fname
;
3574 register unsigned length
= strlen (sys_dir
);
3576 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3578 if (searchptr
->c_system_include_path
)
3589 * Install a name in the assertion hash table.
3591 * If LEN is >= 0, it is the length of the name.
3592 * Otherwise, compute the length by scanning the entire name.
3594 * If HASH is >= 0, it is the precomputed hash code.
3595 * Otherwise, compute the hash code.
3597 static ASSERTION_HASHNODE
*
3598 assertion_install (pfile
, name
, len
, hash
)
3604 register ASSERTION_HASHNODE
*hp
;
3605 register int i
, bucket
;
3606 register U_CHAR
*p
, *q
;
3608 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3609 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3611 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3612 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3613 pfile
->assertion_hashtab
[bucket
] = hp
;
3615 if (hp
->next
!= NULL
)
3616 hp
->next
->prev
= hp
;
3619 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3622 for (i
= 0; i
< len
; i
++)
3628 * find the most recent hash node for name name (ending with first
3629 * non-identifier char) installed by install
3631 * If LEN is >= 0, it is the length of the name.
3632 * Otherwise, compute the length by scanning the entire name.
3634 * If HASH is >= 0, it is the precomputed hash code.
3635 * Otherwise, compute the hash code.
3638 static ASSERTION_HASHNODE
*
3639 assertion_lookup (pfile
, name
, len
, hash
)
3645 register ASSERTION_HASHNODE
*bucket
;
3647 bucket
= pfile
->assertion_hashtab
[hash
];
3649 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3651 bucket
= bucket
->next
;
3657 delete_assertion (hp
)
3658 ASSERTION_HASHNODE
*hp
;
3660 struct tokenlist_list
*tail
;
3661 if (hp
->prev
!= NULL
)
3662 hp
->prev
->next
= hp
->next
;
3663 if (hp
->next
!= NULL
)
3664 hp
->next
->prev
= hp
->prev
;
3666 for (tail
= hp
->value
; tail
; )
3668 struct tokenlist_list
*next
= tail
->next
;
3669 free_token_list (tail
->tokens
);
3674 /* make sure that the bucket chain header that
3675 the deleted guy was on points to the right thing afterwards. */
3676 if (hp
== *hp
->bucket_hdr
)
3677 *hp
->bucket_hdr
= hp
->next
;
3682 /* Convert a character string literal into a nul-terminated string.
3683 The input string is [IN ... LIMIT).
3684 The result is placed in RESULT. RESULT can be the same as IN.
3685 The value returned in the end of the string written to RESULT,
3686 or NULL on error. */
3689 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3691 register U_CHAR
*result
, *in
, *limit
;
3711 char *bpc
= (char *) in
;
3712 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3713 in
= (U_CHAR
*) bpc
;
3715 *result
++ = (U_CHAR
)c
;
3718 /* else fall through */
3728 * interpret #line command. Remembers previously seen fnames
3729 * in its very own hash table.
3731 #define FNAME_HASHSIZE 37
3734 do_line (pfile
, keyword
)
3736 struct directive
*keyword
;
3738 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3740 long old_written
= CPP_WRITTEN (pfile
);
3741 enum file_change_code file_change
= same_file
;
3742 enum cpp_token token
;
3745 token
= get_directive_token (pfile
);
3747 if (token
!= CPP_NUMBER
3748 || !isdigit(pfile
->token_buffer
[old_written
]))
3750 cpp_error (pfile
, "invalid format `#line' command");
3751 goto bad_line_directive
;
3754 /* The Newline at the end of this line remains to be processed.
3755 To put the next line at the specified line number,
3756 we must store a line number now that is one less. */
3757 new_lineno
= atoi (pfile
->token_buffer
+ old_written
) - 1;
3758 CPP_SET_WRITTEN (pfile
, old_written
);
3760 /* NEW_LINENO is one less than the actual line number here. */
3761 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3762 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3764 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3765 if (PEEKC() && !is_space
[PEEKC()]) {
3766 cpp_error (pfile
, "invalid format `#line' command");
3767 goto bad_line_directive
;
3771 token
= get_directive_token (pfile
);
3773 if (token
== CPP_STRING
) {
3774 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3776 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3777 HASHNODE
*hp
, **hash_bucket
;
3782 /* Turn the file name, which is a character string literal,
3783 into a null-terminated string. Do this in place. */
3784 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3785 if (end_name
== NULL
)
3787 cpp_error (pfile
, "invalid format `#line' command");
3788 goto bad_line_directive
;
3791 fname_length
= end_name
- fname
;
3793 num_start
= CPP_WRITTEN (pfile
);
3794 token
= get_directive_token (pfile
);
3795 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3796 p
= pfile
->token_buffer
+ num_start
;
3797 if (CPP_PEDANTIC (pfile
))
3798 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3800 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3802 cpp_error (pfile
, "invalid format `#line' command");
3803 goto bad_line_directive
;
3806 file_change
= enter_file
;
3808 file_change
= leave_file
;
3810 ip
->system_header_p
= 1;
3811 else /* if (*p == 4) */
3812 ip
->system_header_p
= 2;
3814 CPP_SET_WRITTEN (pfile
, num_start
);
3815 token
= get_directive_token (pfile
);
3816 p
= pfile
->token_buffer
+ num_start
;
3817 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3818 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3819 token
= get_directive_token (pfile
);
3821 if (token
!= CPP_VSPACE
) {
3822 cpp_error (pfile
, "invalid format `#line' command");
3823 goto bad_line_directive
;
3828 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3829 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3830 if (hp
->length
== fname_length
&&
3831 strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3832 ip
->nominal_fname
= hp
->value
.cpval
;
3836 /* Didn't find it; cons up a new one. */
3837 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3838 hp
->next
= *hash_bucket
;
3841 hp
->length
= fname_length
;
3842 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3843 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3846 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3847 cpp_error (pfile
, "invalid format `#line' command");
3848 goto bad_line_directive
;
3851 ip
->lineno
= new_lineno
;
3853 skip_rest_of_line (pfile
);
3854 CPP_SET_WRITTEN (pfile
, old_written
);
3855 output_line_command (pfile
, 0, file_change
);
3860 * remove the definition of a symbol from the symbol table.
3861 * according to un*x /lib/cpp, it is not an error to undef
3862 * something that has no definitions, so it isn't one here either.
3866 do_undef (pfile
, keyword
, buf
, limit
)
3868 struct directive
*keyword
;
3869 U_CHAR
*buf
, *limit
;
3873 U_CHAR
*orig_buf
= buf
;
3876 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3877 if (pcp_outfile
&& keyword
)
3878 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3881 SKIP_WHITE_SPACE (buf
);
3882 sym_length
= check_macro_name (pfile
, buf
, "macro");
3884 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3886 /* If we are generating additional info for debugging (with -g) we
3887 need to pass through all effective #undef commands. */
3888 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3889 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3890 if (hp
->type
!= T_MACRO
)
3891 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3895 if (CPP_PEDANTIC (pfile
)) {
3897 SKIP_WHITE_SPACE (buf
);
3899 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3905 * Report an error detected by the program we are processing.
3906 * Use the text of the line in the error message.
3907 * (We use error because it prints the filename & line#.)
3911 do_error (pfile
, keyword
, buf
, limit
)
3913 struct directive
*keyword
;
3914 U_CHAR
*buf
, *limit
;
3916 int length
= limit
- buf
;
3917 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
3918 bcopy (buf
, copy
, length
);
3920 SKIP_WHITE_SPACE (copy
);
3921 cpp_error (pfile
, "#error %s", copy
);
3926 * Report a warning detected by the program we are processing.
3927 * Use the text of the line in the warning message, then continue.
3928 * (We use error because it prints the filename & line#.)
3932 do_warning (pfile
, keyword
, buf
, limit
)
3934 struct directive
*keyword
;
3935 U_CHAR
*buf
, *limit
;
3937 int length
= limit
- buf
;
3938 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
3939 bcopy (buf
, copy
, length
);
3941 SKIP_WHITE_SPACE (copy
);
3942 cpp_warning (pfile
, "#warning %s", copy
);
3946 /* Remember the name of the current file being read from so that we can
3947 avoid ever including it again. */
3953 cpp_buffer
*ip
= NULL
;
3954 struct file_name_list
*new;
3956 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3960 if (ip
->fname
!= NULL
)
3965 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3966 new->next
= pfile
->dont_repeat_files
;
3967 pfile
->dont_repeat_files
= new;
3968 new->fname
= savestring (ip
->fname
);
3969 new->control_macro
= 0;
3970 new->got_name_map
= 0;
3971 new->c_system_include_path
= 0;
3976 /* #ident has already been copied to the output file, so just ignore it. */
3979 do_ident (pfile
, keyword
, buf
, limit
)
3981 struct directive
*keyword
;
3982 U_CHAR
*buf
, *limit
;
3984 /* long old_written = CPP_WRITTEN (pfile);*/
3987 /* Allow #ident in system headers, since that's not user's fault. */
3988 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3989 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3991 /* Leave rest of line to be read by later calls to cpp_get_token. */
3996 /* #pragma and its argument line have already been copied to the output file.
3997 Just check for some recognized pragmas that need validation here. */
4000 do_pragma (pfile
, keyword
, buf
, limit
)
4002 struct directive
*keyword
;
4003 U_CHAR
*buf
, *limit
;
4005 while (*buf
== ' ' || *buf
== '\t')
4007 if (!strncmp (buf
, "once", 4)) {
4008 /* Allow #pragma once in system headers, since that's not the user's
4010 if (!CPP_BUFFER (pfile
)->system_header_p
)
4011 cpp_warning (pfile
, "`#pragma once' is obsolete");
4015 if (!strncmp (buf
, "implementation", 14)) {
4016 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4017 been included yet. */
4018 struct file_name_list
*ptr
;
4019 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
4021 SKIP_WHITE_SPACE (p
);
4022 if (*p
== '\n' || *p
!= '\"')
4026 p
= (U_CHAR
*) index (fname
, '\"');
4027 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
4029 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
4030 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
4031 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
4032 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
4034 "`#pragma implementation' for `%s' appears after file is included",
4043 /* This was a fun hack, but #pragma seems to start to be useful.
4044 By failing to recognize it, we pass it through unchanged to cc1. */
4047 * the behavior of the #pragma directive is implementation defined.
4048 * this implementation defines it as follows.
4055 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
4058 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4060 execl ("/usr/games/hack", "#pragma", 0);
4061 execl ("/usr/games/rogue", "#pragma", 0);
4062 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4063 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4065 fatal ("You are in a maze of twisty compiler features, all different");
4069 /* Just ignore #sccs, on systems where we define it at all. */
4072 do_sccs (pfile
, keyword
, buf
, limit
)
4074 struct directive
*keyword
;
4075 U_CHAR
*buf
, *limit
;
4077 if (CPP_PEDANTIC (pfile
))
4078 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4083 * handle #if command by
4084 * 1) inserting special `defined' keyword into the hash table
4085 * that gets turned into 0 or 1 by special_symbol (thus,
4086 * if the luser has a symbol called `defined' already, it won't
4087 * work inside the #if command)
4088 * 2) rescan the input into a temporary output buffer
4089 * 3) pass the output buffer to the yacc parser and collect a value
4090 * 4) clean up the mess left from steps 1 and 2.
4091 * 5) call conditional_skip to skip til the next #endif (etc.),
4092 * or not, depending on the value from step 3.
4096 do_if (pfile
, keyword
, buf
, limit
)
4098 struct directive
*keyword
;
4099 U_CHAR
*buf
, *limit
;
4101 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4102 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4107 * handle a #elif directive by not changing if_stack either.
4108 * see the comment above do_else.
4112 do_elif (pfile
, keyword
, buf
, limit
)
4114 struct directive
*keyword
;
4115 U_CHAR
*buf
, *limit
;
4117 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4118 cpp_error (pfile
, "`#elif' not within a conditional");
4121 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4122 cpp_error (pfile
, "`#elif' after `#else'");
4124 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4126 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4127 && strcmp (pfile
->if_stack
->fname
,
4128 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4129 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4130 fprintf (stderr
, ")\n");
4132 pfile
->if_stack
->type
= T_ELIF
;
4135 if (pfile
->if_stack
->if_succeeded
)
4136 skip_if_group (pfile
, 0);
4138 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4140 skip_if_group (pfile
, 0);
4142 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4143 output_line_command (pfile
, 1, same_file
);
4150 * evaluate a #if expression in BUF, of length LENGTH,
4151 * then parse the result as a C expression and return the value as an int.
4153 static HOST_WIDE_INT
4154 eval_if_expression (pfile
, buf
, length
)
4159 HASHNODE
*save_defined
;
4160 HOST_WIDE_INT value
;
4161 long old_written
= CPP_WRITTEN (pfile
);
4163 save_defined
= install ("defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4164 pfile
->pcp_inside_if
= 1;
4166 value
= cpp_parse_expr (pfile
);
4167 pfile
->pcp_inside_if
= 0;
4168 delete_macro (save_defined
); /* clean up special symbol */
4170 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4176 * routine to handle ifdef/ifndef. Try to look up the symbol,
4177 * then do or don't skip to the #endif/#else/#elif depending
4178 * on what directive is actually being processed.
4182 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4184 struct directive
*keyword
;
4185 U_CHAR
*unused1
, *unused2
;
4188 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4191 enum cpp_token token
;
4192 int start_of_file
= 0;
4193 U_CHAR
*control_macro
= 0;
4194 int old_written
= CPP_WRITTEN (pfile
);
4196 /* Detect a #ifndef at start of file (not counting comments). */
4197 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4198 start_of_file
= pfile
->only_seen_white
== 2;
4200 pfile
->no_macro_expand
++;
4201 token
= get_directive_token (pfile
);
4202 pfile
->no_macro_expand
--;
4204 ident
= pfile
->token_buffer
+ old_written
;
4205 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4206 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4208 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4210 skip
= (keyword
->type
== T_IFDEF
);
4211 if (! CPP_TRADITIONAL (pfile
))
4212 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4214 else if (token
== CPP_NAME
)
4216 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4217 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4218 if (start_of_file
&& !skip
)
4220 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4221 bcopy (ident
, control_macro
, ident_length
+ 1);
4226 skip
= (keyword
->type
== T_IFDEF
);
4227 if (! CPP_TRADITIONAL (pfile
))
4228 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4231 if (!CPP_TRADITIONAL (pfile
))
4233 cpp_skip_hspace (pfile
);
4235 if (c
!= EOF
&& c
!= '\n')
4236 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4238 skip_rest_of_line (pfile
);
4242 /* Output a precondition for this macro. */
4243 if (hp
&& hp
->value
.defn
->predefined
)
4244 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4247 fprintf (pcp_outfile
, "#undef ");
4248 while (is_idchar
[*cp
]) /* Ick! */
4249 fputc (*cp
++, pcp_outfile
);
4250 putc ('\n', pcp_outfile
);
4254 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4258 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4259 If this is a #ifndef starting at the beginning of a file,
4260 CONTROL_MACRO is the macro name tested by the #ifndef.
4261 Otherwise, CONTROL_MACRO is 0. */
4264 conditional_skip (pfile
, skip
, type
, control_macro
)
4267 enum node_type type
;
4268 U_CHAR
*control_macro
;
4270 IF_STACK_FRAME
*temp
;
4272 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4273 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4275 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4277 temp
->next
= pfile
->if_stack
;
4278 temp
->control_macro
= control_macro
;
4279 pfile
->if_stack
= temp
;
4281 pfile
->if_stack
->type
= type
;
4284 skip_if_group (pfile
, 0);
4287 ++pfile
->if_stack
->if_succeeded
;
4288 output_line_command (pfile
, 1, same_file
);
4293 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4294 * leaves input ptr at the sharp sign found.
4295 * If ANY is nonzero, return at next directive of any sort.
4298 skip_if_group (pfile
, any
)
4303 int at_beg_of_line
= 1;
4304 struct directive
*kt
;
4305 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4307 U_CHAR
*beg_of_line
= bp
;
4309 register int ident_length
;
4310 U_CHAR
*ident
, *after_ident
;
4311 struct parse_marker line_start_mark
;
4313 parse_set_mark (&line_start_mark
, pfile
);
4315 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4316 static char failed
[] = "#failed\n";
4317 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4319 output_line_command (pfile
, 1, same_file
);
4323 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4325 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4326 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4327 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4329 parse_move_mark (&line_start_mark
, pfile
);
4330 if (!CPP_TRADITIONAL (pfile
))
4331 cpp_skip_hspace (pfile
);
4335 int old_written
= CPP_WRITTEN (pfile
);
4336 cpp_skip_hspace (pfile
);
4338 parse_name (pfile
, GETC());
4339 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4340 ident
= pfile
->token_buffer
+ old_written
;
4341 pfile
->limit
= ident
;
4343 if (ident_length
== 0)
4344 goto not_a_directive
;
4346 /* Handle # followed by a line number. */
4348 /* Avoid error for `###' and similar cases unless -pedantic. */
4351 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4353 IF_STACK_FRAME
*temp
;
4354 if (ident_length
== kt
->length
4355 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4357 /* If we are asked to return on next directive, do so now. */
4367 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4368 temp
->next
= pfile
->if_stack
;
4369 pfile
->if_stack
= temp
;
4371 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4373 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4374 temp
->type
= kt
->type
;
4378 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4379 validate_else (pfile
,
4380 kt
->type
== T_ELSE
? "#else" : "#endif");
4382 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4385 "`#%s' not within a conditional", kt
->name
);
4388 else if (pfile
->if_stack
== save_if_stack
)
4389 goto done
; /* found what we came for */
4391 if (kt
->type
!= T_ENDIF
)
4393 if (pfile
->if_stack
->type
== T_ELSE
)
4394 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4395 pfile
->if_stack
->type
= kt
->type
;
4399 temp
= pfile
->if_stack
;
4400 pfile
->if_stack
= temp
->next
;
4407 /* Don't let erroneous code go by. */
4408 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4409 && CPP_PEDANTIC (pfile
))
4410 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4414 /* We're in the middle of a line. Skip the rest of it. */
4421 case '/': /* possible comment */
4422 c
= skip_comment (pfile
, NULL
);
4429 old
= CPP_WRITTEN (pfile
);
4430 cpp_get_token (pfile
);
4431 CPP_SET_WRITTEN (pfile
, old
);
4434 /* Char after backslash loses its special meaning. */
4435 if (PEEKC() == '\n')
4445 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4446 static char end_failed
[] = "#endfailed\n";
4447 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4450 pfile
->only_seen_white
= 1;
4451 parse_goto_mark (&line_start_mark
, pfile
);
4452 parse_clear_mark (&line_start_mark
);
4456 * handle a #else directive. Do this by just continuing processing
4457 * without changing if_stack ; this is so that the error message
4458 * for missing #endif's etc. will point to the original #if. It
4459 * is possible that something different would be better.
4463 do_else (pfile
, keyword
, buf
, limit
)
4465 struct directive
*keyword
;
4466 U_CHAR
*buf
, *limit
;
4468 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4470 if (CPP_PEDANTIC (pfile
))
4471 validate_else (pfile
, "#else");
4472 skip_rest_of_line (pfile
);
4474 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4475 cpp_error (pfile
, "`#else' not within a conditional");
4478 /* #ifndef can't have its special treatment for containing the whole file
4479 if it has a #else clause. */
4480 pfile
->if_stack
->control_macro
= 0;
4482 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4483 cpp_error (pfile
, "`#else' after `#else'");
4484 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4485 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4486 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4487 fprintf (stderr
, ")\n");
4489 pfile
->if_stack
->type
= T_ELSE
;
4492 if (pfile
->if_stack
->if_succeeded
)
4493 skip_if_group (pfile
, 0);
4495 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4496 output_line_command (pfile
, 1, same_file
);
4502 * unstack after #endif command
4506 do_endif (pfile
, keyword
, buf
, limit
)
4508 struct directive
*keyword
;
4509 U_CHAR
*buf
, *limit
;
4511 if (CPP_PEDANTIC (pfile
))
4512 validate_else (pfile
, "#endif");
4513 skip_rest_of_line (pfile
);
4515 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4516 cpp_error (pfile
, "unbalanced `#endif'");
4519 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4520 pfile
->if_stack
= temp
->next
;
4521 if (temp
->control_macro
!= 0)
4523 /* This #endif matched a #ifndef at the start of the file.
4524 See if it is at the end of the file. */
4525 struct parse_marker start_mark
;
4528 parse_set_mark (&start_mark
, pfile
);
4532 cpp_skip_hspace (pfile
);
4537 parse_goto_mark (&start_mark
, pfile
);
4538 parse_clear_mark (&start_mark
);
4542 /* If we get here, this #endif ends a #ifndef
4543 that contains all of the file (aside from whitespace).
4544 Arrange not to include the file again
4545 if the macro that was tested is defined.
4547 Do not do this for the top-level file in a -include or any
4548 file in a -imacros. */
4552 && ! (indepth
== 1 && pfile
->no_record_file
)
4553 && ! (pfile
->no_record_file
&& no_output
))
4556 struct file_name_list
*ifile
= pfile
->all_include_files
;
4558 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4560 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4562 ifile
->control_macro
= temp
->control_macro
;
4570 output_line_command (pfile
, 1, same_file
);
4575 /* When an #else or #endif is found while skipping failed conditional,
4576 if -pedantic was specified, this is called to warn about text after
4577 the command name. P points to the first char after the command name. */
4580 validate_else (pfile
, directive
)
4585 cpp_skip_hspace (pfile
);
4587 if (c
!= EOF
&& c
!= '\n')
4589 "text following `%s' violates ANSI standard", directive
);
4592 /* Get the next token, and add it to the text in pfile->token_buffer.
4593 Return the kind of token we got. */
4597 cpp_get_token (pfile
)
4600 register int c
, c2
, c3
;
4602 long start_line
, start_column
;
4603 enum cpp_token token
;
4604 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4605 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4611 if (CPP_BUFFER (pfile
)->seen_eof
)
4613 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4620 cpp_buffer
*next_buf
4621 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4622 CPP_BUFFER (pfile
)->seen_eof
= 1;
4623 if (CPP_BUFFER (pfile
)->nominal_fname
&& next_buf
!= 0)
4625 /* We're about to return from an #include file.
4626 Emit #line information now (as part of the CPP_POP) result.
4627 But the #line refers to the file we will pop to. */
4628 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4629 CPP_BUFFER (pfile
) = next_buf
;
4630 pfile
->input_stack_listing_current
= 0;
4631 output_line_command (pfile
, 0, leave_file
);
4632 CPP_BUFFER (pfile
) = cur_buffer
;
4642 struct parse_marker start_mark
;
4644 if (PEEKC () == '=')
4646 if (opts
->put_out_comments
)
4647 parse_set_mark (&start_mark
, pfile
);
4649 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4650 &start_line
, &start_column
);
4651 c
= skip_comment (pfile
, &newlines
);
4652 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4653 parse_clear_mark (&start_mark
);
4658 cpp_error_with_line (pfile
, start_line
, start_column
,
4659 "unterminated comment");
4662 c
= '/'; /* Initial letter of comment. */
4664 /* Comments are equivalent to spaces.
4665 For -traditional, a comment is equivalent to nothing. */
4666 if (opts
->put_out_comments
)
4668 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4670 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4671 int len
= pbuf
->cur
- start
;
4672 CPP_RESERVE(pfile
, 1 + len
);
4673 CPP_PUTC_Q (pfile
, c
);
4674 CPP_PUTS_Q (pfile
, start
, len
);
4675 pfile
->lineno
+= newlines
;
4676 parse_clear_mark (&start_mark
);
4679 else if (CPP_TRADITIONAL (pfile
))
4686 /* This may not work if cpp_get_token is called recursively,
4687 since many places look for horizontal space. */
4690 /* Copy the newlines into the output buffer, in order to
4691 avoid the pain of a #line every time a multiline comment
4693 CPP_RESERVE(pfile
, newlines
);
4694 while (--newlines
>= 0)
4696 CPP_PUTC_Q (pfile
, '\n');
4702 CPP_RESERVE(pfile
, 1);
4703 CPP_PUTC_Q (pfile
, ' ');
4707 if (opts
->for_lint
) {
4710 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4712 if (lintcmd
!= NULL
) {
4713 /* I believe it is always safe to emit this newline: */
4715 bcopy ("#pragma lint ", (char *) obp
, 13);
4717 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4722 bcopy (argbp
, (char *) obp
, arglen
);
4726 /* OK, now bring us back to the state we were in before we entered
4727 this branch. We need #line b/c the newline for the pragma
4728 could fuck things up. */
4729 output_line_command (pfile
, 0, same_file
);
4730 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4738 /* If this is expanding a macro definition, don't recognize
4739 preprocessor directives. */
4742 /* If this is expand_into_temp_buffer, recognize them
4743 only after an actual newline at this level,
4744 not at the beginning of the input level. */
4745 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4751 if (!pfile
->only_seen_white
)
4753 if (handle_directive (pfile
))
4754 return CPP_DIRECTIVE
;
4755 pfile
->only_seen_white
= 0;
4760 /* A single quoted string is treated like a double -- some
4761 programs (e.g., troff) are perverse this way */
4762 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4763 &start_line
, &start_column
);
4764 old_written
= CPP_WRITTEN (pfile
);
4766 CPP_PUTC (pfile
, c
);
4772 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4774 /* try harder: this string crosses a macro expansion
4775 boundary. This can happen naturally if -traditional.
4776 Otherwise, only -D can make a macro with an unmatched
4778 cpp_buffer
*next_buf
4779 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4780 (*CPP_BUFFER (pfile
)->cleanup
)
4781 (CPP_BUFFER (pfile
), pfile
);
4782 CPP_BUFFER (pfile
) = next_buf
;
4785 if (!CPP_TRADITIONAL (pfile
))
4787 cpp_error_with_line (pfile
, start_line
, start_column
,
4788 "unterminated string or character constant");
4789 if (pfile
->multiline_string_line
!= start_line
4790 && pfile
->multiline_string_line
!= 0)
4791 cpp_error_with_line (pfile
,
4792 pfile
->multiline_string_line
, -1,
4793 "possible real start of unterminated constant");
4794 pfile
->multiline_string_line
= 0;
4798 CPP_PUTC (pfile
, cc
);
4802 /* Traditionally, end of line ends a string constant with
4803 no error. So exit the loop and record the new line. */
4804 if (CPP_TRADITIONAL (pfile
))
4808 cpp_error_with_line (pfile
, start_line
, start_column
,
4809 "unterminated character constant");
4812 if (CPP_PEDANTIC (pfile
)
4813 && pfile
->multiline_string_line
== 0)
4815 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4816 "string constant runs past end of line");
4818 if (pfile
->multiline_string_line
== 0)
4819 pfile
->multiline_string_line
= start_line
;
4826 /* Backslash newline is replaced by nothing at all. */
4827 CPP_ADJUST_WRITTEN (pfile
, -1);
4832 /* ANSI stupidly requires that in \\ the second \
4833 is *not* prevented from combining with a newline. */
4836 CPP_PUTC (pfile
, cc
);
4848 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4849 CPP_PWRITTEN (pfile
));
4850 pfile
->only_seen_white
= 0;
4851 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4854 if (!opts
->dollars_in_ident
)
4859 if (opts
->cplusplus
&& PEEKC () == ':')
4868 if (c2
== c
|| c2
== '=')
4878 if (PEEKC () == '=')
4885 if (c2
== '-' && opts
->chill
)
4887 /* Chill style comment */
4888 if (opts
->put_out_comments
)
4889 parse_set_mark (&start_mark
, pfile
);
4890 FORWARD(1); /* Skip second '-'. */
4898 /* Don't consider final '\n' to be part of comment. */
4904 goto return_comment
;
4906 if (c2
== '-' || c2
== '=' || c2
== '>')
4911 if (pfile
->parsing_include_directive
)
4915 CPP_PUTC (pfile
, c
);
4920 if (c
== '\n' || c
== EOF
)
4923 "missing '>' in `#include <FILENAME>'");
4929 /* else fall through */
4938 CPP_RESERVE (pfile
, 4);
4939 CPP_PUTC (pfile
, c
);
4940 CPP_PUTC (pfile
, c2
);
4944 CPP_PUTC_Q (pfile
, GETC ());
4945 CPP_NUL_TERMINATE_Q (pfile
);
4946 pfile
->only_seen_white
= 0;
4950 if (CPP_BUFFER (pfile
)->has_escapes
)
4955 if (pfile
->output_escapes
)
4956 CPP_PUTS (pfile
, "@-", 2);
4957 parse_name (pfile
, GETC ());
4960 else if (is_space
[c
])
4962 CPP_RESERVE (pfile
, 2);
4963 if (pfile
->output_escapes
)
4964 CPP_PUTC_Q (pfile
, '@');
4965 CPP_PUTC_Q (pfile
, c
);
4969 if (pfile
->output_escapes
)
4971 CPP_PUTS (pfile
, "@@", 2);
4981 CPP_RESERVE(pfile
, 2);
4982 CPP_PUTC_Q (pfile
, '.');
4986 /* FIXME - misses the case "..\\\n." */
4987 if (c2
== '.' && PEEKN(1) == '.')
4989 CPP_RESERVE(pfile
, 4);
4990 CPP_PUTC_Q (pfile
, '.');
4991 CPP_PUTC_Q (pfile
, '.');
4992 CPP_PUTC_Q (pfile
, '.');
4994 CPP_NUL_TERMINATE_Q (pfile
);
4995 pfile
->only_seen_white
= 0;
5002 pfile
->only_seen_white
= 0;
5004 CPP_RESERVE(pfile
, 3);
5005 CPP_PUTC_Q (pfile
, c
);
5006 CPP_PUTC_Q (pfile
, GETC ());
5007 CPP_NUL_TERMINATE_Q (pfile
);
5013 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
5015 CPP_PUTC (pfile
, c
);
5021 case '0': case '1': case '2': case '3': case '4':
5022 case '5': case '6': case '7': case '8': case '9':
5027 CPP_RESERVE (pfile
, 2);
5028 CPP_PUTC_Q (pfile
, c
);
5033 if (!is_idchar
[c
] && c
!= '.'
5034 && ((c2
!= 'e' && c2
!= 'E') || (c
!= '+' && c
!= '-')))
5039 CPP_NUL_TERMINATE_Q (pfile
);
5040 pfile
->only_seen_white
= 0;
5042 case 'b': case 'c': case 'd': case 'h': case 'o':
5043 case 'B': case 'C': case 'D': case 'H': case 'O':
5044 if (opts
->chill
&& PEEKC () == '\'')
5046 pfile
->only_seen_white
= 0;
5047 CPP_RESERVE (pfile
, 2);
5048 CPP_PUTC_Q (pfile
, c
);
5049 CPP_PUTC_Q (pfile
, '\'');
5055 goto chill_number_eof
;
5058 if (c
== '\\' && PEEKC() == '\n')
5065 CPP_PUTC (pfile
, c
);
5069 CPP_RESERVE (pfile
, 2);
5070 CPP_PUTC_Q (pfile
, c
);
5071 CPP_NUL_TERMINATE_Q (pfile
);
5078 CPP_NUL_TERMINATE (pfile
);
5085 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5086 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5087 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5088 case 'x': case 'y': case 'z':
5089 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5090 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5091 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5096 unsigned char *ident
;
5097 int before_name_written
= CPP_WRITTEN (pfile
);
5099 parse_name (pfile
, c
);
5100 pfile
->only_seen_white
= 0;
5101 if (pfile
->no_macro_expand
)
5103 ident
= pfile
->token_buffer
+ before_name_written
;
5104 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5105 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5108 if (hp
->type
== T_DISABLED
)
5110 if (pfile
->output_escapes
)
5111 { /* Return "@-IDENT", followed by '\0'. */
5113 CPP_RESERVE (pfile
, 3);
5114 ident
= pfile
->token_buffer
+ before_name_written
;
5115 CPP_ADJUST_WRITTEN (pfile
, 2);
5116 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5123 /* If macro wants an arglist, verify that a '(' follows.
5124 first skip all whitespace, copying it to the output
5125 after the macro name. Then, if there is no '(',
5126 decide this is not a macro call and leave things that way. */
5127 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5129 struct parse_marker macro_mark
;
5131 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5133 cpp_buffer
*next_buf
;
5134 cpp_skip_hspace (pfile
);
5135 if (PEEKC () != EOF
)
5137 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5138 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5139 CPP_BUFFER (pfile
) = next_buf
;
5141 parse_set_mark (¯o_mark
, pfile
);
5144 cpp_skip_hspace (pfile
);
5146 is_macro_call
= c
== '(';
5152 parse_goto_mark (¯o_mark
, pfile
);
5153 parse_clear_mark (¯o_mark
);
5157 /* This is now known to be a macro call. */
5159 /* it might not actually be a macro. */
5160 if (hp
->type
!= T_MACRO
) {
5161 int xbuf_len
; U_CHAR
*xbuf
;
5162 CPP_SET_WRITTEN (pfile
, before_name_written
);
5163 special_symbol (hp
, pfile
);
5164 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5165 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5166 CPP_SET_WRITTEN (pfile
, before_name_written
);
5167 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5168 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5172 /* Expand the macro, reading arguments as needed,
5173 and push the expansion on the input stack. */
5174 macroexpand (pfile
, hp
);
5175 CPP_SET_WRITTEN (pfile
, before_name_written
);
5178 /* An extra "@ " is added to the end of a macro expansion
5179 to prevent accidental token pasting. We prefer to avoid
5180 unneeded extra spaces (for the sake of cpp-using tools like
5181 imake). Here we remove the space if it is safe to do so. */
5182 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5183 && pfile
->buffer
->rlimit
[-2] == '@'
5184 && pfile
->buffer
->rlimit
[-1] == ' ')
5186 int c1
= pfile
->buffer
->rlimit
[-3];
5187 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5188 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5189 pfile
->buffer
->rlimit
-= 2;
5194 case ' ': case '\t': case '\v': case '\r':
5197 CPP_PUTC (pfile
, c
);
5199 if (c
== EOF
|| !is_hor_space
[c
])
5213 CPP_PUTC (pfile
, c
);
5214 if (pfile
->only_seen_white
== 0)
5215 pfile
->only_seen_white
= 1;
5217 output_line_command (pfile
, 1, same_file
);
5220 case '(': token
= CPP_LPAREN
; goto char1
;
5221 case ')': token
= CPP_RPAREN
; goto char1
;
5222 case '{': token
= CPP_LBRACE
; goto char1
;
5223 case '}': token
= CPP_RBRACE
; goto char1
;
5224 case ',': token
= CPP_COMMA
; goto char1
;
5225 case ';': token
= CPP_SEMICOLON
; goto char1
;
5231 pfile
->only_seen_white
= 0;
5232 CPP_PUTC (pfile
, c
);
5238 /* Like cpp_get_token, but skip spaces and comments. */
5240 cpp_get_non_space_token (pfile
)
5243 int old_written
= CPP_WRITTEN (pfile
);
5246 enum cpp_token token
= cpp_get_token (pfile
);
5247 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5248 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5250 CPP_SET_WRITTEN (pfile
, old_written
);
5254 /* Parse an identifier starting with C. */
5257 parse_name (pfile
, c
)
5258 cpp_reader
*pfile
; int c
;
5264 if (c
== '\\' && PEEKC() == '\n')
5273 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5274 CPP_PUTC_Q (pfile
, c
);
5279 CPP_NUL_TERMINATE_Q (pfile
);
5284 /* Maintain and search list of included files, for #import. */
5286 /* Hash a file name for import_hash_table. */
5294 while (*f
) val
+= *f
++;
5295 return (val
%IMPORT_HASH_SIZE
);
5298 /* Search for file FILENAME in import_hash_table.
5299 Return -2 if found, either a matching name or a matching inode.
5300 Otherwise, open the file and return a file descriptor if successful
5301 or -1 if unsuccessful. */
5304 lookup_import (pfile
, filename
, searchptr
)
5307 struct file_name_list
*searchptr
;
5309 struct import_file
*i
;
5315 hashval
= import_hash (filename
);
5317 /* Attempt to find file in list of already included files */
5318 i
= pfile
->import_hash_table
[hashval
];
5321 if (!strcmp (filename
, i
->name
))
5322 return -2; /* return found */
5325 /* Open it and try a match on inode/dev */
5326 fd
= open_include_file (pfile
, filename
, searchptr
);
5330 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5331 i
= pfile
->import_hash_table
[h
];
5333 /* Compare the inode and the device.
5334 Supposedly on some systems the inode is not a scalar. */
5335 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5336 && i
->dev
== sb
.st_dev
) {
5338 return -2; /* return found */
5343 return fd
; /* Not found, return open file */
5346 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5349 add_import (pfile
, fd
, fname
)
5354 struct import_file
*i
;
5358 hashval
= import_hash (fname
);
5360 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5361 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5362 strcpy (i
->name
, fname
);
5363 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5365 i
->next
= pfile
->import_hash_table
[hashval
];
5366 pfile
->import_hash_table
[hashval
] = i
;
5369 /* The file_name_map structure holds a mapping of file names for a
5370 particular directory. This mapping is read from the file named
5371 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5372 map filenames on a file system with severe filename restrictions,
5373 such as DOS. The format of the file name map file is just a series
5374 of lines with two tokens on each line. The first token is the name
5375 to map, and the second token is the actual name to use. */
5377 struct file_name_map
5379 struct file_name_map
*map_next
;
5384 #define FILE_NAME_MAP_FILE "header.gcc"
5386 /* Read a space delimited string of unlimited length from a stdio
5390 read_filename_string (ch
, f
)
5398 set
= alloc
= xmalloc (len
+ 1);
5402 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5404 if (set
- alloc
== len
)
5407 alloc
= xrealloc (alloc
, len
+ 1);
5408 set
= alloc
+ len
/ 2;
5418 /* This structure holds a linked list of file name maps, one per directory. */
5419 struct file_name_map_list
5421 struct file_name_map_list
*map_list_next
;
5422 char *map_list_name
;
5423 struct file_name_map
*map_list_map
;
5426 /* Read the file name map file for DIRNAME. */
5428 static struct file_name_map
*
5429 read_name_map (pfile
, dirname
)
5433 register struct file_name_map_list
*map_list_ptr
;
5437 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5438 map_list_ptr
= map_list_ptr
->map_list_next
)
5439 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5440 return map_list_ptr
->map_list_map
;
5442 map_list_ptr
= ((struct file_name_map_list
*)
5443 xmalloc (sizeof (struct file_name_map_list
)));
5444 map_list_ptr
->map_list_name
= savestring (dirname
);
5445 map_list_ptr
->map_list_map
= NULL
;
5447 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5448 strcpy (name
, dirname
);
5451 strcat (name
, FILE_NAME_MAP_FILE
);
5452 f
= fopen (name
, "r");
5454 map_list_ptr
->map_list_map
= NULL
;
5458 int dirlen
= strlen (dirname
);
5460 while ((ch
= getc (f
)) != EOF
)
5463 struct file_name_map
*ptr
;
5467 from
= read_filename_string (ch
, f
);
5468 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5470 to
= read_filename_string (ch
, f
);
5472 ptr
= ((struct file_name_map
*)
5473 xmalloc (sizeof (struct file_name_map
)));
5474 ptr
->map_from
= from
;
5476 /* Make the real filename absolute. */
5481 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5482 strcpy (ptr
->map_to
, dirname
);
5483 ptr
->map_to
[dirlen
] = '/';
5484 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5488 ptr
->map_next
= map_list_ptr
->map_list_map
;
5489 map_list_ptr
->map_list_map
= ptr
;
5491 while ((ch
= getc (f
)) != '\n')
5498 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5499 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5501 return map_list_ptr
->map_list_map
;
5504 /* Try to open include file FILENAME. SEARCHPTR is the directory
5505 being tried from the include file search path. This function maps
5506 filenames on file systems based on information read by
5510 open_include_file (pfile
, filename
, searchptr
)
5513 struct file_name_list
*searchptr
;
5515 register struct file_name_map
*map
;
5516 register char *from
;
5519 if (searchptr
&& ! searchptr
->got_name_map
)
5521 searchptr
->name_map
= read_name_map (pfile
,
5523 ? searchptr
->fname
: ".");
5524 searchptr
->got_name_map
= 1;
5527 /* First check the mapping for the directory we are using. */
5528 if (searchptr
&& searchptr
->name_map
)
5531 if (searchptr
->fname
)
5532 from
+= strlen (searchptr
->fname
) + 1;
5533 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5535 if (! strcmp (map
->map_from
, from
))
5537 /* Found a match. */
5538 return open (map
->map_to
, O_RDONLY
, 0666);
5543 /* Try to find a mapping file for the particular directory we are
5544 looking in. Thus #include <sys/types.h> will look up sys/types.h
5545 in /usr/include/header.gcc and look up types.h in
5546 /usr/include/sys/header.gcc. */
5547 p
= rindex (filename
, '/');
5552 && strlen (searchptr
->fname
) == p
- filename
5553 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5555 /* FILENAME is in SEARCHPTR, which we've already checked. */
5556 return open (filename
, O_RDONLY
, 0666);
5566 dir
= (char *) alloca (p
- filename
+ 1);
5567 bcopy (filename
, dir
, p
- filename
);
5568 dir
[p
- filename
] = '\0';
5571 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5572 if (! strcmp (map
->map_from
, from
))
5573 return open (map
->map_to
, O_RDONLY
, 0666);
5575 return open (filename
, O_RDONLY
, 0666);
5578 /* Process the contents of include file FNAME, already open on descriptor F,
5580 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5581 "system" include directories (as decided by the `is_system_include'
5583 DIRPTR is the link in the dir path through which this file was found,
5584 or 0 if the file name was absolute or via the current directory.
5585 Return 1 on success, 0 on failure.
5587 The caller is responsible for the cpp_push_buffer. */
5590 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5594 int system_header_p
;
5595 struct file_name_list
*dirptr
;
5601 cpp_buffer
*fp
; /* For input stack frame */
5602 int missing_newline
= 0;
5604 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
5606 cpp_perror_with_name (pfile
, fname
);
5608 cpp_pop_buffer (pfile
);
5612 fp
= CPP_BUFFER (pfile
);
5613 fp
->nominal_fname
= fp
->fname
= fname
;
5618 fp
->system_header_p
= system_header_p
;
5621 fp
->cleanup
= file_cleanup
;
5623 if (S_ISREG (st_mode
)) {
5624 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5625 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5628 /* Read the file contents, knowing that st_size is an upper bound
5629 on the number of bytes we can read. */
5630 length
= safe_read (f
, fp
->buf
, st_size
);
5631 fp
->rlimit
= fp
->buf
+ length
;
5632 if (length
< 0) goto nope
;
5634 else if (S_ISDIR (st_mode
)) {
5635 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5639 /* Cannot count its file size before reading.
5640 First read the entire file into heap and
5641 copy them into buffer on stack. */
5646 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5649 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5651 goto nope
; /* error! */
5653 if (st_size
!= bsize
)
5654 break; /* End of file */
5656 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5662 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5663 /* Backslash-newline at end is not good enough. */
5664 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5665 fp
->buf
[length
++] = '\n';
5667 missing_newline
= 1;
5670 fp
->buf
[length
] = '\0';
5671 fp
->rlimit
= fp
->buf
+ length
;
5673 /* Close descriptor now, so nesting does not use lots of descriptors. */
5676 /* Must do this before calling trigraph_pcp, so that the correct file name
5677 will be printed in warning messages. */
5679 pfile
->input_stack_listing_current
= 0;
5689 if (missing_newline
)
5692 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5693 pedwarn ("file does not end in newline");
5696 input_file_stack_tick
++;
5703 cpp_perror_with_name (pfile
, fname
);
5710 push_parse_file (pfile
, fname
)
5714 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5715 struct cpp_pending
*pend
;
5720 /* The code looks at the defaults through this pointer, rather than through
5721 the constant structure above. This pointer gets changed if an environment
5722 variable specifies other defaults. */
5723 struct default_include
*include_defaults
= include_defaults_array
;
5725 /* Add dirs from CPATH after dirs from -I. */
5726 /* There seems to be confusion about what CPATH should do,
5727 so for the moment it is not documented. */
5728 /* Some people say that CPATH should replace the standard include dirs,
5729 but that seems pointless: it comes before them, so it overrides them
5731 p
= (char *) getenv ("CPATH");
5732 if (p
!= 0 && ! opts
->no_standard_includes
)
5733 path_include (pfile
, p
);
5735 /* Now that dollars_in_ident is known, initialize is_idchar. */
5736 initialize_char_syntax (opts
);
5738 /* Do partial setup of input buffer for the sake of generating
5739 early #line directives (when -g is in effect). */
5740 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5741 if (opts
->in_fname
== NULL
)
5742 opts
->in_fname
= "";
5743 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5746 /* Install __LINE__, etc. Must follow initialize_char_syntax
5747 and option processing. */
5748 initialize_builtins (pfile
);
5750 /* Do standard #defines and assertions
5751 that identify system and machine type. */
5753 if (!opts
->inhibit_predefs
) {
5754 char *p
= (char *) alloca (strlen (predefs
) + 1);
5755 strcpy (p
, predefs
);
5758 while (*p
== ' ' || *p
== '\t')
5760 /* Handle -D options. */
5761 if (p
[0] == '-' && p
[1] == 'D') {
5763 while (*p
&& *p
!= ' ' && *p
!= '\t')
5767 if (opts
->debug_output
)
5768 output_line_command (pfile
, 0, same_file
);
5769 cpp_define (pfile
, q
);
5770 while (*p
== ' ' || *p
== '\t')
5772 } else if (p
[0] == '-' && p
[1] == 'A') {
5773 /* Handle -A options (assertions). */
5782 past_name
= assertion
;
5783 /* Locate end of name. */
5784 while (*past_name
&& *past_name
!= ' '
5785 && *past_name
!= '\t' && *past_name
!= '(')
5787 /* Locate `(' at start of value. */
5789 while (*value
&& (*value
== ' ' || *value
== '\t'))
5791 if (*value
++ != '(')
5793 while (*value
&& (*value
== ' ' || *value
== '\t'))
5796 /* Locate end of value. */
5797 while (*past_value
&& *past_value
!= ' '
5798 && *past_value
!= '\t' && *past_value
!= ')')
5800 termination
= past_value
;
5801 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5803 if (*termination
++ != ')')
5805 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5807 /* Temporarily null-terminate the value. */
5808 save_char
= *termination
;
5809 *termination
= '\0';
5810 /* Install the assertion. */
5811 make_assertion (pfile
, "-A", assertion
);
5812 *termination
= (char) save_char
;
5814 while (*p
== ' ' || *p
== '\t')
5822 /* Now handle the command line options. */
5824 /* Do -U's, -D's and -A's in the order they were seen. */
5825 /* First reverse the list. */
5826 opts
->pending
= nreverse_pending (opts
->pending
);
5828 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5830 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5832 switch (pend
->cmd
[1])
5835 if (opts
->debug_output
)
5836 output_line_command (pfile
, 0, same_file
);
5837 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5840 if (opts
->debug_output
)
5841 output_line_command (pfile
, 0, same_file
);
5842 cpp_define (pfile
, pend
->arg
);
5845 make_assertion (pfile
, "-A", pend
->arg
);
5851 opts
->done_initializing
= 1;
5853 { /* read the appropriate environment variable and if it exists
5854 replace include_defaults with the listed path. */
5856 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5859 epath
= getenv ("C_INCLUDE_PATH");
5862 epath
= getenv ("CPLUS_INCLUDE_PATH");
5865 epath
= getenv ("OBJC_INCLUDE_PATH");
5868 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
5871 /* If the environment var for this language is set,
5872 add to the default list of include directories. */
5874 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5876 char *startp
, *endp
;
5878 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5879 if (*startp
== PATH_SEPARATOR
)
5882 = (struct default_include
*) xmalloc ((num_dirs
5883 * sizeof (struct default_include
))
5884 + sizeof (include_defaults_array
));
5885 startp
= endp
= epath
;
5888 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5889 if ((*endp
== PATH_SEPARATOR
)
5891 strncpy (nstore
, startp
, endp
-startp
);
5893 strcpy (nstore
, ".");
5895 nstore
[endp
-startp
] = '\0';
5897 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5898 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5899 include_defaults
[num_dirs
].cxx_aware
= 1;
5903 endp
= startp
= endp
+ 1;
5907 /* Put the usual defaults back in at the end. */
5908 bcopy ((char *) include_defaults_array
,
5909 (char *) &include_defaults
[num_dirs
],
5910 sizeof (include_defaults_array
));
5914 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5915 opts
->first_system_include
= opts
->before_system
;
5917 /* Unless -fnostdinc,
5918 tack on the standard include file dirs to the specified list */
5919 if (!opts
->no_standard_includes
) {
5920 struct default_include
*p
= include_defaults
;
5921 char *specd_prefix
= opts
->include_prefix
;
5922 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5923 int default_len
= 0;
5924 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5925 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5926 default_len
= strlen (default_prefix
) - 7;
5927 default_prefix
[default_len
] = 0;
5929 /* Search "translated" versions of GNU directories.
5930 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5931 if (specd_prefix
!= 0 && default_len
!= 0)
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 /* Does this dir start with the prefix? */
5937 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5938 /* Yes; change prefix and add to search list. */
5939 struct file_name_list
*new
5940 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5941 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5942 char *str
= (char *) xmalloc (this_len
+ 1);
5943 strcpy (str
, specd_prefix
);
5944 strcat (str
, p
->fname
+ default_len
);
5946 new->control_macro
= 0;
5947 new->c_system_include_path
= !p
->cxx_aware
;
5948 new->got_name_map
= 0;
5949 append_include_chain (pfile
, new, new);
5950 if (opts
->first_system_include
== 0)
5951 opts
->first_system_include
= new;
5955 /* Search ordinary names for GNU include directories. */
5956 for (p
= include_defaults
; p
->fname
; p
++) {
5957 /* Some standard dirs are only for C++. */
5959 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5960 struct file_name_list
*new
5961 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5962 new->control_macro
= 0;
5963 new->c_system_include_path
= !p
->cxx_aware
;
5964 new->fname
= p
->fname
;
5965 new->got_name_map
= 0;
5966 append_include_chain (pfile
, new, new);
5967 if (opts
->first_system_include
== 0)
5968 opts
->first_system_include
= new;
5973 /* Tack the after_include chain at the end of the include chain. */
5974 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5975 if (opts
->first_system_include
== 0)
5976 opts
->first_system_include
= opts
->after_include
;
5978 /* With -v, print the list of dirs to search. */
5979 if (opts
->verbose
) {
5980 struct file_name_list
*p
;
5981 fprintf (stderr
, "#include \"...\" search starts here:\n");
5982 for (p
= opts
->include
; p
; p
= p
->next
) {
5983 if (p
== opts
->first_bracket_include
)
5984 fprintf (stderr
, "#include <...> search starts here:\n");
5985 fprintf (stderr
, " %s\n", p
->fname
);
5987 fprintf (stderr
, "End of search list.\n");
5990 /* Scan the -imacros files before the main input.
5991 Much like #including them, but with no_output set
5992 so that only their macro definitions matter. */
5994 opts
->no_output
++; pfile
->no_record_file
++;
5995 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5997 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
5999 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6002 cpp_perror_with_name (pfile
, pend
->arg
);
6003 return FATAL_EXIT_CODE
;
6005 cpp_push_buffer (pfile
, NULL
, 0);
6006 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6007 cpp_scan_buffer (pfile
);
6010 opts
->no_output
--; pfile
->no_record_file
--;
6012 /* Copy the entire contents of the main input file into
6013 the stacked input buffer previously allocated for it. */
6014 if (fname
== NULL
|| *fname
== 0) {
6017 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
6018 cpp_pfatal_with_name (pfile
, fname
);
6020 /* -MG doesn't select the form of output and must be specified with one of
6021 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6022 inhibit compilation. */
6023 if (opts
->print_deps_missing_files
6024 && (opts
->print_deps
== 0 || !opts
->no_output
))
6025 fatal (pfile
, "-MG must be specified with one of -M or -MM");
6027 /* Either of two environment variables can specify output of deps.
6028 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6029 where OUTPUT_FILE is the file to write deps info to
6030 and DEPS_TARGET is the target to mention in the deps. */
6032 if (opts
->print_deps
== 0
6033 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6034 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6035 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6041 spec
= getenv ("SUNPRO_DEPENDENCIES");
6042 opts
->print_deps
= 2;
6045 opts
->print_deps
= 1;
6048 /* Find the space before the DEPS_TARGET, if there is one. */
6049 /* This should use index. (mrs) */
6050 while (*s
!= 0 && *s
!= ' ') s
++;
6053 opts
->deps_target
= s
+ 1;
6054 output_file
= (char *) xmalloc (s
- spec
+ 1);
6055 bcopy (spec
, output_file
, s
- spec
);
6056 output_file
[s
- spec
] = 0;
6060 opts
->deps_target
= 0;
6064 opts
->deps_file
= output_file
;
6065 opts
->print_deps_append
= 1;
6068 /* For -M, print the expected object file name
6069 as the target of this Make-rule. */
6070 if (opts
->print_deps
)
6072 pfile
->deps_allocated_size
= 200;
6073 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6074 pfile
->deps_buffer
[0] = 0;
6075 pfile
->deps_size
= 0;
6076 pfile
->deps_column
= 0;
6078 if (opts
->deps_target
)
6079 deps_output (pfile
, opts
->deps_target
, ':');
6080 else if (*opts
->in_fname
== 0)
6081 deps_output (pfile
, "-", ':');
6087 /* Discard all directory prefixes from filename. */
6088 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6089 #ifdef DIR_SEPARATOR
6090 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6097 /* Copy remainder to mungable area. */
6098 p
= (char *) alloca (strlen(q
) + 8);
6101 /* Output P, but remove known suffixes. */
6105 && p
[len
- 2] == '.'
6106 && index("cCsSm", p
[len
- 1]))
6109 && p
[len
- 3] == '.'
6110 && p
[len
- 2] == 'c'
6111 && p
[len
- 1] == 'c')
6114 && p
[len
- 4] == '.'
6115 && p
[len
- 3] == 'c'
6116 && p
[len
- 2] == 'x'
6117 && p
[len
- 1] == 'x')
6120 && p
[len
- 4] == '.'
6121 && p
[len
- 3] == 'c'
6122 && p
[len
- 2] == 'p'
6123 && p
[len
- 1] == 'p')
6126 /* Supply our own suffix. */
6133 deps_output (pfile
, p
, ':');
6134 deps_output (pfile
, opts
->in_fname
, ' ');
6139 /* Make sure data ends with a newline. And put a null after it. */
6141 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6142 /* Backslash-newline at end is not good enough. */
6143 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6144 fp
->buf
[fp
->length
++] = '\n';
6145 missing_newline
= 1;
6147 fp
->buf
[fp
->length
] = '\0';
6149 /* Unless inhibited, convert trigraphs in the input. */
6155 /* Scan the -include files before the main input.
6156 We push these in reverse order, so that the first one is handled first. */
6158 pfile
->no_record_file
++;
6159 opts
->pending
= nreverse_pending (opts
->pending
);
6160 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6162 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6164 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6167 cpp_perror_with_name (pfile
, pend
->arg
);
6168 return FATAL_EXIT_CODE
;
6170 cpp_push_buffer (pfile
, NULL
, 0);
6171 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6174 pfile
->no_record_file
--;
6176 /* Free the pending list. */
6177 for (pend
= opts
->pending
; pend
; )
6179 struct cpp_pending
*next
= pend
->next
;
6183 opts
->pending
= NULL
;
6186 /* Scan the input, processing macros and directives. */
6188 rescan (&outbuf
, 0);
6190 if (missing_newline
)
6193 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6194 pedwarn ("file does not end in newline");
6197 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6198 output_line_command (pfile
, 0, same_file
);
6199 return SUCCESS_EXIT_CODE
;
6203 init_parse_file (pfile
)
6206 bzero ((char *) pfile
, sizeof (cpp_reader
));
6207 pfile
->get_token
= cpp_get_token
;
6209 pfile
->token_buffer_size
= 200;
6210 pfile
->token_buffer
= (U_CHAR
*)xmalloc (pfile
->token_buffer_size
);
6211 CPP_SET_WRITTEN (pfile
, 0);
6213 pfile
->system_include_depth
= 0;
6214 pfile
->dont_repeat_files
= 0;
6215 pfile
->all_include_files
= 0;
6216 pfile
->max_include_len
= 0;
6217 pfile
->timebuf
= NULL
;
6218 pfile
->only_seen_white
= 1;
6219 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6222 static struct cpp_pending
*
6223 nreverse_pending (list
)
6224 struct cpp_pending
*list
;
6227 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6228 for (pend
= list
; pend
; pend
= next
)
6238 push_pending (pfile
, cmd
, arg
)
6243 struct cpp_pending
*pend
6244 = (struct cpp_pending
*)xmalloc (sizeof (struct cpp_pending
));
6247 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6248 CPP_OPTIONS (pfile
)->pending
= pend
;
6251 /* Handle command-line options in (argc, argv).
6252 Can be called multiple times, to handle multiple sets of options.
6253 Returns if an unrecognized option is seen.
6254 Returns number of handled arguments. */
6257 cpp_handle_options (pfile
, argc
, argv
)
6263 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6264 for (i
= 0; i
< argc
; i
++) {
6265 if (argv
[i
][0] != '-') {
6266 if (opts
->out_fname
!= NULL
)
6267 fatal ("Usage: %s [switches] input output", argv
[0]);
6268 else if (opts
->in_fname
!= NULL
)
6269 opts
->out_fname
= argv
[i
];
6271 opts
->in_fname
= argv
[i
];
6273 switch (argv
[i
][1]) {
6276 if (!strcmp (argv
[i
], "-include")
6277 || !strcmp (argv
[i
], "-imacros")) {
6279 fatal ("Filename missing after `%s' option", argv
[i
]);
6281 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6283 if (!strcmp (argv
[i
], "-iprefix")) {
6285 fatal ("Filename missing after `-iprefix' option");
6287 opts
->include_prefix
= argv
[++i
];
6289 if (!strcmp (argv
[i
], "-ifoutput")) {
6290 opts
->output_conditionals
= 1;
6292 if (!strcmp (argv
[i
], "-isystem")) {
6293 struct file_name_list
*dirtmp
;
6296 fatal ("Filename missing after `-isystem' option");
6298 dirtmp
= (struct file_name_list
*)
6299 xmalloc (sizeof (struct file_name_list
));
6301 dirtmp
->control_macro
= 0;
6302 dirtmp
->c_system_include_path
= 1;
6303 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6304 strcpy (dirtmp
->fname
, argv
[++i
]);
6305 dirtmp
->got_name_map
= 0;
6307 if (opts
->before_system
== 0)
6308 opts
->before_system
= dirtmp
;
6310 opts
->last_before_system
->next
= dirtmp
;
6311 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6313 /* Add directory to end of path for includes,
6314 with the default prefix at the front of its name. */
6315 if (!strcmp (argv
[i
], "-iwithprefix")) {
6316 struct file_name_list
*dirtmp
;
6319 if (opts
->include_prefix
!= 0)
6320 prefix
= opts
->include_prefix
;
6322 prefix
= savestring (GCC_INCLUDE_DIR
);
6323 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6324 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6325 prefix
[strlen (prefix
) - 7] = 0;
6328 dirtmp
= (struct file_name_list
*)
6329 xmalloc (sizeof (struct file_name_list
));
6330 dirtmp
->next
= 0; /* New one goes on the end */
6331 dirtmp
->control_macro
= 0;
6332 dirtmp
->c_system_include_path
= 0;
6334 fatal ("Directory name missing after `-iwithprefix' option");
6336 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6337 + strlen (prefix
) + 1);
6338 strcpy (dirtmp
->fname
, prefix
);
6339 strcat (dirtmp
->fname
, argv
[++i
]);
6340 dirtmp
->got_name_map
= 0;
6342 if (opts
->after_include
== 0)
6343 opts
->after_include
= dirtmp
;
6345 opts
->last_after_include
->next
= dirtmp
;
6346 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6348 /* Add directory to main path for includes,
6349 with the default prefix at the front of its name. */
6350 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6351 struct file_name_list
*dirtmp
;
6354 if (opts
->include_prefix
!= 0)
6355 prefix
= opts
->include_prefix
;
6357 prefix
= savestring (GCC_INCLUDE_DIR
);
6358 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6359 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6360 prefix
[strlen (prefix
) - 7] = 0;
6363 dirtmp
= (struct file_name_list
*)
6364 xmalloc (sizeof (struct file_name_list
));
6365 dirtmp
->next
= 0; /* New one goes on the end */
6366 dirtmp
->control_macro
= 0;
6367 dirtmp
->c_system_include_path
= 0;
6369 fatal ("Directory name missing after `-iwithprefixbefore' option");
6371 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6372 + strlen (prefix
) + 1);
6373 strcpy (dirtmp
->fname
, prefix
);
6374 strcat (dirtmp
->fname
, argv
[++i
]);
6375 dirtmp
->got_name_map
= 0;
6377 append_include_chain (pfile
, dirtmp
, dirtmp
);
6379 /* Add directory to end of path for includes. */
6380 if (!strcmp (argv
[i
], "-idirafter")) {
6381 struct file_name_list
*dirtmp
;
6383 dirtmp
= (struct file_name_list
*)
6384 xmalloc (sizeof (struct file_name_list
));
6385 dirtmp
->next
= 0; /* New one goes on the end */
6386 dirtmp
->control_macro
= 0;
6387 dirtmp
->c_system_include_path
= 0;
6389 fatal ("Directory name missing after `-idirafter' option");
6391 dirtmp
->fname
= argv
[++i
];
6392 dirtmp
->got_name_map
= 0;
6394 if (opts
->after_include
== 0)
6395 opts
->after_include
= dirtmp
;
6397 opts
->last_after_include
->next
= dirtmp
;
6398 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6403 if (opts
->out_fname
!= NULL
)
6404 fatal ("Output filename specified twice");
6406 fatal ("Filename missing after -o option");
6407 opts
->out_fname
= argv
[++i
];
6408 if (!strcmp (opts
->out_fname
, "-"))
6409 opts
->out_fname
= "";
6413 if (!strcmp (argv
[i
], "-pedantic"))
6414 CPP_PEDANTIC (pfile
) = 1;
6415 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6416 CPP_PEDANTIC (pfile
) = 1;
6417 opts
->pedantic_errors
= 1;
6420 else if (!strcmp (argv
[i
], "-pcp")) {
6421 char *pcp_fname
= argv
[++i
];
6423 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6424 ? fopen (pcp_fname
, "w")
6425 : fdopen (dup (fileno (stdout
)), "w"));
6426 if (pcp_outfile
== 0)
6427 cpp_pfatal_with_name (pfile
, pcp_fname
);
6434 if (!strcmp (argv
[i
], "-traditional")) {
6435 opts
->traditional
= 1;
6436 if (opts
->dollars_in_ident
> 0)
6437 opts
->dollars_in_ident
= 1;
6438 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6440 opts
->no_trigraphs
= 0;
6445 if (! strcmp (argv
[i
], "-lang-c"))
6446 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->objc
= 0;
6447 if (! strcmp (argv
[i
], "-lang-c++"))
6448 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->objc
= 0;
6449 if (! strcmp (argv
[i
], "-lang-c-c++-comments"))
6450 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->objc
= 0;
6451 if (! strcmp (argv
[i
], "-lang-objc"))
6452 opts
->objc
= 1, opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1;
6453 if (! strcmp (argv
[i
], "-lang-objc++"))
6454 opts
->objc
= 1, opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6455 if (! strcmp (argv
[i
], "-lang-asm"))
6457 if (! strcmp (argv
[i
], "-lint"))
6459 if (! strcmp (argv
[i
], "-lang-chill"))
6460 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6461 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6465 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6469 opts
->inhibit_warnings
= 1;
6473 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6474 opts
->warn_trigraphs
= 1;
6475 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6476 opts
->warn_trigraphs
= 0;
6477 else if (!strcmp (argv
[i
], "-Wcomment"))
6478 opts
->warn_comments
= 1;
6479 else if (!strcmp (argv
[i
], "-Wno-comment"))
6480 opts
->warn_comments
= 0;
6481 else if (!strcmp (argv
[i
], "-Wcomments"))
6482 opts
->warn_comments
= 1;
6483 else if (!strcmp (argv
[i
], "-Wno-comments"))
6484 opts
->warn_comments
= 0;
6485 else if (!strcmp (argv
[i
], "-Wtraditional"))
6486 opts
->warn_stringify
= 1;
6487 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6488 opts
->warn_stringify
= 0;
6489 else if (!strcmp (argv
[i
], "-Wimport"))
6490 opts
->warn_import
= 1;
6491 else if (!strcmp (argv
[i
], "-Wno-import"))
6492 opts
->warn_import
= 0;
6493 else if (!strcmp (argv
[i
], "-Werror"))
6494 opts
->warnings_are_errors
= 1;
6495 else if (!strcmp (argv
[i
], "-Wno-error"))
6496 opts
->warnings_are_errors
= 0;
6497 else if (!strcmp (argv
[i
], "-Wall"))
6499 opts
->warn_trigraphs
= 1;
6500 opts
->warn_comments
= 1;
6505 /* The style of the choices here is a bit mixed.
6506 The chosen scheme is a hybrid of keeping all options in one string
6507 and specifying each option in a separate argument:
6508 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6509 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6510 -M[M][G][D file]. This is awkward to handle in specs, and is not
6512 /* ??? -MG must be specified in addition to one of -M or -MM.
6513 This can be relaxed in the future without breaking anything.
6514 The converse isn't true. */
6516 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6517 if (!strcmp (argv
[i
], "-MG"))
6519 opts
->print_deps_missing_files
= 1;
6522 if (!strcmp (argv
[i
], "-M"))
6523 opts
->print_deps
= 2;
6524 else if (!strcmp (argv
[i
], "-MM"))
6525 opts
->print_deps
= 1;
6526 else if (!strcmp (argv
[i
], "-MD"))
6527 opts
->print_deps
= 2;
6528 else if (!strcmp (argv
[i
], "-MMD"))
6529 opts
->print_deps
= 1;
6530 /* For -MD and -MMD options, write deps on file named by next arg. */
6531 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6534 fatal ("Filename missing after %s option", argv
[i
]);
6535 opts
->deps_file
= argv
[++i
];
6539 /* For -M and -MM, write deps on standard output
6540 and suppress the usual output. */
6541 opts
->no_output
= 1;
6547 char *p
= argv
[i
] + 2;
6549 while ((c
= *p
++) != 0) {
6550 /* Arg to -d specifies what parts of macros to dump */
6553 opts
->dump_macros
= dump_only
;
6554 opts
->no_output
= 1;
6557 opts
->dump_macros
= dump_names
;
6560 opts
->dump_macros
= dump_definitions
;
6568 if (argv
[i
][2] == '3')
6569 opts
->debug_output
= 1;
6573 fprintf (stderr
, "GNU CPP version %s", version_string
);
6574 #ifdef TARGET_VERSION
6577 fprintf (stderr
, "\n");
6582 opts
->print_include_names
= 1;
6586 if (argv
[i
][2] != 0)
6587 push_pending (pfile
, "-D", argv
[i
] + 2);
6588 else if (i
+ 1 == argc
)
6589 fatal ("Macro name missing after -D option");
6591 i
++, push_pending (pfile
, "-D", argv
[i
]);
6598 if (argv
[i
][2] != 0)
6600 else if (i
+ 1 == argc
)
6601 fatal ("Assertion missing after -A option");
6605 if (!strcmp (p
, "-")) {
6606 struct cpp_pending
**ptr
;
6607 /* -A- eliminates all predefined macros and assertions.
6608 Let's include also any that were specified earlier
6609 on the command line. That way we can get rid of any
6610 that were passed automatically in from GCC. */
6612 opts
->inhibit_predefs
= 1;
6613 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6615 struct cpp_pending
*pend
= *ptr
;
6616 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6617 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6626 push_pending (pfile
, "-A", p
);
6631 case 'U': /* JF #undef something */
6632 if (argv
[i
][2] != 0)
6633 push_pending (pfile
, "-U", argv
[i
] + 2);
6634 else if (i
+ 1 == argc
)
6635 fatal ("Macro name missing after -U option");
6637 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6641 opts
->put_out_comments
= 1;
6644 case 'E': /* -E comes from cc -E; ignore it. */
6648 opts
->no_line_commands
= 1;
6651 case '$': /* Don't include $ in identifiers. */
6652 opts
->dollars_in_ident
= 0;
6655 case 'I': /* Add directory to path for includes. */
6657 struct file_name_list
*dirtmp
;
6659 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6660 && !strcmp (argv
[i
] + 2, "-")) {
6661 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6662 /* Don't use any preceding -I directories for #include <...>. */
6663 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6666 dirtmp
= (struct file_name_list
*)
6667 xmalloc (sizeof (struct file_name_list
));
6668 dirtmp
->next
= 0; /* New one goes on the end */
6669 dirtmp
->control_macro
= 0;
6670 dirtmp
->c_system_include_path
= 0;
6671 if (argv
[i
][2] != 0)
6672 dirtmp
->fname
= argv
[i
] + 2;
6673 else if (i
+ 1 == argc
)
6674 fatal ("Directory name missing after -I option");
6676 dirtmp
->fname
= argv
[++i
];
6677 dirtmp
->got_name_map
= 0;
6678 append_include_chain (pfile
, dirtmp
, dirtmp
);
6684 if (!strcmp (argv
[i
], "-nostdinc"))
6685 /* -nostdinc causes no default include directories.
6686 You must specify all include-file directories with -I. */
6687 opts
->no_standard_includes
= 1;
6688 else if (!strcmp (argv
[i
], "-nostdinc++"))
6689 /* -nostdinc++ causes no default C++-specific include directories. */
6690 opts
->no_standard_cplusplus_includes
= 1;
6692 else if (!strcmp (argv
[i
], "-noprecomp"))
6698 /* Sun compiler passes undocumented switch "-undef".
6699 Let's assume it means to inhibit the predefined symbols. */
6700 opts
->inhibit_predefs
= 1;
6703 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6704 if (opts
->in_fname
== NULL
) {
6705 opts
->in_fname
= "";
6707 } else if (opts
->out_fname
== NULL
) {
6708 opts
->out_fname
= "";
6710 } /* else fall through into error */
6724 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6726 if (opts
->print_deps
)
6728 /* Stream on which to print the dependency information. */
6731 /* Don't actually write the deps file if compilation has failed. */
6732 if (pfile
->errors
== 0)
6734 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6735 if (opts
->deps_file
== 0)
6736 deps_stream
= stdout
;
6737 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6738 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6739 fputs (pfile
->deps_buffer
, deps_stream
);
6740 putc ('\n', deps_stream
);
6741 if (opts
->deps_file
)
6743 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6744 fatal ("I/O error on output");
6750 /* Free resources used by PFILE. */
6757 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6758 cpp_pop_buffer (pfile
);
6760 if (pfile
->token_buffer
)
6762 free (pfile
->token_buffer
);
6763 pfile
->token_buffer
= NULL
;
6766 if (pfile
->deps_buffer
)
6768 free (pfile
->deps_buffer
);
6769 pfile
->deps_buffer
= NULL
;
6770 pfile
->deps_allocated_size
= 0;
6773 while (pfile
->if_stack
)
6775 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6776 pfile
->if_stack
= temp
->next
;
6780 while (pfile
->dont_repeat_files
)
6782 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6783 pfile
->dont_repeat_files
= temp
->next
;
6788 while (pfile
->all_include_files
)
6790 struct file_name_list
*temp
= pfile
->all_include_files
;
6791 pfile
->all_include_files
= temp
->next
;
6796 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6798 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6801 struct import_file
*next
= imp
->next
;
6806 pfile
->import_hash_table
[i
] = 0;
6809 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6811 while (pfile
->assertion_hashtab
[i
])
6812 delete_assertion (pfile
->assertion_hashtab
[i
]);
6815 cpp_hash_cleanup (pfile
);
6819 do_assert (pfile
, keyword
, buf
, limit
)
6821 struct directive
*keyword
;
6822 U_CHAR
*buf
, *limit
;
6824 long symstart
; /* remember where symbol name starts */
6826 int sym_length
; /* and how long it is */
6827 struct arglist
*tokens
= NULL
;
6829 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6830 && !CPP_BUFFER (pfile
)->system_header_p
)
6831 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6833 cpp_skip_hspace (pfile
);
6834 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6835 parse_name (pfile
, GETC());
6836 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6839 cpp_skip_hspace (pfile
);
6840 if (PEEKC() != '(') {
6841 cpp_error (pfile
, "missing token-sequence in `#assert'");
6847 tokens
= read_token_list (pfile
, &error_flag
);
6851 cpp_error (pfile
, "empty token-sequence in `#assert'");
6854 cpp_skip_hspace (pfile
);
6856 if (c
!= EOF
&& c
!= '\n')
6857 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6858 skip_rest_of_line (pfile
);
6861 /* If this name isn't already an assertion name, make it one.
6862 Error if it was already in use in some other way. */
6865 ASSERTION_HASHNODE
*hp
;
6866 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6867 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6868 struct tokenlist_list
*value
6869 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6871 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6873 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6874 cpp_error (pfile
, "`defined' redefined as assertion");
6875 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6878 /* Add the spec'd token-sequence to the list of such. */
6879 value
->tokens
= tokens
;
6880 value
->next
= hp
->value
;
6883 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6886 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6887 skip_rest_of_line (pfile
);
6892 do_unassert (pfile
, keyword
, buf
, limit
)
6894 struct directive
*keyword
;
6895 U_CHAR
*buf
, *limit
;
6897 long symstart
; /* remember where symbol name starts */
6898 int sym_length
; /* and how long it is */
6901 struct arglist
*tokens
= NULL
;
6902 int tokens_specified
= 0;
6904 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6905 && !CPP_BUFFER (pfile
)->system_header_p
)
6906 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6908 cpp_skip_hspace (pfile
);
6910 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6911 parse_name (pfile
, GETC());
6912 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6915 cpp_skip_hspace (pfile
);
6916 if (PEEKC() == '(') {
6919 tokens
= read_token_list (pfile
, &error_flag
);
6923 cpp_error (pfile
, "empty token list in `#unassert'");
6927 tokens_specified
= 1;
6930 cpp_skip_hspace (pfile
);
6932 if (c
!= EOF
&& c
!= '\n')
6933 cpp_error (pfile
, "junk at end of `#unassert'");
6934 skip_rest_of_line (pfile
);
6937 ASSERTION_HASHNODE
*hp
;
6938 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6939 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6940 struct tokenlist_list
*tail
, *prev
;
6942 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6946 /* If no token list was specified, then eliminate this assertion
6948 if (! tokens_specified
)
6949 delete_assertion (hp
);
6951 /* If a list of tokens was given, then delete any matching list. */
6956 struct tokenlist_list
*next
= tail
->next
;
6957 if (compare_token_lists (tail
->tokens
, tokens
)) {
6961 hp
->value
= tail
->next
;
6962 free_token_list (tail
->tokens
);
6972 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6975 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6976 skip_rest_of_line (pfile
);
6980 /* Test whether there is an assertion named NAME
6981 and optionally whether it has an asserted token list TOKENS.
6982 NAME is not null terminated; its length is SYM_LENGTH.
6983 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6986 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
6990 int tokens_specified
;
6991 struct arglist
*tokens
;
6993 ASSERTION_HASHNODE
*hp
;
6994 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6996 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
6997 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
6999 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
7001 /* It is not an assertion; just return false. */
7004 /* If no token list was specified, then value is 1. */
7005 if (! tokens_specified
)
7009 struct tokenlist_list
*tail
;
7013 /* If a list of tokens was given,
7014 then succeed if the assertion records a matching list. */
7017 if (compare_token_lists (tail
->tokens
, tokens
))
7022 /* Fail if the assertion has no matching list. */
7027 /* Compare two lists of tokens for equality including order of tokens. */
7030 compare_token_lists (l1
, l2
)
7031 struct arglist
*l1
, *l2
;
7034 if (l1
->length
!= l2
->length
)
7036 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7042 /* Succeed if both lists end at the same time. */
7047 reverse_token_list (tokens
)
7048 struct arglist
*tokens
;
7050 register struct arglist
*prev
= 0, *this, *next
;
7051 for (this = tokens
; this; this = next
)
7060 /* Read a space-separated list of tokens ending in a close parenthesis.
7061 Return a list of strings, in the order they were written.
7062 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7064 static struct arglist
*
7065 read_token_list (pfile
, error_flag
)
7069 struct arglist
*token_ptrs
= 0;
7074 FORWARD (1); /* Skip '(' */
7076 /* Loop over the assertion value tokens. */
7079 struct arglist
*temp
;
7080 long name_written
= CPP_WRITTEN (pfile
);
7081 int eofp
= 0; int c
;
7083 cpp_skip_hspace (pfile
);
7087 /* Find the end of the token. */
7090 CPP_PUTC (pfile
, c
);
7098 CPP_PUTC (pfile
, c
);
7100 else if (c
== '"' || c
== '\'')
7103 cpp_get_token (pfile
);
7109 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7110 && c
!= '"' && c
!= '\'')
7112 CPP_PUTC (pfile
, c
);
7115 if (c
!= EOF
) FORWARD(-1);
7118 length
= CPP_WRITTEN (pfile
) - name_written
;
7119 temp
= (struct arglist
*)
7120 xmalloc (sizeof (struct arglist
) + length
+ 1);
7121 temp
->name
= (U_CHAR
*) (temp
+ 1);
7122 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7123 (char *) temp
->name
, length
);
7124 temp
->name
[length
] = 0;
7125 temp
->next
= token_ptrs
;
7127 temp
->length
= length
;
7129 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7131 if (c
== EOF
|| c
== '\n')
7134 "unterminated token sequence following `#' operator");
7139 /* We accumulated the names in reverse order.
7140 Now reverse them to get the proper order. */
7141 return reverse_token_list (token_ptrs
);
7145 free_token_list (tokens
)
7146 struct arglist
*tokens
;
7149 struct arglist
*next
= tokens
->next
;
7150 free (tokens
->name
);
7156 /* Get the file-mode and data size of the file open on FD
7157 and store them in *MODE_POINTER and *SIZE_POINTER. */
7160 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
7163 long int *size_pointer
;
7167 if (fstat (fd
, &sbuf
) < 0) return (-1);
7168 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
7169 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
7173 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7174 retrying if necessary. Return a negative value if an error occurs,
7175 otherwise return the actual number of bytes read,
7176 which must be LEN unless end-of-file was reached. */
7179 safe_read (desc
, ptr
, len
)
7186 int nchars
= read (desc
, ptr
, left
);
7207 unsigned size
= strlen (input
);
7208 char *output
= xmalloc (size
+ 1);
7209 strcpy (output
, input
);
7213 /* Initialize PMARK to remember the current position of PFILE. */
7215 parse_set_mark (pmark
, pfile
)
7216 struct parse_marker
*pmark
;
7219 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7220 pmark
->next
= pbuf
->marks
;
7221 pbuf
->marks
= pmark
;
7223 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7226 /* Cleanup PMARK - we no longer need it. */
7228 parse_clear_mark (pmark
)
7229 struct parse_marker
*pmark
;
7231 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7232 for (; ; pp
= &(*pp
)->next
) {
7233 if (*pp
== NULL
) fatal ("internal error", "in parse_set_mark");
7234 if (*pp
== pmark
) break;
7239 /* Backup the current position of PFILE to that saved in PMARK. */
7242 parse_goto_mark (pmark
, pfile
)
7243 struct parse_marker
*pmark
;
7246 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7247 if (pbuf
!= pmark
->buf
)
7248 fatal ("internal error %s", "parse_goto_mark");
7249 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7252 /* Reset PMARK to point to the current position of PFILE. (Same
7253 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7256 parse_move_mark (pmark
, pfile
)
7257 struct parse_marker
*pmark
;
7260 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7261 if (pbuf
!= pmark
->buf
)
7262 fatal ("internal error %s", "parse_move_mark");
7263 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7267 cpp_read_check_assertion (pfile
)
7270 int name_start
= CPP_WRITTEN (pfile
);
7271 int name_length
, name_written
;
7273 FORWARD (1); /* Skip '#' */
7274 cpp_skip_hspace (pfile
);
7275 parse_name (pfile
, GETC ());
7276 name_written
= CPP_WRITTEN (pfile
);
7277 name_length
= name_written
- name_start
;
7278 cpp_skip_hspace (pfile
);
7279 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7282 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7283 result
= check_assertion (pfile
,
7284 pfile
->token_buffer
+ name_start
, name_length
,
7288 result
= check_assertion (pfile
,
7289 pfile
->token_buffer
+ name_start
, name_length
,
7291 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7296 cpp_print_file_and_line (pfile
)
7299 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7304 cpp_buf_line_and_col (ip
, &line
, &col
);
7305 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
7306 line
, pfile
->show_column
? col
: -1);
7311 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7314 char *arg1
, *arg2
, *arg3
;
7316 cpp_print_containing_files (pfile
);
7317 cpp_print_file_and_line (pfile
);
7318 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7321 /* Print error message but don't count it. */
7324 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7327 char *arg1
, *arg2
, *arg3
;
7329 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7332 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7335 cpp_print_containing_files (pfile
);
7336 cpp_print_file_and_line (pfile
);
7337 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7340 /* Print an error message and maybe count it. */
7343 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7346 char *arg1
, *arg2
, *arg3
;
7348 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7349 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7351 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7355 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7359 char *arg1
, *arg2
, *arg3
;
7362 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7364 cpp_print_containing_files (pfile
);
7367 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7369 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7373 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7377 char *arg1
, *arg2
, *arg3
;
7382 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7385 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7388 cpp_print_containing_files (pfile
);
7390 ip
= cpp_file_buffer (pfile
);
7393 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7395 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7399 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7403 char *arg1
, *arg2
, *arg3
;
7405 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7406 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7408 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7411 /* Report a warning (or an error if pedantic_errors)
7412 giving specified file name and line number, not current. */
7415 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7420 char *arg1
, *arg2
, *arg3
;
7422 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7423 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7426 cpp_file_line_for_message (pfile
, file
, line
, -1);
7427 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7428 msg
, arg1
, arg2
, arg3
);
7431 /* This defines "errno" properly for VMS, and gives us EACCES. */
7438 #ifndef HAVE_STRERROR
7439 extern int sys_nerr
;
7441 extern const char *const sys_errlist
[];
7443 extern char *sys_errlist
[];
7445 #else /* HAVE_STRERROR */
7449 char *strerror (int,...);
7453 * my_strerror - return the descriptive text associated with an `errno' code.
7457 my_strerror (errnum
)
7463 #ifndef HAVE_STRERROR
7464 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7466 result
= strerror (errnum
);
7469 /* VAXCRTL's strerror() takes an optional second argument, which only
7470 matters when the first argument is EVMSERR. However, it's simplest
7471 just to pass it unconditionally. `vaxc$errno' is declared in
7472 <errno.h>, and maintained by the library in parallel with `errno'.
7473 We assume that caller's `errnum' either matches the last setting of
7474 `errno' by the library or else does not have the value `EVMSERR'. */
7476 result
= strerror (errnum
, vaxc$errno
);
7480 result
= "undocumented I/O error";
7485 /* Error including a message from `errno'. */
7488 cpp_error_from_errno (pfile
, name
)
7493 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7495 cpp_print_containing_files (pfile
);
7498 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
7500 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (errno
));
7504 cpp_perror_with_name (pfile
, name
)
7508 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7512 * No pre-compiled header file support.
7514 * Possibly different enum token codes for each C/C++ token.
7516 * Should clean up remaining directives to that do_XXX functions
7517 * only take two arguments and all have command_reads_line.
7519 * Find and cleanup remaining uses of static variables,
7521 * Support for trigraphs.
7523 * Support -dM flag (dump_all_macros).
7525 * Support for_lint flag.