2 Copyright (C) 1986, 87, 89, 92-5, 1996 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 ();
266 extern void fancy_abort ();
268 static void pipe_closed ();
269 static void print_containing_files ();
270 static int lookup_import ();
271 static int redundant_include_p ();
272 static is_system_include ();
273 static struct file_name_map
*read_name_map ();
274 static char *read_filename_string ();
275 static int open_include_file ();
276 static int check_preconditions ();
277 static void pcfinclude ();
278 static void pcstring_used ();
279 static int check_macro_name ();
280 static int compare_defs ();
281 static int compare_token_lists ();
282 static HOST_WIDE_INT
eval_if_expression ();
283 static int change_newlines ();
285 static int file_size_and_mode ();
286 static struct arglist
*read_token_list ();
287 static void free_token_list ();
288 static int safe_read ();
289 static void push_macro_expansion
PARAMS ((cpp_reader
*,
290 U_CHAR
*, int, HASHNODE
*));
291 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
292 extern char *xrealloc ();
293 static char *xcalloc ();
294 static char *savestring ();
296 static void conditional_skip ();
297 static void skip_if_group ();
299 /* Last arg to output_line_command. */
300 enum file_change_code
{same_file
, enter_file
, leave_file
};
302 /* External declarations. */
304 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
306 extern char *getenv ();
307 extern FILE *fdopen ();
308 extern char *version_string
;
309 extern struct tm
*localtime ();
311 /* These functions are declared to return int instead of void since they
312 are going to be placed in a table and some old compilers have trouble with
313 pointers to functions returning void. */
315 static int do_define ();
316 static int do_line ();
317 static int do_include ();
318 static int do_undef ();
319 static int do_error ();
320 static int do_pragma ();
321 static int do_ident ();
323 static int do_xifdef ();
324 static int do_else ();
325 static int do_elif ();
326 static int do_endif ();
327 static int do_sccs ();
328 static int do_once ();
329 static int do_assert ();
330 static int do_unassert ();
331 static int do_warning ();
333 struct file_name_list
335 struct file_name_list
*next
;
337 /* If the following is nonzero, it is a macro name.
338 Don't include the file again if that macro is defined. */
339 U_CHAR
*control_macro
;
340 /* If the following is nonzero, it is a C-language system include
342 int c_system_include_path
;
343 /* Mapping of file names for this directory. */
344 struct file_name_map
*name_map
;
345 /* Non-zero if name_map is valid. */
349 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
350 via the same directory as the file that #included it. */
351 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
353 /* #include "file" looks in source file dir, then stack. */
354 /* #include <file> just looks in the stack. */
355 /* -I directories are added to the end, then the defaults are added. */
357 static struct default_include
{
358 char *fname
; /* The name of the directory. */
359 int cplusplus
; /* Only look here if we're compiling C++. */
360 int cxx_aware
; /* Includes in this directory don't need to
361 be wrapped in extern "C" when compiling
363 } include_defaults_array
[]
364 #ifdef INCLUDE_DEFAULTS
368 /* Pick up GNU C++ specific include files. */
369 { GPLUSPLUS_INCLUDE_DIR
, 1, 1 },
370 { OLD_GPLUSPLUS_INCLUDE_DIR
, 1, 1 },
372 /* This is the dir for fixincludes. Put it just before
373 the files that we fix. */
374 { GCC_INCLUDE_DIR
, 0, 0 },
375 /* For cross-compilation, this dir name is generated
376 automatically in Makefile.in. */
377 { CROSS_INCLUDE_DIR
, 0, 0 },
378 /* This is another place that the target system's headers might be. */
379 { TOOL_INCLUDE_DIR
, 0, 1 },
380 #else /* not CROSS_COMPILE */
381 /* This should be /usr/local/include and should come before
382 the fixincludes-fixed header files. */
383 { LOCAL_INCLUDE_DIR
, 0, 1 },
384 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
385 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
386 { TOOL_INCLUDE_DIR
, 0, 1 },
387 /* This is the dir for fixincludes. Put it just before
388 the files that we fix. */
389 { GCC_INCLUDE_DIR
, 0, 0 },
390 /* Some systems have an extra dir of include files. */
391 #ifdef SYSTEM_INCLUDE_DIR
392 { SYSTEM_INCLUDE_DIR
, 0, 0 },
394 { STANDARD_INCLUDE_DIR
, 0, 0 },
395 #endif /* not CROSS_COMPILE */
398 #endif /* no INCLUDE_DEFAULTS */
400 /* `struct directive' defines one #-directive, including how to handle it. */
403 int length
; /* Length of name */
404 int (*func
)(); /* Function to handle directive */
405 char *name
; /* Name of directive */
406 enum node_type type
; /* Code which describes which directive. */
407 char command_reads_line
; /* One if rest of line is read by func. */
408 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
409 char pass_thru
; /* Copy preprocessed directive to output file.*/
412 /* Here is the actual list of #-directives, most-often-used first.
413 The initialize_builtins function assumes #define is the very first. */
415 static struct directive directive_table
[] = {
416 { 6, do_define
, "define", T_DEFINE
, 0, 1},
417 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
418 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
419 { 7, do_include
, "include", T_INCLUDE
, 1},
420 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
421 { 6, do_include
, "import", T_IMPORT
, 1},
422 { 5, do_endif
, "endif", T_ENDIF
, 1},
423 { 4, do_else
, "else", T_ELSE
, 1},
424 { 2, do_if
, "if", T_IF
, 1},
425 { 4, do_elif
, "elif", T_ELIF
, 1},
426 { 5, do_undef
, "undef", T_UNDEF
},
427 { 5, do_error
, "error", T_ERROR
},
428 { 7, do_warning
, "warning", T_WARNING
},
429 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
430 { 4, do_line
, "line", T_LINE
, 1},
431 { 5, do_ident
, "ident", T_IDENT
, 1, 0, 1},
432 #ifdef SCCS_DIRECTIVE
433 { 4, do_sccs
, "sccs", T_SCCS
},
435 { 6, do_assert
, "assert", T_ASSERT
, 1},
436 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
437 { -1, 0, "", T_UNUSED
},
440 /* table to tell if char can be part of a C identifier. */
441 U_CHAR is_idchar
[256];
442 /* table to tell if char can be first char of a c identifier. */
443 U_CHAR is_idstart
[256];
444 /* table to tell if c is horizontal space. */
445 U_CHAR is_hor_space
[256];
446 /* table to tell if c is horizontal or vertical space. */
447 static U_CHAR is_space
[256];
449 /* Initialize syntactic classifications of characters. */
452 initialize_char_syntax (opts
)
453 struct cpp_options
*opts
;
458 * Set up is_idchar and is_idstart tables. These should be
459 * faster than saying (is_alpha (c) || c == '_'), etc.
460 * Set up these things before calling any routines tthat
463 for (i
= 'a'; i
<= 'z'; i
++) {
464 is_idchar
[i
- 'a' + 'A'] = 1;
466 is_idstart
[i
- 'a' + 'A'] = 1;
469 for (i
= '0'; i
<= '9'; i
++)
473 is_idchar
['$'] = opts
->dollars_in_ident
;
474 is_idstart
['$'] = opts
->dollars_in_ident
;
476 /* horizontal space table */
477 is_hor_space
[' '] = 1;
478 is_hor_space
['\t'] = 1;
479 is_hor_space
['\v'] = 1;
480 is_hor_space
['\f'] = 1;
481 is_hor_space
['\r'] = 1;
492 /* Place into PFILE a quoted string representing the string SRC.
493 Caller must reserve enough space in pfile->token_buffer. */
496 quote_string (pfile
, src
)
502 CPP_PUTC_Q (pfile
, '\"');
504 switch ((c
= *src
++))
508 CPP_PUTC_Q (pfile
, c
);
511 sprintf (CPP_PWRITTEN (pfile
), "\\%03o", c
);
512 CPP_ADJUST_WRITTEN (pfile
, 4);
518 CPP_PUTC_Q (pfile
, '\\');
519 CPP_PUTC_Q (pfile
, c
);
523 CPP_PUTC_Q (pfile
, '\"');
524 CPP_NUL_TERMINATE_Q (pfile
);
529 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
532 cpp_grow_buffer (pfile
, n
)
536 long old_written
= CPP_WRITTEN (pfile
);
537 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
538 pfile
->token_buffer
= (U_CHAR
*)
539 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
540 CPP_SET_WRITTEN (pfile
, old_written
);
545 * process a given definition string, for initialization
546 * If STR is just an identifier, define it with value 1.
547 * If STR has anything after the identifier, then it should
548 * be identifier=definition.
552 cpp_define (pfile
, str
)
562 cpp_error (pfile
, "malformed option `-D %s'", str
);
565 while (is_idchar
[*++p
])
569 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
570 strcpy ((char *)buf
, str
);
571 strcat ((char *)buf
, " 1");
575 cpp_error (pfile
, "malformed option `-D %s'", str
);
581 /* Copy the entire option so we can modify it. */
582 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
583 strncpy (buf
, str
, p
- str
);
584 /* Change the = to a space. */
586 /* Scan for any backslash-newline and remove it. */
591 if (*p
== '\\' && p
[1] == '\n')
599 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
602 /* Process the string STR as if it appeared as the body of a #assert.
603 OPTION is the option name for which STR was the argument. */
606 make_assertion (pfile
, option
, str
)
611 struct directive
*kt
;
614 /* Copy the entire option so we can modify it. */
615 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
616 strcpy ((char *) buf
, str
);
617 /* Scan for any backslash-newline and remove it. */
621 if (*p
== '\\' && p
[1] == '\n')
630 if (!is_idstart
[*p
]) {
631 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
634 while (is_idchar
[*++p
])
636 while (*p
== ' ' || *p
== '\t') p
++;
637 if (! (*p
== 0 || *p
== '(')) {
638 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
642 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
644 do_assert (pfile
, NULL
, NULL
, NULL
);
645 cpp_pop_buffer (pfile
);
649 /* Append a chain of `struct file_name_list's
650 to the end of the main include chain.
651 FIRST is the beginning of the chain to append, and LAST is the end. */
654 append_include_chain (pfile
, first
, last
)
656 struct file_name_list
*first
, *last
;
658 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
659 struct file_name_list
*dir
;
664 if (opts
->include
== 0)
665 opts
->include
= first
;
667 opts
->last_include
->next
= first
;
669 if (opts
->first_bracket_include
== 0)
670 opts
->first_bracket_include
= first
;
672 for (dir
= first
; ; dir
= dir
->next
) {
673 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
674 if (len
> pfile
->max_include_len
)
675 pfile
->max_include_len
= len
;
681 opts
->last_include
= last
;
684 /* Add output to `deps_buffer' for the -M switch.
685 STRING points to the text to be output.
686 SPACER is ':' for targets, ' ' for dependencies, zero for text
687 to be inserted literally. */
690 deps_output (pfile
, string
, spacer
)
695 int size
= strlen (string
);
700 #ifndef MAX_OUTPUT_COLUMNS
701 #define MAX_OUTPUT_COLUMNS 72
704 && pfile
->deps_column
> 0
705 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
707 deps_output (pfile
, " \\\n ", 0);
708 pfile
->deps_column
= 0;
711 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
713 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
714 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
715 pfile
->deps_allocated_size
);
717 if (spacer
== ' ' && pfile
->deps_column
> 0)
718 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
719 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
720 pfile
->deps_size
+= size
;
721 pfile
->deps_column
+= size
;
723 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
724 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
727 /* Given a colon-separated list of file names PATH,
728 add all the names to the search path for include files. */
731 path_include (pfile
, path
)
743 struct file_name_list
*dirtmp
;
745 /* Find the end of this name. */
746 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
748 /* An empty name in the path stands for the current directory. */
749 name
= (char *) xmalloc (2);
753 /* Otherwise use the directory that is named. */
754 name
= (char *) xmalloc (q
- p
+ 1);
755 bcopy (p
, name
, q
- p
);
759 dirtmp
= (struct file_name_list
*)
760 xmalloc (sizeof (struct file_name_list
));
761 dirtmp
->next
= 0; /* New one goes on the end */
762 dirtmp
->control_macro
= 0;
763 dirtmp
->c_system_include_path
= 0;
764 dirtmp
->fname
= name
;
765 dirtmp
->got_name_map
= 0;
766 append_include_chain (pfile
, dirtmp
, dirtmp
);
768 /* Advance past this name. */
772 /* Skip the colon. */
778 cpp_options_init (opts
)
781 bzero ((char *) opts
, sizeof *opts
);
782 opts
->in_fname
= NULL
;
783 opts
->out_fname
= NULL
;
785 /* Initialize is_idchar to allow $. */
786 opts
->dollars_in_ident
= 1;
787 initialize_char_syntax (opts
);
788 opts
->dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
> 0;
790 opts
->no_line_commands
= 0;
791 opts
->no_trigraphs
= 1;
792 opts
->put_out_comments
= 0;
793 opts
->print_include_names
= 0;
794 opts
->dump_macros
= dump_none
;
797 opts
->cplusplus_comments
= 0;
804 opts
->pedantic_errors
= 0;
805 opts
->inhibit_warnings
= 0;
806 opts
->warn_comments
= 0;
807 opts
->warn_import
= 1;
808 opts
->warnings_are_errors
= 0;
812 null_underflow (pfile
)
819 null_cleanup (pbuf
, pfile
)
827 macro_cleanup (pbuf
, pfile
)
831 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
832 if (macro
->type
== T_DISABLED
)
833 macro
->type
= T_MACRO
;
834 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
840 file_cleanup (pbuf
, pfile
)
852 /* Assuming we have read '/'.
853 If this is the start of a comment (followed by '*' or '/'),
854 skip to the end of the comment, and return ' '.
855 Return EOF if we reached the end of file before the end of the comment.
856 If not the start of a comment, return '/'. */
859 skip_comment (pfile
, linep
)
864 while (PEEKC() == '\\' && PEEKN(1) == '\n')
879 while (c
== '\\' && PEEKC() == '\n')
883 FORWARD(1), c
= GETC();
885 if (prev_c
== '*' && c
== '/')
887 if (c
== '\n' && linep
)
891 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
898 return ' '; /* Allow // to be terminated by EOF. */
899 while (c
== '\\' && PEEKC() == '\n')
908 /* Don't consider final '\n' to be part of comment. */
918 /* Skip whitespace \-newline and comments. Does not macro-expand. */
921 cpp_skip_hspace (pfile
)
931 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
932 cpp_pedwarn (pfile
, "%s in preprocessing directive",
933 c
== '\f' ? "formfeed" : "vertical tab");
939 c
= skip_comment (pfile
, NULL
);
942 if (c
== EOF
|| c
== '/')
945 else if (c
== '\\' && PEEKN(1) == '\n') {
948 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
949 && is_hor_space
[PEEKN(1)])
955 /* Read the rest of the current line.
956 The line is appended to PFILE's output buffer. */
959 copy_rest_of_line (pfile
)
962 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
979 goto scan_directive_token
;
983 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '*'))
984 goto scan_directive_token
;
988 if (CPP_PEDANTIC (pfile
))
989 cpp_pedwarn (pfile
, "%s in preprocessing directive",
990 c
== '\f' ? "formfeed" : "vertical tab");
996 scan_directive_token
:
998 cpp_get_token (pfile
);
1001 CPP_PUTC (pfile
, c
);
1004 CPP_NUL_TERMINATE (pfile
);
1008 skip_rest_of_line (pfile
)
1011 long old
= CPP_WRITTEN (pfile
);
1012 copy_rest_of_line (pfile
);
1013 CPP_SET_WRITTEN (pfile
, old
);
1016 /* Handle a possible # directive.
1017 '#' has already been read. */
1020 handle_directive (pfile
)
1023 register struct directive
*kt
;
1026 U_CHAR
*ident
, *line_end
;
1027 long old_written
= CPP_WRITTEN (pfile
);
1029 cpp_skip_hspace (pfile
);
1032 if (c
>= '0' && c
<= '9')
1034 /* Handle # followed by a line number. */
1035 if (CPP_PEDANTIC (pfile
))
1036 cpp_pedwarn (pfile
, "`#' followed by integer");
1037 do_line (pfile
, NULL
);
1038 goto done_a_directive
;
1041 /* Now find the directive name. */
1042 CPP_PUTC (pfile
, '#');
1043 parse_name (pfile
, GETC());
1044 ident
= pfile
->token_buffer
+ old_written
+ 1;
1045 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1046 if (ident_length
== 0 && PEEKC() == '\n')
1048 /* A line of just `#' becomes blank. */
1049 goto done_a_directive
;
1053 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1055 while (is_idchar
[*p
]) {
1056 if (*p
< '0' || *p
> '9')
1060 /* Avoid error for `###' and similar cases unless -pedantic. */
1062 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1064 if (pedantic
&& !lang_asm
)
1065 cpp_warning (pfile
, "invalid preprocessor directive");
1071 cpp_error (pfile
, "invalid preprocessor directive name");
1077 * Decode the keyword and call the appropriate expansion
1078 * routine, after moving the input pointer up to the next line.
1080 for (kt
= directive_table
; ; kt
++) {
1081 if (kt
->length
<= 0)
1082 goto not_a_directive
;
1083 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1087 if (kt
->command_reads_line
)
1091 /* Nonzero means do not delete comments within the directive.
1092 #define needs this when -traditional. */
1093 int comments
= CPP_TRADITIONAL (pfile
) && kt
->traditional_comments
;
1094 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1095 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1096 after_ident
= CPP_WRITTEN (pfile
);
1097 copy_rest_of_line (pfile
);
1098 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1101 /* For #pragma and #define, we may want to pass through the directive.
1102 Other directives may create output, but we don't want the directive
1103 itself out, so we pop it now. For example #include may write a #line
1104 command (see comment in do_include), and conditionals may emit
1105 #failed ... #endfailed stuff. But note that popping the buffer
1106 means the parameters to kt->func may point after pfile->limit
1107 so these parameters are invalid as soon as something gets appended
1108 to the token_buffer. */
1110 line_end
= CPP_PWRITTEN (pfile
);
1111 if (!kt
->pass_thru
&& kt
->type
!= T_DEFINE
)
1112 CPP_SET_WRITTEN (pfile
, old_written
);
1114 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1116 || (kt
->type
== T_DEFINE
1117 && CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
))
1119 /* Just leave the entire #define in the output stack. */
1121 else if (kt
->type
== T_DEFINE
1122 && CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1124 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7; /* Skip "#define". */
1125 SKIP_WHITE_SPACE (p
);
1126 while (is_idchar
[*p
]) p
++;
1128 CPP_PUTC (pfile
, '\n');
1130 else if (kt
->type
== T_DEFINE
)
1131 CPP_SET_WRITTEN (pfile
, old_written
);
1139 /* Pass a directive through to the output file.
1140 BUF points to the contents of the directive, as a contiguous string.
1141 LIMIT points to the first character past the end of the directive.
1142 KEYWORD is the keyword-table entry for the directive. */
1145 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1146 U_CHAR
*buf
, *limit
;
1148 struct directive
*keyword
;
1150 register unsigned keyword_length
= keyword
->length
;
1152 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1153 CPP_PUTC_Q (pfile
, '#');
1154 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1155 if (limit
!= buf
&& buf
[0] != ' ')
1156 CPP_PUTC_Q (pfile
, ' ');
1157 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1159 CPP_PUTS_Q (pfile
, '\n');
1160 /* Count the line we have just made in the output,
1161 to get in sync properly. */
1166 /* The arglist structure is built by do_define to tell
1167 collect_definition where the argument names begin. That
1168 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1169 would contain pointers to the strings x, y, and z.
1170 Collect_definition would then build a DEFINITION node,
1171 with reflist nodes pointing to the places x, y, and z had
1172 appeared. So the arglist is just convenience data passed
1173 between these two routines. It is not kept around after
1174 the current #define has been processed and entered into the
1178 struct arglist
*next
;
1185 /* Read a replacement list for a macro with parameters.
1186 Build the DEFINITION structure.
1187 Reads characters of text starting at BUF until END.
1188 ARGLIST specifies the formal parameters to look for
1189 in the text of the definition; NARGS is the number of args
1190 in that list, or -1 for a macro name that wants no argument list.
1191 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1192 and NAMELEN is its length in characters.
1194 Note that comments, backslash-newlines, and leading white space
1195 have already been deleted from the argument. */
1198 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1200 U_CHAR
*buf
, *limit
;
1202 struct arglist
*arglist
;
1205 register U_CHAR
*p
, *lastp
, *exp_p
;
1206 struct reflist
*endpat
= NULL
;
1207 /* Pointer to first nonspace after last ## seen. */
1209 /* Pointer to first nonspace after last single-# seen. */
1210 U_CHAR
*stringify
= 0;
1212 int expected_delimiter
= '\0';
1214 /* Scan thru the replacement list, ignoring comments and quoted
1215 strings, picking up on the macro calls. It does a linear search
1216 thru the arg list on every potential symbol. Profiling might say
1217 that something smarter should happen. */
1222 /* Find the beginning of the trailing whitespace. */
1224 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1226 /* Allocate space for the text in the macro definition.
1227 Leading and trailing whitespace chars need 2 bytes each.
1228 Each other input char may or may not need 1 byte,
1229 so this is an upper bound. The extra 5 are for invented
1230 leading and trailing newline-marker and final null. */
1231 maxsize
= (sizeof (DEFINITION
)
1233 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1237 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1239 defn
->nargs
= nargs
;
1240 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1245 /* Add one initial space escape-marker to prevent accidental
1246 token-pasting (often removed by macroexpand). */
1250 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1251 cpp_error (pfile
, "`##' at start of macro definition");
1255 /* Process the main body of the definition. */
1257 int skipped_arg
= 0;
1258 register U_CHAR c
= *p
++;
1262 if (!CPP_TRADITIONAL (pfile
)) {
1266 if (expected_delimiter
!= '\0') {
1267 if (c
== expected_delimiter
)
1268 expected_delimiter
= '\0';
1270 expected_delimiter
= c
;
1274 if (p
< limit
&& expected_delimiter
) {
1275 /* In a string, backslash goes through
1276 and makes next char ordinary. */
1282 /* An '@' in a string or character constant stands for itself,
1283 and does not need to be escaped. */
1284 if (!expected_delimiter
)
1289 /* # is ordinary inside a string. */
1290 if (expected_delimiter
)
1292 if (p
< limit
&& *p
== '#') {
1293 /* ##: concatenate preceding and following tokens. */
1294 /* Take out the first #, discard preceding whitespace. */
1296 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1298 /* Skip the second #. */
1300 /* Discard following whitespace. */
1301 SKIP_WHITE_SPACE (p
);
1304 cpp_error (pfile
, "`##' at end of macro definition");
1305 } else if (nargs
>= 0) {
1306 /* Single #: stringify following argument ref.
1307 Don't leave the # in the expansion. */
1309 SKIP_WHITE_SPACE (p
);
1310 if (p
== limit
|| ! is_idstart
[*p
])
1312 "`#' operator is not followed by a macro argument name");
1319 /* In -traditional mode, recognize arguments inside strings and
1320 and character constants, and ignore special properties of #.
1321 Arguments inside strings are considered "stringified", but no
1322 extra quote marks are supplied. */
1326 if (expected_delimiter
!= '\0') {
1327 if (c
== expected_delimiter
)
1328 expected_delimiter
= '\0';
1330 expected_delimiter
= c
;
1334 /* Backslash quotes delimiters and itself, but not macro args. */
1335 if (expected_delimiter
!= 0 && p
< limit
1336 && (*p
== expected_delimiter
|| *p
== '\\')) {
1343 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1346 /* If we find a comment that wasn't removed by handle_directive,
1347 this must be -traditional. So replace the comment with
1351 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1354 /* Mark this as a concatenation-point, as if it had been ##. */
1362 /* Handle the start of a symbol. */
1363 if (is_idchar
[c
] && nargs
> 0) {
1364 U_CHAR
*id_beg
= p
- 1;
1368 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1369 id_len
= p
- id_beg
;
1371 if (is_idstart
[c
]) {
1372 register struct arglist
*arg
;
1374 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1375 struct reflist
*tpat
;
1377 if (arg
->name
[0] == c
1378 && arg
->length
== id_len
1379 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1380 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1381 if (CPP_TRADITIONAL (pfile
)) {
1382 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1386 "macro arg `%.*s' would be stringified with -traditional.",
1390 /* If ANSI, don't actually substitute inside a string. */
1391 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1393 /* make a pat node for this arg and append it to the end of
1395 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1397 tpat
->raw_before
= concat
== id_beg
;
1398 tpat
->raw_after
= 0;
1399 tpat
->rest_args
= arg
->rest_args
;
1400 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1401 ? expected_delimiter
!= '\0'
1402 : stringify
== id_beg
);
1405 defn
->pattern
= tpat
;
1407 endpat
->next
= tpat
;
1410 tpat
->argno
= arg
->argno
;
1411 tpat
->nchars
= exp_p
- lastp
;
1413 register U_CHAR
*p1
= p
;
1414 SKIP_WHITE_SPACE (p1
);
1415 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1416 tpat
->raw_after
= 1;
1418 lastp
= exp_p
; /* place to start copying from next time */
1425 /* If this was not a macro arg, copy it into the expansion. */
1426 if (! skipped_arg
) {
1427 register U_CHAR
*lim1
= p
;
1431 if (stringify
== id_beg
)
1433 "`#' operator should be followed by a macro argument name");
1438 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1440 /* If ANSI, put in a "@ " marker to prevent token pasting.
1441 But not if "inside a string" (which in ANSI mode
1442 happens only for -D option). */
1449 defn
->length
= exp_p
- defn
->expansion
;
1451 /* Crash now if we overrun the allocated size. */
1452 if (defn
->length
+ 1 > maxsize
)
1456 /* This isn't worth the time it takes. */
1457 /* give back excess storage */
1458 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1465 * special extension string that can be added to the last macro argument to
1466 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1467 * #define wow(a, b...) process (b, a, b)
1468 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1469 * { wow (one, two); } -> { process (two, one, two); }
1470 * if this "rest_arg" is used with the concat token '##' and if it is not
1471 * supplied then the token attached to with ## will not be outputted. Ex:
1472 * #define wow (a, b...) process (b ## , a, ## b)
1473 * { wow (1, 2); } -> { process (2, 1, 2); }
1474 * { wow (one); } -> { process (one); {
1476 static char rest_extension
[] = "...";
1477 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1479 /* Create a DEFINITION node from a #define directive. Arguments are
1480 as for do_define. */
1483 create_definition (buf
, limit
, pfile
, predefinition
)
1484 U_CHAR
*buf
, *limit
;
1488 U_CHAR
*bp
; /* temp ptr into input buffer */
1489 U_CHAR
*symname
; /* remember where symbol name starts */
1490 int sym_length
; /* and how long it is */
1493 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1495 int arglengths
= 0; /* Accumulate lengths of arg names
1496 plus number of args. */
1498 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1502 while (is_hor_space
[*bp
])
1505 symname
= bp
; /* remember where it starts */
1507 sym_length
= check_macro_name (pfile
, bp
, "macro");
1510 /* Lossage will occur if identifiers or control keywords are broken
1511 across lines using backslash. This is not the right place to take
1515 struct arglist
*arg_ptrs
= NULL
;
1518 bp
++; /* skip '(' */
1519 SKIP_WHITE_SPACE (bp
);
1521 /* Loop over macro argument names. */
1522 while (*bp
!= ')') {
1523 struct arglist
*temp
;
1525 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1527 temp
->next
= arg_ptrs
;
1528 temp
->argno
= argno
++;
1529 temp
->rest_args
= 0;
1533 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1535 if (!is_idstart
[*bp
])
1536 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1538 /* Find the end of the arg name. */
1539 while (is_idchar
[*bp
]) {
1541 /* do we have a "special" rest-args extension here? */
1542 if (limit
- bp
> REST_EXTENSION_LENGTH
&&
1543 strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1545 temp
->rest_args
= 1;
1549 temp
->length
= bp
- temp
->name
;
1551 bp
+= REST_EXTENSION_LENGTH
;
1552 arglengths
+= temp
->length
+ 2;
1553 SKIP_WHITE_SPACE (bp
);
1554 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1555 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1560 SKIP_WHITE_SPACE (bp
);
1563 cpp_error (pfile
, "unterminated parameter list in `#define'");
1567 struct arglist
*otemp
;
1569 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1570 if (temp
->length
== otemp
->length
&&
1571 strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1574 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1575 (void) strncpy (name
, temp
->name
, temp
->length
);
1576 name
[temp
->length
] = '\0';
1578 "duplicate argument name `%s' in `#define'", name
);
1584 ++bp
; /* skip paren */
1585 SKIP_WHITE_SPACE (bp
);
1586 /* now everything from bp before limit is the definition. */
1587 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1588 defn
->rest_args
= rest_args
;
1590 /* Now set defn->args.argnames to the result of concatenating
1591 the argument names in reverse order
1592 with comma-space between them. */
1593 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1595 struct arglist
*temp
;
1597 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1598 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1600 if (temp
->next
!= 0) {
1601 defn
->args
.argnames
[i
++] = ',';
1602 defn
->args
.argnames
[i
++] = ' ';
1605 defn
->args
.argnames
[i
] = 0;
1608 /* Simple expansion or empty definition. */
1612 if (is_hor_space
[*bp
]) {
1614 SKIP_WHITE_SPACE (bp
);
1617 case '!': case '"': case '#': case '%': case '&': case '\'':
1618 case ')': case '*': case '+': case ',': case '-': case '.':
1619 case '/': case ':': case ';': case '<': case '=': case '>':
1620 case '?': case '[': case '\\': case ']': case '^': case '{':
1621 case '|': case '}': case '~':
1622 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1623 sym_length
, symname
);
1627 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1628 sym_length
, symname
);
1633 /* now everything from bp before limit is the definition. */
1634 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1635 defn
->args
.argnames
= (U_CHAR
*) "";
1641 /* OP is null if this is a predefinition */
1642 defn
->predefined
= predefinition
;
1644 mdef
.symnam
= symname
;
1645 mdef
.symlen
= sym_length
;
1654 /* Check a purported macro name SYMNAME, and yield its length.
1655 USAGE is the kind of name this is intended for. */
1658 check_macro_name (pfile
, symname
, usage
)
1666 for (p
= symname
; is_idchar
[*p
]; p
++)
1668 sym_length
= p
- symname
;
1669 if (sym_length
== 0)
1670 cpp_error (pfile
, "invalid %s name", usage
);
1671 else if (!is_idstart
[*symname
]) {
1672 U_CHAR
*msg
; /* what pain... */
1673 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1674 bcopy (symname
, msg
, sym_length
);
1675 msg
[sym_length
] = 0;
1676 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1678 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1679 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1684 /* Return zero if two DEFINITIONs are isomorphic. */
1687 compare_defs (d1
, d2
)
1688 DEFINITION
*d1
, *d2
;
1690 register struct reflist
*a1
, *a2
;
1691 register U_CHAR
*p1
= d1
->expansion
;
1692 register U_CHAR
*p2
= d2
->expansion
;
1695 if (d1
->nargs
!= d2
->nargs
)
1697 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1699 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1700 a1
= a1
->next
, a2
= a2
->next
) {
1701 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1702 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1703 || a1
->argno
!= a2
->argno
1704 || a1
->stringify
!= a2
->stringify
1705 || a1
->raw_before
!= a2
->raw_before
1706 || a1
->raw_after
!= a2
->raw_after
)
1714 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1715 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1720 /* Return 1 if two parts of two macro definitions are effectively different.
1721 One of the parts starts at BEG1 and has LEN1 chars;
1722 the other has LEN2 chars at BEG2.
1723 Any sequence of whitespace matches any other sequence of whitespace.
1724 FIRST means these parts are the first of a macro definition;
1725 so ignore leading whitespace entirely.
1726 LAST means these parts are the last of a macro definition;
1727 so ignore trailing whitespace entirely. */
1730 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1732 U_CHAR
*beg1
, *beg2
;
1736 register U_CHAR
*end1
= beg1
+ len1
;
1737 register U_CHAR
*end2
= beg2
+ len2
;
1739 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1740 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1743 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1744 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1746 while (beg1
!= end1
&& beg2
!= end2
) {
1747 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1748 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1749 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1750 } else if (*beg1
== *beg2
) {
1754 return (beg1
!= end1
) || (beg2
!= end2
);
1757 /* Process a #define command.
1758 BUF points to the contents of the #define command, as a contiguous string.
1759 LIMIT points to the first character past the end of the definition.
1760 KEYWORD is the keyword-table entry for #define,
1761 or NULL for a "predefined" macro. */
1764 do_define (pfile
, keyword
, buf
, limit
)
1766 struct directive
*keyword
;
1767 U_CHAR
*buf
, *limit
;
1774 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1775 if (pcp_outfile
&& keyword
)
1776 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1779 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1783 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1785 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1788 /* Redefining a precompiled key is ok. */
1789 if (hp
->type
== T_PCSTRING
)
1791 /* Redefining a macro is ok if the definitions are the same. */
1792 else if (hp
->type
== T_MACRO
)
1793 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
1794 /* Redefining a constant is ok with -D. */
1795 else if (hp
->type
== T_CONST
)
1796 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1797 /* Print the warning if it's not ok. */
1800 U_CHAR
*msg
; /* what pain... */
1802 /* If we are passing through #define and #undef directives, do
1803 that for this re-definition now. */
1804 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1805 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1807 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1809 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1810 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1811 cpp_pedwarn (pfile
, msg
);
1812 if (hp
->type
== T_MACRO
)
1813 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1814 "this is the location of the previous definition");
1816 /* Replace the old definition. */
1818 hp
->value
.defn
= mdef
.defn
;
1822 /* If we are passing through #define and #undef directives, do
1823 that for this new definition now. */
1824 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1825 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1826 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1827 (char *) mdef
.defn
, hashcode
);
1837 /* This structure represents one parsed argument in a macro call.
1838 `raw' points to the argument text as written (`raw_length' is its length).
1839 `expanded' points to the argument's macro-expansion
1840 (its length is `expand_length').
1841 `stringified_length' is the length the argument would have
1843 `use_count' is the number of times this macro arg is substituted
1844 into the macro. If the actual use count exceeds 10,
1845 the value stored is 10. */
1847 /* raw and expanded are relative to ARG_BASE */
1848 #define ARG_BASE ((pfile)->token_buffer)
1851 /* Strings relative to pfile->token_buffer */
1852 long raw
, expanded
, stringified
;
1853 int raw_length
, expand_length
;
1854 int stringified_length
;
1859 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1860 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1861 as the new input buffer.
1862 Return the new buffer, or NULL on failure. */
1865 cpp_push_buffer (pfile
, buffer
, length
)
1870 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1871 if (buf
== pfile
->buffer_stack
)
1873 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1878 bzero ((char *) buf
, sizeof (cpp_buffer
));
1879 CPP_BUFFER (pfile
) = buf
;
1880 buf
->if_stack
= pfile
->if_stack
;
1881 buf
->cleanup
= null_cleanup
;
1882 buf
->underflow
= null_underflow
;
1883 buf
->buf
= buf
->cur
= buffer
;
1884 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1890 cpp_pop_buffer (pfile
)
1893 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1894 (*buf
->cleanup
) (buf
, pfile
);
1895 return ++CPP_BUFFER (pfile
);
1898 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1899 Pop the buffer when done. */
1902 cpp_scan_buffer (pfile
)
1905 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1908 enum cpp_token token
= cpp_get_token (pfile
);
1909 if (token
== CPP_EOF
) /* Should not happen ... */
1911 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1913 cpp_pop_buffer (pfile
);
1920 * Rescan a string (which may have escape marks) into pfile's buffer.
1921 * Place the result in pfile->token_buffer.
1923 * The input is copied before it is scanned, so it is safe to pass
1924 * it something from the token_buffer that will get overwritten
1925 * (because it follows CPP_WRITTEN). This is used by do_include.
1929 cpp_expand_to_buffer (pfile
, buf
, length
)
1934 register cpp_buffer
*ip
;
1936 U_CHAR
*limit
= buf
+ length
;
1939 int odepth
= indepth
;
1945 /* Set up the input on the input stack. */
1947 buf1
= (U_CHAR
*) alloca (length
+ 1);
1949 register U_CHAR
*p1
= buf
;
1950 register U_CHAR
*p2
= buf1
;
1957 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1960 ip
->has_escapes
= 1;
1962 ip
->lineno
= obuf
.lineno
= 1;
1965 /* Scan the input, create the output. */
1966 cpp_scan_buffer (pfile
);
1969 if (indepth
!= odepth
)
1973 CPP_NUL_TERMINATE (pfile
);
1978 adjust_position (buf
, limit
, linep
, colp
)
1988 (*linep
)++, (*colp
) = 1;
1994 /* Move line_base forward, updating lineno and colno. */
1997 update_position (pbuf
)
1998 register cpp_buffer
*pbuf
;
2000 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
2001 unsigned char *new_pos
= pbuf
->cur
;
2002 register struct parse_marker
*mark
;
2003 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
2005 if (pbuf
->buf
+ mark
->position
< new_pos
)
2006 new_pos
= pbuf
->buf
+ mark
->position
;
2008 pbuf
->line_base
+= new_pos
- old_pos
;
2009 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
2013 cpp_buf_line_and_col (pbuf
, linep
, colp
)
2014 register cpp_buffer
*pbuf
;
2022 *linep
= pbuf
->lineno
;
2023 *colp
= pbuf
->colno
;
2024 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2033 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2036 cpp_file_buffer (pfile
)
2039 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2041 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2042 if (ip
->fname
!= NULL
)
2048 count_newlines (buf
, limit
)
2049 register U_CHAR
*buf
;
2050 register U_CHAR
*limit
;
2052 register long count
= 0;
2063 * write out a #line command, for instance, after an #include file.
2064 * If CONDITIONAL is nonzero, we can omit the #line if it would
2065 * appear to be a no-op, and we can output a few newlines instead
2066 * if we want to increase the line number by a small amount.
2067 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2071 output_line_command (pfile
, conditional
, file_change
)
2074 enum file_change_code file_change
;
2077 char *line_cmd_buf
, *line_end
;
2079 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2081 if (ip
->fname
== NULL
)
2084 update_position (ip
);
2086 if (CPP_OPTIONS (pfile
)->no_line_commands
2087 || CPP_OPTIONS (pfile
)->no_output
)
2090 line
= CPP_BUFFER (pfile
)->lineno
;
2091 col
= CPP_BUFFER (pfile
)->colno
;
2092 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2094 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2098 if (line
== pfile
->lineno
)
2101 /* If the inherited line number is a little too small,
2102 output some newlines instead of a #line command. */
2103 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2104 CPP_RESERVE (pfile
, 20);
2105 while (line
> pfile
->lineno
) {
2106 CPP_PUTC_Q (pfile
, '\n');
2114 /* Don't output a line number of 0 if we can help it. */
2115 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2116 && *ip
->bufp
== '\n') {
2122 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2124 #ifdef OUTPUT_LINE_COMMANDS
2125 static char sharp_line
[] = "#line ";
2127 static char sharp_line
[] = "# ";
2129 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2132 sprintf (CPP_PWRITTEN (pfile
), "%d ", line
);
2133 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2135 quote_string (pfile
, ip
->nominal_fname
);
2136 if (file_change
!= same_file
) {
2137 CPP_PUTC_Q (pfile
, ' ');
2138 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2140 /* Tell cc1 if following text comes from a system header file. */
2141 if (ip
->system_header_p
) {
2142 CPP_PUTC_Q (pfile
, ' ');
2143 CPP_PUTC_Q (pfile
, '3');
2145 #ifndef NO_IMPLICIT_EXTERN_C
2146 /* Tell cc1plus if following text should be treated as C. */
2147 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2148 CPP_PUTC_Q (pfile
, ' ');
2149 CPP_PUTC_Q (pfile
, '4');
2152 CPP_PUTC_Q (pfile
, '\n');
2153 pfile
->lineno
= line
;
2157 * Parse a macro argument and append the info on PFILE's token_buffer.
2158 * REST_ARGS means to absorb the rest of the args.
2159 * Return nonzero to indicate a syntax error.
2162 static enum cpp_token
2163 macarg (pfile
, rest_args
)
2168 enum cpp_token token
;
2169 long arg_start
= CPP_WRITTEN (pfile
);
2170 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2171 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2173 /* Try to parse as much of the argument as exists at this
2174 input stack level. */
2175 pfile
->no_macro_expand
++;
2178 token
= cpp_get_token (pfile
);
2184 /* If we've hit end of file, it's an error (reported by caller).
2185 Ditto if it's the end of cpp_expand_to_buffer text.
2186 If we've hit end of macro, just continue. */
2187 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2198 /* if we've returned to lowest level and
2199 we aren't absorbing all args */
2200 if (paren
== 0 && rest_args
== 0)
2204 /* Remove ',' or ')' from argument buffer. */
2205 CPP_ADJUST_WRITTEN (pfile
, -1);
2212 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2213 pfile
->no_macro_expand
--;
2218 /* Turn newlines to spaces in the string of length LENGTH at START,
2219 except inside of string constants.
2220 The string is copied into itself with its beginning staying fixed. */
2223 change_newlines (start
, length
)
2227 register U_CHAR
*ibp
;
2228 register U_CHAR
*obp
;
2229 register U_CHAR
*limit
;
2233 limit
= start
+ length
;
2236 while (ibp
< limit
) {
2237 *obp
++ = c
= *ibp
++;
2242 /* Notice and skip strings, so that we don't delete newlines in them. */
2245 while (ibp
< limit
) {
2246 *obp
++ = c
= *ibp
++;
2249 if (c
== '\n' && quotec
== '\'')
2265 if (!pfile
->timebuf
) {
2266 time_t t
= time ((time_t *) 0);
2267 pfile
->timebuf
= localtime (&t
);
2269 return pfile
->timebuf
;
2272 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2273 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2277 * expand things like __FILE__. Place the expansion into the output
2278 * buffer *without* rescanning.
2282 special_symbol (hp
, pfile
)
2289 cpp_buffer
*ip
= NULL
;
2292 int paren
= 0; /* For special `defined' keyword */
2295 if (pcp_outfile
&& pcp_inside_if
2296 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2298 "Predefined macro `%s' used inside `#if' during precompilation",
2302 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2304 if (ip
== CPP_NULL_BUFFER (pfile
))
2306 cpp_error (pfile
, "cccp error: not in any file?!");
2307 return; /* the show must go on */
2309 if (ip
->fname
!= NULL
)
2319 if (hp
->type
== T_BASE_FILE
)
2321 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2322 ip
= CPP_PREV_BUFFER (ip
);
2324 string
= ip
->nominal_fname
;
2328 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2329 quote_string (pfile
, string
);
2333 case T_INCLUDE_LEVEL
:
2335 ip
= CPP_BUFFER (pfile
);
2336 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2337 if (ip
->fname
!= NULL
)
2340 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2341 sprintf (buf
, "%d", true_indepth
- 1);
2345 buf
= (char *) alloca (3 + strlen (version_string
));
2346 sprintf (buf
, "\"%s\"", version_string
);
2349 #ifndef NO_BUILTIN_SIZE_TYPE
2355 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2356 case T_PTRDIFF_TYPE
:
2362 buf
= CPP_WCHAR_TYPE (pfile
);
2365 case T_USER_LABEL_PREFIX_TYPE
:
2366 buf
= USER_LABEL_PREFIX
;
2369 case T_REGISTER_PREFIX_TYPE
:
2370 buf
= REGISTER_PREFIX
;
2374 buf
= (char *) alloca (4 * sizeof (int));
2375 sprintf (buf
, "%d", hp
->value
.ival
);
2377 if (pcp_inside_if
&& pcp_outfile
)
2378 /* Output a precondition for this macro use */
2379 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2385 long line
= ip
->lineno
;
2386 long col
= ip
->colno
;
2387 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2389 buf
= (char *) alloca (10);
2390 sprintf (buf
, "%d", line
);
2396 buf
= (char *) alloca (20);
2397 timebuf
= timestamp (pfile
);
2398 if (hp
->type
== T_DATE
)
2399 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2400 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2402 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2406 case T_SPEC_DEFINED
:
2407 buf
= " 0 "; /* Assume symbol is not defined */
2408 ip
= CPP_BUFFER (pfile
);
2409 SKIP_WHITE_SPACE (ip
->cur
);
2410 if (*ip
->cur
== '(')
2413 ip
->cur
++; /* Skip over the paren */
2414 SKIP_WHITE_SPACE (ip
->cur
);
2417 if (!is_idstart
[*ip
->cur
])
2419 if (hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1))
2422 if (pcp_outfile
&& pcp_inside_if
2423 && (hp
->type
== T_CONST
2424 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2425 /* Output a precondition for this macro use. */
2426 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2432 if (pcp_outfile
&& pcp_inside_if
)
2434 /* Output a precondition for this macro use */
2435 U_CHAR
*cp
= ip
->bufp
;
2436 fprintf (pcp_outfile
, "#undef ");
2437 while (is_idchar
[*cp
]) /* Ick! */
2438 fputc (*cp
++, pcp_outfile
);
2439 putc ('\n', pcp_outfile
);
2442 while (is_idchar
[*ip
->cur
])
2444 SKIP_WHITE_SPACE (ip
->cur
);
2447 if (*ip
->cur
!= ')')
2455 cpp_error (pfile
, "`defined' without an identifier");
2459 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2463 CPP_RESERVE (pfile
, len
+ 1);
2464 CPP_PUTS_Q (pfile
, buf
, len
);
2465 CPP_NUL_TERMINATE_Q (pfile
);
2470 /* Write out a #define command for the special named MACRO_NAME
2471 to PFILE's token_buffer. */
2474 dump_special_to_buffer (pfile
, macro_name
)
2478 static char define_directive
[] = "#define ";
2479 int macro_name_length
= strlen (macro_name
);
2480 output_line_command (pfile
, 0, same_file
);
2481 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2482 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2483 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2484 CPP_PUTC_Q (pfile
, ' ');
2485 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2486 CPP_PUTC (pfile
, '\n');
2489 /* Initialize the built-in macros. */
2492 initialize_builtins (pfile
)
2495 install ("__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2496 install ("__DATE__", -1, T_DATE
, 0, 0, -1);
2497 install ("__FILE__", -1, T_FILE
, 0, 0, -1);
2498 install ("__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2499 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2500 install ("__VERSION__", -1, T_VERSION
, 0, 0, -1);
2501 #ifndef NO_BUILTIN_SIZE_TYPE
2502 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2504 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2505 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2507 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2508 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2509 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2510 install ("__TIME__", -1, T_TIME
, 0, 0, -1);
2511 if (!CPP_TRADITIONAL (pfile
))
2512 install ("__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2513 if (CPP_OPTIONS (pfile
)->objc
)
2514 install ("__OBJC__", -1, T_CONST
, 1, 0, -1);
2515 /* This is supplied using a -D by the compiler driver
2516 so that it is present only when truly compiling with GNU C. */
2517 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2519 if (CPP_OPTIONS (pfile
)->debug_output
)
2521 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2522 dump_special_to_buffer (pfile
, "__VERSION__");
2523 #ifndef NO_BUILTIN_SIZE_TYPE
2524 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2526 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2527 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2529 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2530 dump_special_to_buffer (pfile
, "__DATE__");
2531 dump_special_to_buffer (pfile
, "__TIME__");
2532 if (!CPP_TRADITIONAL (pfile
))
2533 dump_special_to_buffer (pfile
, "__STDC__");
2534 if (CPP_OPTIONS (pfile
)->objc
)
2535 dump_special_to_buffer (pfile
, "__OBJC__");
2539 /* Return 1 iff a token ending in C1 followed directly by a token C2
2540 could cause mis-tokenization. */
2543 unsafe_chars (c1
, c2
)
2549 if (c2
== c1
|| c2
== '=')
2553 case '0': case '1': case '2': case '3': case '4':
2554 case '5': case '6': case '7': case '8': case '9':
2556 if (c2
== '-' || c2
== '+')
2557 return 1; /* could extend a pre-processing number */
2560 if (c2
== '\'' || c2
== '\"')
2561 return 1; /* Could turn into L"xxx" or L'xxx'. */
2565 case 'a': case 'b': case 'c': case 'd': case 'f':
2566 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2567 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2568 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2570 case 'A': case 'B': case 'C': case 'D': case 'F':
2571 case 'G': case 'H': case 'I': case 'J': case 'K':
2572 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2573 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2575 /* We're in the middle of either a name or a pre-processing number. */
2576 return (is_idchar
[c2
] || c2
== '.');
2577 case '<': case '>': case '!': case '%': case '#': case ':':
2578 case '^': case '&': case '|': case '*': case '/': case '=':
2579 return (c2
== c1
|| c2
== '=');
2584 /* Expand a macro call.
2585 HP points to the symbol that is the macro being called.
2586 Put the result of expansion onto the input stack
2587 so that subsequent input by our caller will use it.
2589 If macro wants arguments, caller has already verified that
2590 an argument list follows; arguments come from the input stack. */
2593 macroexpand (pfile
, hp
)
2598 DEFINITION
*defn
= hp
->value
.defn
;
2599 register U_CHAR
*xbuf
;
2600 long start_line
, start_column
;
2602 struct argdata
*args
;
2603 long old_written
= CPP_WRITTEN (pfile
);
2605 int start_line
= instack
[indepth
].lineno
;
2607 int rest_args
, rest_zero
;
2611 CHECK_DEPTH (return;);
2615 /* This macro is being used inside a #if, which means it must be */
2616 /* recorded as a precondition. */
2617 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2618 dump_single_macro (hp
, pcp_outfile
);
2621 pfile
->output_escapes
++;
2622 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2624 nargs
= defn
->nargs
;
2628 enum cpp_token token
;
2630 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2632 for (i
= 0; i
< nargs
; i
++)
2634 args
[i
].raw
= args
[i
].expanded
= 0;
2635 args
[i
].raw_length
= 0;
2636 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2637 args
[i
].use_count
= 0;
2640 /* Parse all the macro args that are supplied. I counts them.
2641 The first NARGS args are stored in ARGS.
2642 The rest are discarded. If rest_args is set then we assume
2643 macarg absorbed the rest of the args. */
2647 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2652 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2654 /* if we are working on last arg which absorbs rest of args... */
2655 if (i
== nargs
- 1 && defn
->rest_args
)
2657 args
[i
].raw
= CPP_WRITTEN (pfile
);
2658 token
= macarg (pfile
, rest_args
);
2659 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2660 args
[i
].newlines
= 0; /* FIXME */
2663 token
= macarg (pfile
, 0);
2664 if (token
== CPP_EOF
|| token
== CPP_POP
)
2666 cpp_error_with_line (pfile
, start_line
, start_column
,
2667 "unterminated macro call");
2671 } while (token
== CPP_COMMA
);
2673 /* If we got one arg but it was just whitespace, call that 0 args. */
2676 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2677 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2678 /* cpp.texi says for foo ( ) we provide one argument.
2679 However, if foo wants just 0 arguments, treat this as 0. */
2681 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2686 /* Don't output an error message if we have already output one for
2687 a parse error above. */
2689 if (nargs
== 0 && i
> 0)
2691 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2695 /* traditional C allows foo() if foo wants one argument. */
2696 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2698 /* the rest args token is allowed to absorb 0 tokens */
2699 else if (i
== nargs
- 1 && defn
->rest_args
)
2702 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2704 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2706 cpp_error (pfile
, "macro `%s' used with only %d args",
2712 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2716 /* If macro wants zero args, we parsed the arglist for checking only.
2717 Read directly from the macro definition. */
2720 xbuf
= defn
->expansion
;
2721 xbuf_len
= defn
->length
;
2725 register U_CHAR
*exp
= defn
->expansion
;
2726 register int offset
; /* offset in expansion,
2727 copied a piece at a time */
2728 register int totlen
; /* total amount of exp buffer filled so far */
2730 register struct reflist
*ap
, *last_ap
;
2732 /* Macro really takes args. Compute the expansion of this call. */
2734 /* Compute length in characters of the macro's expansion.
2735 Also count number of times each arg is used. */
2736 xbuf_len
= defn
->length
;
2737 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2741 register struct argdata
*arg
= &args
[ap
->argno
];
2742 /* Stringify it it hasn't already been */
2743 if (arg
->stringified_length
< 0)
2745 int arglen
= arg
->raw_length
;
2749 /* Initially need_space is -1. Otherwise, 1 means the
2750 previous character was a space, but we suppressed it;
2751 0 means the previous character was a non-space. */
2752 int need_space
= -1;
2754 arg
->stringified
= CPP_WRITTEN (pfile
);
2755 if (!CPP_TRADITIONAL (pfile
))
2756 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2757 for (; i
< arglen
; i
++)
2759 c
= (ARG_BASE
+ arg
->raw
)[i
];
2763 /* Internal sequences of whitespace are replaced by
2764 one space except within an string or char token.*/
2767 if (CPP_WRITTEN (pfile
) > arg
->stringified
2768 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2770 /* "@ " escape markers are removed */
2771 CPP_ADJUST_WRITTEN (pfile
, -1);
2774 if (need_space
== 0)
2778 else if (need_space
> 0)
2779 CPP_PUTC (pfile
, ' ');
2794 else if (c
== '\"' || c
== '\'')
2798 /* Escape these chars */
2799 if (c
== '\"' || (in_string
&& c
== '\\'))
2800 CPP_PUTC (pfile
, '\\');
2802 CPP_PUTC (pfile
, c
);
2805 CPP_RESERVE (pfile
, 4);
2806 sprintf (CPP_PWRITTEN (pfile
), "\\%03o",
2808 CPP_ADJUST_WRITTEN (pfile
, 4);
2811 if (!CPP_TRADITIONAL (pfile
))
2812 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2813 arg
->stringified_length
2814 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2816 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2818 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2819 /* Add 4 for two newline-space markers to prevent
2820 token concatenation. */
2821 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2824 /* We have an ordinary (expanded) occurrence of the arg.
2825 So compute its expansion, if we have not already. */
2826 if (args
[ap
->argno
].expand_length
< 0)
2828 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2829 cpp_expand_to_buffer (pfile
,
2830 ARG_BASE
+ args
[ap
->argno
].raw
,
2831 args
[ap
->argno
].raw_length
);
2833 args
[ap
->argno
].expand_length
2834 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2837 /* Add 4 for two newline-space markers to prevent
2838 token concatenation. */
2839 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2841 if (args
[ap
->argno
].use_count
< 10)
2842 args
[ap
->argno
].use_count
++;
2845 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2847 /* Generate in XBUF the complete expansion
2848 with arguments substituted in.
2849 TOTLEN is the total size generated so far.
2850 OFFSET is the index in the definition
2851 of where we are copying from. */
2852 offset
= totlen
= 0;
2853 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2854 last_ap
= ap
, ap
= ap
->next
)
2856 register struct argdata
*arg
= &args
[ap
->argno
];
2857 int count_before
= totlen
;
2859 /* Add chars to XBUF. */
2860 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2861 xbuf
[totlen
++] = exp
[offset
];
2863 /* If followed by an empty rest arg with concatenation,
2864 delete the last run of nonwhite chars. */
2865 if (rest_zero
&& totlen
> count_before
2866 && ((ap
->rest_args
&& ap
->raw_before
)
2867 || (last_ap
!= NULL
&& last_ap
->rest_args
2868 && last_ap
->raw_after
)))
2870 /* Delete final whitespace. */
2871 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2874 /* Delete the nonwhites before them. */
2875 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2879 if (ap
->stringify
!= 0)
2881 bcopy (ARG_BASE
+ arg
->stringified
,
2882 xbuf
+ totlen
, arg
->stringified_length
);
2883 totlen
+= arg
->stringified_length
;
2885 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2887 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2888 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2891 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2892 while (p1
!= l1
&& is_idchar
[*p1
])
2893 xbuf
[totlen
++] = *p1
++;
2897 /* Arg is concatenated after: delete trailing whitespace,
2898 whitespace markers, and no-reexpansion markers. */
2901 if (is_space
[l1
[-1]]) l1
--;
2902 else if (l1
[-1] == '-')
2904 U_CHAR
*p2
= l1
- 1;
2905 /* If a `-' is preceded by an odd number of newlines then it
2906 and the last newline are a no-reexpansion marker. */
2907 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2908 if ((l1
- 1 - p2
) & 1) {
2917 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2922 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2923 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2924 && !CPP_TRADITIONAL(pfile
)
2925 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2927 xbuf
[totlen
++] = '@';
2928 xbuf
[totlen
++] = ' ';
2931 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2932 totlen
+= arg
->expand_length
;
2934 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2935 && !CPP_TRADITIONAL(pfile
)
2936 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2938 xbuf
[totlen
++] = '@';
2939 xbuf
[totlen
++] = ' ';
2942 /* If a macro argument with newlines is used multiple times,
2943 then only expand the newlines once. This avoids creating
2944 output lines which don't correspond to any input line,
2945 which confuses gdb and gcov. */
2946 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2948 /* Don't bother doing change_newlines for subsequent
2952 = change_newlines (expanded
, arg
->expand_length
);
2956 if (totlen
> xbuf_len
)
2960 /* if there is anything left of the definition
2961 after handling the arg list, copy that in too. */
2963 for (i
= offset
; i
< defn
->length
; i
++)
2965 /* if we've reached the end of the macro */
2968 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2969 && last_ap
->raw_after
))
2970 xbuf
[totlen
++] = exp
[i
];
2978 pfile
->output_escapes
--;
2980 /* Now put the expansion on the input stack
2981 so our caller will commence reading from it. */
2982 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2983 CPP_BUFFER (pfile
)->has_escapes
= 1;
2985 /* Pop the space we've used in the token_buffer for argument expansion. */
2986 CPP_SET_WRITTEN (pfile
, old_written
);
2988 /* Recursive macro use sometimes works traditionally.
2989 #define foo(x,y) bar (x (y,0), y)
2992 if (!CPP_TRADITIONAL (pfile
))
2993 hp
->type
= T_DISABLED
;
2997 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
2999 register U_CHAR
*xbuf
;
3003 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
3006 mbuf
->cleanup
= macro_cleanup
;
3009 /* The first chars of the expansion should be a "@ " added by
3010 collect_expansion. This is to prevent accidental token-pasting
3011 between the text preceding the macro invocation, and the macro
3014 We would like to avoid adding unneeded spaces (for the sake of
3015 tools that use cpp, such as imake). In some common cases we can
3016 tell that it is safe to omit the space.
3018 The character before the macro invocation cannot have been an
3019 idchar (or else it would have been pasted with the idchars of
3020 the macro name). Therefore, if the first non-space character
3021 of the expansion is an idchar, we do not need the extra space
3022 to prevent token pasting.
3024 Also, we don't need the extra space if the first char is '(',
3025 or some other (less common) characters. */
3027 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3028 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3029 || xbuf
[2] == '\"'))
3033 /* Like cpp_get_token, except that it does not read past end-of-line.
3034 Also, horizontal space is skipped, and macros are popped. */
3036 static enum cpp_token
3037 get_directive_token (pfile
)
3042 long old_written
= CPP_WRITTEN (pfile
);
3043 enum cpp_token token
;
3044 cpp_skip_hspace (pfile
);
3045 if (PEEKC () == '\n')
3047 token
= cpp_get_token (pfile
);
3051 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3053 /* ... else fall though ... */
3054 case CPP_HSPACE
: case CPP_COMMENT
:
3055 CPP_SET_WRITTEN (pfile
, old_written
);
3063 /* Handle #include and #import.
3064 This function expects to see "fname" or <fname> on the input.
3066 The input is normally in part of the output_buffer following
3067 CPP_WRITTEN, and will get overwritten by output_line_command.
3068 I.e. in input file specification has been popped by handle_directive.
3072 do_include (pfile
, keyword
, unused1
, unused2
)
3074 struct directive
*keyword
;
3075 U_CHAR
*unused1
, *unused2
;
3077 int importing
= (keyword
->type
== T_IMPORT
);
3078 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3079 char *fname
; /* Dynamically allocated fname buffer */
3082 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3083 enum cpp_token token
;
3085 /* Chain of dirs to search */
3086 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3087 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3088 struct file_name_list
*searchptr
= 0;
3089 long old_written
= CPP_WRITTEN (pfile
);
3093 int f
; /* file number */
3095 int retried
= 0; /* Have already tried macro
3096 expanding the include line */
3097 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3102 f
= -1; /* JF we iz paranoid! */
3104 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3105 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3106 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3108 pfile
->import_warning
= 1;
3109 cpp_warning (pfile
, "using `#import' is not recommended");
3110 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3111 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3112 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3113 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3114 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3115 fprintf (stderr
, " ... <real contents of file> ...\n");
3116 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3117 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3118 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3119 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3122 pfile
->parsing_include_directive
++;
3123 token
= get_directive_token (pfile
);
3124 pfile
->parsing_include_directive
--;
3126 if (token
== CPP_STRING
)
3128 /* FIXME - check no trailing garbage */
3129 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3130 fend
= CPP_PWRITTEN (pfile
) - 1;
3131 if (fbeg
[-1] == '<')
3134 /* If -I-, start with the first -I dir after the -I-. */
3135 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3136 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3138 /* If -I- was specified, don't search current dir, only spec'd ones. */
3139 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3141 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3142 /* We have "filename". Figure out directory this source
3143 file is coming from and put it on the front of the list. */
3145 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3150 if ((nam
= fp
->nominal_fname
) != NULL
)
3152 /* Found a named file. Figure out dir of the file,
3153 and put it in front of the search list. */
3154 dsp
[0].next
= search_start
;
3157 ep
= rindex (nam
, '/');
3159 ep
= rindex (nam
, ']');
3160 if (ep
== NULL
) ep
= rindex (nam
, '>');
3161 if (ep
== NULL
) ep
= rindex (nam
, ':');
3162 if (ep
!= NULL
) ep
++;
3167 dsp
[0].fname
= (char *) alloca (n
+ 1);
3168 strncpy (dsp
[0].fname
, nam
, n
);
3169 dsp
[0].fname
[n
] = '\0';
3170 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3171 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3175 dsp
[0].fname
= 0; /* Current directory */
3177 dsp
[0].got_name_map
= 0;
3184 else if (token
== CPP_NAME
)
3187 * Support '#include xyz' like VAX-C to allow for easy use of all the
3188 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3189 * code from case '<' is repeated here) and generates a warning.
3192 "VAX-C-style include specification found, use '#include <filename.h>' !");
3194 /* If -I-, start with the first -I dir after the -I-. */
3195 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3196 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3197 fbeg
= pfile
->token_buffer
+ old_written
;
3198 fend
= CPP_PWRITTEN (pfile
);
3204 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3205 CPP_SET_WRITTEN (pfile
, old_written
);
3206 skip_rest_of_line (pfile
);
3212 token
= get_directive_token (pfile
);
3213 if (token
!= CPP_VSPACE
)
3215 cpp_error (pfile
, "junk at end of `#include'");
3216 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3217 token
= get_directive_token (pfile
);
3220 /* For #include_next, skip in the search path
3221 past the dir in which the containing file was found. */
3224 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3225 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3226 if (fp
->fname
!= NULL
)
3228 /* fp->dir is null if the containing file was specified with
3229 an absolute file name. In that case, don't skip anything. */
3230 if (fp
->dir
== SELF_DIR_DUMMY
)
3231 search_start
= CPP_OPTIONS (pfile
)->include
;
3233 search_start
= fp
->dir
->next
;
3238 CPP_SET_WRITTEN (pfile
, old_written
);
3244 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3248 /* Allocate this permanently, because it gets stored in the definitions
3250 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3251 /* + 2 above for slash and terminating null. */
3252 /* + 2 added for '.h' on VMS (to support '#include filename') */
3254 /* If specified file name is absolute, just open it. */
3257 strncpy (fname
, fbeg
, flen
);
3259 if (redundant_include_p (pfile
, fname
))
3262 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3264 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3266 return 0; /* Already included this file */
3268 /* Search directory path, trying to open the file.
3269 Copy each filename tried into FNAME. */
3271 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3272 if (searchptr
->fname
) {
3273 /* The empty string in a search path is ignored.
3274 This makes it possible to turn off entirely
3275 a standard piece of the list. */
3276 if (searchptr
->fname
[0] == 0)
3278 strcpy (fname
, searchptr
->fname
);
3279 strcat (fname
, "/");
3280 fname
[strlen (fname
) + flen
] = 0;
3284 strncat (fname
, fbeg
, flen
);
3286 /* Change this 1/2 Unix 1/2 VMS file specification into a
3287 full VMS file specification */
3288 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3289 /* Fix up the filename */
3290 hack_vms_include_specification (fname
);
3292 /* This is a normal VMS filespec, so use it unchanged. */
3293 strncpy (fname
, fbeg
, flen
);
3295 /* if it's '#include filename', add the missing .h */
3296 if (index(fname
,'.')==NULL
) {
3297 strcat (fname
, ".h");
3301 /* ??? There are currently 3 separate mechanisms for avoiding processing
3302 of redundant include files: #import, #pragma once, and
3303 redundant_include_p. It would be nice if they were unified. */
3304 if (redundant_include_p (pfile
, fname
))
3307 f
= lookup_import (pfile
, fname
, searchptr
);
3309 f
= open_include_file (pfile
, fname
, searchptr
);
3311 return 0; /* Already included this file */
3313 else if (f
== -1 && errno
== EACCES
)
3314 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3324 /* A file that was not found. */
3325 strncpy (fname
, fbeg
, flen
);
3327 /* If generating dependencies and -MG was specified, we assume missing
3328 files are leaf files, living in the same directory as the source file
3329 or other similar place; these missing files may be generated from
3330 other files and may not exist yet (eg: y.tab.h). */
3332 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3333 && CPP_PRINT_DEPS (pfile
)
3334 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3336 /* If it was requested as a system header file,
3337 then assume it belongs in the first place to look for such. */
3340 for (searchptr
= search_start
; searchptr
;
3341 searchptr
= searchptr
->next
)
3343 if (searchptr
->fname
)
3347 if (searchptr
->fname
[0] == 0)
3349 p
= (char *) alloca (strlen (searchptr
->fname
)
3350 + strlen (fname
) + 2);
3351 strcpy (p
, searchptr
->fname
);
3354 deps_output (pfile
, p
, ' ');
3361 /* Otherwise, omit the directory, as if the file existed
3362 in the directory with the source. */
3363 deps_output (pfile
, fname
, ' ');
3366 /* If -M was specified, and this header file won't be added to the
3367 dependency list, then don't count this as an error, because we can
3368 still produce correct output. Otherwise, we can't produce correct
3369 output, because there may be dependencies we need inside the missing
3370 file, and we don't know what directory this missing file exists in.*/
3371 else if (CPP_PRINT_DEPS (pfile
)
3372 && (CPP_PRINT_DEPS (pfile
)
3373 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3374 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3375 else if (search_start
)
3376 cpp_error_from_errno (pfile
, fname
);
3378 cpp_error (pfile
, "No include path in which to find %s", fname
);
3381 /* Check to see if this include file is a once-only include file.
3384 struct file_name_list
*ptr
;
3386 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3387 if (!strcmp (ptr
->fname
, fname
)) {
3389 return 0; /* This file was once'd. */
3393 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3394 if (!strcmp (ptr
->fname
, fname
))
3395 break; /* This file was included before. */
3399 /* This is the first time for this file. */
3400 /* Add it to list of files included. */
3402 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3403 ptr
->control_macro
= 0;
3404 ptr
->c_system_include_path
= 0;
3405 ptr
->next
= pfile
->all_include_files
;
3406 pfile
->all_include_files
= ptr
;
3407 ptr
->fname
= savestring (fname
);
3408 ptr
->got_name_map
= 0;
3410 /* For -M, add this file to the dependencies. */
3411 if (CPP_PRINT_DEPS (pfile
)
3412 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3413 deps_output (pfile
, fname
, ' ');
3416 /* Handle -H option. */
3417 if (CPP_OPTIONS(pfile
)->print_include_names
)
3419 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3420 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3422 fprintf (stderr
, "%s\n", fname
);
3426 pfile
->system_include_depth
++;
3428 /* Actually process the file. */
3430 /* Record file on "seen" list for #import. */
3431 add_import (pfile
, f
, fname
);
3433 pcftry
= (char *) alloca (strlen (fname
) + 30);
3445 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3447 pcf
= open (pcftry
, O_RDONLY
, 0666);
3453 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3455 || stat_f
.st_dev
!= s
.st_dev
)
3457 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3458 /* Don't need it any more. */
3463 /* Don't need it at all. */
3468 } while (pcf
!= -1 && !pcfbuf
);
3472 /* Actually process the file */
3473 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3475 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3476 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3478 output_line_command (pfile
, 0, enter_file
);
3479 pfile
->only_seen_white
= 2;
3483 pfile
->system_include_depth
--;
3488 /* Return nonzero if there is no need to include file NAME
3489 because it has already been included and it contains a conditional
3490 to make a repeated include do nothing. */
3493 redundant_include_p (pfile
, name
)
3497 struct file_name_list
*l
= pfile
->all_include_files
;
3498 for (; l
; l
= l
->next
)
3499 if (! strcmp (name
, l
->fname
)
3501 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3506 /* Return nonzero if the given FILENAME is an absolute pathname which
3507 designates a file within one of the known "system" include file
3508 directories. We assume here that if the given FILENAME looks like
3509 it is the name of a file which resides either directly in a "system"
3510 include file directory, or within any subdirectory thereof, then the
3511 given file must be a "system" include file. This function tells us
3512 if we should suppress pedantic errors/warnings for the given FILENAME.
3514 The value is 2 if the file is a C-language system header file
3515 for which C++ should (on most systems) assume `extern "C"'. */
3518 is_system_include (pfile
, filename
)
3520 register char *filename
;
3522 struct file_name_list
*searchptr
;
3524 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3525 searchptr
= searchptr
->next
)
3526 if (searchptr
->fname
) {
3527 register char *sys_dir
= searchptr
->fname
;
3528 register unsigned length
= strlen (sys_dir
);
3530 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3532 if (searchptr
->c_system_include_path
)
3543 * Install a name in the assertion hash table.
3545 * If LEN is >= 0, it is the length of the name.
3546 * Otherwise, compute the length by scanning the entire name.
3548 * If HASH is >= 0, it is the precomputed hash code.
3549 * Otherwise, compute the hash code.
3552 static ASSERTION_HASHNODE
*
3553 assertion_install (pfile
, name
, len
, hash
)
3559 register ASSERTION_HASHNODE
*hp
;
3560 register int i
, bucket
;
3561 register U_CHAR
*p
, *q
;
3563 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3564 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3566 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3567 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3568 pfile
->assertion_hashtab
[bucket
] = hp
;
3570 if (hp
->next
!= NULL
)
3571 hp
->next
->prev
= hp
;
3574 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3577 for (i
= 0; i
< len
; i
++)
3583 * find the most recent hash node for name name (ending with first
3584 * non-identifier char) installed by install
3586 * If LEN is >= 0, it is the length of the name.
3587 * Otherwise, compute the length by scanning the entire name.
3589 * If HASH is >= 0, it is the precomputed hash code.
3590 * Otherwise, compute the hash code.
3593 static ASSERTION_HASHNODE
*
3594 assertion_lookup (pfile
, name
, len
, hash
)
3600 register ASSERTION_HASHNODE
*bucket
;
3602 bucket
= pfile
->assertion_hashtab
[hash
];
3604 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3606 bucket
= bucket
->next
;
3612 delete_assertion (hp
)
3613 ASSERTION_HASHNODE
*hp
;
3615 struct tokenlist_list
*tail
;
3616 if (hp
->prev
!= NULL
)
3617 hp
->prev
->next
= hp
->next
;
3618 if (hp
->next
!= NULL
)
3619 hp
->next
->prev
= hp
->prev
;
3621 for (tail
= hp
->value
; tail
; )
3623 struct tokenlist_list
*next
= tail
->next
;
3624 free_token_list (tail
->tokens
);
3629 /* Make sure that the bucket chain header that
3630 the deleted guy was on points to the right thing afterwards. */
3631 if (hp
== *hp
->bucket_hdr
)
3632 *hp
->bucket_hdr
= hp
->next
;
3637 /* Convert a character string literal into a nul-terminated string.
3638 The input string is [IN ... LIMIT).
3639 The result is placed in RESULT. RESULT can be the same as IN.
3640 The value returned in the end of the string written to RESULT,
3641 or NULL on error. */
3644 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3646 register U_CHAR
*result
, *in
, *limit
;
3666 char *bpc
= (char *) in
;
3667 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3668 in
= (U_CHAR
*) bpc
;
3670 *result
++ = (U_CHAR
)c
;
3673 /* else fall through */
3683 * interpret #line command. Remembers previously seen fnames
3684 * in its very own hash table.
3686 #define FNAME_HASHSIZE 37
3689 do_line (pfile
, keyword
)
3691 struct directive
*keyword
;
3693 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3695 long old_written
= CPP_WRITTEN (pfile
);
3696 enum file_change_code file_change
= same_file
;
3697 enum cpp_token token
;
3700 token
= get_directive_token (pfile
);
3702 if (token
!= CPP_NUMBER
3703 || !isdigit(pfile
->token_buffer
[old_written
]))
3705 cpp_error (pfile
, "invalid format `#line' command");
3706 goto bad_line_directive
;
3709 /* The Newline at the end of this line remains to be processed.
3710 To put the next line at the specified line number,
3711 we must store a line number now that is one less. */
3712 new_lineno
= atoi (pfile
->token_buffer
+ old_written
) - 1;
3713 CPP_SET_WRITTEN (pfile
, old_written
);
3715 /* NEW_LINENO is one less than the actual line number here. */
3716 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3717 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3719 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3720 if (PEEKC() && !is_space
[PEEKC()]) {
3721 cpp_error (pfile
, "invalid format `#line' command");
3722 goto bad_line_directive
;
3726 token
= get_directive_token (pfile
);
3728 if (token
== CPP_STRING
) {
3729 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3731 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3732 HASHNODE
*hp
, **hash_bucket
;
3737 /* Turn the file name, which is a character string literal,
3738 into a null-terminated string. Do this in place. */
3739 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3740 if (end_name
== NULL
)
3742 cpp_error (pfile
, "invalid format `#line' command");
3743 goto bad_line_directive
;
3746 fname_length
= end_name
- fname
;
3748 num_start
= CPP_WRITTEN (pfile
);
3749 token
= get_directive_token (pfile
);
3750 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3751 p
= pfile
->token_buffer
+ num_start
;
3752 if (CPP_PEDANTIC (pfile
))
3753 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3755 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3757 cpp_error (pfile
, "invalid format `#line' command");
3758 goto bad_line_directive
;
3761 file_change
= enter_file
;
3763 file_change
= leave_file
;
3765 ip
->system_header_p
= 1;
3766 else /* if (*p == 4) */
3767 ip
->system_header_p
= 2;
3769 CPP_SET_WRITTEN (pfile
, num_start
);
3770 token
= get_directive_token (pfile
);
3771 p
= pfile
->token_buffer
+ num_start
;
3772 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3773 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3774 token
= get_directive_token (pfile
);
3776 if (token
!= CPP_VSPACE
) {
3777 cpp_error (pfile
, "invalid format `#line' command");
3778 goto bad_line_directive
;
3783 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3784 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3785 if (hp
->length
== fname_length
&&
3786 strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3787 ip
->nominal_fname
= hp
->value
.cpval
;
3791 /* Didn't find it; cons up a new one. */
3792 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3793 hp
->next
= *hash_bucket
;
3796 hp
->length
= fname_length
;
3797 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3798 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3801 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3802 cpp_error (pfile
, "invalid format `#line' command");
3803 goto bad_line_directive
;
3806 ip
->lineno
= new_lineno
;
3808 skip_rest_of_line (pfile
);
3809 CPP_SET_WRITTEN (pfile
, old_written
);
3810 output_line_command (pfile
, 0, file_change
);
3815 * remove the definition of a symbol from the symbol table.
3816 * according to un*x /lib/cpp, it is not an error to undef
3817 * something that has no definitions, so it isn't one here either.
3821 do_undef (pfile
, keyword
, buf
, limit
)
3823 struct directive
*keyword
;
3824 U_CHAR
*buf
, *limit
;
3828 U_CHAR
*orig_buf
= buf
;
3831 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3832 if (pcp_outfile
&& keyword
)
3833 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3836 SKIP_WHITE_SPACE (buf
);
3837 sym_length
= check_macro_name (pfile
, buf
, "macro");
3839 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3841 /* If we are generating additional info for debugging (with -g) we
3842 need to pass through all effective #undef commands. */
3843 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3844 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3845 if (hp
->type
!= T_MACRO
)
3846 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3850 if (CPP_PEDANTIC (pfile
)) {
3852 SKIP_WHITE_SPACE (buf
);
3854 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3860 * Report an error detected by the program we are processing.
3861 * Use the text of the line in the error message.
3862 * (We use error because it prints the filename & line#.)
3866 do_error (pfile
, keyword
, buf
, limit
)
3868 struct directive
*keyword
;
3869 U_CHAR
*buf
, *limit
;
3871 int length
= limit
- buf
;
3872 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
3873 bcopy (buf
, copy
, length
);
3875 SKIP_WHITE_SPACE (copy
);
3876 cpp_error (pfile
, "#error %s", copy
);
3881 * Report a warning detected by the program we are processing.
3882 * Use the text of the line in the warning message, then continue.
3883 * (We use error because it prints the filename & line#.)
3887 do_warning (pfile
, keyword
, buf
, limit
)
3889 struct directive
*keyword
;
3890 U_CHAR
*buf
, *limit
;
3892 int length
= limit
- buf
;
3893 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
3894 bcopy (buf
, copy
, length
);
3896 SKIP_WHITE_SPACE (copy
);
3897 cpp_warning (pfile
, "#warning %s", copy
);
3901 /* Remember the name of the current file being read from so that we can
3902 avoid ever including it again. */
3908 cpp_buffer
*ip
= NULL
;
3909 struct file_name_list
*new;
3911 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3913 if (ip
== CPP_NULL_BUFFER (pfile
))
3915 if (ip
->fname
!= NULL
)
3920 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3921 new->next
= pfile
->dont_repeat_files
;
3922 pfile
->dont_repeat_files
= new;
3923 new->fname
= savestring (ip
->fname
);
3924 new->control_macro
= 0;
3925 new->got_name_map
= 0;
3926 new->c_system_include_path
= 0;
3931 /* #ident has already been copied to the output file, so just ignore it. */
3934 do_ident (pfile
, keyword
, buf
, limit
)
3936 struct directive
*keyword
;
3937 U_CHAR
*buf
, *limit
;
3939 /* long old_written = CPP_WRITTEN (pfile);*/
3942 /* Allow #ident in system headers, since that's not user's fault. */
3943 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3944 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3946 /* Leave rest of line to be read by later calls to cpp_get_token. */
3951 /* #pragma and its argument line have already been copied to the output file.
3952 Just check for some recognized pragmas that need validation here. */
3955 do_pragma (pfile
, keyword
, buf
, limit
)
3957 struct directive
*keyword
;
3958 U_CHAR
*buf
, *limit
;
3960 while (*buf
== ' ' || *buf
== '\t')
3962 if (!strncmp (buf
, "once", 4)) {
3963 /* Allow #pragma once in system headers, since that's not the user's
3965 if (!CPP_BUFFER (pfile
)->system_header_p
)
3966 cpp_warning (pfile
, "`#pragma once' is obsolete");
3970 if (!strncmp (buf
, "implementation", 14)) {
3971 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3972 been included yet. */
3973 struct file_name_list
*ptr
;
3974 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
3976 SKIP_WHITE_SPACE (p
);
3977 if (*p
== '\n' || *p
!= '\"')
3981 p
= (U_CHAR
*) index (fname
, '\"');
3982 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
3984 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3985 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
3986 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
3987 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
3989 "`#pragma implementation' for `%s' appears after file is included",
3998 /* This was a fun hack, but #pragma seems to start to be useful.
3999 By failing to recognize it, we pass it through unchanged to cc1. */
4002 * the behavior of the #pragma directive is implementation defined.
4003 * this implementation defines it as follows.
4010 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
4013 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4015 execl ("/usr/games/hack", "#pragma", 0);
4016 execl ("/usr/games/rogue", "#pragma", 0);
4017 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4018 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4020 fatal ("You are in a maze of twisty compiler features, all different");
4024 /* Just ignore #sccs, on systems where we define it at all. */
4027 do_sccs (pfile
, keyword
, buf
, limit
)
4029 struct directive
*keyword
;
4030 U_CHAR
*buf
, *limit
;
4032 if (CPP_PEDANTIC (pfile
))
4033 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4038 * handle #if command by
4039 * 1) inserting special `defined' keyword into the hash table
4040 * that gets turned into 0 or 1 by special_symbol (thus,
4041 * if the luser has a symbol called `defined' already, it won't
4042 * work inside the #if command)
4043 * 2) rescan the input into a temporary output buffer
4044 * 3) pass the output buffer to the yacc parser and collect a value
4045 * 4) clean up the mess left from steps 1 and 2.
4046 * 5) call conditional_skip to skip til the next #endif (etc.),
4047 * or not, depending on the value from step 3.
4051 do_if (pfile
, keyword
, buf
, limit
)
4053 struct directive
*keyword
;
4054 U_CHAR
*buf
, *limit
;
4056 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4057 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4062 * handle a #elif directive by not changing if_stack either.
4063 * see the comment above do_else.
4067 do_elif (pfile
, keyword
, buf
, limit
)
4069 struct directive
*keyword
;
4070 U_CHAR
*buf
, *limit
;
4072 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4073 cpp_error (pfile
, "`#elif' not within a conditional");
4076 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4077 cpp_error (pfile
, "`#elif' after `#else'");
4079 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4081 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4082 && strcmp (pfile
->if_stack
->fname
,
4083 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4084 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4085 fprintf (stderr
, ")\n");
4087 pfile
->if_stack
->type
= T_ELIF
;
4090 if (pfile
->if_stack
->if_succeeded
)
4091 skip_if_group (pfile
, 0);
4093 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4095 skip_if_group (pfile
, 0);
4097 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4098 output_line_command (pfile
, 1, same_file
);
4105 * evaluate a #if expression in BUF, of length LENGTH,
4106 * then parse the result as a C expression and return the value as an int.
4109 static HOST_WIDE_INT
4110 eval_if_expression (pfile
, buf
, length
)
4115 HASHNODE
*save_defined
;
4116 HOST_WIDE_INT value
;
4117 long old_written
= CPP_WRITTEN (pfile
);
4119 save_defined
= install ("defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4120 pfile
->pcp_inside_if
= 1;
4122 value
= cpp_parse_expr (pfile
);
4123 pfile
->pcp_inside_if
= 0;
4124 delete_macro (save_defined
); /* clean up special symbol */
4126 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4132 * routine to handle ifdef/ifndef. Try to look up the symbol,
4133 * then do or don't skip to the #endif/#else/#elif depending
4134 * on what directive is actually being processed.
4138 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4140 struct directive
*keyword
;
4141 U_CHAR
*unused1
, *unused2
;
4144 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4147 enum cpp_token token
;
4148 int start_of_file
= 0;
4149 U_CHAR
*control_macro
= 0;
4150 int old_written
= CPP_WRITTEN (pfile
);
4152 /* Detect a #ifndef at start of file (not counting comments). */
4153 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4154 start_of_file
= pfile
->only_seen_white
== 2;
4156 pfile
->no_macro_expand
++;
4157 token
= get_directive_token (pfile
);
4158 pfile
->no_macro_expand
--;
4160 ident
= pfile
->token_buffer
+ old_written
;
4161 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4162 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4164 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4166 skip
= (keyword
->type
== T_IFDEF
);
4167 if (! CPP_TRADITIONAL (pfile
))
4168 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4170 else if (token
== CPP_NAME
)
4172 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4173 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4174 if (start_of_file
&& !skip
)
4176 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4177 bcopy (ident
, control_macro
, ident_length
+ 1);
4182 skip
= (keyword
->type
== T_IFDEF
);
4183 if (! CPP_TRADITIONAL (pfile
))
4184 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4187 if (!CPP_TRADITIONAL (pfile
))
4189 cpp_skip_hspace (pfile
);
4191 if (c
!= EOF
&& c
!= '\n')
4192 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4194 skip_rest_of_line (pfile
);
4198 /* Output a precondition for this macro. */
4199 if (hp
&& hp
->value
.defn
->predefined
)
4200 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4203 fprintf (pcp_outfile
, "#undef ");
4204 while (is_idchar
[*cp
]) /* Ick! */
4205 fputc (*cp
++, pcp_outfile
);
4206 putc ('\n', pcp_outfile
);
4210 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4214 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4215 If this is a #ifndef starting at the beginning of a file,
4216 CONTROL_MACRO is the macro name tested by the #ifndef.
4217 Otherwise, CONTROL_MACRO is 0. */
4220 conditional_skip (pfile
, skip
, type
, control_macro
)
4223 enum node_type type
;
4224 U_CHAR
*control_macro
;
4226 IF_STACK_FRAME
*temp
;
4228 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4229 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4231 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4233 temp
->next
= pfile
->if_stack
;
4234 temp
->control_macro
= control_macro
;
4235 pfile
->if_stack
= temp
;
4237 pfile
->if_stack
->type
= type
;
4240 skip_if_group (pfile
, 0);
4243 ++pfile
->if_stack
->if_succeeded
;
4244 output_line_command (pfile
, 1, same_file
);
4249 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4250 * leaves input ptr at the sharp sign found.
4251 * If ANY is nonzero, return at next directive of any sort.
4255 skip_if_group (pfile
, any
)
4260 int at_beg_of_line
= 1;
4261 struct directive
*kt
;
4262 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4264 U_CHAR
*beg_of_line
= bp
;
4266 register int ident_length
;
4267 U_CHAR
*ident
, *after_ident
;
4268 struct parse_marker line_start_mark
;
4270 parse_set_mark (&line_start_mark
, pfile
);
4272 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4273 static char failed
[] = "#failed\n";
4274 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4276 output_line_command (pfile
, 1, same_file
);
4280 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4282 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4283 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4284 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4286 parse_move_mark (&line_start_mark
, pfile
);
4287 if (!CPP_TRADITIONAL (pfile
))
4288 cpp_skip_hspace (pfile
);
4292 int old_written
= CPP_WRITTEN (pfile
);
4293 cpp_skip_hspace (pfile
);
4295 parse_name (pfile
, GETC());
4296 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4297 ident
= pfile
->token_buffer
+ old_written
;
4298 pfile
->limit
= ident
;
4300 if (ident_length
== 0)
4301 goto not_a_directive
;
4303 /* Handle # followed by a line number. */
4305 /* Avoid error for `###' and similar cases unless -pedantic. */
4308 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4310 IF_STACK_FRAME
*temp
;
4311 if (ident_length
== kt
->length
4312 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4314 /* If we are asked to return on next directive, do so now. */
4324 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4325 temp
->next
= pfile
->if_stack
;
4326 pfile
->if_stack
= temp
;
4328 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4330 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4331 temp
->type
= kt
->type
;
4335 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4336 validate_else (pfile
,
4337 kt
->type
== T_ELSE
? "#else" : "#endif");
4339 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4342 "`#%s' not within a conditional", kt
->name
);
4345 else if (pfile
->if_stack
== save_if_stack
)
4346 goto done
; /* found what we came for */
4348 if (kt
->type
!= T_ENDIF
)
4350 if (pfile
->if_stack
->type
== T_ELSE
)
4351 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4352 pfile
->if_stack
->type
= kt
->type
;
4356 temp
= pfile
->if_stack
;
4357 pfile
->if_stack
= temp
->next
;
4364 /* Don't let erroneous code go by. */
4365 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4366 && CPP_PEDANTIC (pfile
))
4367 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4371 /* We're in the middle of a line. Skip the rest of it. */
4378 case '/': /* possible comment */
4379 c
= skip_comment (pfile
, NULL
);
4386 old
= CPP_WRITTEN (pfile
);
4387 cpp_get_token (pfile
);
4388 CPP_SET_WRITTEN (pfile
, old
);
4391 /* Char after backslash loses its special meaning. */
4392 if (PEEKC() == '\n')
4402 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4403 static char end_failed
[] = "#endfailed\n";
4404 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4407 pfile
->only_seen_white
= 1;
4408 parse_goto_mark (&line_start_mark
, pfile
);
4409 parse_clear_mark (&line_start_mark
);
4413 * handle a #else directive. Do this by just continuing processing
4414 * without changing if_stack ; this is so that the error message
4415 * for missing #endif's etc. will point to the original #if. It
4416 * is possible that something different would be better.
4420 do_else (pfile
, keyword
, buf
, limit
)
4422 struct directive
*keyword
;
4423 U_CHAR
*buf
, *limit
;
4425 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4427 if (CPP_PEDANTIC (pfile
))
4428 validate_else (pfile
, "#else");
4429 skip_rest_of_line (pfile
);
4431 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4432 cpp_error (pfile
, "`#else' not within a conditional");
4435 /* #ifndef can't have its special treatment for containing the whole file
4436 if it has a #else clause. */
4437 pfile
->if_stack
->control_macro
= 0;
4439 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4440 cpp_error (pfile
, "`#else' after `#else'");
4441 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4442 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4443 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4444 fprintf (stderr
, ")\n");
4446 pfile
->if_stack
->type
= T_ELSE
;
4449 if (pfile
->if_stack
->if_succeeded
)
4450 skip_if_group (pfile
, 0);
4452 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4453 output_line_command (pfile
, 1, same_file
);
4459 * unstack after #endif command
4463 do_endif (pfile
, keyword
, buf
, limit
)
4465 struct directive
*keyword
;
4466 U_CHAR
*buf
, *limit
;
4468 if (CPP_PEDANTIC (pfile
))
4469 validate_else (pfile
, "#endif");
4470 skip_rest_of_line (pfile
);
4472 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4473 cpp_error (pfile
, "unbalanced `#endif'");
4476 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4477 pfile
->if_stack
= temp
->next
;
4478 if (temp
->control_macro
!= 0)
4480 /* This #endif matched a #ifndef at the start of the file.
4481 See if it is at the end of the file. */
4482 struct parse_marker start_mark
;
4485 parse_set_mark (&start_mark
, pfile
);
4489 cpp_skip_hspace (pfile
);
4494 parse_goto_mark (&start_mark
, pfile
);
4495 parse_clear_mark (&start_mark
);
4499 /* If we get here, this #endif ends a #ifndef
4500 that contains all of the file (aside from whitespace).
4501 Arrange not to include the file again
4502 if the macro that was tested is defined.
4504 Do not do this for the top-level file in a -include or any
4505 file in a -imacros. */
4509 && ! (indepth
== 1 && pfile
->no_record_file
)
4510 && ! (pfile
->no_record_file
&& no_output
))
4513 struct file_name_list
*ifile
= pfile
->all_include_files
;
4515 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4517 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4519 ifile
->control_macro
= temp
->control_macro
;
4527 output_line_command (pfile
, 1, same_file
);
4532 /* When an #else or #endif is found while skipping failed conditional,
4533 if -pedantic was specified, this is called to warn about text after
4534 the command name. P points to the first char after the command name. */
4537 validate_else (pfile
, directive
)
4542 cpp_skip_hspace (pfile
);
4544 if (c
!= EOF
&& c
!= '\n')
4546 "text following `%s' violates ANSI standard", directive
);
4549 /* Get the next token, and add it to the text in pfile->token_buffer.
4550 Return the kind of token we got. */
4553 cpp_get_token (pfile
)
4556 register int c
, c2
, c3
;
4558 long start_line
, start_column
;
4559 enum cpp_token token
;
4560 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4561 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4567 if (CPP_BUFFER (pfile
)->seen_eof
)
4569 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4576 cpp_buffer
*next_buf
4577 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4578 CPP_BUFFER (pfile
)->seen_eof
= 1;
4579 if (CPP_BUFFER (pfile
)->nominal_fname
4580 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4582 /* We're about to return from an #include file.
4583 Emit #line information now (as part of the CPP_POP) result.
4584 But the #line refers to the file we will pop to. */
4585 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4586 CPP_BUFFER (pfile
) = next_buf
;
4587 pfile
->input_stack_listing_current
= 0;
4588 output_line_command (pfile
, 0, leave_file
);
4589 CPP_BUFFER (pfile
) = cur_buffer
;
4599 struct parse_marker start_mark
;
4601 if (PEEKC () == '=')
4603 if (opts
->put_out_comments
)
4604 parse_set_mark (&start_mark
, pfile
);
4606 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4607 &start_line
, &start_column
);
4608 c
= skip_comment (pfile
, &newlines
);
4609 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4610 parse_clear_mark (&start_mark
);
4615 cpp_error_with_line (pfile
, start_line
, start_column
,
4616 "unterminated comment");
4619 c
= '/'; /* Initial letter of comment. */
4621 /* Comments are equivalent to spaces.
4622 For -traditional, a comment is equivalent to nothing. */
4623 if (opts
->put_out_comments
)
4625 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4627 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4628 int len
= pbuf
->cur
- start
;
4629 CPP_RESERVE(pfile
, 1 + len
);
4630 CPP_PUTC_Q (pfile
, c
);
4631 CPP_PUTS_Q (pfile
, start
, len
);
4632 pfile
->lineno
+= newlines
;
4633 parse_clear_mark (&start_mark
);
4636 else if (CPP_TRADITIONAL (pfile
))
4643 /* This may not work if cpp_get_token is called recursively,
4644 since many places look for horizontal space. */
4647 /* Copy the newlines into the output buffer, in order to
4648 avoid the pain of a #line every time a multiline comment
4650 CPP_RESERVE(pfile
, newlines
);
4651 while (--newlines
>= 0)
4653 CPP_PUTC_Q (pfile
, '\n');
4659 CPP_RESERVE(pfile
, 1);
4660 CPP_PUTC_Q (pfile
, ' ');
4664 if (opts
->for_lint
) {
4667 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4669 if (lintcmd
!= NULL
) {
4670 /* I believe it is always safe to emit this newline: */
4672 bcopy ("#pragma lint ", (char *) obp
, 13);
4674 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4679 bcopy (argbp
, (char *) obp
, arglen
);
4683 /* OK, now bring us back to the state we were in before we entered
4684 this branch. We need #line b/c the newline for the pragma
4685 could fuck things up. */
4686 output_line_command (pfile
, 0, same_file
);
4687 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4695 /* If this is expanding a macro definition, don't recognize
4696 preprocessor directives. */
4699 /* If this is expand_into_temp_buffer, recognize them
4700 only after an actual newline at this level,
4701 not at the beginning of the input level. */
4702 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4708 if (!pfile
->only_seen_white
)
4710 if (handle_directive (pfile
))
4711 return CPP_DIRECTIVE
;
4712 pfile
->only_seen_white
= 0;
4717 /* A single quoted string is treated like a double -- some
4718 programs (e.g., troff) are perverse this way */
4719 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4720 &start_line
, &start_column
);
4721 old_written
= CPP_WRITTEN (pfile
);
4723 CPP_PUTC (pfile
, c
);
4729 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4731 /* try harder: this string crosses a macro expansion
4732 boundary. This can happen naturally if -traditional.
4733 Otherwise, only -D can make a macro with an unmatched
4735 cpp_buffer
*next_buf
4736 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4737 (*CPP_BUFFER (pfile
)->cleanup
)
4738 (CPP_BUFFER (pfile
), pfile
);
4739 CPP_BUFFER (pfile
) = next_buf
;
4742 if (!CPP_TRADITIONAL (pfile
))
4744 cpp_error_with_line (pfile
, start_line
, start_column
,
4745 "unterminated string or character constant");
4746 if (pfile
->multiline_string_line
!= start_line
4747 && pfile
->multiline_string_line
!= 0)
4748 cpp_error_with_line (pfile
,
4749 pfile
->multiline_string_line
, -1,
4750 "possible real start of unterminated constant");
4751 pfile
->multiline_string_line
= 0;
4755 CPP_PUTC (pfile
, cc
);
4759 /* Traditionally, end of line ends a string constant with
4760 no error. So exit the loop and record the new line. */
4761 if (CPP_TRADITIONAL (pfile
))
4765 cpp_error_with_line (pfile
, start_line
, start_column
,
4766 "unterminated character constant");
4769 if (CPP_PEDANTIC (pfile
)
4770 && pfile
->multiline_string_line
== 0)
4772 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4773 "string constant runs past end of line");
4775 if (pfile
->multiline_string_line
== 0)
4776 pfile
->multiline_string_line
= start_line
;
4783 /* Backslash newline is replaced by nothing at all. */
4784 CPP_ADJUST_WRITTEN (pfile
, -1);
4789 /* ANSI stupidly requires that in \\ the second \
4790 is *not* prevented from combining with a newline. */
4793 CPP_PUTC (pfile
, cc
);
4805 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4806 CPP_PWRITTEN (pfile
));
4807 pfile
->only_seen_white
= 0;
4808 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4811 if (!opts
->dollars_in_ident
)
4816 if (opts
->cplusplus
&& PEEKC () == ':')
4825 if (c2
== c
|| c2
== '=')
4835 if (PEEKC () == '=')
4842 if (c2
== '-' && opts
->chill
)
4844 /* Chill style comment */
4845 if (opts
->put_out_comments
)
4846 parse_set_mark (&start_mark
, pfile
);
4847 FORWARD(1); /* Skip second '-'. */
4855 /* Don't consider final '\n' to be part of comment. */
4861 goto return_comment
;
4863 if (c2
== '-' || c2
== '=' || c2
== '>')
4868 if (pfile
->parsing_include_directive
)
4872 CPP_PUTC (pfile
, c
);
4877 if (c
== '\n' || c
== EOF
)
4880 "missing '>' in `#include <FILENAME>'");
4886 /* else fall through */
4895 CPP_RESERVE (pfile
, 4);
4896 CPP_PUTC (pfile
, c
);
4897 CPP_PUTC (pfile
, c2
);
4901 CPP_PUTC_Q (pfile
, GETC ());
4902 CPP_NUL_TERMINATE_Q (pfile
);
4903 pfile
->only_seen_white
= 0;
4907 if (CPP_BUFFER (pfile
)->has_escapes
)
4912 if (pfile
->output_escapes
)
4913 CPP_PUTS (pfile
, "@-", 2);
4914 parse_name (pfile
, GETC ());
4917 else if (is_space
[c
])
4919 CPP_RESERVE (pfile
, 2);
4920 if (pfile
->output_escapes
)
4921 CPP_PUTC_Q (pfile
, '@');
4922 CPP_PUTC_Q (pfile
, c
);
4926 if (pfile
->output_escapes
)
4928 CPP_PUTS (pfile
, "@@", 2);
4938 CPP_RESERVE(pfile
, 2);
4939 CPP_PUTC_Q (pfile
, '.');
4943 /* FIXME - misses the case "..\\\n." */
4944 if (c2
== '.' && PEEKN(1) == '.')
4946 CPP_RESERVE(pfile
, 4);
4947 CPP_PUTC_Q (pfile
, '.');
4948 CPP_PUTC_Q (pfile
, '.');
4949 CPP_PUTC_Q (pfile
, '.');
4951 CPP_NUL_TERMINATE_Q (pfile
);
4952 pfile
->only_seen_white
= 0;
4959 pfile
->only_seen_white
= 0;
4961 CPP_RESERVE(pfile
, 3);
4962 CPP_PUTC_Q (pfile
, c
);
4963 CPP_PUTC_Q (pfile
, GETC ());
4964 CPP_NUL_TERMINATE_Q (pfile
);
4970 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4972 CPP_PUTC (pfile
, c
);
4978 case '0': case '1': case '2': case '3': case '4':
4979 case '5': case '6': case '7': case '8': case '9':
4984 CPP_RESERVE (pfile
, 2);
4985 CPP_PUTC_Q (pfile
, c
);
4990 if (!is_idchar
[c
] && c
!= '.'
4991 && ((c2
!= 'e' && c2
!= 'E') || (c
!= '+' && c
!= '-')))
4996 CPP_NUL_TERMINATE_Q (pfile
);
4997 pfile
->only_seen_white
= 0;
4999 case 'b': case 'c': case 'd': case 'h': case 'o':
5000 case 'B': case 'C': case 'D': case 'H': case 'O':
5001 if (opts
->chill
&& PEEKC () == '\'')
5003 pfile
->only_seen_white
= 0;
5004 CPP_RESERVE (pfile
, 2);
5005 CPP_PUTC_Q (pfile
, c
);
5006 CPP_PUTC_Q (pfile
, '\'');
5012 goto chill_number_eof
;
5015 if (c
== '\\' && PEEKC() == '\n')
5022 CPP_PUTC (pfile
, c
);
5026 CPP_RESERVE (pfile
, 2);
5027 CPP_PUTC_Q (pfile
, c
);
5028 CPP_NUL_TERMINATE_Q (pfile
);
5035 CPP_NUL_TERMINATE (pfile
);
5042 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5043 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5044 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5045 case 'x': case 'y': case 'z':
5046 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5047 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5048 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5053 unsigned char *ident
;
5054 int before_name_written
= CPP_WRITTEN (pfile
);
5056 parse_name (pfile
, c
);
5057 pfile
->only_seen_white
= 0;
5058 if (pfile
->no_macro_expand
)
5060 ident
= pfile
->token_buffer
+ before_name_written
;
5061 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5062 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5065 if (hp
->type
== T_DISABLED
)
5067 if (pfile
->output_escapes
)
5068 { /* Return "@-IDENT", followed by '\0'. */
5070 CPP_RESERVE (pfile
, 3);
5071 ident
= pfile
->token_buffer
+ before_name_written
;
5072 CPP_ADJUST_WRITTEN (pfile
, 2);
5073 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5080 /* If macro wants an arglist, verify that a '(' follows.
5081 first skip all whitespace, copying it to the output
5082 after the macro name. Then, if there is no '(',
5083 decide this is not a macro call and leave things that way. */
5084 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5086 struct parse_marker macro_mark
;
5088 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5090 cpp_buffer
*next_buf
;
5091 cpp_skip_hspace (pfile
);
5092 if (PEEKC () != EOF
)
5094 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5095 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5096 CPP_BUFFER (pfile
) = next_buf
;
5098 parse_set_mark (¯o_mark
, pfile
);
5101 cpp_skip_hspace (pfile
);
5103 is_macro_call
= c
== '(';
5109 parse_goto_mark (¯o_mark
, pfile
);
5110 parse_clear_mark (¯o_mark
);
5114 /* This is now known to be a macro call. */
5116 /* it might not actually be a macro. */
5117 if (hp
->type
!= T_MACRO
) {
5118 int xbuf_len
; U_CHAR
*xbuf
;
5119 CPP_SET_WRITTEN (pfile
, before_name_written
);
5120 special_symbol (hp
, pfile
);
5121 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5122 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5123 CPP_SET_WRITTEN (pfile
, before_name_written
);
5124 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5125 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5129 /* Expand the macro, reading arguments as needed,
5130 and push the expansion on the input stack. */
5131 macroexpand (pfile
, hp
);
5132 CPP_SET_WRITTEN (pfile
, before_name_written
);
5135 /* An extra "@ " is added to the end of a macro expansion
5136 to prevent accidental token pasting. We prefer to avoid
5137 unneeded extra spaces (for the sake of cpp-using tools like
5138 imake). Here we remove the space if it is safe to do so. */
5139 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5140 && pfile
->buffer
->rlimit
[-2] == '@'
5141 && pfile
->buffer
->rlimit
[-1] == ' ')
5143 int c1
= pfile
->buffer
->rlimit
[-3];
5144 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5145 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5146 pfile
->buffer
->rlimit
-= 2;
5151 case ' ': case '\t': case '\v': case '\r':
5154 CPP_PUTC (pfile
, c
);
5156 if (c
== EOF
|| !is_hor_space
[c
])
5170 CPP_PUTC (pfile
, c
);
5171 if (pfile
->only_seen_white
== 0)
5172 pfile
->only_seen_white
= 1;
5174 output_line_command (pfile
, 1, same_file
);
5177 case '(': token
= CPP_LPAREN
; goto char1
;
5178 case ')': token
= CPP_RPAREN
; goto char1
;
5179 case '{': token
= CPP_LBRACE
; goto char1
;
5180 case '}': token
= CPP_RBRACE
; goto char1
;
5181 case ',': token
= CPP_COMMA
; goto char1
;
5182 case ';': token
= CPP_SEMICOLON
; goto char1
;
5188 pfile
->only_seen_white
= 0;
5189 CPP_PUTC (pfile
, c
);
5195 /* Like cpp_get_token, but skip spaces and comments. */
5198 cpp_get_non_space_token (pfile
)
5201 int old_written
= CPP_WRITTEN (pfile
);
5204 enum cpp_token token
= cpp_get_token (pfile
);
5205 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5206 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5208 CPP_SET_WRITTEN (pfile
, old_written
);
5212 /* Parse an identifier starting with C. */
5215 parse_name (pfile
, c
)
5216 cpp_reader
*pfile
; int c
;
5222 if (c
== '\\' && PEEKC() == '\n')
5231 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5232 CPP_PUTC_Q (pfile
, c
);
5237 CPP_NUL_TERMINATE_Q (pfile
);
5242 /* Maintain and search list of included files, for #import. */
5244 /* Hash a file name for import_hash_table. */
5252 while (*f
) val
+= *f
++;
5253 return (val
%IMPORT_HASH_SIZE
);
5256 /* Search for file FILENAME in import_hash_table.
5257 Return -2 if found, either a matching name or a matching inode.
5258 Otherwise, open the file and return a file descriptor if successful
5259 or -1 if unsuccessful. */
5262 lookup_import (pfile
, filename
, searchptr
)
5265 struct file_name_list
*searchptr
;
5267 struct import_file
*i
;
5273 hashval
= import_hash (filename
);
5275 /* Attempt to find file in list of already included files */
5276 i
= pfile
->import_hash_table
[hashval
];
5279 if (!strcmp (filename
, i
->name
))
5280 return -2; /* return found */
5283 /* Open it and try a match on inode/dev */
5284 fd
= open_include_file (pfile
, filename
, searchptr
);
5288 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5289 i
= pfile
->import_hash_table
[h
];
5291 /* Compare the inode and the device.
5292 Supposedly on some systems the inode is not a scalar. */
5293 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5294 && i
->dev
== sb
.st_dev
) {
5296 return -2; /* return found */
5301 return fd
; /* Not found, return open file */
5304 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5307 add_import (pfile
, fd
, fname
)
5312 struct import_file
*i
;
5316 hashval
= import_hash (fname
);
5318 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5319 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5320 strcpy (i
->name
, fname
);
5321 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5323 i
->next
= pfile
->import_hash_table
[hashval
];
5324 pfile
->import_hash_table
[hashval
] = i
;
5327 /* The file_name_map structure holds a mapping of file names for a
5328 particular directory. This mapping is read from the file named
5329 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5330 map filenames on a file system with severe filename restrictions,
5331 such as DOS. The format of the file name map file is just a series
5332 of lines with two tokens on each line. The first token is the name
5333 to map, and the second token is the actual name to use. */
5335 struct file_name_map
5337 struct file_name_map
*map_next
;
5342 #define FILE_NAME_MAP_FILE "header.gcc"
5344 /* Read a space delimited string of unlimited length from a stdio
5348 read_filename_string (ch
, f
)
5356 set
= alloc
= xmalloc (len
+ 1);
5360 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5362 if (set
- alloc
== len
)
5365 alloc
= xrealloc (alloc
, len
+ 1);
5366 set
= alloc
+ len
/ 2;
5376 /* This structure holds a linked list of file name maps, one per directory. */
5378 struct file_name_map_list
5380 struct file_name_map_list
*map_list_next
;
5381 char *map_list_name
;
5382 struct file_name_map
*map_list_map
;
5385 /* Read the file name map file for DIRNAME. */
5387 static struct file_name_map
*
5388 read_name_map (pfile
, dirname
)
5392 register struct file_name_map_list
*map_list_ptr
;
5396 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5397 map_list_ptr
= map_list_ptr
->map_list_next
)
5398 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5399 return map_list_ptr
->map_list_map
;
5401 map_list_ptr
= ((struct file_name_map_list
*)
5402 xmalloc (sizeof (struct file_name_map_list
)));
5403 map_list_ptr
->map_list_name
= savestring (dirname
);
5404 map_list_ptr
->map_list_map
= NULL
;
5406 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5407 strcpy (name
, dirname
);
5410 strcat (name
, FILE_NAME_MAP_FILE
);
5411 f
= fopen (name
, "r");
5413 map_list_ptr
->map_list_map
= NULL
;
5417 int dirlen
= strlen (dirname
);
5419 while ((ch
= getc (f
)) != EOF
)
5422 struct file_name_map
*ptr
;
5426 from
= read_filename_string (ch
, f
);
5427 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5429 to
= read_filename_string (ch
, f
);
5431 ptr
= ((struct file_name_map
*)
5432 xmalloc (sizeof (struct file_name_map
)));
5433 ptr
->map_from
= from
;
5435 /* Make the real filename absolute. */
5440 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5441 strcpy (ptr
->map_to
, dirname
);
5442 ptr
->map_to
[dirlen
] = '/';
5443 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5447 ptr
->map_next
= map_list_ptr
->map_list_map
;
5448 map_list_ptr
->map_list_map
= ptr
;
5450 while ((ch
= getc (f
)) != '\n')
5457 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5458 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5460 return map_list_ptr
->map_list_map
;
5463 /* Try to open include file FILENAME. SEARCHPTR is the directory
5464 being tried from the include file search path. This function maps
5465 filenames on file systems based on information read by
5469 open_include_file (pfile
, filename
, searchptr
)
5472 struct file_name_list
*searchptr
;
5474 register struct file_name_map
*map
;
5475 register char *from
;
5478 if (searchptr
&& ! searchptr
->got_name_map
)
5480 searchptr
->name_map
= read_name_map (pfile
,
5482 ? searchptr
->fname
: ".");
5483 searchptr
->got_name_map
= 1;
5486 /* First check the mapping for the directory we are using. */
5487 if (searchptr
&& searchptr
->name_map
)
5490 if (searchptr
->fname
)
5491 from
+= strlen (searchptr
->fname
) + 1;
5492 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5494 if (! strcmp (map
->map_from
, from
))
5496 /* Found a match. */
5497 return open (map
->map_to
, O_RDONLY
, 0666);
5502 /* Try to find a mapping file for the particular directory we are
5503 looking in. Thus #include <sys/types.h> will look up sys/types.h
5504 in /usr/include/header.gcc and look up types.h in
5505 /usr/include/sys/header.gcc. */
5506 p
= rindex (filename
, '/');
5511 && strlen (searchptr
->fname
) == p
- filename
5512 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5514 /* FILENAME is in SEARCHPTR, which we've already checked. */
5515 return open (filename
, O_RDONLY
, 0666);
5525 dir
= (char *) alloca (p
- filename
+ 1);
5526 bcopy (filename
, dir
, p
- filename
);
5527 dir
[p
- filename
] = '\0';
5530 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5531 if (! strcmp (map
->map_from
, from
))
5532 return open (map
->map_to
, O_RDONLY
, 0666);
5534 return open (filename
, O_RDONLY
, 0666);
5537 /* Process the contents of include file FNAME, already open on descriptor F,
5539 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5540 "system" include directories (as decided by the `is_system_include'
5542 DIRPTR is the link in the dir path through which this file was found,
5543 or 0 if the file name was absolute or via the current directory.
5544 Return 1 on success, 0 on failure.
5546 The caller is responsible for the cpp_push_buffer. */
5549 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5553 int system_header_p
;
5554 struct file_name_list
*dirptr
;
5560 cpp_buffer
*fp
; /* For input stack frame */
5561 int missing_newline
= 0;
5563 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
5565 cpp_perror_with_name (pfile
, fname
);
5567 cpp_pop_buffer (pfile
);
5571 fp
= CPP_BUFFER (pfile
);
5572 fp
->nominal_fname
= fp
->fname
= fname
;
5577 fp
->system_header_p
= system_header_p
;
5580 fp
->cleanup
= file_cleanup
;
5582 if (S_ISREG (st_mode
)) {
5583 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5584 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5587 /* Read the file contents, knowing that st_size is an upper bound
5588 on the number of bytes we can read. */
5589 length
= safe_read (f
, fp
->buf
, st_size
);
5590 fp
->rlimit
= fp
->buf
+ length
;
5591 if (length
< 0) goto nope
;
5593 else if (S_ISDIR (st_mode
)) {
5594 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5598 /* Cannot count its file size before reading.
5599 First read the entire file into heap and
5600 copy them into buffer on stack. */
5605 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5608 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5610 goto nope
; /* error! */
5612 if (st_size
!= bsize
)
5613 break; /* End of file */
5615 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5621 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5622 /* Backslash-newline at end is not good enough. */
5623 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5624 fp
->buf
[length
++] = '\n';
5626 missing_newline
= 1;
5629 fp
->buf
[length
] = '\0';
5630 fp
->rlimit
= fp
->buf
+ length
;
5632 /* Close descriptor now, so nesting does not use lots of descriptors. */
5635 /* Must do this before calling trigraph_pcp, so that the correct file name
5636 will be printed in warning messages. */
5638 pfile
->input_stack_listing_current
= 0;
5648 if (missing_newline
)
5651 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5652 pedwarn ("file does not end in newline");
5655 input_file_stack_tick
++;
5662 cpp_perror_with_name (pfile
, fname
);
5668 /* This is called after options have been processed.
5669 * Check options for consistency, and setup for processing input
5670 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5671 * Return 1 on succes, 0 on failure.
5675 cpp_start_read (pfile
, fname
)
5679 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5680 struct cpp_pending
*pend
;
5685 /* The code looks at the defaults through this pointer, rather than through
5686 the constant structure above. This pointer gets changed if an environment
5687 variable specifies other defaults. */
5688 struct default_include
*include_defaults
= include_defaults_array
;
5690 /* Add dirs from CPATH after dirs from -I. */
5691 /* There seems to be confusion about what CPATH should do,
5692 so for the moment it is not documented. */
5693 /* Some people say that CPATH should replace the standard include dirs,
5694 but that seems pointless: it comes before them, so it overrides them
5696 p
= (char *) getenv ("CPATH");
5697 if (p
!= 0 && ! opts
->no_standard_includes
)
5698 path_include (pfile
, p
);
5700 /* Now that dollars_in_ident is known, initialize is_idchar. */
5701 initialize_char_syntax (opts
);
5703 /* Do partial setup of input buffer for the sake of generating
5704 early #line directives (when -g is in effect). */
5705 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5708 if (opts
->in_fname
== NULL
)
5709 opts
->in_fname
= "";
5710 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5713 /* Install __LINE__, etc. Must follow initialize_char_syntax
5714 and option processing. */
5715 initialize_builtins (pfile
);
5717 /* Do standard #defines and assertions
5718 that identify system and machine type. */
5720 if (!opts
->inhibit_predefs
) {
5721 char *p
= (char *) alloca (strlen (predefs
) + 1);
5722 strcpy (p
, predefs
);
5725 while (*p
== ' ' || *p
== '\t')
5727 /* Handle -D options. */
5728 if (p
[0] == '-' && p
[1] == 'D') {
5730 while (*p
&& *p
!= ' ' && *p
!= '\t')
5734 if (opts
->debug_output
)
5735 output_line_command (pfile
, 0, same_file
);
5736 cpp_define (pfile
, q
);
5737 while (*p
== ' ' || *p
== '\t')
5739 } else if (p
[0] == '-' && p
[1] == 'A') {
5740 /* Handle -A options (assertions). */
5749 past_name
= assertion
;
5750 /* Locate end of name. */
5751 while (*past_name
&& *past_name
!= ' '
5752 && *past_name
!= '\t' && *past_name
!= '(')
5754 /* Locate `(' at start of value. */
5756 while (*value
&& (*value
== ' ' || *value
== '\t'))
5758 if (*value
++ != '(')
5760 while (*value
&& (*value
== ' ' || *value
== '\t'))
5763 /* Locate end of value. */
5764 while (*past_value
&& *past_value
!= ' '
5765 && *past_value
!= '\t' && *past_value
!= ')')
5767 termination
= past_value
;
5768 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5770 if (*termination
++ != ')')
5772 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5774 /* Temporarily null-terminate the value. */
5775 save_char
= *termination
;
5776 *termination
= '\0';
5777 /* Install the assertion. */
5778 make_assertion (pfile
, "-A", assertion
);
5779 *termination
= (char) save_char
;
5781 while (*p
== ' ' || *p
== '\t')
5789 /* Now handle the command line options. */
5791 /* Do -U's, -D's and -A's in the order they were seen. */
5792 /* First reverse the list. */
5793 opts
->pending
= nreverse_pending (opts
->pending
);
5795 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5797 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5799 switch (pend
->cmd
[1])
5802 if (opts
->debug_output
)
5803 output_line_command (pfile
, 0, same_file
);
5804 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5807 if (opts
->debug_output
)
5808 output_line_command (pfile
, 0, same_file
);
5809 cpp_define (pfile
, pend
->arg
);
5812 make_assertion (pfile
, "-A", pend
->arg
);
5818 opts
->done_initializing
= 1;
5820 { /* Read the appropriate environment variable and if it exists
5821 replace include_defaults with the listed path. */
5823 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5826 epath
= getenv ("C_INCLUDE_PATH");
5829 epath
= getenv ("CPLUS_INCLUDE_PATH");
5832 epath
= getenv ("OBJC_INCLUDE_PATH");
5835 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
5838 /* If the environment var for this language is set,
5839 add to the default list of include directories. */
5841 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5843 char *startp
, *endp
;
5845 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5846 if (*startp
== PATH_SEPARATOR
)
5849 = (struct default_include
*) xmalloc ((num_dirs
5850 * sizeof (struct default_include
))
5851 + sizeof (include_defaults_array
));
5852 startp
= endp
= epath
;
5855 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5856 if ((*endp
== PATH_SEPARATOR
)
5858 strncpy (nstore
, startp
, endp
-startp
);
5860 strcpy (nstore
, ".");
5862 nstore
[endp
-startp
] = '\0';
5864 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5865 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5866 include_defaults
[num_dirs
].cxx_aware
= 1;
5870 endp
= startp
= endp
+ 1;
5874 /* Put the usual defaults back in at the end. */
5875 bcopy ((char *) include_defaults_array
,
5876 (char *) &include_defaults
[num_dirs
],
5877 sizeof (include_defaults_array
));
5881 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5882 opts
->first_system_include
= opts
->before_system
;
5884 /* Unless -fnostdinc,
5885 tack on the standard include file dirs to the specified list */
5886 if (!opts
->no_standard_includes
) {
5887 struct default_include
*p
= include_defaults
;
5888 char *specd_prefix
= opts
->include_prefix
;
5889 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5890 int default_len
= 0;
5891 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5892 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5893 default_len
= strlen (default_prefix
) - 7;
5894 default_prefix
[default_len
] = 0;
5896 /* Search "translated" versions of GNU directories.
5897 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5898 if (specd_prefix
!= 0 && default_len
!= 0)
5899 for (p
= include_defaults
; p
->fname
; p
++) {
5900 /* Some standard dirs are only for C++. */
5902 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5903 /* Does this dir start with the prefix? */
5904 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5905 /* Yes; change prefix and add to search list. */
5906 struct file_name_list
*new
5907 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5908 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5909 char *str
= (char *) xmalloc (this_len
+ 1);
5910 strcpy (str
, specd_prefix
);
5911 strcat (str
, p
->fname
+ default_len
);
5913 new->control_macro
= 0;
5914 new->c_system_include_path
= !p
->cxx_aware
;
5915 new->got_name_map
= 0;
5916 append_include_chain (pfile
, new, new);
5917 if (opts
->first_system_include
== 0)
5918 opts
->first_system_include
= new;
5922 /* Search ordinary names for GNU include directories. */
5923 for (p
= include_defaults
; p
->fname
; p
++) {
5924 /* Some standard dirs are only for C++. */
5926 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5927 struct file_name_list
*new
5928 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5929 new->control_macro
= 0;
5930 new->c_system_include_path
= !p
->cxx_aware
;
5931 new->fname
= p
->fname
;
5932 new->got_name_map
= 0;
5933 append_include_chain (pfile
, new, new);
5934 if (opts
->first_system_include
== 0)
5935 opts
->first_system_include
= new;
5940 /* Tack the after_include chain at the end of the include chain. */
5941 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5942 if (opts
->first_system_include
== 0)
5943 opts
->first_system_include
= opts
->after_include
;
5945 /* With -v, print the list of dirs to search. */
5946 if (opts
->verbose
) {
5947 struct file_name_list
*p
;
5948 fprintf (stderr
, "#include \"...\" search starts here:\n");
5949 for (p
= opts
->include
; p
; p
= p
->next
) {
5950 if (p
== opts
->first_bracket_include
)
5951 fprintf (stderr
, "#include <...> search starts here:\n");
5952 fprintf (stderr
, " %s\n", p
->fname
);
5954 fprintf (stderr
, "End of search list.\n");
5957 /* Scan the -imacros files before the main input.
5958 Much like #including them, but with no_output set
5959 so that only their macro definitions matter. */
5961 opts
->no_output
++; pfile
->no_record_file
++;
5962 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5964 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
5966 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5969 cpp_perror_with_name (pfile
, pend
->arg
);
5972 if (!cpp_push_buffer (pfile
, NULL
, 0))
5974 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
5975 cpp_scan_buffer (pfile
);
5978 opts
->no_output
--; pfile
->no_record_file
--;
5980 /* Copy the entire contents of the main input file into
5981 the stacked input buffer previously allocated for it. */
5982 if (fname
== NULL
|| *fname
== 0) {
5985 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
5986 cpp_pfatal_with_name (pfile
, fname
);
5988 /* -MG doesn't select the form of output and must be specified with one of
5989 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5990 inhibit compilation. */
5991 if (opts
->print_deps_missing_files
5992 && (opts
->print_deps
== 0 || !opts
->no_output
))
5994 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
5998 /* Either of two environment variables can specify output of deps.
5999 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6000 where OUTPUT_FILE is the file to write deps info to
6001 and DEPS_TARGET is the target to mention in the deps. */
6003 if (opts
->print_deps
== 0
6004 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6005 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6006 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6012 spec
= getenv ("SUNPRO_DEPENDENCIES");
6013 opts
->print_deps
= 2;
6016 opts
->print_deps
= 1;
6019 /* Find the space before the DEPS_TARGET, if there is one. */
6020 /* This should use index. (mrs) */
6021 while (*s
!= 0 && *s
!= ' ') s
++;
6024 opts
->deps_target
= s
+ 1;
6025 output_file
= (char *) xmalloc (s
- spec
+ 1);
6026 bcopy (spec
, output_file
, s
- spec
);
6027 output_file
[s
- spec
] = 0;
6031 opts
->deps_target
= 0;
6035 opts
->deps_file
= output_file
;
6036 opts
->print_deps_append
= 1;
6039 /* For -M, print the expected object file name
6040 as the target of this Make-rule. */
6041 if (opts
->print_deps
)
6043 pfile
->deps_allocated_size
= 200;
6044 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6045 pfile
->deps_buffer
[0] = 0;
6046 pfile
->deps_size
= 0;
6047 pfile
->deps_column
= 0;
6049 if (opts
->deps_target
)
6050 deps_output (pfile
, opts
->deps_target
, ':');
6051 else if (*opts
->in_fname
== 0)
6052 deps_output (pfile
, "-", ':');
6058 /* Discard all directory prefixes from filename. */
6059 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6060 #ifdef DIR_SEPARATOR
6061 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6068 /* Copy remainder to mungable area. */
6069 p
= (char *) alloca (strlen(q
) + 8);
6072 /* Output P, but remove known suffixes. */
6076 && p
[len
- 2] == '.'
6077 && index("cCsSm", p
[len
- 1]))
6080 && p
[len
- 3] == '.'
6081 && p
[len
- 2] == 'c'
6082 && p
[len
- 1] == 'c')
6085 && p
[len
- 4] == '.'
6086 && p
[len
- 3] == 'c'
6087 && p
[len
- 2] == 'x'
6088 && p
[len
- 1] == 'x')
6091 && p
[len
- 4] == '.'
6092 && p
[len
- 3] == 'c'
6093 && p
[len
- 2] == 'p'
6094 && p
[len
- 1] == 'p')
6097 /* Supply our own suffix. */
6104 deps_output (pfile
, p
, ':');
6105 deps_output (pfile
, opts
->in_fname
, ' ');
6110 /* Make sure data ends with a newline. And put a null after it. */
6112 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6113 /* Backslash-newline at end is not good enough. */
6114 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6115 fp
->buf
[fp
->length
++] = '\n';
6116 missing_newline
= 1;
6118 fp
->buf
[fp
->length
] = '\0';
6120 /* Unless inhibited, convert trigraphs in the input. */
6126 /* Scan the -include files before the main input.
6127 We push these in reverse order, so that the first one is handled first. */
6129 pfile
->no_record_file
++;
6130 opts
->pending
= nreverse_pending (opts
->pending
);
6131 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6133 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6135 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6138 cpp_perror_with_name (pfile
, pend
->arg
);
6141 if (!cpp_push_buffer (pfile
, NULL
, 0))
6143 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6146 pfile
->no_record_file
--;
6148 /* Free the pending list. */
6149 for (pend
= opts
->pending
; pend
; )
6151 struct cpp_pending
*next
= pend
->next
;
6155 opts
->pending
= NULL
;
6158 /* Scan the input, processing macros and directives. */
6160 rescan (&outbuf
, 0);
6162 if (missing_newline
)
6165 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6166 pedwarn ("file does not end in newline");
6169 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6170 output_line_command (pfile
, 0, same_file
);
6175 cpp_reader_init (pfile
)
6178 bzero ((char *) pfile
, sizeof (cpp_reader
));
6179 pfile
->get_token
= cpp_get_token
;
6181 pfile
->token_buffer_size
= 200;
6182 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6183 CPP_SET_WRITTEN (pfile
, 0);
6185 pfile
->system_include_depth
= 0;
6186 pfile
->dont_repeat_files
= 0;
6187 pfile
->all_include_files
= 0;
6188 pfile
->max_include_len
= 0;
6189 pfile
->timebuf
= NULL
;
6190 pfile
->only_seen_white
= 1;
6191 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6194 static struct cpp_pending
*
6195 nreverse_pending (list
)
6196 struct cpp_pending
*list
;
6199 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6200 for (pend
= list
; pend
; pend
= next
)
6210 push_pending (pfile
, cmd
, arg
)
6215 struct cpp_pending
*pend
6216 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6219 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6220 CPP_OPTIONS (pfile
)->pending
= pend
;
6223 /* Handle command-line options in (argc, argv).
6224 Can be called multiple times, to handle multiple sets of options.
6225 Returns if an unrecognized option is seen.
6226 Returns number of handled arguments. */
6229 cpp_handle_options (pfile
, argc
, argv
)
6235 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6236 for (i
= 0; i
< argc
; i
++) {
6237 if (argv
[i
][0] != '-') {
6238 if (opts
->out_fname
!= NULL
)
6240 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6243 else if (opts
->in_fname
!= NULL
)
6244 opts
->out_fname
= argv
[i
];
6246 opts
->in_fname
= argv
[i
];
6248 switch (argv
[i
][1]) {
6251 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6254 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6258 if (!strcmp (argv
[i
], "-include")
6259 || !strcmp (argv
[i
], "-imacros")) {
6261 goto missing_filename
;
6263 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6265 if (!strcmp (argv
[i
], "-iprefix")) {
6267 goto missing_filename
;
6269 opts
->include_prefix
= argv
[++i
];
6271 if (!strcmp (argv
[i
], "-ifoutput")) {
6272 opts
->output_conditionals
= 1;
6274 if (!strcmp (argv
[i
], "-isystem")) {
6275 struct file_name_list
*dirtmp
;
6278 goto missing_filename
;
6280 dirtmp
= (struct file_name_list
*)
6281 xmalloc (sizeof (struct file_name_list
));
6283 dirtmp
->control_macro
= 0;
6284 dirtmp
->c_system_include_path
= 1;
6285 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6286 strcpy (dirtmp
->fname
, argv
[++i
]);
6287 dirtmp
->got_name_map
= 0;
6289 if (opts
->before_system
== 0)
6290 opts
->before_system
= dirtmp
;
6292 opts
->last_before_system
->next
= dirtmp
;
6293 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6295 /* Add directory to end of path for includes,
6296 with the default prefix at the front of its name. */
6297 if (!strcmp (argv
[i
], "-iwithprefix")) {
6298 struct file_name_list
*dirtmp
;
6301 if (opts
->include_prefix
!= 0)
6302 prefix
= opts
->include_prefix
;
6304 prefix
= savestring (GCC_INCLUDE_DIR
);
6305 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6306 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6307 prefix
[strlen (prefix
) - 7] = 0;
6310 dirtmp
= (struct file_name_list
*)
6311 xmalloc (sizeof (struct file_name_list
));
6312 dirtmp
->next
= 0; /* New one goes on the end */
6313 dirtmp
->control_macro
= 0;
6314 dirtmp
->c_system_include_path
= 0;
6316 goto missing_dirname
;
6318 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6319 + strlen (prefix
) + 1);
6320 strcpy (dirtmp
->fname
, prefix
);
6321 strcat (dirtmp
->fname
, argv
[++i
]);
6322 dirtmp
->got_name_map
= 0;
6324 if (opts
->after_include
== 0)
6325 opts
->after_include
= dirtmp
;
6327 opts
->last_after_include
->next
= dirtmp
;
6328 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6330 /* Add directory to main path for includes,
6331 with the default prefix at the front of its name. */
6332 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6333 struct file_name_list
*dirtmp
;
6336 if (opts
->include_prefix
!= 0)
6337 prefix
= opts
->include_prefix
;
6339 prefix
= savestring (GCC_INCLUDE_DIR
);
6340 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6341 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6342 prefix
[strlen (prefix
) - 7] = 0;
6345 dirtmp
= (struct file_name_list
*)
6346 xmalloc (sizeof (struct file_name_list
));
6347 dirtmp
->next
= 0; /* New one goes on the end */
6348 dirtmp
->control_macro
= 0;
6349 dirtmp
->c_system_include_path
= 0;
6351 goto missing_dirname
;
6353 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6354 + strlen (prefix
) + 1);
6355 strcpy (dirtmp
->fname
, prefix
);
6356 strcat (dirtmp
->fname
, argv
[++i
]);
6357 dirtmp
->got_name_map
= 0;
6359 append_include_chain (pfile
, dirtmp
, dirtmp
);
6361 /* Add directory to end of path for includes. */
6362 if (!strcmp (argv
[i
], "-idirafter")) {
6363 struct file_name_list
*dirtmp
;
6365 dirtmp
= (struct file_name_list
*)
6366 xmalloc (sizeof (struct file_name_list
));
6367 dirtmp
->next
= 0; /* New one goes on the end */
6368 dirtmp
->control_macro
= 0;
6369 dirtmp
->c_system_include_path
= 0;
6371 goto missing_dirname
;
6373 dirtmp
->fname
= argv
[++i
];
6374 dirtmp
->got_name_map
= 0;
6376 if (opts
->after_include
== 0)
6377 opts
->after_include
= dirtmp
;
6379 opts
->last_after_include
->next
= dirtmp
;
6380 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6385 if (opts
->out_fname
!= NULL
)
6387 cpp_fatal (pfile
, "Output filename specified twice");
6391 goto missing_filename
;
6392 opts
->out_fname
= argv
[++i
];
6393 if (!strcmp (opts
->out_fname
, "-"))
6394 opts
->out_fname
= "";
6398 if (!strcmp (argv
[i
], "-pedantic"))
6399 CPP_PEDANTIC (pfile
) = 1;
6400 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6401 CPP_PEDANTIC (pfile
) = 1;
6402 opts
->pedantic_errors
= 1;
6405 else if (!strcmp (argv
[i
], "-pcp")) {
6406 char *pcp_fname
= argv
[++i
];
6408 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6409 ? fopen (pcp_fname
, "w")
6410 : fdopen (dup (fileno (stdout
)), "w"));
6411 if (pcp_outfile
== 0)
6412 cpp_pfatal_with_name (pfile
, pcp_fname
);
6419 if (!strcmp (argv
[i
], "-traditional")) {
6420 opts
->traditional
= 1;
6421 if (opts
->dollars_in_ident
> 0)
6422 opts
->dollars_in_ident
= 1;
6423 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6425 opts
->no_trigraphs
= 0;
6430 if (! strcmp (argv
[i
], "-lang-c"))
6431 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->objc
= 0;
6432 if (! strcmp (argv
[i
], "-lang-c++"))
6433 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->objc
= 0;
6434 if (! strcmp (argv
[i
], "-lang-c-c++-comments"))
6435 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->objc
= 0;
6436 if (! strcmp (argv
[i
], "-lang-objc"))
6437 opts
->objc
= 1, opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1;
6438 if (! strcmp (argv
[i
], "-lang-objc++"))
6439 opts
->objc
= 1, opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6440 if (! strcmp (argv
[i
], "-lang-asm"))
6442 if (! strcmp (argv
[i
], "-lint"))
6444 if (! strcmp (argv
[i
], "-lang-chill"))
6445 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6446 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6450 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6454 opts
->inhibit_warnings
= 1;
6458 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6459 opts
->warn_trigraphs
= 1;
6460 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6461 opts
->warn_trigraphs
= 0;
6462 else if (!strcmp (argv
[i
], "-Wcomment"))
6463 opts
->warn_comments
= 1;
6464 else if (!strcmp (argv
[i
], "-Wno-comment"))
6465 opts
->warn_comments
= 0;
6466 else if (!strcmp (argv
[i
], "-Wcomments"))
6467 opts
->warn_comments
= 1;
6468 else if (!strcmp (argv
[i
], "-Wno-comments"))
6469 opts
->warn_comments
= 0;
6470 else if (!strcmp (argv
[i
], "-Wtraditional"))
6471 opts
->warn_stringify
= 1;
6472 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6473 opts
->warn_stringify
= 0;
6474 else if (!strcmp (argv
[i
], "-Wimport"))
6475 opts
->warn_import
= 1;
6476 else if (!strcmp (argv
[i
], "-Wno-import"))
6477 opts
->warn_import
= 0;
6478 else if (!strcmp (argv
[i
], "-Werror"))
6479 opts
->warnings_are_errors
= 1;
6480 else if (!strcmp (argv
[i
], "-Wno-error"))
6481 opts
->warnings_are_errors
= 0;
6482 else if (!strcmp (argv
[i
], "-Wall"))
6484 opts
->warn_trigraphs
= 1;
6485 opts
->warn_comments
= 1;
6490 /* The style of the choices here is a bit mixed.
6491 The chosen scheme is a hybrid of keeping all options in one string
6492 and specifying each option in a separate argument:
6493 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6494 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6495 -M[M][G][D file]. This is awkward to handle in specs, and is not
6497 /* ??? -MG must be specified in addition to one of -M or -MM.
6498 This can be relaxed in the future without breaking anything.
6499 The converse isn't true. */
6501 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6502 if (!strcmp (argv
[i
], "-MG"))
6504 opts
->print_deps_missing_files
= 1;
6507 if (!strcmp (argv
[i
], "-M"))
6508 opts
->print_deps
= 2;
6509 else if (!strcmp (argv
[i
], "-MM"))
6510 opts
->print_deps
= 1;
6511 else if (!strcmp (argv
[i
], "-MD"))
6512 opts
->print_deps
= 2;
6513 else if (!strcmp (argv
[i
], "-MMD"))
6514 opts
->print_deps
= 1;
6515 /* For -MD and -MMD options, write deps on file named by next arg. */
6516 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6519 goto missing_filename
;
6520 opts
->deps_file
= argv
[++i
];
6524 /* For -M and -MM, write deps on standard output
6525 and suppress the usual output. */
6526 opts
->no_output
= 1;
6532 char *p
= argv
[i
] + 2;
6534 while ((c
= *p
++) != 0) {
6535 /* Arg to -d specifies what parts of macros to dump */
6538 opts
->dump_macros
= dump_only
;
6539 opts
->no_output
= 1;
6542 opts
->dump_macros
= dump_names
;
6545 opts
->dump_macros
= dump_definitions
;
6553 if (argv
[i
][2] == '3')
6554 opts
->debug_output
= 1;
6558 fprintf (stderr
, "GNU CPP version %s", version_string
);
6559 #ifdef TARGET_VERSION
6562 fprintf (stderr
, "\n");
6567 opts
->print_include_names
= 1;
6571 if (argv
[i
][2] != 0)
6572 push_pending (pfile
, "-D", argv
[i
] + 2);
6573 else if (i
+ 1 == argc
)
6575 cpp_fatal (pfile
, "Macro name missing after -D option");
6579 i
++, push_pending (pfile
, "-D", argv
[i
]);
6586 if (argv
[i
][2] != 0)
6588 else if (i
+ 1 == argc
)
6590 cpp_fatal (pfile
, "Assertion missing after -A option");
6596 if (!strcmp (p
, "-")) {
6597 struct cpp_pending
**ptr
;
6598 /* -A- eliminates all predefined macros and assertions.
6599 Let's include also any that were specified earlier
6600 on the command line. That way we can get rid of any
6601 that were passed automatically in from GCC. */
6603 opts
->inhibit_predefs
= 1;
6604 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6606 struct cpp_pending
*pend
= *ptr
;
6607 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6608 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6617 push_pending (pfile
, "-A", p
);
6622 case 'U': /* JF #undef something */
6623 if (argv
[i
][2] != 0)
6624 push_pending (pfile
, "-U", argv
[i
] + 2);
6625 else if (i
+ 1 == argc
)
6627 cpp_fatal (pfile
, "Macro name missing after -U option", NULL
);
6631 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6635 opts
->put_out_comments
= 1;
6638 case 'E': /* -E comes from cc -E; ignore it. */
6642 opts
->no_line_commands
= 1;
6645 case '$': /* Don't include $ in identifiers. */
6646 opts
->dollars_in_ident
= 0;
6649 case 'I': /* Add directory to path for includes. */
6651 struct file_name_list
*dirtmp
;
6653 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6654 && !strcmp (argv
[i
] + 2, "-")) {
6655 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6656 /* Don't use any preceding -I directories for #include <...>. */
6657 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6660 dirtmp
= (struct file_name_list
*)
6661 xmalloc (sizeof (struct file_name_list
));
6662 dirtmp
->next
= 0; /* New one goes on the end */
6663 dirtmp
->control_macro
= 0;
6664 dirtmp
->c_system_include_path
= 0;
6665 if (argv
[i
][2] != 0)
6666 dirtmp
->fname
= argv
[i
] + 2;
6667 else if (i
+ 1 == argc
)
6668 goto missing_dirname
;
6670 dirtmp
->fname
= argv
[++i
];
6671 dirtmp
->got_name_map
= 0;
6672 append_include_chain (pfile
, dirtmp
, dirtmp
);
6678 if (!strcmp (argv
[i
], "-nostdinc"))
6679 /* -nostdinc causes no default include directories.
6680 You must specify all include-file directories with -I. */
6681 opts
->no_standard_includes
= 1;
6682 else if (!strcmp (argv
[i
], "-nostdinc++"))
6683 /* -nostdinc++ causes no default C++-specific include directories. */
6684 opts
->no_standard_cplusplus_includes
= 1;
6686 else if (!strcmp (argv
[i
], "-noprecomp"))
6692 /* Sun compiler passes undocumented switch "-undef".
6693 Let's assume it means to inhibit the predefined symbols. */
6694 opts
->inhibit_predefs
= 1;
6697 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6698 if (opts
->in_fname
== NULL
) {
6699 opts
->in_fname
= "";
6701 } else if (opts
->out_fname
== NULL
) {
6702 opts
->out_fname
= "";
6704 } /* else fall through into error */
6718 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6720 if (opts
->print_deps
)
6722 /* Stream on which to print the dependency information. */
6725 /* Don't actually write the deps file if compilation has failed. */
6726 if (pfile
->errors
== 0)
6728 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6729 if (opts
->deps_file
== 0)
6730 deps_stream
= stdout
;
6731 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6732 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6733 fputs (pfile
->deps_buffer
, deps_stream
);
6734 putc ('\n', deps_stream
);
6735 if (opts
->deps_file
)
6737 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6738 cpp_fatal (pfile
, "I/O error on output");
6744 /* Free resources used by PFILE.
6745 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6752 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6753 cpp_pop_buffer (pfile
);
6755 if (pfile
->token_buffer
)
6757 free (pfile
->token_buffer
);
6758 pfile
->token_buffer
= NULL
;
6761 if (pfile
->deps_buffer
)
6763 free (pfile
->deps_buffer
);
6764 pfile
->deps_buffer
= NULL
;
6765 pfile
->deps_allocated_size
= 0;
6768 while (pfile
->if_stack
)
6770 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6771 pfile
->if_stack
= temp
->next
;
6775 while (pfile
->dont_repeat_files
)
6777 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6778 pfile
->dont_repeat_files
= temp
->next
;
6783 while (pfile
->all_include_files
)
6785 struct file_name_list
*temp
= pfile
->all_include_files
;
6786 pfile
->all_include_files
= temp
->next
;
6791 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6793 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6796 struct import_file
*next
= imp
->next
;
6801 pfile
->import_hash_table
[i
] = 0;
6804 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6806 while (pfile
->assertion_hashtab
[i
])
6807 delete_assertion (pfile
->assertion_hashtab
[i
]);
6810 cpp_hash_cleanup (pfile
);
6814 do_assert (pfile
, keyword
, buf
, limit
)
6816 struct directive
*keyword
;
6817 U_CHAR
*buf
, *limit
;
6819 long symstart
; /* remember where symbol name starts */
6821 int sym_length
; /* and how long it is */
6822 struct arglist
*tokens
= NULL
;
6824 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6825 && !CPP_BUFFER (pfile
)->system_header_p
)
6826 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6828 cpp_skip_hspace (pfile
);
6829 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6830 parse_name (pfile
, GETC());
6831 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6834 cpp_skip_hspace (pfile
);
6835 if (PEEKC() != '(') {
6836 cpp_error (pfile
, "missing token-sequence in `#assert'");
6842 tokens
= read_token_list (pfile
, &error_flag
);
6846 cpp_error (pfile
, "empty token-sequence in `#assert'");
6849 cpp_skip_hspace (pfile
);
6851 if (c
!= EOF
&& c
!= '\n')
6852 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6853 skip_rest_of_line (pfile
);
6856 /* If this name isn't already an assertion name, make it one.
6857 Error if it was already in use in some other way. */
6860 ASSERTION_HASHNODE
*hp
;
6861 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6862 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6863 struct tokenlist_list
*value
6864 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6866 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6868 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6869 cpp_error (pfile
, "`defined' redefined as assertion");
6870 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6873 /* Add the spec'd token-sequence to the list of such. */
6874 value
->tokens
= tokens
;
6875 value
->next
= hp
->value
;
6878 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6881 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6882 skip_rest_of_line (pfile
);
6887 do_unassert (pfile
, keyword
, buf
, limit
)
6889 struct directive
*keyword
;
6890 U_CHAR
*buf
, *limit
;
6892 long symstart
; /* remember where symbol name starts */
6893 int sym_length
; /* and how long it is */
6896 struct arglist
*tokens
= NULL
;
6897 int tokens_specified
= 0;
6899 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6900 && !CPP_BUFFER (pfile
)->system_header_p
)
6901 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6903 cpp_skip_hspace (pfile
);
6905 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6906 parse_name (pfile
, GETC());
6907 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6910 cpp_skip_hspace (pfile
);
6911 if (PEEKC() == '(') {
6914 tokens
= read_token_list (pfile
, &error_flag
);
6918 cpp_error (pfile
, "empty token list in `#unassert'");
6922 tokens_specified
= 1;
6925 cpp_skip_hspace (pfile
);
6927 if (c
!= EOF
&& c
!= '\n')
6928 cpp_error (pfile
, "junk at end of `#unassert'");
6929 skip_rest_of_line (pfile
);
6932 ASSERTION_HASHNODE
*hp
;
6933 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6934 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6935 struct tokenlist_list
*tail
, *prev
;
6937 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6941 /* If no token list was specified, then eliminate this assertion
6943 if (! tokens_specified
)
6944 delete_assertion (hp
);
6946 /* If a list of tokens was given, then delete any matching list. */
6951 struct tokenlist_list
*next
= tail
->next
;
6952 if (compare_token_lists (tail
->tokens
, tokens
)) {
6956 hp
->value
= tail
->next
;
6957 free_token_list (tail
->tokens
);
6967 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6970 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6971 skip_rest_of_line (pfile
);
6975 /* Test whether there is an assertion named NAME
6976 and optionally whether it has an asserted token list TOKENS.
6977 NAME is not null terminated; its length is SYM_LENGTH.
6978 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6981 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
6985 int tokens_specified
;
6986 struct arglist
*tokens
;
6988 ASSERTION_HASHNODE
*hp
;
6989 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6991 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
6992 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
6994 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
6996 /* It is not an assertion; just return false. */
6999 /* If no token list was specified, then value is 1. */
7000 if (! tokens_specified
)
7004 struct tokenlist_list
*tail
;
7008 /* If a list of tokens was given,
7009 then succeed if the assertion records a matching list. */
7012 if (compare_token_lists (tail
->tokens
, tokens
))
7017 /* Fail if the assertion has no matching list. */
7022 /* Compare two lists of tokens for equality including order of tokens. */
7025 compare_token_lists (l1
, l2
)
7026 struct arglist
*l1
, *l2
;
7029 if (l1
->length
!= l2
->length
)
7031 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7037 /* Succeed if both lists end at the same time. */
7042 reverse_token_list (tokens
)
7043 struct arglist
*tokens
;
7045 register struct arglist
*prev
= 0, *this, *next
;
7046 for (this = tokens
; this; this = next
)
7055 /* Read a space-separated list of tokens ending in a close parenthesis.
7056 Return a list of strings, in the order they were written.
7057 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7059 static struct arglist
*
7060 read_token_list (pfile
, error_flag
)
7064 struct arglist
*token_ptrs
= 0;
7069 FORWARD (1); /* Skip '(' */
7071 /* Loop over the assertion value tokens. */
7074 struct arglist
*temp
;
7075 long name_written
= CPP_WRITTEN (pfile
);
7076 int eofp
= 0; int c
;
7078 cpp_skip_hspace (pfile
);
7082 /* Find the end of the token. */
7085 CPP_PUTC (pfile
, c
);
7093 CPP_PUTC (pfile
, c
);
7095 else if (c
== '"' || c
== '\'')
7098 cpp_get_token (pfile
);
7104 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7105 && c
!= '"' && c
!= '\'')
7107 CPP_PUTC (pfile
, c
);
7110 if (c
!= EOF
) FORWARD(-1);
7113 length
= CPP_WRITTEN (pfile
) - name_written
;
7114 temp
= (struct arglist
*)
7115 xmalloc (sizeof (struct arglist
) + length
+ 1);
7116 temp
->name
= (U_CHAR
*) (temp
+ 1);
7117 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7118 (char *) temp
->name
, length
);
7119 temp
->name
[length
] = 0;
7120 temp
->next
= token_ptrs
;
7122 temp
->length
= length
;
7124 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7126 if (c
== EOF
|| c
== '\n')
7129 "unterminated token sequence following `#' operator");
7134 /* We accumulated the names in reverse order.
7135 Now reverse them to get the proper order. */
7136 return reverse_token_list (token_ptrs
);
7140 free_token_list (tokens
)
7141 struct arglist
*tokens
;
7144 struct arglist
*next
= tokens
->next
;
7145 free (tokens
->name
);
7151 /* Get the file-mode and data size of the file open on FD
7152 and store them in *MODE_POINTER and *SIZE_POINTER. */
7155 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
7158 long int *size_pointer
;
7162 if (fstat (fd
, &sbuf
) < 0) return (-1);
7163 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
7164 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
7168 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7169 retrying if necessary. Return a negative value if an error occurs,
7170 otherwise return the actual number of bytes read,
7171 which must be LEN unless end-of-file was reached. */
7174 safe_read (desc
, ptr
, len
)
7181 int nchars
= read (desc
, ptr
, left
);
7199 xcalloc (number
, size
)
7200 unsigned number
, size
;
7202 register unsigned total
= number
* size
;
7203 register char *ptr
= (char *) xmalloc (total
);
7212 unsigned size
= strlen (input
);
7213 char *output
= xmalloc (size
+ 1);
7214 strcpy (output
, input
);
7218 /* Initialize PMARK to remember the current position of PFILE. */
7221 parse_set_mark (pmark
, pfile
)
7222 struct parse_marker
*pmark
;
7225 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7226 pmark
->next
= pbuf
->marks
;
7227 pbuf
->marks
= pmark
;
7229 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7232 /* Cleanup PMARK - we no longer need it. */
7235 parse_clear_mark (pmark
)
7236 struct parse_marker
*pmark
;
7238 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7239 for (; ; pp
= &(*pp
)->next
) {
7240 if (*pp
== NULL
) abort ();
7241 if (*pp
== pmark
) break;
7246 /* Backup the current position of PFILE to that saved in PMARK. */
7249 parse_goto_mark (pmark
, pfile
)
7250 struct parse_marker
*pmark
;
7253 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7254 if (pbuf
!= pmark
->buf
)
7255 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7256 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7259 /* Reset PMARK to point to the current position of PFILE. (Same
7260 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7263 parse_move_mark (pmark
, pfile
)
7264 struct parse_marker
*pmark
;
7267 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7268 if (pbuf
!= pmark
->buf
)
7269 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7270 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7274 cpp_read_check_assertion (pfile
)
7277 int name_start
= CPP_WRITTEN (pfile
);
7278 int name_length
, name_written
;
7280 FORWARD (1); /* Skip '#' */
7281 cpp_skip_hspace (pfile
);
7282 parse_name (pfile
, GETC ());
7283 name_written
= CPP_WRITTEN (pfile
);
7284 name_length
= name_written
- name_start
;
7285 cpp_skip_hspace (pfile
);
7286 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7289 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7290 result
= check_assertion (pfile
,
7291 pfile
->token_buffer
+ name_start
, name_length
,
7295 result
= check_assertion (pfile
,
7296 pfile
->token_buffer
+ name_start
, name_length
,
7298 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7303 cpp_print_file_and_line (pfile
)
7306 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7311 cpp_buf_line_and_col (ip
, &line
, &col
);
7312 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
7313 line
, pfile
->show_column
? col
: -1);
7318 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7321 char *arg1
, *arg2
, *arg3
;
7323 cpp_print_containing_files (pfile
);
7324 cpp_print_file_and_line (pfile
);
7325 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7328 /* Print error message but don't count it. */
7331 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7334 char *arg1
, *arg2
, *arg3
;
7336 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7339 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7342 cpp_print_containing_files (pfile
);
7343 cpp_print_file_and_line (pfile
);
7344 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7347 /* Print an error message and maybe count it. */
7350 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7353 char *arg1
, *arg2
, *arg3
;
7355 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7356 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7358 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7362 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7366 char *arg1
, *arg2
, *arg3
;
7369 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7371 cpp_print_containing_files (pfile
);
7374 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7376 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7380 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7384 char *arg1
, *arg2
, *arg3
;
7389 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7392 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7395 cpp_print_containing_files (pfile
);
7397 ip
= cpp_file_buffer (pfile
);
7400 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7402 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7406 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7410 char *arg1
, *arg2
, *arg3
;
7412 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7413 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7415 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7418 /* Report a warning (or an error if pedantic_errors)
7419 giving specified file name and line number, not current. */
7422 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7427 char *arg1
, *arg2
, *arg3
;
7429 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7430 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7433 cpp_file_line_for_message (pfile
, file
, line
, -1);
7434 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7435 msg
, arg1
, arg2
, arg3
);
7438 /* This defines "errno" properly for VMS, and gives us EACCES. */
7445 #ifndef HAVE_STRERROR
7446 extern int sys_nerr
;
7448 extern const char *const sys_errlist
[];
7450 extern char *sys_errlist
[];
7452 #else /* HAVE_STRERROR */
7456 char *strerror (int,...);
7459 /* my_strerror - return the descriptive text associated with an
7463 my_strerror (errnum
)
7469 #ifndef HAVE_STRERROR
7470 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7472 result
= strerror (errnum
);
7475 /* VAXCRTL's strerror() takes an optional second argument, which only
7476 matters when the first argument is EVMSERR. However, it's simplest
7477 just to pass it unconditionally. `vaxc$errno' is declared in
7478 <errno.h>, and maintained by the library in parallel with `errno'.
7479 We assume that caller's `errnum' either matches the last setting of
7480 `errno' by the library or else does not have the value `EVMSERR'. */
7482 result
= strerror (errnum
, vaxc$errno
);
7486 result
= "undocumented I/O error";
7491 /* Error including a message from `errno'. */
7494 cpp_error_from_errno (pfile
, name
)
7499 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7501 cpp_print_containing_files (pfile
);
7504 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
7506 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (errno
));
7510 cpp_perror_with_name (pfile
, name
)
7514 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7518 * No pre-compiled header file support.
7520 * Possibly different enum token codes for each C/C++ token.
7522 * Should clean up remaining directives to that do_XXX functions
7523 * only take two arguments and all have command_reads_line.
7525 * Find and cleanup remaining uses of static variables,
7527 * Support for trigraphs.
7529 * Support -dM flag (dump_all_macros).
7531 * Support for_lint flag.