1 /* Definitions for CPP library.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
24 #include <sys/types.h>
27 #ifndef HOST_BITS_PER_WIDE_INT
29 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
30 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
31 #define HOST_WIDE_INT long
33 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
34 #define HOST_WIDE_INT int
39 #define STATIC_BUFFERS
41 typedef struct cpp_reader cpp_reader;
42 typedef struct cpp_buffer cpp_buffer;
43 typedef struct cpp_options cpp_options;
50 CPP_VSPACE, /* newlines and #line directives */
61 CPP_SEMICOLON, /* ";" */
62 CPP_3DOTS, /* "..." */
63 /* POP_TOKEN is returned when we've popped a cpp_buffer. */
67 typedef enum cpp_token (*parse_underflow_t) (cpp_reader *);
68 typedef int (*parse_cleanup_t) (cpp_buffer *, cpp_reader *);
70 /* A parse_marker indicates a previous position,
71 which we can backtrack to. */
75 struct parse_marker *next;
79 extern int cpp_handle_options(cpp_reader * pfile, int, char **);
80 extern enum cpp_token cpp_get_token(cpp_reader * pfile);
81 extern void cpp_skip_hspace(cpp_reader * pfile);
83 /* Maintain and search list of included files, for #import. */
85 #define IMPORT_HASH_SIZE 31
91 struct import_file *next;
94 /* If we have a huge buffer, may need to cache more recent counts */
95 #define CPP_LINE_BASE(BUF) ((BUF)->buf + (BUF)->line_base)
98 dump_none = 0, dump_only, dump_names, dump_definitions
104 unsigned char *rlimit; /* end of valid data */
105 unsigned char *alimit; /* end of allocated buffer */
106 unsigned char *prev; /* start of current token */
109 /* Filename specified with #line command. */
110 const char *nominal_fname;
112 /* Record where in the search path this file was found.
113 * For #include_next. */
114 struct file_name_list *dir;
117 long lineno; /* Line number at CPP_LINE_BASE. */
118 long colno; /* Column number at CPP_LINE_BASE. */
119 #ifndef STATIC_BUFFERS
122 parse_underflow_t underflow;
123 parse_cleanup_t cleanup;
125 struct parse_marker *marks;
126 /* Value of if_stack at start of this file.
127 * Used to prohibit unmatched #endif (etc) in an include file. */
128 struct if_stack *if_stack;
130 /* True if this is a header file included using <FILENAME>. */
131 char system_header_p;
134 /* True if buffer contains escape sequences.
135 * Currently there are are only two kind:
136 * "@-" means following identifier should not be macro-expanded.
137 * "@ " means a token-separator. This turns into " " in final output
138 * if not stringizing and needed to separate tokens; otherwise nothing.
139 * "@@" means a normal '@'.
140 * (An '@' inside a string stands for itself and is never an escape.) */
144 struct cpp_pending; /* Forward declaration - for C++. */
145 struct file_name_map_list;
147 typedef struct assertion_hashnode ASSERTION_HASHNODE;
149 #define ASSERTION_HASHSIZE 37
151 #ifdef STATIC_BUFFERS
152 /* Maximum nesting of cpp_buffers. We use a static limit, partly for
153 efficiency, and partly to limit runaway recursion. */
154 #define CPP_STACK_MAX 200
158 unsigned char *limit;
159 parse_underflow_t get_token;
161 #ifdef STATIC_BUFFERS
162 cpp_buffer buffer_stack[CPP_STACK_MAX];
165 int errors; /* Error counter for exit code */
168 unsigned char *token_buffer;
169 int token_buffer_size;
171 /* Line where a newline was first seen in a string constant. */
172 int multiline_string_line;
174 /* Current depth in #include directives that use <...>. */
175 int system_include_depth;
177 /* List of included files that contained #pragma once. */
178 struct file_name_list *dont_repeat_files;
180 /* List of other included files.
181 * If ->control_macro if nonzero, the file had a #ifndef
182 * around the entire contents, and ->control_macro gives the macro name. */
183 struct file_name_list *all_include_files;
185 /* Current maximum length of directory names in the search path
186 * for include files. (Altered as we get more of them.) */
189 /* Hash table of files already included with #include or #import. */
190 struct import_file *import_hash_table[IMPORT_HASH_SIZE];
192 struct if_stack *if_stack;
194 /* Nonzero means we are inside an IF during a -pcp run. In this mode
195 * macro expansion is done, and preconditions are output for all macro
196 * uses requiring them. */
199 /* Nonzero means we have printed (while error reporting) a list of
200 * containing files that matches the current status. */
201 char input_stack_listing_current;
203 /* If non-zero, macros are not expanded. */
204 char no_macro_expand;
206 /* Print column number in error messages. */
209 /* We're printed a warning recommending against using #import. */
212 /* If true, character between '<' and '>' are a single (string) token. */
213 char parsing_include_directive;
215 /* True if escape sequences (as described for has_escapes in
216 * parse_buffer) should be emitted. */
219 /* 0: Have seen non-white-space on this line.
220 * 1: Only seen white space so far on this line.
221 * 2: Only seen white space so far in this file. */
222 char only_seen_white;
224 /* Nonzero means this file was included with a -imacros or -include
225 * command line and should not be recorded as an include file. */
233 ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
235 /* Buffer of -M output. */
238 /* Number of bytes allocated in above. */
239 int deps_allocated_size;
241 /* Number of bytes used. */
244 /* Number of bytes since the last newline. */
248 #define CPP_BUF_PEEK(BUFFER) \
249 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur : EOF)
250 #define CPP_BUF_GET(BUFFER) \
251 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
252 #define CPP_FORWARD(BUFFER, N) ((BUFFER)->cur += (N))
254 /* Number of characters currently in PFILE's output buffer. */
255 #define CPP_WRITTEN(PFILE) ((PFILE)->limit - (PFILE)->token_buffer)
256 #define CPP_PWRITTEN(PFILE) ((PFILE)->limit)
258 /* Make sure PFILE->token_buffer has space for at least N more characters. */
259 #define CPP_RESERVE(PFILE, N) \
260 ((unsigned int)(CPP_WRITTEN (PFILE) + N) > (unsigned int) (PFILE)->token_buffer_size \
261 && (cpp_grow_buffer (PFILE, N), 0))
263 /* Append string STR (of length N) to PFILE's output buffer.
264 Assume there is enough space. */
265 #define CPP_PUTS_Q(PFILE, STR, N) \
266 do { memcpy ((PFILE)->limit, STR, (N)); (PFILE)->limit += (N); } while(0)
267 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
268 #define CPP_PUTS(PFILE, STR, N) \
269 do { CPP_RESERVE(PFILE, N); CPP_PUTS_Q(PFILE, STR,N); } while(0)
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) \
274 do { CPP_RESERVE (PFILE, 1); CPP_PUTC_Q (PFILE, CH); } while(0)
275 /* Make sure PFILE->limit is followed by '\0'. */
276 #define CPP_NUL_TERMINATE_Q(PFILE) (*(PFILE)->limit = 0)
277 #define CPP_NUL_TERMINATE(PFILE) \
278 do { CPP_RESERVE(PFILE, 1); *(PFILE)->limit = 0; } while(0)
279 #define CPP_ADJUST_WRITTEN(PFILE,DELTA) ((PFILE)->limit += (DELTA))
280 #define CPP_SET_WRITTEN(PFILE,N) ((PFILE)->limit = (PFILE)->token_buffer + (N))
282 #define CPP_OPTIONS(PFILE) ((cpp_options*)(PFILE)->data)
283 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
284 #ifdef STATIC_BUFFERS
285 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)+1)
286 #define CPP_NULL_BUFFER(PFILE) (&(PFILE)->buffer_stack[CPP_STACK_MAX])
288 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)->chain)
289 #define CPP_NULL_BUFFER(PFILE) ((cpp_buffer*)0)
292 /* Pointed to by parse_file::data. */
294 const char *in_fname;
296 /* Name of output file, for error messages. */
297 const char *out_fname;
299 struct file_name_map_list *map_list;
301 /* Non-0 means -v, so print the full set of include dirs. */
304 /* Nonzero means use extra default include directories for C++. */
308 /* Nonzero means handle cplusplus style comments */
310 char cplusplus_comments;
312 /* Nonzero means handle #import, for objective C. */
316 /* Nonzero means this is an assembly file, and allow
317 * unknown directives, which could be comments. */
321 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
325 /* Nonzero means handle CHILL comment syntax
326 * and output CHILL string delimiter for __DATE___ etc. */
330 /* Nonzero means copy comments into the output file. */
332 char put_out_comments;
334 /* Nonzero means don't process the ANSI trigraph sequences. */
338 /* Nonzero means print the names of included files rather than
339 * the preprocessed output. 1 means just the #include "...",
340 * 2 means #include <...> as well. */
344 /* Nonzero if missing .h files in -M output are assumed to be generated
345 * files and not errors. */
347 char print_deps_missing_files;
349 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
350 char print_deps_append;
352 /* Nonzero means print names of header files (-H). */
354 char print_include_names;
356 /* Nonzero means try to make failure to fit ANSI C an error. */
358 char pedantic_errors;
360 /* Nonzero means don't print warning messages. -w. */
362 char inhibit_warnings;
364 /* Nonzero means warn if slash-star appears in a comment. */
368 /* Nonzero means warn if there are any trigraphs. */
372 /* Nonzero means warn if #import is used. */
376 /* Nonzero means warn if a macro argument is (or would be)
377 * stringified with -traditional. */
381 /* Nonzero means turn warnings into errors. */
383 char warnings_are_errors;
385 /* Nonzero causes output not to be done,
386 * but directives such as #define that have side effects
387 * are still obeyed. */
391 /* Nonzero means don't output line number information. */
393 char no_line_commands;
395 /* Nonzero means output the text in failing conditionals,
396 inside #failed ... #endfailed. */
398 char output_conditionals;
400 /* Nonzero means -I- has been seen,
401 * so don't look for #include "foo" the source-file directory. */
404 /* Zero means dollar signs are punctuation.
405 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
406 This must be 0 for correct processing of this ANSI C program:
408 #define lose(b) foo (b)
411 char dollars_in_ident;
412 #ifndef DOLLARS_IN_IDENTIFIERS
413 #define DOLLARS_IN_IDENTIFIERS 1
416 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
419 /* Nonzero means give all the error messages the ANSI standard requires. */
422 char done_initializing;
424 struct file_name_list *include; /* First dir to search */
425 /* First dir to search for <file> */
426 /* This is the first element to use for #include <...>.
427 * If it is 0, use the entire chain for such includes. */
428 struct file_name_list *first_bracket_include;
429 /* This is the first element in the chain that corresponds to
430 * a directory of system header files. */
431 struct file_name_list *first_system_include;
432 struct file_name_list *last_include; /* Last in chain */
434 /* Chain of include directories to put at the end of the other chain. */
435 struct file_name_list *after_include;
436 struct file_name_list *last_after_include; /* Last in chain */
438 /* Chain to put at the start of the system include files. */
439 struct file_name_list *before_system;
440 struct file_name_list *last_before_system; /* Last in chain */
442 /* Directory prefix that should replace `/usr' in the standard
443 * include file directories. */
444 char *include_prefix;
446 char inhibit_predefs;
447 char no_standard_includes;
448 char no_standard_cplusplus_includes;
450 /* dump_only means inhibit output of the preprocessed text
451 and instead output the definitions of all user-defined
452 macros in a form suitable for use as input to cccp.
453 dump_names means pass #define and the macro name through to output.
454 dump_definitions means pass the whole definition (plus #define) through
457 enum dump_type dump_macros;
459 /* Nonzero means pass all #define and #undef directives which we actually
460 process through to the output stream. This feature is used primarily
461 to allow cc1 to record the #defines and #undefs for the sake of
462 debuggers which understand about preprocessor macros, but it may
463 also be useful with -E to figure out how symbols are defined, and
464 where they are defined. */
467 /* Pending -D, -U and -A options, in reverse order. */
468 struct cpp_pending *pending;
470 /* File name which deps are being written to.
471 * This is 0 if deps are being written to stdout. */
474 /* Target-name to write with the dependency information. */
477 /* Target file to write all include file */
478 const char *watchfile;
481 #define CPP_TRADITIONAL(PFILE) (CPP_OPTIONS(PFILE)-> traditional)
482 #define CPP_PEDANTIC(PFILE) (CPP_OPTIONS (PFILE)->pedantic)
483 #define CPP_PRINT_DEPS(PFILE) (CPP_OPTIONS (PFILE)->print_deps)
485 /* Name under which this program was invoked. */
487 extern char *progname;
489 /* The structure of a node in the hash table. The hash table
490 has entries for all tokens defined by #define commands (type T_MACRO),
491 plus some special tokens like __LINE__ (these each have their own
492 type, and the appropriate code is run when that type of node is seen.
493 It does not contain control words like "#define", which are recognized
494 by a separate piece of code. */
496 /* different flavors of hash nodes --- also used in keyword table */
498 T_DEFINE = 1, /* the `#define' keyword */
499 T_INCLUDE, /* the `#include' keyword */
500 T_INCLUDE_NEXT, /* the `#include_next' keyword */
501 T_IMPORT, /* the `#import' keyword */
502 T_IFDEF, /* the `#ifdef' keyword */
503 T_IFNDEF, /* the `#ifndef' keyword */
504 T_IF, /* the `#if' keyword */
505 T_ELSE, /* `#else' */
506 T_PRAGMA, /* `#pragma' */
507 T_ELIF, /* `#elif' */
508 T_UNDEF, /* `#undef' */
509 T_LINE, /* `#line' */
510 T_ERROR, /* `#error' */
511 T_WARNING, /* `#warning' */
512 T_ENDIF, /* `#endif' */
513 T_SCCS, /* `#sccs', used on system V. */
514 T_IDENT, /* `#ident', used on system V. */
515 T_ASSERT, /* `#assert', taken from system V. */
516 T_UNASSERT, /* `#unassert', taken from system V. */
517 T_SPECLINE, /* special symbol `__LINE__' */
518 T_DATE, /* `__DATE__' */
519 T_FILE, /* `__FILE__' */
520 T_BASE_FILE, /* `__BASE_FILE__' */
521 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
522 T_VERSION, /* `__VERSION__' */
523 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
524 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
525 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
526 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
527 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
528 T_TIME, /* `__TIME__' */
529 T_CONST, /* Constant value, used by `__STDC__' */
530 T_MACRO, /* macro defined by `#define' */
531 T_DISABLED, /* macro temporarily turned off for rescan */
532 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
533 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
534 T_UNUSED /* Used for something not defined. */
537 /* Structure allocated for every #define. For a simple replacement
540 nargs = -1, the `pattern' list is null, and the expansion is just
541 the replacement text. Nargs = 0 means a functionlike macro with no args,
543 #define getchar() getc (stdin) .
544 When there are args, the expansion is the replacement text with the
545 args squashed out, and the reflist is a list describing how to
546 build the output from the input: e.g., "3 chars, then the 1st arg,
547 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
548 The chars here come from the expansion. Whatever is left of the
549 expansion after the last arg-occurrence is copied after that arg.
550 Note that the reflist can be arbitrarily long---
551 its length depends on the number of times the arguments appear in
552 the replacement text, not how many args there are. Example:
553 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
555 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
556 where (x, y) means (nchars, argno). */
558 typedef struct reflist reflist;
561 char stringify; /* nonzero if this arg was preceded by a
563 char raw_before; /* Nonzero if a ## operator before arg. */
564 char raw_after; /* Nonzero if a ## operator after arg. */
565 char rest_args; /* Nonzero if this arg. absorbs the rest */
566 int nchars; /* Number of literal chars to copy before
567 * this arg occurrence. */
568 int argno; /* Number of arg to substitute (origin-0) */
571 typedef struct definition DEFINITION;
574 int length; /* length of expansion string */
575 int predefined; /* True if the macro was builtin or */
576 /* came from the command line */
577 unsigned char *expansion;
578 int line; /* Line number of definition */
579 const char *file; /* File of definition */
580 char rest_args; /* Nonzero if last arg. absorbs the rest */
583 /* Names of macro args, concatenated in reverse order
584 * with comma-space between them.
585 * The only use of this is that we warn on redefinition
586 * if this differs between the old and new definitions. */
587 unsigned char *argnames;
591 extern unsigned char is_idchar[256];
593 /* Stack of conditionals currently in progress
594 (including both successful and failing conditionals). */
597 struct if_stack *next; /* for chaining to the next stack frame */
598 const char *fname; /* copied from input when frame is made */
599 int lineno; /* similarly */
600 int if_succeeded; /* true if a leg of this if-group
601 * has been passed through rescan */
602 unsigned char *control_macro; /* For #ifndef at start of file,
603 * this is the macro name tested. */
604 enum node_type type; /* type of last directive seen in this group */
606 typedef struct if_stack IF_STACK_FRAME;
608 extern void cpp_buf_line_and_col(cpp_buffer *, long *, long *);
609 extern cpp_buffer *cpp_file_buffer(cpp_reader *);
610 extern void cpp_define(cpp_reader *, unsigned char *);
612 extern void cpp_error(cpp_reader * pfile, const char *msg, ...);
613 extern void cpp_warning(cpp_reader * pfile, const char *msg, ...);
614 extern void cpp_pedwarn(cpp_reader * pfile, const char *msg, ...);
615 extern void cpp_fatal(const char *msg, ...);
616 extern void cpp_file_line_for_message(cpp_reader * pfile,
617 const char *filename, int line,
619 extern void cpp_perror_with_name(cpp_reader * pfile, const char *name);
620 extern void cpp_pfatal_with_name(cpp_reader * pfile, const char *name);
621 extern void cpp_message(cpp_reader * pfile, int is_error,
622 const char *msg, ...);
623 extern void cpp_message_v(cpp_reader * pfile, int is_error,
624 const char *msg, va_list args);
626 extern void cpp_grow_buffer(cpp_reader * pfile, long n);
627 extern int cpp_parse_escape(cpp_reader * pfile, char **string_ptr);
629 void cpp_print_containing_files(cpp_reader * pfile);
630 HOST_WIDE_INT cpp_parse_expr(cpp_reader * pfile);
631 void skip_rest_of_line(cpp_reader * pfile);
632 void init_parse_file(cpp_reader * pfile);
633 void init_parse_options(struct cpp_options *opts);
634 int push_parse_file(cpp_reader * pfile, const char *fname);
635 void cpp_finish(cpp_reader * pfile);
636 int cpp_read_check_assertion(cpp_reader * pfile);
638 void *xmalloc(unsigned size);
639 void *xrealloc(void *old, unsigned size);
640 void *xcalloc(unsigned number, unsigned size);
642 void using_file(const char *filename);
645 #define PATH_SEPARATOR ';'