1 /* Definitions for CPP library.
2 Copyright (C) 1995, 1996, 1997 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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! */
23 #include <sys/types.h>
30 typedef unsigned char U_CHAR;
32 typedef struct cpp_reader cpp_reader;
33 typedef struct cpp_buffer cpp_buffer;
34 typedef struct cpp_options cpp_options;
35 typedef struct hashnode cpp_hashnode;
42 CPP_VSPACE, /* newlines and #line directives */
53 CPP_SEMICOLON,/* ";" */
54 CPP_3DOTS, /* "..." */
56 CPP_ANDAND, /* "&&" */
65 CPP_MINMIN, /* "--" */
67 /* POP_TOKEN is returned when we've popped a cpp_buffer. */
79 typedef enum cpp_token (*parse_underflow_t) PARAMS((cpp_reader *));
80 typedef int (*parse_cleanup_t) PARAMS((cpp_buffer *, cpp_reader *));
82 /* A parse_marker indicates a previous position,
83 which we can backtrack to. */
87 struct parse_marker *next;
91 extern void parse_set_mark PARAMS ((struct parse_marker *, cpp_reader *));
92 extern void parse_clear_mark PARAMS ((struct parse_marker *));
93 extern void parse_goto_mark PARAMS((struct parse_marker *, cpp_reader *));
94 extern void parse_move_mark PARAMS((struct parse_marker *, cpp_reader *));
96 extern int cpp_handle_options PARAMS ((cpp_reader *, int, char **));
97 extern enum cpp_token cpp_get_token PARAMS ((struct parse_marker *));
98 extern void cpp_skip_hspace PARAMS((cpp_reader *));
99 extern enum cpp_token cpp_get_non_space_token PARAMS ((cpp_reader *));
101 /* This frees resources used by PFILE. */
102 extern void cpp_cleanup PARAMS ((cpp_reader *PFILE));
104 /* Maintain and search list of included files, for #import. */
106 #define IMPORT_HASH_SIZE 31
112 struct import_file *next;
115 /* If we have a huge buffer, may need to cache more recent counts */
116 #define CPP_LINE_BASE(BUF) ((BUF)->buf + (BUF)->line_base)
121 unsigned char *rlimit; /* end of valid data */
122 unsigned char *alimit; /* end of allocated buffer */
123 unsigned char *prev; /* start of current token */
126 /* Filename specified with #line command. */
129 /* Record where in the search path this file was found.
130 For #include_next. */
131 struct file_name_list *dir;
134 long lineno; /* Line number at CPP_LINE_BASE. */
135 long colno; /* Column number at CPP_LINE_BASE. */
136 parse_underflow_t underflow;
137 parse_cleanup_t cleanup;
139 struct parse_marker *marks;
140 /* Value of if_stack at start of this file.
141 Used to prohibit unmatched #endif (etc) in an include file. */
142 struct if_stack *if_stack;
144 /* True if this is a header file included using <FILENAME>. */
145 char system_header_p;
148 /* True if buffer contains escape sequences.
149 Currently there are three kinds:
150 "@-" means following identifier should not be macro-expanded.
151 "@ " means a token-separator. This turns into " " in final output
152 if not stringizing and needed to separate tokens; otherwise nothing.
153 "@@" means a normal '@'.
154 (An '@' inside a string stands for itself and is never an escape.) */
158 struct cpp_pending; /* Forward declaration - for C++. */
159 struct file_name_map_list;
161 typedef struct assertion_hashnode ASSERTION_HASHNODE;
162 #define ASSERTION_HASHSIZE 37
164 /* Maximum nesting of cpp_buffers. We use a static limit, partly for
165 efficiency, and partly to limit runaway recursion. */
166 #define CPP_STACK_MAX 200
168 /* A cpp_reader encapsulates the "state" of a pre-processor run.
169 Applying cpp_get_token repeatedly yields a stream of pre-processor
170 tokens. Usually, there is only one cpp_reader object active. */
173 parse_underflow_t get_token;
175 cpp_buffer buffer_stack[CPP_STACK_MAX];
177 int errors; /* Error counter for exit code */
180 /* A buffer used for both for cpp_get_token's output, and also internally. */
181 unsigned char *token_buffer;
182 /* Alocated size of token_buffer. CPP_RESERVE allocates space. */
183 int token_buffer_size;
184 /* End of the written part of token_buffer. */
185 unsigned char *limit;
187 /* Line where a newline was first seen in a string constant. */
188 int multiline_string_line;
190 /* Current depth in #include directives that use <...>. */
191 int system_include_depth;
193 /* List of included files that contained #pragma once. */
194 struct file_name_list *dont_repeat_files;
196 /* List of other included files.
197 If ->control_macro if nonzero, the file had a #ifndef
198 around the entire contents, and ->control_macro gives the macro name. */
199 struct file_name_list *all_include_files;
201 /* Current maximum length of directory names in the search path
202 for include files. (Altered as we get more of them.) */
205 /* Hash table of files already included with #include or #import. */
206 struct import_file *import_hash_table[IMPORT_HASH_SIZE];
208 struct if_stack *if_stack;
210 /* Nonzero means we are inside an IF during a -pcp run. In this mode
211 macro expansion is done, and preconditions are output for all macro
212 uses requiring them. */
215 /* Nonzero means we have printed (while error reporting) a list of
216 containing files that matches the current status. */
217 char input_stack_listing_current;
219 /* If non-zero, macros are not expanded. */
220 char no_macro_expand;
222 /* Print column number in error messages. */
225 /* We're printed a warning recommending against using #import. */
228 /* If true, character between '<' and '>' are a single (string) token. */
229 char parsing_include_directive;
231 /* True if escape sequences (as described for has_escapes in
232 parse_buffer) should be emitted. */
235 /* 0: Have seen non-white-space on this line.
236 1: Only seen white space so far on this line.
237 2: Only seen white space so far in this file. */
238 char only_seen_white;
240 /* Nonzero means this file was included with a -imacros or -include
241 command line and should not be recorded as an include file. */
249 ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
251 /* Buffer of -M output. */
254 /* Number of bytes allocated in above. */
255 int deps_allocated_size;
257 /* Number of bytes used. */
260 /* Number of bytes since the last newline. */
264 ~cpp_reader () { cpp_cleanup (this); }
268 #define CPP_FATAL_LIMIT 1000
269 /* True if we have seen a "fatal" error. */
270 #define CPP_FATAL_ERRORS(READER) ((READER)->errors >= CPP_FATAL_LIMIT)
272 #define CPP_BUF_PEEK(BUFFER) \
273 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur : EOF)
274 #define CPP_BUF_GET(BUFFER) \
275 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
276 #define CPP_FORWARD(BUFFER, N) ((BUFFER)->cur += (N))
278 /* Macros for manipulating the token_buffer. */
280 #define CPP_OUT_BUFFER(PFILE) ((PFILE)->token_buffer)
282 /* Number of characters currently in PFILE's output buffer. */
283 #define CPP_WRITTEN(PFILE) ((PFILE)->limit - (PFILE)->token_buffer)
284 #define CPP_PWRITTEN(PFILE) ((PFILE)->limit)
286 /* Make sure PFILE->token_buffer has space for at least N more characters. */
287 #define CPP_RESERVE(PFILE, N) \
288 (CPP_WRITTEN (PFILE) + N > (PFILE)->token_buffer_size \
289 && (cpp_grow_buffer (PFILE, N), 0))
291 /* Append string STR (of length N) to PFILE's output buffer.
292 Assume there is enough space. */
293 #define CPP_PUTS_Q(PFILE, STR, N) \
294 (bcopy (STR, (PFILE)->limit, (N)), (PFILE)->limit += (N))
295 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
296 #define CPP_PUTS(PFILE, STR, N) CPP_RESERVE(PFILE, N), CPP_PUTS_Q(PFILE, STR,N)
297 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
298 #define CPP_PUTC_Q(PFILE, CH) (*(PFILE)->limit++ = (CH))
299 /* Append character CH to PFILE's output buffer. Make space if need be. */
300 #define CPP_PUTC(PFILE, CH) (CPP_RESERVE (PFILE, 1), CPP_PUTC_Q (PFILE, CH))
301 /* Make sure PFILE->limit is followed by '\0'. */
302 #define CPP_NUL_TERMINATE_Q(PFILE) (*(PFILE)->limit = 0)
303 #define CPP_NUL_TERMINATE(PFILE) (CPP_RESERVE(PFILE, 1), *(PFILE)->limit = 0)
304 #define CPP_ADJUST_WRITTEN(PFILE,DELTA) ((PFILE)->limit += (DELTA))
305 #define CPP_SET_WRITTEN(PFILE,N) ((PFILE)->limit = (PFILE)->token_buffer + (N))
307 #define CPP_OPTIONS(PFILE) ((cpp_options *) (PFILE)->data)
309 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
310 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)+1)
311 /* The bottom of the buffer stack. */
312 #define CPP_NULL_BUFFER(PFILE) (&(PFILE)->buffer_stack[CPP_STACK_MAX])
314 /* Pointed to by cpp_reader::data. */
318 /* Name of output file, for error messages. */
321 struct file_name_map_list *map_list;
323 /* Non-0 means -v, so print the full set of include dirs. */
326 /* Nonzero means use extra default include directories for C++. */
330 /* Nonzero means handle cplusplus style comments */
332 char cplusplus_comments;
334 /* Nonzero means handle #import, for objective C. */
338 /* Nonzero means this is an assembly file, and allow
339 unknown directives, which could be comments. */
343 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
347 /* Nonzero means handle CHILL comment syntax
348 and output CHILL string delimiter for __DATE___ etc. */
352 /* Nonzero means copy comments into the output file. */
354 char put_out_comments;
356 /* Nonzero means don't process the ANSI trigraph sequences. */
360 /* Nonzero means print the names of included files rather than
361 the preprocessed output. 1 means just the #include "...",
362 2 means #include <...> as well. */
366 /* Nonzero if missing .h files in -M output are assumed to be generated
367 files and not errors. */
369 char print_deps_missing_files;
371 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
372 char print_deps_append;
374 /* Nonzero means print names of header files (-H). */
376 char print_include_names;
378 /* Nonzero means try to make failure to fit ANSI C an error. */
380 char pedantic_errors;
382 /* Nonzero means don't print warning messages. -w. */
384 char inhibit_warnings;
386 /* Nonzero means warn if slash-star appears in a comment. */
390 /* Nonzero means warn if there are any trigraphs. */
394 /* Nonzero means warn if #import is used. */
398 /* Nonzero means warn if a macro argument is (or would be)
399 stringified with -traditional. */
403 /* Nonzero means turn warnings into errors. */
405 char warnings_are_errors;
407 /* Nonzero causes output not to be done,
408 but directives such as #define that have side effects
413 /* Nonzero means don't output line number information. */
415 char no_line_commands;
417 /* Nonzero means output the text in failing conditionals,
418 inside #failed ... #endfailed. */
420 char output_conditionals;
422 /* Nonzero means -I- has been seen,
423 so don't look for #include "foo" the source-file directory. */
426 /* Zero means dollar signs are punctuation.
427 This used to be needed for conformance to the C Standard,
428 before the C Standard was corrected. */
429 char dollars_in_ident;
431 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
434 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
437 /* Nonzero means give all the error messages the ANSI standard requires. */
440 char done_initializing;
442 struct file_name_list *include; /* First dir to search */
443 /* First dir to search for <file> */
444 /* This is the first element to use for #include <...>.
445 If it is 0, use the entire chain for such includes. */
446 struct file_name_list *first_bracket_include;
447 /* This is the first element in the chain that corresponds to
448 a directory of system header files. */
449 struct file_name_list *first_system_include;
450 struct file_name_list *last_include; /* Last in chain */
452 /* Chain of include directories to put at the end of the other chain. */
453 struct file_name_list *after_include;
454 struct file_name_list *last_after_include; /* Last in chain */
456 /* Chain to put at the start of the system include files. */
457 struct file_name_list *before_system;
458 struct file_name_list *last_before_system; /* Last in chain */
460 /* Directory prefix that should replace `/usr' in the standard
461 include file directories. */
462 char *include_prefix;
464 char inhibit_predefs;
465 char no_standard_includes;
466 char no_standard_cplusplus_includes;
468 /* dump_only means inhibit output of the preprocessed text
469 and instead output the definitions of all user-defined
470 macros in a form suitable for use as input to cccp.
471 dump_names means pass #define and the macro name through to output.
472 dump_definitions means pass the whole definition (plus #define) through
475 enum {dump_none = 0, dump_only, dump_names, dump_definitions}
478 /* Nonzero means pass all #define and #undef directives which we actually
479 process through to the output stream. This feature is used primarily
480 to allow cc1 to record the #defines and #undefs for the sake of
481 debuggers which understand about preprocessor macros, but it may
482 also be useful with -E to figure out how symbols are defined, and
483 where they are defined. */
486 /* Pending -D, -U and -A options, in reverse order. */
487 struct cpp_pending *pending;
489 /* File name which deps are being written to.
490 This is 0 if deps are being written to stdout. */
493 /* Target-name to write with the dependency information. */
497 #define CPP_TRADITIONAL(PFILE) (CPP_OPTIONS(PFILE)-> traditional)
498 #define CPP_C89(PFILE) (CPP_OPTIONS(PFILE)->c89)
499 #define CPP_PEDANTIC(PFILE) (CPP_OPTIONS (PFILE)->pedantic)
500 #define CPP_PRINT_DEPS(PFILE) (CPP_OPTIONS (PFILE)->print_deps)
502 /* Name under which this program was invoked. */
504 extern char *progname;
506 /* The structure of a node in the hash table. The hash table
507 has entries for all tokens defined by #define commands (type T_MACRO),
508 plus some special tokens like __LINE__ (these each have their own
509 type, and the appropriate code is run when that type of node is seen.
510 It does not contain control words like "#define", which are recognized
511 by a separate piece of code. */
513 /* different flavors of hash nodes --- also used in keyword table */
515 T_DEFINE = 1, /* the `#define' keyword */
516 T_INCLUDE, /* the `#include' keyword */
517 T_INCLUDE_NEXT, /* the `#include_next' keyword */
518 T_IMPORT, /* the `#import' keyword */
519 T_IFDEF, /* the `#ifdef' keyword */
520 T_IFNDEF, /* the `#ifndef' keyword */
521 T_IF, /* the `#if' keyword */
522 T_ELSE, /* `#else' */
523 T_PRAGMA, /* `#pragma' */
524 T_ELIF, /* `#elif' */
525 T_UNDEF, /* `#undef' */
526 T_LINE, /* `#line' */
527 T_ERROR, /* `#error' */
528 T_WARNING, /* `#warning' */
529 T_ENDIF, /* `#endif' */
530 T_SCCS, /* `#sccs', used on system V. */
531 T_IDENT, /* `#ident', used on system V. */
532 T_ASSERT, /* `#assert', taken from system V. */
533 T_UNASSERT, /* `#unassert', taken from system V. */
534 T_SPECLINE, /* special symbol `__LINE__' */
535 T_DATE, /* `__DATE__' */
536 T_FILE, /* `__FILE__' */
537 T_BASE_FILE, /* `__BASE_FILE__' */
538 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
539 T_VERSION, /* `__VERSION__' */
540 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
541 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
542 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
543 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
544 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
545 T_TIME, /* `__TIME__' */
546 T_CONST, /* Constant value, used by `__STDC__' */
547 T_MACRO, /* macro defined by `#define' */
548 T_DISABLED, /* macro temporarily turned off for rescan */
549 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
550 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
551 T_UNUSED /* Used for something not defined. */
554 /* Structure returned by create_definition */
555 typedef struct macrodef MACRODEF;
558 struct definition *defn;
559 unsigned char *symnam;
563 /* Structure allocated for every #define. For a simple replacement
566 nargs = -1, the `pattern' list is null, and the expansion is just
567 the replacement text. Nargs = 0 means a functionlike macro with no args,
569 #define getchar() getc (stdin) .
570 When there are args, the expansion is the replacement text with the
571 args squashed out, and the reflist is a list describing how to
572 build the output from the input: e.g., "3 chars, then the 1st arg,
573 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
574 The chars here come from the expansion. Whatever is left of the
575 expansion after the last arg-occurrence is copied after that arg.
576 Note that the reflist can be arbitrarily long---
577 its length depends on the number of times the arguments appear in
578 the replacement text, not how many args there are. Example:
579 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
581 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
582 where (x, y) means (nchars, argno). */
584 typedef struct definition DEFINITION;
587 int length; /* length of expansion string */
588 int predefined; /* True if the macro was builtin or */
589 /* came from the command line */
590 unsigned char *expansion;
591 int line; /* Line number of definition */
592 char *file; /* File of definition */
593 char rest_args; /* Nonzero if last arg. absorbs the rest */
595 struct reflist *next;
596 char stringify; /* nonzero if this arg was preceded by a
598 char raw_before; /* Nonzero if a ## operator before arg. */
599 char raw_after; /* Nonzero if a ## operator after arg. */
600 char rest_args; /* Nonzero if this arg. absorbs the rest */
601 int nchars; /* Number of literal chars to copy before
602 this arg occurrence. */
603 int argno; /* Number of arg to substitute (origin-0) */
606 /* Names of macro args, concatenated in reverse order
607 with comma-space between them.
608 The only use of this is that we warn on redefinition
609 if this differs between the old and new definitions. */
610 unsigned char *argnames;
614 extern unsigned char is_idchar[256];
616 /* Stack of conditionals currently in progress
617 (including both successful and failing conditionals). */
620 struct if_stack *next; /* for chaining to the next stack frame */
621 char *fname; /* copied from input when frame is made */
622 int lineno; /* similarly */
623 int if_succeeded; /* true if a leg of this if-group
624 has been passed through rescan */
625 unsigned char *control_macro; /* For #ifndef at start of file,
626 this is the macro name tested. */
627 enum node_type type; /* type of last directive seen in this group */
629 typedef struct if_stack IF_STACK_FRAME;
631 extern void cpp_buf_line_and_col PARAMS((cpp_buffer *, long *, long *));
632 extern cpp_buffer* cpp_file_buffer PARAMS((cpp_reader *));
633 extern void cpp_define PARAMS ((cpp_reader*, unsigned char *));
635 extern void cpp_error ();
636 extern void cpp_warning ();
637 extern void cpp_pedwarn ();
638 extern void cpp_error_with_line ();
639 extern void cpp_pedwarn_with_line ();
640 extern void cpp_pedwarn_with_file_and_line ();
641 extern void fatal ();
642 extern void cpp_error_from_errno ();
643 extern void cpp_perror_with_name ();
644 extern void cpp_pfatal_with_name ();
646 extern void cpp_grow_buffer PARAMS ((cpp_reader *, long));
647 extern int cpp_parse_escape PARAMS ((cpp_reader *, char **));
648 extern cpp_buffer *cpp_push_buffer PARAMS ((cpp_reader *,
649 unsigned char *, long));
650 extern cpp_buffer *cpp_pop_buffer PARAMS ((cpp_reader *));
652 extern cpp_hashnode *cpp_lookup PARAMS ((cpp_reader *, const unsigned char *,