2 /* Definitions for CPP library.
3 Copyright (C) 1995 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994-95.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
25 #include <sys/types.h>
28 #ifndef HOST_BITS_PER_WIDE_INT
30 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
31 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
32 #define HOST_WIDE_INT long
34 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
35 #define HOST_WIDE_INT int
40 #define STATIC_BUFFERS
42 typedef struct cpp_reader cpp_reader;
43 typedef struct cpp_buffer cpp_buffer;
44 typedef struct cpp_options cpp_options;
51 CPP_VSPACE, /* newlines and #line directives */
62 CPP_SEMICOLON, /* ";" */
63 CPP_3DOTS, /* "..." */
64 /* POP_TOKEN is returned when we've popped a cpp_buffer. */
68 typedef enum cpp_token (*parse_underflow_t) (cpp_reader *);
69 typedef int (*parse_cleanup_t) (cpp_buffer *, cpp_reader *);
71 /* A parse_marker indicates a previous position,
72 which we can backtrack to. */
76 struct parse_marker *next;
80 extern int cpp_handle_options(cpp_reader * pfile, int, char **);
81 extern enum cpp_token cpp_get_token(cpp_reader * pfile);
82 extern void cpp_skip_hspace(cpp_reader * pfile);
84 /* Maintain and search list of included files, for #import. */
86 #define IMPORT_HASH_SIZE 31
92 struct import_file *next;
95 /* If we have a huge buffer, may need to cache more recent counts */
96 #define CPP_LINE_BASE(BUF) ((BUF)->buf + (BUF)->line_base)
99 dump_none = 0, dump_only, dump_names, dump_definitions
105 unsigned char *rlimit; /* end of valid data */
106 unsigned char *alimit; /* end of allocated buffer */
107 unsigned char *prev; /* start of current token */
110 /* Filename specified with #line command. */
111 const char *nominal_fname;
113 /* Record where in the search path this file was found.
114 * For #include_next. */
115 struct file_name_list *dir;
118 long lineno; /* Line number at CPP_LINE_BASE. */
119 long colno; /* Column number at CPP_LINE_BASE. */
120 #ifndef STATIC_BUFFERS
123 parse_underflow_t underflow;
124 parse_cleanup_t cleanup;
126 struct parse_marker *marks;
127 /* Value of if_stack at start of this file.
128 * Used to prohibit unmatched #endif (etc) in an include file. */
129 struct if_stack *if_stack;
131 /* True if this is a header file included using <FILENAME>. */
132 char system_header_p;
135 /* True if buffer contains escape sequences.
136 * Currently there are are only two kind:
137 * "@-" means following identifier should not be macro-expanded.
138 * "@ " means a token-separator. This turns into " " in final output
139 * if not stringizing and needed to separate tokens; otherwise nothing.
140 * "@@" means a normal '@'.
141 * (An '@' inside a string stands for itself and is never an escape.) */
145 struct cpp_pending; /* Forward declaration - for C++. */
146 struct file_name_map_list;
148 typedef struct assertion_hashnode ASSERTION_HASHNODE;
150 #define ASSERTION_HASHSIZE 37
152 #ifdef STATIC_BUFFERS
153 /* Maximum nesting of cpp_buffers. We use a static limit, partly for
154 efficiency, and partly to limit runaway recursion. */
155 #define CPP_STACK_MAX 200
159 unsigned char *limit;
160 parse_underflow_t get_token;
162 #ifdef STATIC_BUFFERS
163 cpp_buffer buffer_stack[CPP_STACK_MAX];
166 int errors; /* Error counter for exit code */
169 unsigned char *token_buffer;
170 int token_buffer_size;
172 /* Line where a newline was first seen in a string constant. */
173 int multiline_string_line;
175 /* Current depth in #include directives that use <...>. */
176 int system_include_depth;
178 /* List of included files that contained #pragma once. */
179 struct file_name_list *dont_repeat_files;
181 /* List of other included files.
182 * If ->control_macro if nonzero, the file had a #ifndef
183 * around the entire contents, and ->control_macro gives the macro name. */
184 struct file_name_list *all_include_files;
186 /* Current maximum length of directory names in the search path
187 * for include files. (Altered as we get more of them.) */
190 /* Hash table of files already included with #include or #import. */
191 struct import_file *import_hash_table[IMPORT_HASH_SIZE];
193 struct if_stack *if_stack;
195 /* Nonzero means we are inside an IF during a -pcp run. In this mode
196 * macro expansion is done, and preconditions are output for all macro
197 * uses requiring them. */
200 /* Nonzero means we have printed (while error reporting) a list of
201 * containing files that matches the current status. */
202 char input_stack_listing_current;
204 /* If non-zero, macros are not expanded. */
205 char no_macro_expand;
207 /* Print column number in error messages. */
210 /* We're printed a warning recommending against using #import. */
213 /* If true, character between '<' and '>' are a single (string) token. */
214 char parsing_include_directive;
216 /* True if escape sequences (as described for has_escapes in
217 * parse_buffer) should be emitted. */
220 /* 0: Have seen non-white-space on this line.
221 * 1: Only seen white space so far on this line.
222 * 2: Only seen white space so far in this file. */
223 char only_seen_white;
225 /* Nonzero means this file was included with a -imacros or -include
226 * command line and should not be recorded as an include file. */
234 ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
236 /* Buffer of -M output. */
239 /* Number of bytes allocated in above. */
240 int deps_allocated_size;
242 /* Number of bytes used. */
245 /* Number of bytes since the last newline. */
249 #define CPP_BUF_PEEK(BUFFER) \
250 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur : EOF)
251 #define CPP_BUF_GET(BUFFER) \
252 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
253 #define CPP_FORWARD(BUFFER, N) ((BUFFER)->cur += (N))
255 /* Number of characters currently in PFILE's output buffer. */
256 #define CPP_WRITTEN(PFILE) ((PFILE)->limit - (PFILE)->token_buffer)
257 #define CPP_PWRITTEN(PFILE) ((PFILE)->limit)
259 /* Make sure PFILE->token_buffer has space for at least N more characters. */
260 #define CPP_RESERVE(PFILE, N) \
261 ((unsigned int)(CPP_WRITTEN (PFILE) + N) > (unsigned int) (PFILE)->token_buffer_size \
262 && (cpp_grow_buffer (PFILE, N), 0))
264 /* Append string STR (of length N) to PFILE's output buffer.
265 Assume there is enough space. */
266 #define CPP_PUTS_Q(PFILE, STR, N) \
267 (memcpy ((PFILE)->limit, STR, (N)), (PFILE)->limit += (N))
268 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
269 #define CPP_PUTS(PFILE, STR, N) CPP_RESERVE(PFILE, N), CPP_PUTS_Q(PFILE, STR,N)
270 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
271 #define CPP_PUTC_Q(PFILE, CH) (*(PFILE)->limit++ = (CH))
272 /* Append character CH to PFILE's output buffer. Make space if need be. */
273 #define CPP_PUTC(PFILE, CH) (CPP_RESERVE (PFILE, 1), CPP_PUTC_Q (PFILE, CH))
274 /* Make sure PFILE->limit is followed by '\0'. */
275 #define CPP_NUL_TERMINATE_Q(PFILE) (*(PFILE)->limit = 0)
276 #define CPP_NUL_TERMINATE(PFILE) (CPP_RESERVE(PFILE, 1), *(PFILE)->limit = 0)
277 #define CPP_ADJUST_WRITTEN(PFILE,DELTA) ((PFILE)->limit += (DELTA))
278 #define CPP_SET_WRITTEN(PFILE,N) ((PFILE)->limit = (PFILE)->token_buffer + (N))
280 #define CPP_OPTIONS(PFILE) ((cpp_options*)(PFILE)->data)
281 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
282 #ifdef STATIC_BUFFERS
283 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)+1)
284 #define CPP_NULL_BUFFER(PFILE) (&(PFILE)->buffer_stack[CPP_STACK_MAX])
286 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)->chain)
287 #define CPP_NULL_BUFFER(PFILE) ((cpp_buffer*)0)
290 /* Pointed to by parse_file::data. */
292 const char *in_fname;
294 /* Name of output file, for error messages. */
295 const char *out_fname;
297 struct file_name_map_list *map_list;
299 /* Non-0 means -v, so print the full set of include dirs. */
302 /* Nonzero means use extra default include directories for C++. */
306 /* Nonzero means handle cplusplus style comments */
308 char cplusplus_comments;
310 /* Nonzero means handle #import, for objective C. */
314 /* Nonzero means this is an assembly file, and allow
315 * unknown directives, which could be comments. */
319 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
323 /* Nonzero means handle CHILL comment syntax
324 * and output CHILL string delimiter for __DATE___ etc. */
328 /* Nonzero means copy comments into the output file. */
330 char put_out_comments;
332 /* Nonzero means don't process the ANSI trigraph sequences. */
336 /* Nonzero means print the names of included files rather than
337 * the preprocessed output. 1 means just the #include "...",
338 * 2 means #include <...> as well. */
342 /* Nonzero if missing .h files in -M output are assumed to be generated
343 * files and not errors. */
345 char print_deps_missing_files;
347 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
348 char print_deps_append;
350 /* Nonzero means print names of header files (-H). */
352 char print_include_names;
354 /* Nonzero means try to make failure to fit ANSI C an error. */
356 char pedantic_errors;
358 /* Nonzero means don't print warning messages. -w. */
360 char inhibit_warnings;
362 /* Nonzero means warn if slash-star appears in a comment. */
366 /* Nonzero means warn if there are any trigraphs. */
370 /* Nonzero means warn if #import is used. */
374 /* Nonzero means warn if a macro argument is (or would be)
375 * stringified with -traditional. */
379 /* Nonzero means turn warnings into errors. */
381 char warnings_are_errors;
383 /* Nonzero causes output not to be done,
384 * but directives such as #define that have side effects
385 * are still obeyed. */
389 /* Nonzero means don't output line number information. */
391 char no_line_commands;
393 /* Nonzero means output the text in failing conditionals,
394 inside #failed ... #endfailed. */
396 char output_conditionals;
398 /* Nonzero means -I- has been seen,
399 * so don't look for #include "foo" the source-file directory. */
402 /* Zero means dollar signs are punctuation.
403 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
404 This must be 0 for correct processing of this ANSI C program:
406 #define lose(b) foo (b)
409 char dollars_in_ident;
410 #ifndef DOLLARS_IN_IDENTIFIERS
411 #define DOLLARS_IN_IDENTIFIERS 1
414 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
417 /* Nonzero means give all the error messages the ANSI standard requires. */
420 char done_initializing;
422 struct file_name_list *include; /* First dir to search */
423 /* First dir to search for <file> */
424 /* This is the first element to use for #include <...>.
425 * If it is 0, use the entire chain for such includes. */
426 struct file_name_list *first_bracket_include;
427 /* This is the first element in the chain that corresponds to
428 * a directory of system header files. */
429 struct file_name_list *first_system_include;
430 struct file_name_list *last_include; /* Last in chain */
432 /* Chain of include directories to put at the end of the other chain. */
433 struct file_name_list *after_include;
434 struct file_name_list *last_after_include; /* Last in chain */
436 /* Chain to put at the start of the system include files. */
437 struct file_name_list *before_system;
438 struct file_name_list *last_before_system; /* Last in chain */
440 /* Directory prefix that should replace `/usr' in the standard
441 * include file directories. */
442 char *include_prefix;
444 char inhibit_predefs;
445 char no_standard_includes;
446 char no_standard_cplusplus_includes;
448 /* dump_only means inhibit output of the preprocessed text
449 and instead output the definitions of all user-defined
450 macros in a form suitable for use as input to cccp.
451 dump_names means pass #define and the macro name through to output.
452 dump_definitions means pass the whole definition (plus #define) through
455 enum dump_type dump_macros;
457 /* Nonzero means pass all #define and #undef directives which we actually
458 process through to the output stream. This feature is used primarily
459 to allow cc1 to record the #defines and #undefs for the sake of
460 debuggers which understand about preprocessor macros, but it may
461 also be useful with -E to figure out how symbols are defined, and
462 where they are defined. */
465 /* Pending -D, -U and -A options, in reverse order. */
466 struct cpp_pending *pending;
468 /* File name which deps are being written to.
469 * This is 0 if deps are being written to stdout. */
472 /* Target-name to write with the dependency information. */
476 #define CPP_TRADITIONAL(PFILE) (CPP_OPTIONS(PFILE)-> traditional)
477 #define CPP_PEDANTIC(PFILE) (CPP_OPTIONS (PFILE)->pedantic)
478 #define CPP_PRINT_DEPS(PFILE) (CPP_OPTIONS (PFILE)->print_deps)
480 /* Name under which this program was invoked. */
482 extern char *progname;
484 /* The structure of a node in the hash table. The hash table
485 has entries for all tokens defined by #define commands (type T_MACRO),
486 plus some special tokens like __LINE__ (these each have their own
487 type, and the appropriate code is run when that type of node is seen.
488 It does not contain control words like "#define", which are recognized
489 by a separate piece of code. */
491 /* different flavors of hash nodes --- also used in keyword table */
493 T_DEFINE = 1, /* the `#define' keyword */
494 T_INCLUDE, /* the `#include' keyword */
495 T_INCLUDE_NEXT, /* the `#include_next' keyword */
496 T_IMPORT, /* the `#import' keyword */
497 T_IFDEF, /* the `#ifdef' keyword */
498 T_IFNDEF, /* the `#ifndef' keyword */
499 T_IF, /* the `#if' keyword */
500 T_ELSE, /* `#else' */
501 T_PRAGMA, /* `#pragma' */
502 T_ELIF, /* `#elif' */
503 T_UNDEF, /* `#undef' */
504 T_LINE, /* `#line' */
505 T_ERROR, /* `#error' */
506 T_WARNING, /* `#warning' */
507 T_ENDIF, /* `#endif' */
508 T_SCCS, /* `#sccs', used on system V. */
509 T_IDENT, /* `#ident', used on system V. */
510 T_ASSERT, /* `#assert', taken from system V. */
511 T_UNASSERT, /* `#unassert', taken from system V. */
512 T_SPECLINE, /* special symbol `__LINE__' */
513 T_DATE, /* `__DATE__' */
514 T_FILE, /* `__FILE__' */
515 T_BASE_FILE, /* `__BASE_FILE__' */
516 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
517 T_VERSION, /* `__VERSION__' */
518 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
519 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
520 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
521 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
522 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
523 T_TIME, /* `__TIME__' */
524 T_CONST, /* Constant value, used by `__STDC__' */
525 T_MACRO, /* macro defined by `#define' */
526 T_DISABLED, /* macro temporarily turned off for rescan */
527 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
528 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
529 T_UNUSED /* Used for something not defined. */
532 /* Structure allocated for every #define. For a simple replacement
535 nargs = -1, the `pattern' list is null, and the expansion is just
536 the replacement text. Nargs = 0 means a functionlike macro with no args,
538 #define getchar() getc (stdin) .
539 When there are args, the expansion is the replacement text with the
540 args squashed out, and the reflist is a list describing how to
541 build the output from the input: e.g., "3 chars, then the 1st arg,
542 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
543 The chars here come from the expansion. Whatever is left of the
544 expansion after the last arg-occurrence is copied after that arg.
545 Note that the reflist can be arbitrarily long---
546 its length depends on the number of times the arguments appear in
547 the replacement text, not how many args there are. Example:
548 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
550 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
551 where (x, y) means (nchars, argno). */
554 struct reflist *next;
555 char stringify; /* nonzero if this arg was preceded by a
557 char raw_before; /* Nonzero if a ## operator before arg. */
558 char raw_after; /* Nonzero if a ## operator after arg. */
559 char rest_args; /* Nonzero if this arg. absorbs the rest */
560 int nchars; /* Number of literal chars to copy before
561 * this arg occurrence. */
562 int argno; /* Number of arg to substitute (origin-0) */
565 typedef struct definition DEFINITION;
568 int length; /* length of expansion string */
569 int predefined; /* True if the macro was builtin or */
570 /* came from the command line */
571 unsigned char *expansion;
572 int line; /* Line number of definition */
573 const char *file; /* File of definition */
574 char rest_args; /* Nonzero if last arg. absorbs the rest */
575 struct reflist *pattern;
577 /* Names of macro args, concatenated in reverse order
578 * with comma-space between them.
579 * The only use of this is that we warn on redefinition
580 * if this differs between the old and new definitions. */
581 unsigned char *argnames;
585 extern unsigned char is_idchar[256];
587 /* Stack of conditionals currently in progress
588 (including both successful and failing conditionals). */
591 struct if_stack *next; /* for chaining to the next stack frame */
592 const char *fname; /* copied from input when frame is made */
593 int lineno; /* similarly */
594 int if_succeeded; /* true if a leg of this if-group
595 * has been passed through rescan */
596 unsigned char *control_macro; /* For #ifndef at start of file,
597 * this is the macro name tested. */
598 enum node_type type; /* type of last directive seen in this group */
600 typedef struct if_stack IF_STACK_FRAME;
602 extern void cpp_buf_line_and_col(cpp_buffer *, long *, long *);
603 extern cpp_buffer *cpp_file_buffer(cpp_reader *);
604 extern void cpp_define(cpp_reader *, unsigned char *);
606 extern void cpp_error(cpp_reader * pfile, const char *msg, ...);
607 extern void cpp_warning(cpp_reader * pfile, const char *msg, ...);
608 extern void cpp_pedwarn(cpp_reader * pfile, const char *msg, ...);
609 extern void cpp_fatal(const char *msg, ...);
610 extern void cpp_file_line_for_message(cpp_reader * pfile,
611 const char *filename, int line,
613 extern void cpp_perror_with_name(cpp_reader * pfile, const char *name);
614 extern void cpp_pfatal_with_name(cpp_reader * pfile, const char *name);
615 extern void cpp_message(cpp_reader * pfile, int is_error,
616 const char *msg, ...);
617 extern void cpp_message_v(cpp_reader * pfile, int is_error,
618 const char *msg, va_list args);
620 extern void cpp_grow_buffer(cpp_reader * pfile, long n);
621 extern int cpp_parse_escape(cpp_reader * pfile, char **string_ptr);
623 void cpp_print_containing_files(cpp_reader * pfile);
624 HOST_WIDE_INT cpp_parse_expr(cpp_reader * pfile);
625 void skip_rest_of_line(cpp_reader * pfile);
626 void init_parse_file(cpp_reader * pfile);
627 void init_parse_options(struct cpp_options *opts);
628 int push_parse_file(cpp_reader * pfile, const char *fname);
629 void cpp_finish(cpp_reader * pfile);
630 int cpp_read_check_assertion(cpp_reader * pfile);
632 void *xmalloc(unsigned size);
633 void *xrealloc(void *old, unsigned size);
634 void *xcalloc(unsigned number, unsigned size);
637 #define PATH_SEPARATOR ';'