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>
35 typedef unsigned char U_CHAR;
37 typedef struct cpp_reader cpp_reader;
38 typedef struct cpp_buffer cpp_buffer;
39 typedef struct cpp_options cpp_options;
40 typedef struct hashnode cpp_hashnode;
47 CPP_VSPACE, /* newlines and #line directives */
58 CPP_SEMICOLON,/* ";" */
59 CPP_3DOTS, /* "..." */
61 CPP_ANDAND, /* "&&" */
70 CPP_MINMIN, /* "--" */
72 /* POP_TOKEN is returned when we've popped a cpp_buffer. */
77 #define PARAMS(P) PROTO(P)
80 typedef enum cpp_token (*parse_underflow_t) PARAMS((cpp_reader *));
81 typedef int (*parse_cleanup_t) PARAMS((cpp_buffer *, cpp_reader *));
83 /* A parse_marker indicates a previous position,
84 which we can backtrack to. */
88 struct parse_marker *next;
92 extern void parse_set_mark PARAMS ((struct parse_marker *, cpp_reader *));
93 extern void parse_clear_mark PARAMS ((struct parse_marker *));
94 extern void parse_goto_mark PARAMS((struct parse_marker *, cpp_reader *));
95 extern void parse_move_mark PARAMS((struct parse_marker *, cpp_reader *));
97 extern int cpp_handle_options PARAMS ((cpp_reader *, int, char **));
98 extern enum cpp_token cpp_get_token PARAMS ((cpp_reader *));
99 extern void cpp_skip_hspace PARAMS((cpp_reader *));
100 extern enum cpp_token cpp_get_non_space_token PARAMS ((cpp_reader *));
102 /* This frees resources used by PFILE. */
103 extern void cpp_cleanup PARAMS ((cpp_reader *PFILE));
105 /* Maintain and search list of included files, for #import. */
107 #define IMPORT_HASH_SIZE 31
113 struct import_file *next;
116 /* If we have a huge buffer, may need to cache more recent counts */
117 #define CPP_LINE_BASE(BUF) ((BUF)->buf + (BUF)->line_base)
122 unsigned char *rlimit; /* end of valid data */
123 unsigned char *alimit; /* end of allocated buffer */
124 unsigned char *prev; /* start of current token */
127 /* Filename specified with #line command. */
130 /* Record where in the search path this file was found.
131 For #include_next. */
132 struct file_name_list *dir;
135 long lineno; /* Line number at CPP_LINE_BASE. */
136 long colno; /* Column number at CPP_LINE_BASE. */
137 parse_underflow_t underflow;
138 parse_cleanup_t cleanup;
140 struct parse_marker *marks;
141 /* Value of if_stack at start of this file.
142 Used to prohibit unmatched #endif (etc) in an include file. */
143 struct if_stack *if_stack;
145 /* True if this is a header file included using <FILENAME>. */
146 char system_header_p;
149 /* True if buffer contains escape sequences.
150 Currently there are three kinds:
151 "@-" means following identifier should not be macro-expanded.
152 "@ " means a token-separator. This turns into " " in final output
153 if not stringizing and needed to separate tokens; otherwise nothing.
154 "@@" means a normal '@'.
155 (An '@' inside a string stands for itself and is never an escape.) */
159 struct cpp_pending; /* Forward declaration - for C++. */
160 struct file_name_map_list;
162 typedef struct assertion_hashnode ASSERTION_HASHNODE;
163 #define ASSERTION_HASHSIZE 37
165 /* Maximum nesting of cpp_buffers. We use a static limit, partly for
166 efficiency, and partly to limit runaway recursion. */
167 #define CPP_STACK_MAX 200
169 /* A cpp_reader encapsulates the "state" of a pre-processor run.
170 Applying cpp_get_token repeatedly yields a stream of pre-processor
171 tokens. Usually, there is only one cpp_reader object active. */
174 parse_underflow_t get_token;
176 cpp_buffer buffer_stack[CPP_STACK_MAX];
178 int errors; /* Error counter for exit code */
181 /* A buffer used for both for cpp_get_token's output, and also internally. */
182 unsigned char *token_buffer;
183 /* Allocated size of token_buffer. CPP_RESERVE allocates space. */
184 int token_buffer_size;
185 /* End of the written part of token_buffer. */
186 unsigned char *limit;
188 /* Line where a newline was first seen in a string constant. */
189 int multiline_string_line;
191 /* Current depth in #include directives that use <...>. */
192 int system_include_depth;
194 /* List of included files that contained #pragma once. */
195 struct file_name_list *dont_repeat_files;
197 /* List of other included files.
198 If ->control_macro if nonzero, the file had a #ifndef
199 around the entire contents, and ->control_macro gives the macro name. */
200 struct file_name_list *all_include_files;
202 /* Current maximum length of directory names in the search path
203 for include files. (Altered as we get more of them.) */
206 /* Hash table of files already included with #include or #import. */
207 struct import_file *import_hash_table[IMPORT_HASH_SIZE];
209 struct if_stack *if_stack;
211 /* Nonzero means we are inside an IF during a -pcp run. In this mode
212 macro expansion is done, and preconditions are output for all macro
213 uses requiring them. */
216 /* Nonzero means we have printed (while error reporting) a list of
217 containing files that matches the current status. */
218 char input_stack_listing_current;
220 /* If non-zero, macros are not expanded. */
221 char no_macro_expand;
223 /* Print column number in error messages. */
226 /* We're printed a warning recommending against using #import. */
229 /* If true, character between '<' and '>' are a single (string) token. */
230 char parsing_include_directive;
232 /* True if escape sequences (as described for has_escapes in
233 parse_buffer) should be emitted. */
236 /* 0: Have seen non-white-space on this line.
237 1: Only seen white space so far on this line.
238 2: Only seen white space so far in this file. */
239 char only_seen_white;
241 /* Nonzero means this file was included with a -imacros or -include
242 command line and should not be recorded as an include file. */
250 ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
252 /* Buffer of -M output. */
255 /* Number of bytes allocated in above. */
256 int deps_allocated_size;
258 /* Number of bytes used. */
261 /* Number of bytes since the last newline. */
265 ~cpp_reader () { cpp_cleanup (this); }
269 #define CPP_FATAL_LIMIT 1000
270 /* True if we have seen a "fatal" error. */
271 #define CPP_FATAL_ERRORS(READER) ((READER)->errors >= CPP_FATAL_LIMIT)
273 #define CPP_BUF_PEEK(BUFFER) \
274 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur : EOF)
275 #define CPP_BUF_GET(BUFFER) \
276 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
277 #define CPP_FORWARD(BUFFER, N) ((BUFFER)->cur += (N))
279 /* Macros for manipulating the token_buffer. */
281 #define CPP_OUT_BUFFER(PFILE) ((PFILE)->token_buffer)
283 /* Number of characters currently in PFILE's output buffer. */
284 #define CPP_WRITTEN(PFILE) ((PFILE)->limit - (PFILE)->token_buffer)
285 #define CPP_PWRITTEN(PFILE) ((PFILE)->limit)
287 /* Make sure PFILE->token_buffer has space for at least N more characters. */
288 #define CPP_RESERVE(PFILE, N) \
289 (CPP_WRITTEN (PFILE) + N > (PFILE)->token_buffer_size \
290 && (cpp_grow_buffer (PFILE, N), 0))
292 /* Append string STR (of length N) to PFILE's output buffer.
293 Assume there is enough space. */
294 #define CPP_PUTS_Q(PFILE, STR, N) \
295 (bcopy (STR, (PFILE)->limit, (N)), (PFILE)->limit += (N))
296 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
297 #define CPP_PUTS(PFILE, STR, N) CPP_RESERVE(PFILE, N), CPP_PUTS_Q(PFILE, STR,N)
298 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
299 #define CPP_PUTC_Q(PFILE, CH) (*(PFILE)->limit++ = (CH))
300 /* Append character CH to PFILE's output buffer. Make space if need be. */
301 #define CPP_PUTC(PFILE, CH) (CPP_RESERVE (PFILE, 1), CPP_PUTC_Q (PFILE, CH))
302 /* Make sure PFILE->limit is followed by '\0'. */
303 #define CPP_NUL_TERMINATE_Q(PFILE) (*(PFILE)->limit = 0)
304 #define CPP_NUL_TERMINATE(PFILE) (CPP_RESERVE(PFILE, 1), *(PFILE)->limit = 0)
305 #define CPP_ADJUST_WRITTEN(PFILE,DELTA) ((PFILE)->limit += (DELTA))
306 #define CPP_SET_WRITTEN(PFILE,N) ((PFILE)->limit = (PFILE)->token_buffer + (N))
308 #define CPP_OPTIONS(PFILE) ((cpp_options *) (PFILE)->data)
310 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
311 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)+1)
312 /* The bottom of the buffer stack. */
313 #define CPP_NULL_BUFFER(PFILE) (&(PFILE)->buffer_stack[CPP_STACK_MAX])
315 /* Pointed to by cpp_reader::data. */
319 /* Name of output file, for error messages. */
322 struct file_name_map_list *map_list;
324 /* Non-0 means -v, so print the full set of include dirs. */
327 /* Nonzero means use extra default include directories for C++. */
331 /* Nonzero means handle cplusplus style comments */
333 char cplusplus_comments;
335 /* Nonzero means handle #import, for objective C. */
339 /* Nonzero means this is an assembly file, and allow
340 unknown directives, which could be comments. */
344 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
348 /* Nonzero means handle CHILL comment syntax
349 and output CHILL string delimiter for __DATE___ etc. */
353 /* Nonzero means copy comments into the output file. */
355 char put_out_comments;
357 /* Nonzero means don't process the ANSI trigraph sequences. */
361 /* Nonzero means print the names of included files rather than
362 the preprocessed output. 1 means just the #include "...",
363 2 means #include <...> as well. */
367 /* Nonzero if missing .h files in -M output are assumed to be generated
368 files and not errors. */
370 char print_deps_missing_files;
372 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
373 char print_deps_append;
375 /* Nonzero means print names of header files (-H). */
377 char print_include_names;
379 /* Nonzero means try to make failure to fit ANSI C an error. */
381 char pedantic_errors;
383 /* Nonzero means don't print warning messages. -w. */
385 char inhibit_warnings;
387 /* Nonzero means warn if slash-star appears in a comment. */
391 /* Nonzero means warn if there are any trigraphs. */
395 /* Nonzero means warn if #import is used. */
399 /* Nonzero means warn if a macro argument is (or would be)
400 stringified with -traditional. */
404 /* Nonzero means turn warnings into errors. */
406 char warnings_are_errors;
408 /* Nonzero causes output not to be done,
409 but directives such as #define that have side effects
414 /* Nonzero means we should look for header.gcc files that remap file
418 /* Nonzero means don't output line number information. */
420 char no_line_commands;
422 /* Nonzero means output the text in failing conditionals,
423 inside #failed ... #endfailed. */
425 char output_conditionals;
427 /* Nonzero means -I- has been seen,
428 so don't look for #include "foo" the source-file directory. */
431 /* Zero means dollar signs are punctuation.
432 This used to be needed for conformance to the C Standard,
433 before the C Standard was corrected. */
434 char dollars_in_ident;
436 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
439 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
442 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
445 /* Nonzero means give all the error messages the ANSI standard requires. */
448 char done_initializing;
450 struct file_name_list *include; /* First dir to search */
451 /* First dir to search for <file> */
452 /* This is the first element to use for #include <...>.
453 If it is 0, use the entire chain for such includes. */
454 struct file_name_list *first_bracket_include;
455 /* This is the first element in the chain that corresponds to
456 a directory of system header files. */
457 struct file_name_list *first_system_include;
458 struct file_name_list *last_include; /* Last in chain */
460 /* Chain of include directories to put at the end of the other chain. */
461 struct file_name_list *after_include;
462 struct file_name_list *last_after_include; /* Last in chain */
464 /* Chain to put at the start of the system include files. */
465 struct file_name_list *before_system;
466 struct file_name_list *last_before_system; /* Last in chain */
468 /* Directory prefix that should replace `/usr' in the standard
469 include file directories. */
470 char *include_prefix;
472 char inhibit_predefs;
473 char no_standard_includes;
474 char no_standard_cplusplus_includes;
476 /* dump_only means inhibit output of the preprocessed text
477 and instead output the definitions of all user-defined
478 macros in a form suitable for use as input to cccp.
479 dump_names means pass #define and the macro name through to output.
480 dump_definitions means pass the whole definition (plus #define) through
483 enum {dump_none = 0, dump_only, dump_names, dump_definitions}
486 /* Nonzero means pass all #define and #undef directives which we actually
487 process through to the output stream. This feature is used primarily
488 to allow cc1 to record the #defines and #undefs for the sake of
489 debuggers which understand about preprocessor macros, but it may
490 also be useful with -E to figure out how symbols are defined, and
491 where they are defined. */
494 /* Nonzero means pass #include lines through to the output,
495 even if they are ifdefed out. */
498 /* Pending -D, -U and -A options, in reverse order. */
499 struct cpp_pending *pending;
501 /* File name which deps are being written to.
502 This is 0 if deps are being written to stdout. */
505 /* Target-name to write with the dependency information. */
509 #define CPP_TRADITIONAL(PFILE) (CPP_OPTIONS(PFILE)-> traditional)
510 #define CPP_WARN_UNDEF(PFILE) (CPP_OPTIONS(PFILE)->warn_undef)
511 #define CPP_C89(PFILE) (CPP_OPTIONS(PFILE)->c89)
512 #define CPP_PEDANTIC(PFILE) (CPP_OPTIONS (PFILE)->pedantic)
513 #define CPP_PRINT_DEPS(PFILE) (CPP_OPTIONS (PFILE)->print_deps)
515 /* Name under which this program was invoked. */
517 extern char *progname;
519 /* The structure of a node in the hash table. The hash table
520 has entries for all tokens defined by #define commands (type T_MACRO),
521 plus some special tokens like __LINE__ (these each have their own
522 type, and the appropriate code is run when that type of node is seen.
523 It does not contain control words like "#define", which are recognized
524 by a separate piece of code. */
526 /* different flavors of hash nodes --- also used in keyword table */
528 T_DEFINE = 1, /* the `#define' keyword */
529 T_INCLUDE, /* the `#include' keyword */
530 T_INCLUDE_NEXT, /* the `#include_next' keyword */
531 T_IMPORT, /* the `#import' keyword */
532 T_IFDEF, /* the `#ifdef' keyword */
533 T_IFNDEF, /* the `#ifndef' keyword */
534 T_IF, /* the `#if' keyword */
535 T_ELSE, /* `#else' */
536 T_PRAGMA, /* `#pragma' */
537 T_ELIF, /* `#elif' */
538 T_UNDEF, /* `#undef' */
539 T_LINE, /* `#line' */
540 T_ERROR, /* `#error' */
541 T_WARNING, /* `#warning' */
542 T_ENDIF, /* `#endif' */
543 T_SCCS, /* `#sccs', used on system V. */
544 T_IDENT, /* `#ident', used on system V. */
545 T_ASSERT, /* `#assert', taken from system V. */
546 T_UNASSERT, /* `#unassert', taken from system V. */
547 T_SPECLINE, /* special symbol `__LINE__' */
548 T_DATE, /* `__DATE__' */
549 T_FILE, /* `__FILE__' */
550 T_BASE_FILE, /* `__BASE_FILE__' */
551 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
552 T_VERSION, /* `__VERSION__' */
553 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
554 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
555 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
556 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
557 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
558 T_TIME, /* `__TIME__' */
559 T_CONST, /* Constant value, used by `__STDC__' */
560 T_MACRO, /* macro defined by `#define' */
561 T_DISABLED, /* macro temporarily turned off for rescan */
562 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
563 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
564 T_UNUSED /* Used for something not defined. */
567 /* Structure returned by create_definition */
568 typedef struct macrodef MACRODEF;
571 struct definition *defn;
572 unsigned char *symnam;
576 /* Structure allocated for every #define. For a simple replacement
579 nargs = -1, the `pattern' list is null, and the expansion is just
580 the replacement text. Nargs = 0 means a functionlike macro with no args,
582 #define getchar() getc (stdin) .
583 When there are args, the expansion is the replacement text with the
584 args squashed out, and the reflist is a list describing how to
585 build the output from the input: e.g., "3 chars, then the 1st arg,
586 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
587 The chars here come from the expansion. Whatever is left of the
588 expansion after the last arg-occurrence is copied after that arg.
589 Note that the reflist can be arbitrarily long---
590 its length depends on the number of times the arguments appear in
591 the replacement text, not how many args there are. Example:
592 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
594 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
595 where (x, y) means (nchars, argno). */
597 typedef struct definition DEFINITION;
600 int length; /* length of expansion string */
601 int predefined; /* True if the macro was builtin or */
602 /* came from the command line */
603 unsigned char *expansion;
604 int line; /* Line number of definition */
605 char *file; /* File of definition */
606 char rest_args; /* Nonzero if last arg. absorbs the rest */
608 struct reflist *next;
609 char stringify; /* nonzero if this arg was preceded by a
611 char raw_before; /* Nonzero if a ## operator before arg. */
612 char raw_after; /* Nonzero if a ## operator after arg. */
613 char rest_args; /* Nonzero if this arg. absorbs the rest */
614 int nchars; /* Number of literal chars to copy before
615 this arg occurrence. */
616 int argno; /* Number of arg to substitute (origin-0) */
619 /* Names of macro args, concatenated in reverse order
620 with comma-space between them.
621 The only use of this is that we warn on redefinition
622 if this differs between the old and new definitions. */
623 unsigned char *argnames;
627 extern unsigned char is_idchar[256];
629 /* Stack of conditionals currently in progress
630 (including both successful and failing conditionals). */
633 struct if_stack *next; /* for chaining to the next stack frame */
634 char *fname; /* copied from input when frame is made */
635 int lineno; /* similarly */
636 int if_succeeded; /* true if a leg of this if-group
637 has been passed through rescan */
638 unsigned char *control_macro; /* For #ifndef at start of file,
639 this is the macro name tested. */
640 enum node_type type; /* type of last directive seen in this group */
642 typedef struct if_stack IF_STACK_FRAME;
644 extern void cpp_buf_line_and_col PARAMS((cpp_buffer *, long *, long *));
645 extern cpp_buffer* cpp_file_buffer PARAMS((cpp_reader *));
646 extern void cpp_define PARAMS ((cpp_reader*, unsigned char *));
648 extern void cpp_error PVPROTO ((cpp_reader *, const char *, ...))
650 extern void cpp_warning PVPROTO ((cpp_reader *, const char *, ...))
652 extern void cpp_pedwarn PVPROTO ((cpp_reader *, const char *, ...))
654 extern void cpp_error_with_line PVPROTO ((cpp_reader *, int, int, const char *, ...))
656 extern void cpp_pedwarn_with_line PVPROTO ((cpp_reader *, int, int, const char *, ...))
658 extern void cpp_pedwarn_with_file_and_line PVPROTO ((cpp_reader *, char *, int, const char *, ...))
660 extern void cpp_error_from_errno PROTO ((cpp_reader *, const char *));
661 extern void cpp_perror_with_name PROTO ((cpp_reader *, const char *));
662 extern void v_cpp_message PROTO ((cpp_reader *, int, const char *, va_list));
664 extern void cpp_grow_buffer PARAMS ((cpp_reader *, long));
665 extern int cpp_parse_escape PARAMS ((cpp_reader *, char **));
666 extern cpp_buffer *cpp_push_buffer PARAMS ((cpp_reader *,
667 unsigned char *, long));
668 extern cpp_buffer *cpp_pop_buffer PARAMS ((cpp_reader *));
670 extern cpp_hashnode *cpp_lookup PARAMS ((cpp_reader *, const unsigned char *,
672 extern void cpp_reader_init PARAMS ((cpp_reader *));
673 extern void cpp_options_init PARAMS ((cpp_options *));
674 extern int cpp_start_read PARAMS ((cpp_reader *, char *));
675 extern int cpp_read_check_assertion PARAMS ((cpp_reader *));
676 extern int scan_decls PARAMS ((cpp_reader *, int, char **));
677 extern void skip_rest_of_line PARAMS ((cpp_reader *));
678 extern void cpp_finish PARAMS ((cpp_reader *));
680 /* From cpperror.c */
681 extern void cpp_fatal PVPROTO ((cpp_reader *, const char *, ...))
683 extern void cpp_message PVPROTO ((cpp_reader *, int, const char *, ...))
685 extern void cpp_pfatal_with_name PROTO ((cpp_reader *, const char *));
686 extern void cpp_file_line_for_message PROTO ((cpp_reader *, char *, int, int));
687 extern void cpp_print_containing_files PROTO ((cpp_reader *));