blob: 4f0a7092dfbe4a55a5ac4f61d6be81032832ada6 [file] [log] [blame]
PulkoMandy17fc7592022-07-28 18:27:54 +02001/*
2 * (c) Thomas Pornin 1999, 2000
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 4. The name of the authors may not be used to endorse or promote
13 * products derived from this software without specific prior written
14 * permission.
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#ifndef UCPP__CPP__
31#define UCPP__CPP__
32
33/*
34 * Uncomment the following if you want ucpp to use externally provided
35 * error-reporting functions (ucpp_warning(), ucpp_error() and ucpp_ouch())
36 */
37/* #define NO_UCPP_ERROR_FUNCTIONS */
38
39/*
40 * Tokens (do not change the order unless checking operators_name[] in cpp.c)
41 *
42 * It is important that the token NONE is 0
43 * Check the STRING_TOKEN macro
44 */
45#define CPPERR 512
46enum {
47 NONE, /* whitespace */
48 NEWLINE, /* newline */
49 COMMENT, /* comment */
50 NUMBER, /* number constant */
51 NAME, /* identifier */
52 BUNCH, /* non-C characters */
53 PRAGMA, /* a #pragma directive */
54 CONTEXT, /* new file or #line */
55 STRING, /* constant "xxx" */
56 CHAR, /* constant 'xxx' */
57 SLASH, /* / */
58 ASSLASH, /* /= */
59 MINUS, /* - */
60 MMINUS, /* -- */
61 ASMINUS, /* -= */
62 ARROW, /* -> */
63 PLUS, /* + */
64 PPLUS, /* ++ */
65 ASPLUS, /* += */
66 LT, /* < */
67 LEQ, /* <= */
68 LSH, /* << */
69 ASLSH, /* <<= */
70 GT, /* > */
71 GEQ, /* >= */
72 RSH, /* >> */
73 ASRSH, /* >>= */
74 ASGN, /* = */
75 SAME, /* == */
76#ifdef CAST_OP
77 CAST, /* => */
78#endif
79 NOT, /* ~ */
80 NEQ, /* != */
81 AND, /* & */
82 LAND, /* && */
83 ASAND, /* &= */
84 OR, /* | */
85 LOR, /* || */
86 ASOR, /* |= */
87 PCT, /* % */
88 ASPCT, /* %= */
89 STAR, /* * */
90 ASSTAR, /* *= */
91 CIRC, /* ^ */
92 ASCIRC, /* ^= */
93 LNOT, /* ! */
94 ASNOT, /* ~= */
95 LBRA, /* { */
96 RBRA, /* } */
97 LBRK, /* [ */
98 RBRK, /* ] */
99 LPAR, /* ( */
100 RPAR, /* ) */
101 COMMA, /* , */
102 QUEST, /* ? */
103 SEMIC, /* ; */
104 COLON, /* : */
105 DOT, /* . */
106 MDOTS, /* ... */
107 SHARP, /* # */
108 DSHARP, /* ## */
109
110 OPT_NONE, /* optional space to separate tokens in text output */
111
112 DIGRAPH_TOKENS, /* there begin digraph tokens */
113
114 /* for DIG_*, do not change order, unless checking undig() in cpp.c */
115 DIG_LBRK, /* <: */
116 DIG_RBRK, /* :> */
117 DIG_LBRA, /* <% */
118 DIG_RBRA, /* %> */
119 DIG_SHARP, /* %: */
120 DIG_DSHARP, /* %:%: */
121
122 DIGRAPH_TOKENS_END, /* digraph tokens end here */
123
124 LAST_MEANINGFUL_TOKEN, /* reserved words will go there */
125
126 MACROARG, /* special token for representing macro arguments */
127
128 UPLUS = CPPERR, /* unary + */
129 UMINUS /* unary - */
130};
131
132#include <stdio.h>
133#include <setjmp.h>
134#include "tune.h"
135
136struct token {
137 int type;
138 long line;
139 char *name;
140};
141
142struct token_fifo {
143 struct token *t;
144 size_t nt, art;
145};
146
147struct lexer_state {
148 /* input control */
149 FILE *input;
150#ifndef NO_UCPP_BUF
151 unsigned char *input_buf;
152#ifdef UCPP_MMAP
153 int from_mmap;
154 unsigned char *input_buf_sav;
155#endif
156#endif
157 unsigned char *input_string;
158 size_t ebuf;
159 size_t pbuf;
160 int lka[2];
161 int nlka;
162 int macfile;
163 int last;
164 int discard;
165 unsigned long utf8;
166 unsigned char copy_line[COPY_LINE_LENGTH];
167 int cli;
168
169 /* output control */
170
171 FILE *output;
172 struct token_fifo *output_fifo, *toplevel_of;
173#ifndef NO_UCPP_BUF
174 unsigned char *output_buf;
175#endif
176 size_t sbuf;
177
178 /* token control */
179 struct token *ctok;
180 struct token *save_ctok;
181 int tknl;
182 int ltwnl;
183 int pending_token;
184#ifdef INMACRO_FLAG
185 int inmacro;
186 long macro_count;
187#endif
188
189 /* lexer options */
190 long line;
191 long oline;
192 unsigned long flags;
193 long count_trigraphs;
194 struct garbage_fifo *gf;
195 int ifnest;
196 int condnest;
197 int condcomp;
198 int condmet;
199 unsigned long condf[2];
200};
201
202/*
203 * Flags for struct lexer_state
204 */
205/* warning flags */
206#define WARN_STANDARD 0x000001UL /* emit standard warnings */
207#define WARN_ANNOYING 0x000002UL /* emit annoying warnings */
208#define WARN_TRIGRAPHS 0x000004UL /* warn when trigraphs are used */
209#define WARN_TRIGRAPHS_MORE 0x000008UL /* extra-warn for trigraphs */
210#define WARN_PRAGMA 0x000010UL /* warn for pragmas in non-lexer mode */
211
212/* error flags */
213#define FAIL_SHARP 0x000020UL /* emit errors on rogue '#' */
214#define CCHARSET 0x000040UL /* emit errors on non-C characters */
215
216/* emission flags */
217#define DISCARD_COMMENTS 0x000080UL /* discard comments from text output */
218#define CPLUSPLUS_COMMENTS 0x000100UL /* understand C++-like comments */
219#define LINE_NUM 0x000200UL /* emit #line directives in output */
220#define GCC_LINE_NUM 0x000400UL /* same as #line, with gcc-syntax */
221
222/* language flags */
223#define HANDLE_ASSERTIONS 0x000800UL /* understand assertions */
224#define HANDLE_PRAGMA 0x001000UL /* emit PRAGMA tokens in lexer mode */
225#define MACRO_VAARG 0x002000UL /* understand macros with '...' */
226#define UTF8_SOURCE 0x004000UL /* identifiers are in UTF8 encoding */
227#define HANDLE_TRIGRAPHS 0x008000UL /* handle trigraphs */
228
229/* global ucpp behaviour */
230#define LEXER 0x010000UL /* behave as a lexer */
231#define KEEP_OUTPUT 0x020000UL /* emit the result of preprocessing */
232#define COPY_LINE 0x040000UL /* make a copy of the parsed line */
233
234/* internal flags */
235#define READ_AGAIN 0x080000UL /* emit again the last token */
236#define TEXT_OUTPUT 0x100000UL /* output text */
237
238/*
239 * Public function prototypes
240 */
241
242#ifndef NO_UCPP_BUF
243void flush_output(struct lexer_state *);
244#endif
245
246void init_assertions(void);
247int make_assertion(char *);
248int destroy_assertion(char *);
249void print_assertions(void);
250
251void init_macros(void);
252int define_macro(struct lexer_state *, char *);
253int undef_macro(struct lexer_state *, char *);
254void print_defines(void);
255
256void set_init_filename(char *, int);
257void init_cpp(void);
258void init_include_path(char *[]);
259void init_lexer_state(struct lexer_state *);
260void init_lexer_mode(struct lexer_state *);
261void free_lexer_state(struct lexer_state *);
262int lex(struct lexer_state *);
263int check_cpp_errors(struct lexer_state *);
264void add_incpath(char *);
265void init_tables(int);
266void enter_file(struct lexer_state *, unsigned long);
267int cpp(struct lexer_state *);
268
269#ifdef UCPP_MMAP
270FILE *fopen_mmap_file(char *);
271void set_input_file(struct lexer_state *, FILE *);
272#endif
273
274struct stack_context {
275 char *long_name, *name;
276 long line;
277};
278struct stack_context *report_context(void);
279
280extern int no_special_macros, system_macros,
281 emit_dependencies, emit_defines, emit_assertions;
282extern int c99_compliant, c99_hosted;
283extern FILE *emit_output;
284extern char *current_filename, *current_long_filename;
285extern char *operators_name[];
286extern struct protect {
287 char *macro;
288 int state;
289 struct found_file *ff;
290} protect_detect;
291
292void ucpp_ouch(char *, ...);
293void ucpp_error(long, char *, ...);
294void ucpp_warning(long, char *, ...);
295
296extern int *transient_characters;
297
298/*
299 * Errors from CPPERR_EOF and above are not real erros, only show-stoppers.
300 * Errors below CPPERR_EOF are real ones.
301 */
302#define CPPERR_NEST 900
303#define CPPERR_EOF 1000
304
305/*
306 * This macro tells whether the name field of a given token type is
307 * relevant, or not. Irrelevant name field means that it might point
308 * to outerspace.
309 */
310#ifdef SEMPER_FIDELIS
311#define STRING_TOKEN(x) ((x) == NONE || ((x) >= COMMENT && (x) <= CHAR))
312#else
313#define STRING_TOKEN(x) ((x) >= NUMBER && (x) <= CHAR)
314#endif
315
316#endif