]>
Commit | Line | Data |
---|---|---|
a949941c | 1 | /* CPP Library. (Directive handling.) |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
5d8ebbd8 | 3 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
4c8cc616 | 4 | Contributed by Per Bothner, 1994-95. |
d8bfa78c | 5 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
956d6950 | 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
7f2935c7 | 21 | |
956d6950 | 22 | #include "config.h" |
b04cd507 | 23 | #include "system.h" |
7f2935c7 | 24 | |
956d6950 JL |
25 | #include "cpplib.h" |
26 | #include "cpphash.h" | |
c71f835b | 27 | #include "obstack.h" |
7f2935c7 | 28 | |
93c80368 NB |
29 | /* Chained list of answers to an assertion. */ |
30 | struct answer | |
31 | { | |
32 | struct answer *next; | |
33 | unsigned int count; | |
34 | cpp_token first[1]; | |
35 | }; | |
36 | ||
88ae23e7 ZW |
37 | /* Stack of conditionals currently in progress |
38 | (including both successful and failing conditionals). */ | |
88ae23e7 ZW |
39 | struct if_stack |
40 | { | |
41 | struct if_stack *next; | |
50410426 | 42 | unsigned int line; /* Line where condition started. */ |
93c80368 | 43 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
cef0d199 NB |
44 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
45 | bool was_skipping; /* If were skipping on entry. */ | |
46 | int type; /* Most recent conditional, for diagnostics. */ | |
88ae23e7 | 47 | }; |
88ae23e7 | 48 | |
a5da89c6 NB |
49 | /* Contains a registered pragma or pragma namespace. */ |
50 | typedef void (*pragma_cb) PARAMS ((cpp_reader *)); | |
51 | struct pragma_entry | |
52 | { | |
53 | struct pragma_entry *next; | |
4b115ff0 | 54 | const cpp_hashnode *pragma; /* Name and length. */ |
a5da89c6 NB |
55 | int is_nspace; |
56 | union { | |
57 | pragma_cb handler; | |
58 | struct pragma_entry *space; | |
59 | } u; | |
60 | }; | |
61 | ||
93c80368 NB |
62 | /* Values for the origin field of struct directive. KANDR directives |
63 | come from traditional (K&R) C. STDC89 directives come from the | |
64 | 1989 C standard. EXTENSION directives are extensions. */ | |
65 | #define KANDR 0 | |
66 | #define STDC89 1 | |
67 | #define EXTENSION 2 | |
68 | ||
69 | /* Values for the flags field of struct directive. COND indicates a | |
70 | conditional; IF_COND an opening conditional. INCL means to treat | |
71 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
72 | means this directive should be handled even if -fpreprocessed is in | |
1a76916c NB |
73 | effect (these are the directives with callback hooks). |
74 | ||
d97371e0 | 75 | EXPAND is set on directives that are always macro-expanded. */ |
93c80368 NB |
76 | #define COND (1 << 0) |
77 | #define IF_COND (1 << 1) | |
78 | #define INCL (1 << 2) | |
79 | #define IN_I (1 << 3) | |
1a76916c | 80 | #define EXPAND (1 << 4) |
93c80368 NB |
81 | |
82 | /* Defines one #-directive, including how to handle it. */ | |
83 | typedef void (*directive_handler) PARAMS ((cpp_reader *)); | |
84 | typedef struct directive directive; | |
85 | struct directive | |
86 | { | |
87 | directive_handler handler; /* Function to handle directive. */ | |
562a5c27 | 88 | const uchar *name; /* Name of directive. */ |
93c80368 NB |
89 | unsigned short length; /* Length of name. */ |
90 | unsigned char origin; /* Origin of directive. */ | |
91 | unsigned char flags; /* Flags describing this directive. */ | |
92 | }; | |
93 | ||
1316f1f7 ZW |
94 | /* Forward declarations. */ |
95 | ||
93c80368 NB |
96 | static void skip_rest_of_line PARAMS ((cpp_reader *)); |
97 | static void check_eol PARAMS ((cpp_reader *)); | |
fe6c2db9 | 98 | static void start_directive PARAMS ((cpp_reader *)); |
1a76916c | 99 | static void prepare_directive_trad PARAMS ((cpp_reader *)); |
fe6c2db9 | 100 | static void end_directive PARAMS ((cpp_reader *, int)); |
18a9d8ff NB |
101 | static void directive_diagnostics |
102 | PARAMS ((cpp_reader *, const directive *, int)); | |
93c80368 | 103 | static void run_directive PARAMS ((cpp_reader *, int, |
0bda4760 | 104 | const char *, size_t)); |
4ed5bcfb NB |
105 | static const cpp_token *glue_header_name PARAMS ((cpp_reader *)); |
106 | static const cpp_token *parse_include PARAMS ((cpp_reader *)); | |
041c3194 ZW |
107 | static void push_conditional PARAMS ((cpp_reader *, int, int, |
108 | const cpp_hashnode *)); | |
28e0f040 | 109 | static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int)); |
562a5c27 | 110 | static uchar *dequote_string PARAMS ((cpp_reader *, const uchar *, |
dcc229e5 | 111 | unsigned int)); |
562a5c27 | 112 | static int strtoul_for_line PARAMS ((const uchar *, unsigned int, |
041c3194 | 113 | unsigned long *)); |
ebef4e8c | 114 | static void do_diagnostic PARAMS ((cpp_reader *, int, int)); |
b528a07e | 115 | static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *)); |
ba133c96 | 116 | static void do_include_common PARAMS ((cpp_reader *, enum include_type)); |
a5da89c6 | 117 | static struct pragma_entry *lookup_pragma_entry |
4b115ff0 | 118 | PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma)); |
a5da89c6 | 119 | static struct pragma_entry *insert_pragma_entry |
4b115ff0 NB |
120 | PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *, |
121 | pragma_cb)); | |
93c80368 NB |
122 | static void do_pragma_once PARAMS ((cpp_reader *)); |
123 | static void do_pragma_poison PARAMS ((cpp_reader *)); | |
124 | static void do_pragma_system_header PARAMS ((cpp_reader *)); | |
125 | static void do_pragma_dependency PARAMS ((cpp_reader *)); | |
dcc229e5 | 126 | static void do_linemarker PARAMS ((cpp_reader *)); |
4ed5bcfb NB |
127 | static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *)); |
128 | static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *)); | |
87062813 | 129 | static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *)); |
93c80368 NB |
130 | static int parse_answer PARAMS ((cpp_reader *, struct answer **, int)); |
131 | static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **, | |
132 | int)); | |
133 | static struct answer ** find_answer PARAMS ((cpp_hashnode *, | |
134 | const struct answer *)); | |
86368122 | 135 | static void handle_assertion PARAMS ((cpp_reader *, const char *, int)); |
d481b69b | 136 | |
168d3732 ZW |
137 | /* This is the table of directive handlers. It is ordered by |
138 | frequency of occurrence; the numbers at the end are directive | |
139 | counts from all the source code I have lying around (egcs and libc | |
140 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
93c80368 NB |
141 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
142 | is now O(1). All extensions other than #warning and #include_next | |
143 | are deprecated. The name is where the extension appears to have | |
144 | come from. */ | |
168d3732 | 145 | |
93c80368 NB |
146 | #define DIRECTIVE_TABLE \ |
147 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
d97371e0 | 148 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
93c80368 NB |
149 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
150 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
1a76916c | 151 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
93c80368 NB |
152 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
153 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
154 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
1a76916c NB |
155 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
156 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ | |
93c80368 NB |
157 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
158 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
159 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
d97371e0 | 160 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
93c80368 | 161 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
d97371e0 | 162 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
93c80368 NB |
163 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
164 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ | |
74d06cf2 | 165 | D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */ |
07aa0b04 | 166 | |
168d3732 ZW |
167 | /* Use the table to generate a series of prototypes, an enum for the |
168 | directive names, and an array of directive handlers. */ | |
169 | ||
ec5c56db | 170 | /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */ |
711b8824 | 171 | #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *)); |
168d3732 ZW |
172 | DIRECTIVE_TABLE |
173 | #undef D | |
174 | ||
041c3194 | 175 | #define D(n, tag, o, f) tag, |
168d3732 ZW |
176 | enum |
177 | { | |
178 | DIRECTIVE_TABLE | |
179 | N_DIRECTIVES | |
7f2935c7 | 180 | }; |
168d3732 ZW |
181 | #undef D |
182 | ||
ec5c56db | 183 | /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */ |
041c3194 | 184 | #define D(name, t, origin, flags) \ |
562a5c27 | 185 | { CONCAT2(do_,name), (const uchar *) STRINGX(name), \ |
041c3194 | 186 | sizeof STRINGX(name) - 1, origin, flags }, |
93c80368 | 187 | static const directive dtable[] = |
168d3732 ZW |
188 | { |
189 | DIRECTIVE_TABLE | |
190 | }; | |
191 | #undef D | |
192 | #undef DIRECTIVE_TABLE | |
7f2935c7 | 193 | |
dcc229e5 ZW |
194 | /* Wrapper struct directive for linemarkers. |
195 | The origin is more or less true - the original K+R cpp | |
196 | did use this notation in its preprocessed output. */ | |
197 | static const directive linemarker_dir = | |
198 | { | |
199 | do_linemarker, U"#", 1, KANDR, IN_I | |
200 | }; | |
201 | ||
1a76916c | 202 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
67821e3a | 203 | |
93c80368 NB |
204 | /* Skip any remaining tokens in a directive. */ |
205 | static void | |
206 | skip_rest_of_line (pfile) | |
041c3194 | 207 | cpp_reader *pfile; |
c5a04734 | 208 | { |
93c80368 NB |
209 | /* Discard all stacked contexts. */ |
210 | while (pfile->context != &pfile->base_context) | |
211 | _cpp_pop_context (pfile); | |
212 | ||
b528a07e | 213 | /* Sweep up all tokens remaining on the line. */ |
345894b4 NB |
214 | if (! SEEN_EOL ()) |
215 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
216 | ; | |
93c80368 | 217 | } |
c5a04734 | 218 | |
93c80368 NB |
219 | /* Ensure there are no stray tokens at the end of a directive. */ |
220 | static void | |
221 | check_eol (pfile) | |
222 | cpp_reader *pfile; | |
223 | { | |
345894b4 | 224 | if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) |
ebef4e8c NB |
225 | cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive", |
226 | pfile->directive->name); | |
93c80368 NB |
227 | } |
228 | ||
fe6c2db9 NB |
229 | /* Called when entering a directive, _Pragma or command-line directive. */ |
230 | static void | |
231 | start_directive (pfile) | |
93c80368 | 232 | cpp_reader *pfile; |
93c80368 | 233 | { |
7f2f1a66 NB |
234 | /* Setup in-directive state. */ |
235 | pfile->state.in_directive = 1; | |
236 | pfile->state.save_comments = 0; | |
237 | ||
93c80368 | 238 | /* Some handlers need the position of the # for diagnostics. */ |
8bbbef34 | 239 | pfile->directive_line = pfile->line; |
fe6c2db9 NB |
240 | } |
241 | ||
242 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
243 | static void | |
244 | end_directive (pfile, skip_line) | |
245 | cpp_reader *pfile; | |
246 | int skip_line; | |
247 | { | |
1a76916c NB |
248 | if (CPP_OPTION (pfile, traditional)) |
249 | { | |
d97371e0 NB |
250 | /* Revert change of prepare_directive_trad. */ |
251 | pfile->state.prevent_expansion--; | |
252 | ||
b66377c1 | 253 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c NB |
254 | _cpp_remove_overlay (pfile); |
255 | } | |
fe6c2db9 | 256 | /* We don't skip for an assembler #. */ |
b66377c1 | 257 | else if (skip_line) |
67821e3a NB |
258 | { |
259 | skip_rest_of_line (pfile); | |
bdcbe496 NB |
260 | if (!pfile->keep_tokens) |
261 | { | |
262 | pfile->cur_run = &pfile->base_run; | |
263 | pfile->cur_token = pfile->base_run.base; | |
264 | } | |
67821e3a | 265 | } |
fe6c2db9 NB |
266 | |
267 | /* Restore state. */ | |
fe6c2db9 NB |
268 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
269 | pfile->state.in_directive = 0; | |
d97371e0 | 270 | pfile->state.in_expression = 0; |
fe6c2db9 NB |
271 | pfile->state.angled_headers = 0; |
272 | pfile->directive = 0; | |
273 | } | |
274 | ||
1a76916c NB |
275 | /* Prepare to handle the directive in pfile->directive. */ |
276 | static void | |
277 | prepare_directive_trad (pfile) | |
278 | cpp_reader *pfile; | |
279 | { | |
951a0766 | 280 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c | 281 | { |
b66377c1 NB |
282 | bool no_expand = (pfile->directive |
283 | && ! (pfile->directive->flags & EXPAND)); | |
974c43f1 | 284 | bool was_skipping = pfile->state.skipping; |
1a76916c | 285 | |
974c43f1 | 286 | pfile->state.skipping = false; |
d97371e0 NB |
287 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
288 | || pfile->directive == &dtable[T_ELIF]); | |
1a76916c NB |
289 | if (no_expand) |
290 | pfile->state.prevent_expansion++; | |
291 | _cpp_read_logical_line_trad (pfile); | |
292 | if (no_expand) | |
293 | pfile->state.prevent_expansion--; | |
974c43f1 | 294 | pfile->state.skipping = was_skipping; |
1a76916c NB |
295 | _cpp_overlay_buffer (pfile, pfile->out.base, |
296 | pfile->out.cur - pfile->out.base); | |
297 | } | |
d97371e0 NB |
298 | |
299 | /* Stop ISO C from expanding anything. */ | |
300 | pfile->state.prevent_expansion++; | |
1a76916c NB |
301 | } |
302 | ||
18a9d8ff NB |
303 | /* Output diagnostics for a directive DIR. INDENTED is non-zero if |
304 | the '#' was indented. */ | |
18a9d8ff NB |
305 | static void |
306 | directive_diagnostics (pfile, dir, indented) | |
307 | cpp_reader *pfile; | |
308 | const directive *dir; | |
309 | int indented; | |
310 | { | |
dcc229e5 ZW |
311 | /* Issue -pedantic warnings for extensions. */ |
312 | if (CPP_PEDANTIC (pfile) | |
313 | && ! pfile->state.skipping | |
314 | && dir->origin == EXTENSION) | |
ebef4e8c | 315 | cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name); |
dcc229e5 ZW |
316 | |
317 | /* Traditionally, a directive is ignored unless its # is in | |
318 | column 1. Therefore in code intended to work with K+R | |
319 | compilers, directives added by C89 must have their # | |
320 | indented, and directives present in traditional C must not. | |
321 | This is true even of directives in skipped conditional | |
322 | blocks. #elif cannot be used at all. */ | |
323 | if (CPP_WTRADITIONAL (pfile)) | |
18a9d8ff | 324 | { |
dcc229e5 | 325 | if (dir == &dtable[T_ELIF]) |
ebef4e8c NB |
326 | cpp_error (pfile, DL_WARNING, |
327 | "suggest not using #elif in traditional C"); | |
dcc229e5 | 328 | else if (indented && dir->origin == KANDR) |
ebef4e8c NB |
329 | cpp_error (pfile, DL_WARNING, |
330 | "traditional C ignores #%s with the # indented", | |
331 | dir->name); | |
dcc229e5 | 332 | else if (!indented && dir->origin != KANDR) |
ebef4e8c NB |
333 | cpp_error (pfile, DL_WARNING, |
334 | "suggest hiding #%s from traditional C with an indented #", | |
335 | dir->name); | |
18a9d8ff NB |
336 | } |
337 | } | |
338 | ||
339 | /* Check if we have a known directive. INDENTED is non-zero if the | |
340 | '#' of the directive was indented. This function is in this file | |
341 | to save unnecessarily exporting dtable etc. to cpplex.c. Returns | |
342 | non-zero if the line of tokens has been handled, zero if we should | |
343 | continue processing the line. */ | |
fe6c2db9 NB |
344 | int |
345 | _cpp_handle_directive (pfile, indented) | |
346 | cpp_reader *pfile; | |
347 | int indented; | |
348 | { | |
fe6c2db9 | 349 | const directive *dir = 0; |
345894b4 | 350 | const cpp_token *dname; |
e808ec9c | 351 | bool was_parsing_args = pfile->state.parsing_args; |
fe6c2db9 NB |
352 | int skip = 1; |
353 | ||
e808ec9c NB |
354 | if (was_parsing_args) |
355 | { | |
356 | if (CPP_OPTION (pfile, pedantic)) | |
ebef4e8c | 357 | cpp_error (pfile, DL_PEDWARN, |
e808ec9c NB |
358 | "embedding a directive within macro arguments is not portable"); |
359 | pfile->state.parsing_args = 0; | |
360 | pfile->state.prevent_expansion = 0; | |
361 | } | |
fe6c2db9 | 362 | start_directive (pfile); |
345894b4 | 363 | dname = _cpp_lex_token (pfile); |
0d9f234d | 364 | |
345894b4 | 365 | if (dname->type == CPP_NAME) |
0d9f234d | 366 | { |
345894b4 NB |
367 | if (dname->val.node->directive_index) |
368 | dir = &dtable[dname->val.node->directive_index - 1]; | |
0d9f234d | 369 | } |
18a9d8ff NB |
370 | /* We do not recognise the # followed by a number extension in |
371 | assembler code. */ | |
345894b4 | 372 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
0d9f234d | 373 | { |
dcc229e5 ZW |
374 | dir = &linemarker_dir; |
375 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
376 | && ! pfile->state.skipping) | |
ebef4e8c NB |
377 | cpp_error (pfile, DL_PEDWARN, |
378 | "style of line directive is a GCC extension"); | |
93c80368 | 379 | } |
0d9f234d | 380 | |
93c80368 NB |
381 | if (dir) |
382 | { | |
18a9d8ff NB |
383 | /* If we have a directive that is not an opening conditional, |
384 | invalidate any control macro. */ | |
385 | if (! (dir->flags & IF_COND)) | |
386 | pfile->mi_valid = false; | |
387 | ||
388 | /* Kluge alert. In order to be sure that code like this | |
389 | ||
390 | #define HASH # | |
391 | HASH define foo bar | |
392 | ||
393 | does not cause '#define foo bar' to get executed when | |
394 | compiled with -save-temps, we recognize directives in | |
395 | -fpreprocessed mode only if the # is in column 1. cppmacro.c | |
a1f300c0 | 396 | puts a space in front of any '#' at the start of a macro. */ |
18a9d8ff NB |
397 | if (CPP_OPTION (pfile, preprocessed) |
398 | && (indented || !(dir->flags & IN_I))) | |
6d4587f7 | 399 | { |
18a9d8ff NB |
400 | skip = 0; |
401 | dir = 0; | |
6d4587f7 ZW |
402 | } |
403 | else | |
93c80368 | 404 | { |
18a9d8ff NB |
405 | /* In failed conditional groups, all non-conditional |
406 | directives are ignored. Before doing that, whether | |
407 | skipping or not, we should lex angle-bracketed headers | |
408 | correctly, and maybe output some diagnostics. */ | |
409 | pfile->state.angled_headers = dir->flags & INCL; | |
410 | if (! CPP_OPTION (pfile, preprocessed)) | |
411 | directive_diagnostics (pfile, dir, indented); | |
412 | if (pfile->state.skipping && !(dir->flags & COND)) | |
413 | dir = 0; | |
93c80368 NB |
414 | } |
415 | } | |
345894b4 | 416 | else if (dname->type == CPP_EOF) |
18a9d8ff NB |
417 | ; /* CPP_EOF is the "null directive". */ |
418 | else | |
93c80368 NB |
419 | { |
420 | /* An unknown directive. Don't complain about it in assembly | |
421 | source: we don't know where the comments are, and # may | |
422 | introduce assembler pseudo-ops. Don't complain about invalid | |
423 | directives in skipped conditional groups (6.10 p4). */ | |
bdb05a7b | 424 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
18a9d8ff NB |
425 | skip = 0; |
426 | else if (!pfile->state.skipping) | |
ebef4e8c | 427 | cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s", |
345894b4 | 428 | cpp_token_as_text (pfile, dname)); |
0d9f234d NB |
429 | } |
430 | ||
d1a58688 NB |
431 | pfile->directive = dir; |
432 | if (CPP_OPTION (pfile, traditional)) | |
433 | prepare_directive_trad (pfile); | |
434 | ||
18a9d8ff | 435 | if (dir) |
1d63a28a | 436 | (*pfile->directive->handler) (pfile); |
18a9d8ff NB |
437 | else if (skip == 0) |
438 | _cpp_backup_tokens (pfile, 1); | |
439 | ||
440 | end_directive (pfile, skip); | |
e808ec9c NB |
441 | if (was_parsing_args) |
442 | { | |
443 | /* Restore state when within macro args. */ | |
444 | pfile->state.parsing_args = 2; | |
445 | pfile->state.prevent_expansion = 1; | |
446 | pfile->buffer->saved_flags |= PREV_WHITE; | |
447 | } | |
fe6c2db9 | 448 | return skip; |
041c3194 | 449 | } |
7f2935c7 | 450 | |
93c80368 NB |
451 | /* Directive handler wrapper used by the command line option |
452 | processor. */ | |
453 | static void | |
29401c30 | 454 | run_directive (pfile, dir_no, buf, count) |
7f2935c7 | 455 | cpp_reader *pfile; |
93c80368 NB |
456 | int dir_no; |
457 | const char *buf; | |
458 | size_t count; | |
3fdc651f | 459 | { |
562a5c27 | 460 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
29401c30 | 461 | /* from_stage3 */ true, 1); |
0bda4760 | 462 | start_directive (pfile); |
18a9d8ff NB |
463 | /* We don't want a leading # to be interpreted as a directive. */ |
464 | pfile->buffer->saved_flags = 0; | |
f71aebba | 465 | pfile->directive = &dtable[dir_no]; |
1a76916c NB |
466 | if (CPP_OPTION (pfile, traditional)) |
467 | prepare_directive_trad (pfile); | |
f71aebba | 468 | (void) (*pfile->directive->handler) (pfile); |
0bda4760 | 469 | end_directive (pfile, 1); |
ef6e958a | 470 | _cpp_pop_buffer (pfile); |
93c80368 NB |
471 | } |
472 | ||
473 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
474 | #ifndef directives. */ | |
041c3194 | 475 | static cpp_hashnode * |
93c80368 | 476 | lex_macro_node (pfile) |
041c3194 ZW |
477 | cpp_reader *pfile; |
478 | { | |
1a76916c | 479 | const cpp_token *token = _cpp_lex_token (pfile); |
ea4a453b | 480 | |
92936ecf | 481 | /* The token immediately after #define must be an identifier. That |
b8363a24 ZW |
482 | identifier may not be "defined", per C99 6.10.8p4. |
483 | In C++, it may not be any of the "named operators" either, | |
484 | per C++98 [lex.digraph], [lex.key]. | |
485 | Finally, the identifier may not have been poisoned. (In that case | |
1d63a28a | 486 | the lexer has issued the error message for us.) */ |
cbc69f84 | 487 | |
1a76916c NB |
488 | if (token->type == CPP_NAME) |
489 | { | |
490 | cpp_hashnode *node = token->val.node; | |
92936ecf | 491 | |
1a76916c NB |
492 | if (node == pfile->spec_nodes.n_defined) |
493 | cpp_error (pfile, DL_ERROR, | |
494 | "\"defined\" cannot be used as a macro name"); | |
495 | else if (! (node->flags & NODE_POISONED)) | |
496 | return node; | |
ba89d661 | 497 | } |
1a76916c | 498 | else if (token->flags & NAMED_OP) |
cbc69f84 NB |
499 | cpp_error (pfile, DL_ERROR, |
500 | "\"%s\" cannot be used as a macro name as it is an operator in C++", | |
1a76916c NB |
501 | NODE_NAME (token->val.node)); |
502 | else if (token->type == CPP_EOF) | |
503 | cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive", | |
504 | pfile->directive->name); | |
505 | else | |
506 | cpp_error (pfile, DL_ERROR, "macro names must be identifiers"); | |
07aa0b04 | 507 | |
cbc69f84 | 508 | return NULL; |
7f2935c7 | 509 | } |
7f2935c7 | 510 | |
93c80368 | 511 | /* Process a #define directive. Most work is done in cppmacro.c. */ |
711b8824 | 512 | static void |
168d3732 | 513 | do_define (pfile) |
7f2935c7 | 514 | cpp_reader *pfile; |
7f2935c7 | 515 | { |
93c80368 | 516 | cpp_hashnode *node = lex_macro_node (pfile); |
ff2b53ef | 517 | |
93c80368 NB |
518 | if (node) |
519 | { | |
1d63a28a NB |
520 | /* If we have been requested to expand comments into macros, |
521 | then re-enable saving of comments. */ | |
522 | pfile->state.save_comments = | |
523 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
524 | ||
93c80368 NB |
525 | if (_cpp_create_definition (pfile, node)) |
526 | if (pfile->cb.define) | |
8bbbef34 | 527 | (*pfile->cb.define) (pfile, pfile->directive_line, node); |
93c80368 | 528 | } |
041c3194 ZW |
529 | } |
530 | ||
5d8ebbd8 | 531 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
711b8824 | 532 | static void |
041c3194 ZW |
533 | do_undef (pfile) |
534 | cpp_reader *pfile; | |
535 | { | |
df383483 | 536 | cpp_hashnode *node = lex_macro_node (pfile); |
9e62c811 | 537 | |
041c3194 ZW |
538 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier |
539 | is not currently defined as a macro name. */ | |
93c80368 | 540 | if (node && node->type == NT_MACRO) |
9e62c811 | 541 | { |
58fea6af | 542 | if (pfile->cb.undef) |
8bbbef34 | 543 | (*pfile->cb.undef) (pfile, pfile->directive_line, node); |
9e62c811 | 544 | |
618cdda7 | 545 | if (node->flags & NODE_WARN) |
ebef4e8c | 546 | cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node)); |
9e62c811 | 547 | |
041c3194 | 548 | _cpp_free_definition (node); |
9e62c811 | 549 | } |
93c80368 | 550 | check_eol (pfile); |
7f2935c7 PB |
551 | } |
552 | ||
93c80368 NB |
553 | /* Helper routine used by parse_include. Reinterpret the current line |
554 | as an h-char-sequence (< ... >); we are looking at the first token | |
4ed5bcfb NB |
555 | after the <. Returns the header as a token, or NULL on failure. */ |
556 | static const cpp_token * | |
557 | glue_header_name (pfile) | |
93c80368 | 558 | cpp_reader *pfile; |
93c80368 | 559 | { |
4ed5bcfb NB |
560 | cpp_token *header = NULL; |
561 | const cpp_token *token; | |
2450e0b8 NB |
562 | unsigned char *buffer; |
563 | size_t len, total_len = 0, capacity = 1024; | |
93c80368 NB |
564 | |
565 | /* To avoid lexed tokens overwriting our glued name, we can only | |
566 | allocate from the string pool once we've lexed everything. */ | |
2450e0b8 | 567 | buffer = (unsigned char *) xmalloc (capacity); |
93c80368 NB |
568 | for (;;) |
569 | { | |
4ed5bcfb | 570 | token = cpp_get_token (pfile); |
93c80368 | 571 | |
4ed5bcfb | 572 | if (token->type == CPP_GREATER || token->type == CPP_EOF) |
93c80368 NB |
573 | break; |
574 | ||
2450e0b8 NB |
575 | len = cpp_token_len (token); |
576 | if (total_len + len > capacity) | |
93c80368 | 577 | { |
2450e0b8 NB |
578 | capacity = (capacity + len) * 2; |
579 | buffer = (unsigned char *) xrealloc (buffer, capacity); | |
93c80368 NB |
580 | } |
581 | ||
4ed5bcfb | 582 | if (token->flags & PREV_WHITE) |
2450e0b8 | 583 | buffer[total_len++] = ' '; |
93c80368 | 584 | |
2450e0b8 | 585 | total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer; |
93c80368 NB |
586 | } |
587 | ||
4ed5bcfb | 588 | if (token->type == CPP_EOF) |
ebef4e8c | 589 | cpp_error (pfile, DL_ERROR, "missing terminating > character"); |
93c80368 NB |
590 | else |
591 | { | |
2450e0b8 NB |
592 | unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1); |
593 | memcpy (token_mem, buffer, total_len); | |
594 | token_mem[total_len] = '\0'; | |
595 | ||
4ed5bcfb | 596 | header = _cpp_temp_token (pfile); |
93c80368 | 597 | header->type = CPP_HEADER_NAME; |
ece54d54 | 598 | header->flags = 0; |
2450e0b8 NB |
599 | header->val.str.len = total_len; |
600 | header->val.str.text = token_mem; | |
93c80368 | 601 | } |
041c3194 | 602 | |
2450e0b8 | 603 | free ((PTR) buffer); |
4ed5bcfb | 604 | return header; |
93c80368 | 605 | } |
7f2935c7 | 606 | |
4ed5bcfb NB |
607 | /* Returns the header string of #include, #include_next, #import and |
608 | #pragma dependency. Returns NULL on error. */ | |
609 | static const cpp_token * | |
610 | parse_include (pfile) | |
7f2935c7 PB |
611 | cpp_reader *pfile; |
612 | { | |
93c80368 | 613 | const unsigned char *dir; |
4ed5bcfb | 614 | const cpp_token *header; |
7f2935c7 | 615 | |
09b82253 | 616 | if (pfile->directive == &dtable[T_PRAGMA]) |
93c80368 NB |
617 | dir = U"pragma dependency"; |
618 | else | |
619 | dir = pfile->directive->name; | |
620 | ||
621 | /* Allow macro expansion. */ | |
4ed5bcfb | 622 | header = cpp_get_token (pfile); |
93c80368 | 623 | if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME) |
7f2935c7 | 624 | { |
93c80368 | 625 | if (header->type != CPP_LESS) |
041c3194 | 626 | { |
ebef4e8c NB |
627 | cpp_error (pfile, DL_ERROR, |
628 | "#%s expects \"FILENAME\" or <FILENAME>", dir); | |
4ed5bcfb | 629 | return NULL; |
041c3194 | 630 | } |
4ed5bcfb NB |
631 | |
632 | header = glue_header_name (pfile); | |
633 | if (header == NULL) | |
634 | return header; | |
7f2935c7 | 635 | } |
93c80368 NB |
636 | |
637 | if (header->val.str.len == 0) | |
7f2935c7 | 638 | { |
ebef4e8c | 639 | cpp_error (pfile, DL_ERROR, "empty file name in #%s", dir); |
4ed5bcfb | 640 | return NULL; |
7f2935c7 | 641 | } |
7f2935c7 | 642 | |
4ed5bcfb | 643 | return header; |
168d3732 | 644 | } |
3caee4a8 | 645 | |
ba133c96 | 646 | /* Handle #include, #include_next and #import. */ |
711b8824 | 647 | static void |
ba133c96 | 648 | do_include_common (pfile, type) |
168d3732 | 649 | cpp_reader *pfile; |
ba133c96 | 650 | enum include_type type; |
168d3732 | 651 | { |
4ed5bcfb | 652 | const cpp_token *header; |
7f2935c7 | 653 | |
09b82253 NB |
654 | /* For #include_next, if this is the primary source file, warn and |
655 | use the normal search logic. */ | |
656 | if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev) | |
657 | { | |
ebef4e8c | 658 | cpp_error (pfile, DL_WARNING, "#include_next in primary source file"); |
09b82253 NB |
659 | type = IT_INCLUDE; |
660 | } | |
661 | else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import)) | |
662 | { | |
663 | CPP_OPTION (pfile, warn_import) = 0; | |
ebef4e8c | 664 | cpp_error (pfile, DL_WARNING, |
09b82253 NB |
665 | "#import is obsolete, use an #ifndef wrapper in the header file"); |
666 | } | |
667 | ||
4ed5bcfb NB |
668 | header = parse_include (pfile); |
669 | if (header) | |
ba133c96 NB |
670 | { |
671 | /* Prevent #include recursion. */ | |
d8693c6f | 672 | if (pfile->line_maps.depth >= CPP_STACK_MAX) |
bdee42b1 | 673 | cpp_error (pfile, DL_ERROR, "#include nested too deeply"); |
ba133c96 NB |
674 | else |
675 | { | |
09b82253 NB |
676 | check_eol (pfile); |
677 | /* Get out of macro context, if we are. */ | |
bdcbe496 | 678 | skip_rest_of_line (pfile); |
09b82253 | 679 | if (pfile->cb.include) |
8bbbef34 | 680 | (*pfile->cb.include) (pfile, pfile->directive_line, |
4ed5bcfb | 681 | pfile->directive->name, header); |
4ed5bcfb | 682 | _cpp_execute_include (pfile, header, type); |
ba133c96 NB |
683 | } |
684 | } | |
168d3732 | 685 | } |
e8037d57 | 686 | |
711b8824 | 687 | static void |
ba133c96 | 688 | do_include (pfile) |
168d3732 ZW |
689 | cpp_reader *pfile; |
690 | { | |
ba133c96 NB |
691 | do_include_common (pfile, IT_INCLUDE); |
692 | } | |
168d3732 | 693 | |
ba133c96 NB |
694 | static void |
695 | do_import (pfile) | |
696 | cpp_reader *pfile; | |
697 | { | |
ba133c96 | 698 | do_include_common (pfile, IT_IMPORT); |
168d3732 | 699 | } |
7f2935c7 | 700 | |
711b8824 | 701 | static void |
168d3732 ZW |
702 | do_include_next (pfile) |
703 | cpp_reader *pfile; | |
704 | { | |
ba133c96 | 705 | do_include_common (pfile, IT_INCLUDE_NEXT); |
7f2935c7 | 706 | } |
7f2935c7 | 707 | |
dcc229e5 ZW |
708 | /* Subroutine of do_linemarker. Read possible flags after file name. |
709 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
710 | flag if it is valid, 0 at the end of the directive. Otherwise | |
711 | complain. */ | |
642ce434 | 712 | static unsigned int |
28e0f040 | 713 | read_flag (pfile, last) |
d3a34a0a | 714 | cpp_reader *pfile; |
28e0f040 | 715 | unsigned int last; |
d3a34a0a | 716 | { |
345894b4 | 717 | const cpp_token *token = _cpp_lex_token (pfile); |
d3a34a0a | 718 | |
345894b4 | 719 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
d3a34a0a | 720 | { |
345894b4 | 721 | unsigned int flag = token->val.str.text[0] - '0'; |
28e0f040 NB |
722 | |
723 | if (flag > last && flag <= 4 | |
724 | && (flag != 4 || last == 3) | |
725 | && (flag != 2 || last == 0)) | |
726 | return flag; | |
d3a34a0a | 727 | } |
93c80368 | 728 | |
345894b4 | 729 | if (token->type != CPP_EOF) |
ebef4e8c | 730 | cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive", |
345894b4 | 731 | cpp_token_as_text (pfile, token)); |
93c80368 | 732 | return 0; |
d3a34a0a JM |
733 | } |
734 | ||
dcc229e5 ZW |
735 | /* Subroutine of do_line and do_linemarker. Returns a version of STR |
736 | which has a NUL terminator and all escape sequences converted to | |
737 | their equivalents. Temporary, hopefully. */ | |
562a5c27 | 738 | static uchar * |
dcc229e5 ZW |
739 | dequote_string (pfile, str, len) |
740 | cpp_reader *pfile; | |
562a5c27 | 741 | const uchar *str; |
dcc229e5 ZW |
742 | unsigned int len; |
743 | { | |
562a5c27 NB |
744 | uchar *result = _cpp_unaligned_alloc (pfile, len + 1); |
745 | uchar *dst = result; | |
746 | const uchar *limit = str + len; | |
4268e8bb | 747 | cppchar_t c; |
dcc229e5 | 748 | |
dcc229e5 ZW |
749 | while (str < limit) |
750 | { | |
751 | c = *str++; | |
752 | if (c != '\\') | |
753 | *dst++ = c; | |
754 | else | |
4268e8bb | 755 | *dst++ = cpp_parse_escape (pfile, &str, limit, 0); |
dcc229e5 ZW |
756 | } |
757 | *dst++ = '\0'; | |
758 | return result; | |
759 | } | |
760 | ||
761 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, | |
762 | of length LEN, to binary; store it in NUMP, and return 0 if the | |
763 | number was well-formed, 1 if not. Temporary, hopefully. */ | |
041c3194 ZW |
764 | static int |
765 | strtoul_for_line (str, len, nump) | |
562a5c27 | 766 | const uchar *str; |
041c3194 ZW |
767 | unsigned int len; |
768 | unsigned long *nump; | |
769 | { | |
770 | unsigned long reg = 0; | |
562a5c27 | 771 | uchar c; |
041c3194 ZW |
772 | while (len--) |
773 | { | |
774 | c = *str++; | |
775 | if (!ISDIGIT (c)) | |
776 | return 1; | |
777 | reg *= 10; | |
778 | reg += c - '0'; | |
779 | } | |
780 | *nump = reg; | |
781 | return 0; | |
782 | } | |
783 | ||
6de1e2a9 | 784 | /* Interpret #line command. |
dcc229e5 ZW |
785 | Note that the filename string (if any) is a true string constant |
786 | (escapes are interpreted), unlike in #line. */ | |
711b8824 | 787 | static void |
168d3732 | 788 | do_line (pfile) |
7f2935c7 | 789 | cpp_reader *pfile; |
7f2935c7 | 790 | { |
4ed5bcfb | 791 | const cpp_token *token; |
bb74c963 NB |
792 | const char *new_file = pfile->map->to_file; |
793 | unsigned long new_lineno; | |
93c80368 | 794 | |
27e2564a | 795 | /* C99 raised the minimum limit on #line numbers. */ |
dcc229e5 | 796 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
18a9d8ff | 797 | |
93c80368 | 798 | /* #line commands expand macros. */ |
4ed5bcfb NB |
799 | token = cpp_get_token (pfile); |
800 | if (token->type != CPP_NUMBER | |
801 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
802 | &new_lineno)) | |
7f2935c7 | 803 | { |
ebef4e8c NB |
804 | cpp_error (pfile, DL_ERROR, |
805 | "\"%s\" after #line is not a positive integer", | |
4ed5bcfb | 806 | cpp_token_as_text (pfile, token)); |
9ec7291f | 807 | return; |
df383483 | 808 | } |
7f2935c7 | 809 | |
dcc229e5 | 810 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
ebef4e8c | 811 | cpp_error (pfile, DL_PEDWARN, "line number out of range"); |
7f2935c7 | 812 | |
4ed5bcfb NB |
813 | token = cpp_get_token (pfile); |
814 | if (token->type == CPP_STRING) | |
5538ada6 | 815 | { |
dcc229e5 ZW |
816 | new_file = (const char *) dequote_string (pfile, token->val.str.text, |
817 | token->val.str.len); | |
818 | check_eol (pfile); | |
819 | } | |
820 | else if (token->type != CPP_EOF) | |
821 | { | |
ebef4e8c | 822 | cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename", |
dcc229e5 ZW |
823 | cpp_token_as_text (pfile, token)); |
824 | return; | |
825 | } | |
826 | ||
827 | skip_rest_of_line (pfile); | |
828 | _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, | |
829 | pfile->map->sysp); | |
830 | } | |
831 | ||
832 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
833 | different syntax and semantics from #line: Flags are allowed, | |
834 | and we never complain about the line number being too big. */ | |
835 | static void | |
836 | do_linemarker (pfile) | |
837 | cpp_reader *pfile; | |
838 | { | |
839 | const cpp_token *token; | |
840 | const char *new_file = pfile->map->to_file; | |
841 | unsigned long new_lineno; | |
842 | unsigned int new_sysp = pfile->map->sysp; | |
843 | enum lc_reason reason = LC_RENAME; | |
844 | int flag; | |
845 | ||
846 | /* Back up so we can get the number again. Putting this in | |
847 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
848 | some circumstances, which can segfault. */ | |
849 | _cpp_backup_tokens (pfile, 1); | |
850 | ||
851 | /* #line commands expand macros. */ | |
852 | token = cpp_get_token (pfile); | |
853 | if (token->type != CPP_NUMBER | |
854 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
855 | &new_lineno)) | |
856 | { | |
ebef4e8c | 857 | cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer", |
dcc229e5 ZW |
858 | cpp_token_as_text (pfile, token)); |
859 | return; | |
df383483 | 860 | } |
941e09b6 | 861 | |
dcc229e5 ZW |
862 | token = cpp_get_token (pfile); |
863 | if (token->type == CPP_STRING) | |
864 | { | |
865 | new_file = (const char *) dequote_string (pfile, token->val.str.text, | |
866 | token->val.str.len); | |
867 | new_sysp = 0; | |
868 | flag = read_flag (pfile, 0); | |
869 | if (flag == 1) | |
870 | { | |
871 | reason = LC_ENTER; | |
872 | /* Fake an include for cpp_included (). */ | |
873 | _cpp_fake_include (pfile, new_file); | |
874 | flag = read_flag (pfile, flag); | |
875 | } | |
876 | else if (flag == 2) | |
877 | { | |
878 | reason = LC_LEAVE; | |
879 | flag = read_flag (pfile, flag); | |
880 | } | |
881 | if (flag == 3) | |
93c80368 | 882 | { |
dcc229e5 ZW |
883 | new_sysp = 1; |
884 | flag = read_flag (pfile, flag); | |
885 | if (flag == 4) | |
886 | new_sysp = 2; | |
93c80368 | 887 | } |
dcc229e5 | 888 | |
fde84349 | 889 | check_eol (pfile); |
041c3194 | 890 | } |
4ed5bcfb | 891 | else if (token->type != CPP_EOF) |
27e2564a | 892 | { |
ebef4e8c | 893 | cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename", |
4ed5bcfb | 894 | cpp_token_as_text (pfile, token)); |
27e2564a NB |
895 | return; |
896 | } | |
7f2935c7 | 897 | |
bdcbe496 | 898 | skip_rest_of_line (pfile); |
bb74c963 | 899 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
27e2564a NB |
900 | } |
901 | ||
67821e3a | 902 | /* Arrange the file_change callback. pfile->line has changed to |
47d89cf3 | 903 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
a1f300c0 | 904 | header, 2 for a system header that needs to be extern "C" protected, |
47d89cf3 | 905 | and zero otherwise. */ |
eb1f4d9d | 906 | void |
47d89cf3 | 907 | _cpp_do_file_change (pfile, reason, to_file, file_line, sysp) |
27e2564a | 908 | cpp_reader *pfile; |
d82fc108 | 909 | enum lc_reason reason; |
47d89cf3 | 910 | const char *to_file; |
67821e3a | 911 | unsigned int file_line; |
47d89cf3 | 912 | unsigned int sysp; |
27e2564a | 913 | { |
47d89cf3 NB |
914 | pfile->map = add_line_map (&pfile->line_maps, reason, sysp, |
915 | pfile->line, to_file, file_line); | |
d82fc108 | 916 | |
eb1f4d9d | 917 | if (pfile->cb.file_change) |
47d89cf3 | 918 | (*pfile->cb.file_change) (pfile, pfile->map); |
7f2935c7 | 919 | } |
941e09b6 | 920 | |
5d8ebbd8 NB |
921 | /* Report a warning or error detected by the program we are |
922 | processing. Use the directive's tokens in the error message. */ | |
711b8824 | 923 | static void |
29b10746 | 924 | do_diagnostic (pfile, code, print_dir) |
7f2935c7 | 925 | cpp_reader *pfile; |
ebef4e8c | 926 | int code; |
29b10746 | 927 | int print_dir; |
7f2935c7 | 928 | { |
ebef4e8c NB |
929 | if (_cpp_begin_message (pfile, code, |
930 | pfile->cur_token[-1].line, | |
931 | pfile->cur_token[-1].col)) | |
58fea6af | 932 | { |
29b10746 NB |
933 | if (print_dir) |
934 | fprintf (stderr, "#%s ", pfile->directive->name); | |
93c80368 NB |
935 | pfile->state.prevent_expansion++; |
936 | cpp_output_line (pfile, stderr); | |
937 | pfile->state.prevent_expansion--; | |
58fea6af | 938 | } |
7f2935c7 PB |
939 | } |
940 | ||
838f313b NB |
941 | static void |
942 | do_error (pfile) | |
943 | cpp_reader *pfile; | |
944 | { | |
ebef4e8c | 945 | do_diagnostic (pfile, DL_ERROR, 1); |
838f313b | 946 | } |
7f2935c7 | 947 | |
711b8824 | 948 | static void |
168d3732 | 949 | do_warning (pfile) |
7f2935c7 | 950 | cpp_reader *pfile; |
7f2935c7 | 951 | { |
2f878973 | 952 | /* We want #warning diagnostics to be emitted in system headers too. */ |
ebef4e8c | 953 | do_diagnostic (pfile, DL_WARNING_SYSHDR, 1); |
7f2935c7 PB |
954 | } |
955 | ||
a2a76ce7 | 956 | /* Report program identification. */ |
711b8824 | 957 | static void |
168d3732 | 958 | do_ident (pfile) |
7f2935c7 | 959 | cpp_reader *pfile; |
7f2935c7 | 960 | { |
4ed5bcfb | 961 | const cpp_token *str = cpp_get_token (pfile); |
58fea6af | 962 | |
4ed5bcfb | 963 | if (str->type != CPP_STRING) |
ebef4e8c | 964 | cpp_error (pfile, DL_ERROR, "invalid #ident directive"); |
93c80368 | 965 | else if (pfile->cb.ident) |
4ed5bcfb | 966 | (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str); |
a2a76ce7 | 967 | |
93c80368 | 968 | check_eol (pfile); |
7f2935c7 PB |
969 | } |
970 | ||
a5da89c6 NB |
971 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
972 | matching entry, or NULL if none is found. The returned entry could | |
973 | be the start of a namespace chain, or a pragma. */ | |
974 | static struct pragma_entry * | |
975 | lookup_pragma_entry (chain, pragma) | |
976 | struct pragma_entry *chain; | |
4b115ff0 | 977 | const cpp_hashnode *pragma; |
82443371 | 978 | { |
4b115ff0 NB |
979 | while (chain && chain->pragma != pragma) |
980 | chain = chain->next; | |
a5da89c6 NB |
981 | |
982 | return chain; | |
983 | } | |
984 | ||
985 | /* Create and insert a pragma entry for NAME at the beginning of a | |
986 | singly-linked CHAIN. If handler is NULL, it is a namespace, | |
987 | otherwise it is a pragma and its handler. */ | |
988 | static struct pragma_entry * | |
4b115ff0 | 989 | insert_pragma_entry (pfile, chain, pragma, handler) |
58fea6af | 990 | cpp_reader *pfile; |
a5da89c6 | 991 | struct pragma_entry **chain; |
4b115ff0 | 992 | const cpp_hashnode *pragma; |
d82fc108 | 993 | pragma_cb handler; |
82443371 | 994 | { |
a5da89c6 | 995 | struct pragma_entry *new; |
58fea6af | 996 | |
bef985f3 | 997 | new = (struct pragma_entry *) |
8c3b2693 | 998 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
4b115ff0 | 999 | new->pragma = pragma; |
a5da89c6 NB |
1000 | if (handler) |
1001 | { | |
1002 | new->is_nspace = 0; | |
1003 | new->u.handler = handler; | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | new->is_nspace = 1; | |
1008 | new->u.space = NULL; | |
1009 | } | |
58fea6af | 1010 | |
a5da89c6 NB |
1011 | new->next = *chain; |
1012 | *chain = new; | |
1013 | return new; | |
58fea6af | 1014 | } |
82443371 | 1015 | |
a5da89c6 NB |
1016 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
1017 | goes in the global namespace. HANDLER is the handler it will call, | |
1018 | which must be non-NULL. */ | |
58fea6af | 1019 | void |
a5da89c6 | 1020 | cpp_register_pragma (pfile, space, name, handler) |
82443371 | 1021 | cpp_reader *pfile; |
58fea6af | 1022 | const char *space; |
a5da89c6 NB |
1023 | const char *name; |
1024 | pragma_cb handler; | |
82443371 | 1025 | { |
a5da89c6 NB |
1026 | struct pragma_entry **chain = &pfile->pragmas; |
1027 | struct pragma_entry *entry; | |
4b115ff0 | 1028 | const cpp_hashnode *node; |
a5da89c6 NB |
1029 | |
1030 | if (!handler) | |
1031 | abort (); | |
58fea6af | 1032 | |
a5da89c6 | 1033 | if (space) |
58fea6af | 1034 | { |
4b115ff0 NB |
1035 | node = cpp_lookup (pfile, U space, strlen (space)); |
1036 | entry = lookup_pragma_entry (*chain, node); | |
a5da89c6 | 1037 | if (!entry) |
4b115ff0 | 1038 | entry = insert_pragma_entry (pfile, chain, node, NULL); |
a5da89c6 NB |
1039 | else if (!entry->is_nspace) |
1040 | goto clash; | |
1041 | chain = &entry->u.space; | |
58fea6af ZW |
1042 | } |
1043 | ||
a5da89c6 | 1044 | /* Check for duplicates. */ |
4b115ff0 NB |
1045 | node = cpp_lookup (pfile, U name, strlen (name)); |
1046 | entry = lookup_pragma_entry (*chain, node); | |
a5da89c6 NB |
1047 | if (entry) |
1048 | { | |
1049 | if (entry->is_nspace) | |
1050 | clash: | |
ebef4e8c | 1051 | cpp_error (pfile, DL_ICE, |
a5da89c6 | 1052 | "registering \"%s\" as both a pragma and a pragma namespace", |
4b115ff0 | 1053 | NODE_NAME (node)); |
a5da89c6 | 1054 | else if (space) |
ebef4e8c NB |
1055 | cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered", |
1056 | space, name); | |
a5da89c6 | 1057 | else |
ebef4e8c | 1058 | cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name); |
a5da89c6 NB |
1059 | } |
1060 | else | |
4b115ff0 | 1061 | insert_pragma_entry (pfile, chain, node, handler); |
58fea6af | 1062 | } |
a5da89c6 NB |
1063 | |
1064 | /* Register the pragmas the preprocessor itself handles. */ | |
58fea6af ZW |
1065 | void |
1066 | _cpp_init_internal_pragmas (pfile) | |
1067 | cpp_reader *pfile; | |
1068 | { | |
a5da89c6 | 1069 | /* Pragmas in the global namespace. */ |
58fea6af ZW |
1070 | cpp_register_pragma (pfile, 0, "once", do_pragma_once); |
1071 | ||
a5da89c6 | 1072 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
58fea6af ZW |
1073 | cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison); |
1074 | cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header); | |
1075 | cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency); | |
82443371 | 1076 | } |
7f2935c7 | 1077 | |
a5da89c6 NB |
1078 | /* Pragmata handling. We handle some, and pass the rest on to the |
1079 | front end. C99 defines three pragmas and says that no macro | |
1080 | expansion is to be performed on them; whether or not macro | |
1081 | expansion happens for other pragmas is implementation defined. | |
1082 | This implementation never macro-expands the text after #pragma. */ | |
711b8824 | 1083 | static void |
168d3732 | 1084 | do_pragma (pfile) |
7f2935c7 | 1085 | cpp_reader *pfile; |
7f2935c7 | 1086 | { |
a5da89c6 | 1087 | const struct pragma_entry *p = NULL; |
4ed5bcfb | 1088 | const cpp_token *token; |
a5da89c6 | 1089 | unsigned int count = 1; |
add7091b | 1090 | |
93c80368 | 1091 | pfile->state.prevent_expansion++; |
58fea6af | 1092 | |
4ed5bcfb NB |
1093 | token = cpp_get_token (pfile); |
1094 | if (token->type == CPP_NAME) | |
0172e2bc | 1095 | { |
4b115ff0 | 1096 | p = lookup_pragma_entry (pfile->pragmas, token->val.node); |
a5da89c6 | 1097 | if (p && p->is_nspace) |
58fea6af | 1098 | { |
a5da89c6 NB |
1099 | count = 2; |
1100 | token = cpp_get_token (pfile); | |
1101 | if (token->type == CPP_NAME) | |
4b115ff0 | 1102 | p = lookup_pragma_entry (p->u.space, token->val.node); |
a5da89c6 NB |
1103 | else |
1104 | p = NULL; | |
58fea6af | 1105 | } |
58fea6af | 1106 | } |
041c3194 | 1107 | |
97293897 NB |
1108 | /* FIXME. This is an awful kludge to get the front ends to update |
1109 | their notion of line number for diagnostic purposes. The line | |
1110 | number should be passed to the handler and they should do it | |
1111 | themselves. Stand-alone CPP must ignore us, otherwise it will | |
1112 | prefix the directive with spaces, hence the 1. Ugh. */ | |
1113 | if (pfile->cb.line_change) | |
4ed5bcfb | 1114 | (*pfile->cb.line_change)(pfile, token, 1); |
97293897 | 1115 | |
a5da89c6 NB |
1116 | if (p) |
1117 | (*p->u.handler) (pfile); | |
d82fc108 | 1118 | else if (pfile->cb.def_pragma) |
bdcbe496 NB |
1119 | { |
1120 | _cpp_backup_tokens (pfile, count); | |
1121 | (*pfile->cb.def_pragma) (pfile, pfile->directive_line); | |
1122 | } | |
a5da89c6 | 1123 | |
97293897 | 1124 | pfile->state.prevent_expansion--; |
82443371 NS |
1125 | } |
1126 | ||
5d8ebbd8 | 1127 | /* Handle #pragma once. */ |
58fea6af | 1128 | static void |
a2a76ce7 ZW |
1129 | do_pragma_once (pfile) |
1130 | cpp_reader *pfile; | |
1131 | { | |
ebef4e8c | 1132 | cpp_error (pfile, DL_WARNING, "#pragma once is obsolete"); |
df383483 | 1133 | |
642ce434 | 1134 | if (pfile->buffer->prev == NULL) |
ebef4e8c | 1135 | cpp_error (pfile, DL_WARNING, "#pragma once in main file"); |
a2a76ce7 | 1136 | else |
642ce434 | 1137 | _cpp_never_reread (pfile->buffer->inc); |
93c80368 NB |
1138 | |
1139 | check_eol (pfile); | |
a2a76ce7 | 1140 | } |
fc009f96 | 1141 | |
c3bf3e6e NB |
1142 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1143 | that the lexer produces a hard error for each subsequent usage. */ | |
58fea6af | 1144 | static void |
a2a76ce7 ZW |
1145 | do_pragma_poison (pfile) |
1146 | cpp_reader *pfile; | |
1147 | { | |
345894b4 | 1148 | const cpp_token *tok; |
f8f769ea | 1149 | cpp_hashnode *hp; |
a2a76ce7 | 1150 | |
93c80368 | 1151 | pfile->state.poisoned_ok = 1; |
a2a76ce7 ZW |
1152 | for (;;) |
1153 | { | |
345894b4 NB |
1154 | tok = _cpp_lex_token (pfile); |
1155 | if (tok->type == CPP_EOF) | |
a2a76ce7 | 1156 | break; |
345894b4 | 1157 | if (tok->type != CPP_NAME) |
fc009f96 | 1158 | { |
ebef4e8c | 1159 | cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive"); |
93c80368 | 1160 | break; |
fc009f96 GK |
1161 | } |
1162 | ||
345894b4 | 1163 | hp = tok->val.node; |
93c80368 NB |
1164 | if (hp->flags & NODE_POISONED) |
1165 | continue; | |
1166 | ||
1167 | if (hp->type == NT_MACRO) | |
ebef4e8c NB |
1168 | cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"", |
1169 | NODE_NAME (hp)); | |
93c80368 NB |
1170 | _cpp_free_definition (hp); |
1171 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
fc009f96 | 1172 | } |
93c80368 | 1173 | pfile->state.poisoned_ok = 0; |
7f2935c7 | 1174 | } |
2c0b35cb ZW |
1175 | |
1176 | /* Mark the current header as a system header. This will suppress | |
1177 | some categories of warnings (notably those from -pedantic). It is | |
1178 | intended for use in system libraries that cannot be implemented in | |
1179 | conforming C, but cannot be certain that their headers appear in a | |
1180 | system include directory. To prevent abuse, it is rejected in the | |
1181 | primary source file. */ | |
58fea6af | 1182 | static void |
2c0b35cb ZW |
1183 | do_pragma_system_header (pfile) |
1184 | cpp_reader *pfile; | |
1185 | { | |
614c7d37 NB |
1186 | cpp_buffer *buffer = pfile->buffer; |
1187 | ||
1188 | if (buffer->prev == 0) | |
ebef4e8c NB |
1189 | cpp_error (pfile, DL_WARNING, |
1190 | "#pragma system_header ignored outside include file"); | |
2c0b35cb | 1191 | else |
d82fc108 NB |
1192 | { |
1193 | check_eol (pfile); | |
bdcbe496 | 1194 | skip_rest_of_line (pfile); |
d82fc108 NB |
1195 | cpp_make_system_header (pfile, 1, 0); |
1196 | } | |
2c0b35cb | 1197 | } |
f3f751ad NS |
1198 | |
1199 | /* Check the modified date of the current include file against a specified | |
1200 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1201 | determine if a fixed header should be refixed. */ | |
58fea6af | 1202 | static void |
f3f751ad NS |
1203 | do_pragma_dependency (pfile) |
1204 | cpp_reader *pfile; | |
1205 | { | |
4ed5bcfb | 1206 | const cpp_token *header; |
93c80368 | 1207 | int ordering; |
df383483 | 1208 | |
4ed5bcfb NB |
1209 | header = parse_include (pfile); |
1210 | if (!header) | |
58fea6af | 1211 | return; |
041c3194 | 1212 | |
4ed5bcfb | 1213 | ordering = _cpp_compare_file_date (pfile, header); |
f3f751ad | 1214 | if (ordering < 0) |
ebef4e8c NB |
1215 | cpp_error (pfile, DL_WARNING, "cannot find source %s", |
1216 | cpp_token_as_text (pfile, header)); | |
f3f751ad NS |
1217 | else if (ordering > 0) |
1218 | { | |
ebef4e8c NB |
1219 | cpp_error (pfile, DL_WARNING, "current file is older than %s", |
1220 | cpp_token_as_text (pfile, header)); | |
4ed5bcfb | 1221 | if (cpp_get_token (pfile)->type != CPP_EOF) |
bdcbe496 NB |
1222 | { |
1223 | _cpp_backup_tokens (pfile, 1); | |
ebef4e8c | 1224 | do_diagnostic (pfile, DL_WARNING, 0); |
bdcbe496 | 1225 | } |
f3f751ad | 1226 | } |
f3f751ad NS |
1227 | } |
1228 | ||
4ed5bcfb NB |
1229 | /* Get a token but skip padding. */ |
1230 | static const cpp_token * | |
1231 | get_token_no_padding (pfile) | |
a5c3cccd | 1232 | cpp_reader *pfile; |
a5c3cccd | 1233 | { |
4ed5bcfb NB |
1234 | for (;;) |
1235 | { | |
1236 | const cpp_token *result = cpp_get_token (pfile); | |
1237 | if (result->type != CPP_PADDING) | |
1238 | return result; | |
1239 | } | |
1240 | } | |
a5c3cccd | 1241 | |
4ed5bcfb NB |
1242 | /* Check syntax is "(string-literal)". Returns the string on success, |
1243 | or NULL on failure. */ | |
1244 | static const cpp_token * | |
1245 | get__Pragma_string (pfile) | |
1246 | cpp_reader *pfile; | |
1247 | { | |
1248 | const cpp_token *string; | |
a5c3cccd | 1249 | |
4ed5bcfb NB |
1250 | if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN) |
1251 | return NULL; | |
1252 | ||
1253 | string = get_token_no_padding (pfile); | |
a5c3cccd | 1254 | if (string->type != CPP_STRING && string->type != CPP_WSTRING) |
4ed5bcfb NB |
1255 | return NULL; |
1256 | ||
1257 | if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) | |
1258 | return NULL; | |
a5c3cccd | 1259 | |
4ed5bcfb | 1260 | return string; |
a5c3cccd NB |
1261 | } |
1262 | ||
87062813 NB |
1263 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1264 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1265 | static void | |
1266 | destringize_and_run (pfile, in) | |
1267 | cpp_reader *pfile; | |
a5c3cccd | 1268 | const cpp_string *in; |
a5c3cccd NB |
1269 | { |
1270 | const unsigned char *src, *limit; | |
87062813 | 1271 | char *dest, *result; |
a5c3cccd | 1272 | |
4d6baafa | 1273 | dest = result = alloca (in->len + 1); |
a5c3cccd NB |
1274 | for (src = in->text, limit = src + in->len; src < limit;) |
1275 | { | |
1276 | /* We know there is a character following the backslash. */ | |
1277 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1278 | src++; | |
1279 | *dest++ = *src++; | |
1280 | } | |
4d6baafa | 1281 | *dest = '\0'; |
a5c3cccd | 1282 | |
87062813 | 1283 | run_directive (pfile, T_PRAGMA, result, dest - result); |
a5c3cccd NB |
1284 | } |
1285 | ||
87062813 | 1286 | /* Handle the _Pragma operator. */ |
a5c3cccd NB |
1287 | void |
1288 | _cpp_do__Pragma (pfile) | |
1289 | cpp_reader *pfile; | |
1290 | { | |
4ed5bcfb | 1291 | const cpp_token *string = get__Pragma_string (pfile); |
a5c3cccd | 1292 | |
4ed5bcfb | 1293 | if (!string) |
ebef4e8c NB |
1294 | cpp_error (pfile, DL_ERROR, |
1295 | "_Pragma takes a parenthesized string literal"); | |
67821e3a | 1296 | else |
a5c3cccd | 1297 | { |
50410426 NB |
1298 | /* Ideally, we'd like |
1299 | token1 _Pragma ("foo") token2 | |
1300 | to be output as | |
1301 | token1 | |
1302 | # 7 "file.c" | |
1303 | #pragma foo | |
1304 | # 7 "file.c" | |
1305 | token2 | |
1306 | Getting these correct line markers is a little tricky. */ | |
1307 | ||
1308 | unsigned int orig_line = pfile->line; | |
87062813 | 1309 | destringize_and_run (pfile, &string->val.str); |
50410426 NB |
1310 | pfile->line = orig_line; |
1311 | pfile->buffer->saved_flags = BOL; | |
a5c3cccd | 1312 | } |
a5c3cccd NB |
1313 | } |
1314 | ||
74d06cf2 | 1315 | /* Just ignore #sccs on all systems. */ |
711b8824 | 1316 | static void |
168d3732 | 1317 | do_sccs (pfile) |
041c3194 | 1318 | cpp_reader *pfile ATTRIBUTE_UNUSED; |
7f2935c7 | 1319 | { |
7f2935c7 | 1320 | } |
1d0e51ba | 1321 | |
5d8ebbd8 | 1322 | /* Handle #ifdef. */ |
711b8824 | 1323 | static void |
168d3732 ZW |
1324 | do_ifdef (pfile) |
1325 | cpp_reader *pfile; | |
1326 | { | |
93c80368 | 1327 | int skip = 1; |
041c3194 | 1328 | |
cef0d199 | 1329 | if (! pfile->state.skipping) |
93c80368 NB |
1330 | { |
1331 | const cpp_hashnode *node = lex_macro_node (pfile); | |
041c3194 | 1332 | |
93c80368 NB |
1333 | if (node) |
1334 | skip = node->type != NT_MACRO; | |
b43db0b3 GK |
1335 | |
1336 | if (node) | |
1337 | check_eol (pfile); | |
93c80368 | 1338 | } |
168d3732 | 1339 | |
93c80368 NB |
1340 | push_conditional (pfile, skip, T_IFDEF, 0); |
1341 | } | |
168d3732 | 1342 | |
5d8ebbd8 | 1343 | /* Handle #ifndef. */ |
711b8824 | 1344 | static void |
168d3732 ZW |
1345 | do_ifndef (pfile) |
1346 | cpp_reader *pfile; | |
1347 | { | |
93c80368 | 1348 | int skip = 1; |
bfb9dc7f | 1349 | const cpp_hashnode *node = 0; |
168d3732 | 1350 | |
cef0d199 | 1351 | if (! pfile->state.skipping) |
5af7e2c2 | 1352 | { |
93c80368 NB |
1353 | node = lex_macro_node (pfile); |
1354 | if (node) | |
1355 | skip = node->type == NT_MACRO; | |
b43db0b3 GK |
1356 | |
1357 | if (node) | |
1358 | check_eol (pfile); | |
5af7e2c2 | 1359 | } |
041c3194 | 1360 | |
93c80368 | 1361 | push_conditional (pfile, skip, T_IFNDEF, node); |
7f2935c7 PB |
1362 | } |
1363 | ||
6d18adbc NB |
1364 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
1365 | pfile->mi_ind_cmacro so we can handle multiple-include | |
1366 | optimisations. If macro expansion occurs in the expression, we | |
1367 | cannot treat it as a controlling conditional, since the expansion | |
1368 | could change in the future. That is handled by cpp_get_token. */ | |
711b8824 | 1369 | static void |
ea4a453b | 1370 | do_if (pfile) |
7f2935c7 | 1371 | cpp_reader *pfile; |
7f2935c7 | 1372 | { |
93c80368 | 1373 | int skip = 1; |
7f2935c7 | 1374 | |
cef0d199 | 1375 | if (! pfile->state.skipping) |
87ed109f | 1376 | skip = _cpp_parse_expr (pfile) == false; |
93c80368 | 1377 | |
6d18adbc | 1378 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
7f2935c7 PB |
1379 | } |
1380 | ||
b528a07e | 1381 | /* Flip skipping state if appropriate and continue without changing |
ea4a453b ZW |
1382 | if_stack; this is so that the error message for missing #endif's |
1383 | etc. will point to the original #if. */ | |
711b8824 | 1384 | static void |
ea4a453b ZW |
1385 | do_else (pfile) |
1386 | cpp_reader *pfile; | |
ed705a82 | 1387 | { |
b528a07e NB |
1388 | cpp_buffer *buffer = pfile->buffer; |
1389 | struct if_stack *ifs = buffer->if_stack; | |
ff2b53ef | 1390 | |
ea4a453b | 1391 | if (ifs == NULL) |
ebef4e8c | 1392 | cpp_error (pfile, DL_ERROR, "#else without #if"); |
93c80368 | 1393 | else |
ff2b53ef | 1394 | { |
93c80368 NB |
1395 | if (ifs->type == T_ELSE) |
1396 | { | |
ebef4e8c NB |
1397 | cpp_error (pfile, DL_ERROR, "#else after #else"); |
1398 | cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0, | |
93c80368 NB |
1399 | "the conditional began here"); |
1400 | } | |
b528a07e NB |
1401 | ifs->type = T_ELSE; |
1402 | ||
cef0d199 NB |
1403 | /* Skip any future (erroneous) #elses or #elifs. */ |
1404 | pfile->state.skipping = ifs->skip_elses; | |
1405 | ifs->skip_elses = true; | |
7f2935c7 | 1406 | |
93c80368 NB |
1407 | /* Invalidate any controlling macro. */ |
1408 | ifs->mi_cmacro = 0; | |
93c80368 | 1409 | |
cef0d199 | 1410 | /* Only check EOL if was not originally skipping. */ |
909de5da | 1411 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
cef0d199 NB |
1412 | check_eol (pfile); |
1413 | } | |
7f2935c7 PB |
1414 | } |
1415 | ||
5d8ebbd8 | 1416 | /* Handle a #elif directive by not changing if_stack either. See the |
93c80368 | 1417 | comment above do_else. */ |
711b8824 | 1418 | static void |
ea4a453b | 1419 | do_elif (pfile) |
7f2935c7 | 1420 | cpp_reader *pfile; |
7f2935c7 | 1421 | { |
b528a07e NB |
1422 | cpp_buffer *buffer = pfile->buffer; |
1423 | struct if_stack *ifs = buffer->if_stack; | |
7f2935c7 | 1424 | |
ea4a453b | 1425 | if (ifs == NULL) |
ebef4e8c | 1426 | cpp_error (pfile, DL_ERROR, "#elif without #if"); |
b528a07e | 1427 | else |
40ea76de | 1428 | { |
b528a07e NB |
1429 | if (ifs->type == T_ELSE) |
1430 | { | |
ebef4e8c NB |
1431 | cpp_error (pfile, DL_ERROR, "#elif after #else"); |
1432 | cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0, | |
b528a07e NB |
1433 | "the conditional began here"); |
1434 | } | |
1435 | ifs->type = T_ELIF; | |
93c80368 | 1436 | |
cef0d199 NB |
1437 | /* Only evaluate this if we aren't skipping elses. During |
1438 | evaluation, set skipping to false to get lexer warnings. */ | |
1439 | if (ifs->skip_elses) | |
1440 | pfile->state.skipping = 1; | |
1441 | else | |
b528a07e | 1442 | { |
cef0d199 NB |
1443 | pfile->state.skipping = 0; |
1444 | pfile->state.skipping = ! _cpp_parse_expr (pfile); | |
1445 | ifs->skip_elses = ! pfile->state.skipping; | |
b528a07e | 1446 | } |
cef0d199 NB |
1447 | |
1448 | /* Invalidate any controlling macro. */ | |
1449 | ifs->mi_cmacro = 0; | |
40ea76de | 1450 | } |
7f2935c7 PB |
1451 | } |
1452 | ||
cef0d199 | 1453 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
711b8824 | 1454 | static void |
168d3732 | 1455 | do_endif (pfile) |
7f2935c7 | 1456 | cpp_reader *pfile; |
7f2935c7 | 1457 | { |
b528a07e NB |
1458 | cpp_buffer *buffer = pfile->buffer; |
1459 | struct if_stack *ifs = buffer->if_stack; | |
ea4a453b | 1460 | |
ea4a453b | 1461 | if (ifs == NULL) |
ebef4e8c | 1462 | cpp_error (pfile, DL_ERROR, "#endif without #if"); |
7f2935c7 PB |
1463 | else |
1464 | { | |
cef0d199 | 1465 | /* Only check EOL if was not originally skipping. */ |
909de5da | 1466 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
cef0d199 NB |
1467 | check_eol (pfile); |
1468 | ||
93c80368 NB |
1469 | /* If potential control macro, we go back outside again. */ |
1470 | if (ifs->next == 0 && ifs->mi_cmacro) | |
1471 | { | |
6d18adbc | 1472 | pfile->mi_valid = true; |
93c80368 NB |
1473 | pfile->mi_cmacro = ifs->mi_cmacro; |
1474 | } | |
1475 | ||
b528a07e | 1476 | buffer->if_stack = ifs->next; |
cef0d199 | 1477 | pfile->state.skipping = ifs->was_skipping; |
2a967f3d | 1478 | obstack_free (&pfile->buffer_ob, ifs); |
7f2935c7 | 1479 | } |
93c80368 | 1480 | } |
041c3194 | 1481 | |
5d8ebbd8 NB |
1482 | /* Push an if_stack entry for a preprocessor conditional, and set |
1483 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
1484 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
1485 | we need to check here that we are at the top of the file. */ | |
ea4a453b ZW |
1486 | static void |
1487 | push_conditional (pfile, skip, type, cmacro) | |
1488 | cpp_reader *pfile; | |
1489 | int skip; | |
1490 | int type; | |
1491 | const cpp_hashnode *cmacro; | |
1492 | { | |
1493 | struct if_stack *ifs; | |
b528a07e | 1494 | cpp_buffer *buffer = pfile->buffer; |
ea4a453b | 1495 | |
2a967f3d | 1496 | ifs = xobnew (&pfile->buffer_ob, struct if_stack); |
50410426 | 1497 | ifs->line = pfile->directive_line; |
b528a07e | 1498 | ifs->next = buffer->if_stack; |
cef0d199 NB |
1499 | ifs->skip_elses = pfile->state.skipping || !skip; |
1500 | ifs->was_skipping = pfile->state.skipping; | |
ea4a453b | 1501 | ifs->type = type; |
6d18adbc NB |
1502 | /* This condition is effectively a test for top-of-file. */ |
1503 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
93c80368 NB |
1504 | ifs->mi_cmacro = cmacro; |
1505 | else | |
1506 | ifs->mi_cmacro = 0; | |
ea4a453b | 1507 | |
cef0d199 | 1508 | pfile->state.skipping = skip; |
b528a07e | 1509 | buffer->if_stack = ifs; |
782331f4 | 1510 | } |
7061aa5a | 1511 | |
5d8ebbd8 NB |
1512 | /* Read the tokens of the answer into the macro pool, in a directive |
1513 | of type TYPE. Only commit the memory if we intend it as permanent | |
1514 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
1515 | ANSWERP to point to the answer. */ | |
93c80368 NB |
1516 | static int |
1517 | parse_answer (pfile, answerp, type) | |
7f2935c7 | 1518 | cpp_reader *pfile; |
041c3194 | 1519 | struct answer **answerp; |
93c80368 | 1520 | int type; |
7f2935c7 | 1521 | { |
4ed5bcfb | 1522 | const cpp_token *paren; |
93c80368 | 1523 | struct answer *answer; |
8c3b2693 | 1524 | unsigned int acount; |
93c80368 NB |
1525 | |
1526 | /* In a conditional, it is legal to not have an open paren. We | |
1527 | should save the following token in this case. */ | |
4ed5bcfb | 1528 | paren = cpp_get_token (pfile); |
93c80368 NB |
1529 | |
1530 | /* If not a paren, see if we're OK. */ | |
4ed5bcfb | 1531 | if (paren->type != CPP_OPEN_PAREN) |
041c3194 | 1532 | { |
93c80368 NB |
1533 | /* In a conditional no answer is a test for any answer. It |
1534 | could be followed by any token. */ | |
1535 | if (type == T_IF) | |
bdcbe496 NB |
1536 | { |
1537 | _cpp_backup_tokens (pfile, 1); | |
1538 | return 0; | |
1539 | } | |
93c80368 NB |
1540 | |
1541 | /* #unassert with no answer is valid - it removes all answers. */ | |
4ed5bcfb | 1542 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
93c80368 | 1543 | return 0; |
15dad1d9 | 1544 | |
ebef4e8c | 1545 | cpp_error (pfile, DL_ERROR, "missing '(' after predicate"); |
93c80368 | 1546 | return 1; |
041c3194 | 1547 | } |
7061aa5a | 1548 | |
8c3b2693 | 1549 | for (acount = 0;; acount++) |
041c3194 | 1550 | { |
8c3b2693 NB |
1551 | size_t room_needed; |
1552 | const cpp_token *token = cpp_get_token (pfile); | |
1553 | cpp_token *dest; | |
93c80368 | 1554 | |
041c3194 ZW |
1555 | if (token->type == CPP_CLOSE_PAREN) |
1556 | break; | |
a7abcbbf | 1557 | |
93c80368 | 1558 | if (token->type == CPP_EOF) |
041c3194 | 1559 | { |
ebef4e8c | 1560 | cpp_error (pfile, DL_ERROR, "missing ')' to complete answer"); |
93c80368 | 1561 | return 1; |
041c3194 | 1562 | } |
8c3b2693 NB |
1563 | |
1564 | /* struct answer includes the space for one token. */ | |
1565 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
1566 | ||
1567 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
1568 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
1569 | ||
1570 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
1571 | *dest = *token; | |
1572 | ||
1573 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
1574 | if (acount == 0) | |
1575 | dest->flags &= ~PREV_WHITE; | |
041c3194 | 1576 | } |
15dad1d9 | 1577 | |
8c3b2693 | 1578 | if (acount == 0) |
7061aa5a | 1579 | { |
ebef4e8c | 1580 | cpp_error (pfile, DL_ERROR, "predicate's answer is empty"); |
93c80368 | 1581 | return 1; |
7f2935c7 | 1582 | } |
041c3194 | 1583 | |
8c3b2693 NB |
1584 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
1585 | answer->count = acount; | |
1586 | answer->next = NULL; | |
93c80368 | 1587 | *answerp = answer; |
041c3194 | 1588 | |
93c80368 NB |
1589 | return 0; |
1590 | } | |
041c3194 | 1591 | |
5d8ebbd8 NB |
1592 | /* Parses an assertion directive of type TYPE, returning a pointer to |
1593 | the hash node of the predicate, or 0 on error. If an answer was | |
1594 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
93c80368 NB |
1595 | static cpp_hashnode * |
1596 | parse_assertion (pfile, answerp, type) | |
1597 | cpp_reader *pfile; | |
1598 | struct answer **answerp; | |
1599 | int type; | |
1600 | { | |
1601 | cpp_hashnode *result = 0; | |
4ed5bcfb | 1602 | const cpp_token *predicate; |
93c80368 NB |
1603 | |
1604 | /* We don't expand predicates or answers. */ | |
1605 | pfile->state.prevent_expansion++; | |
1606 | ||
93c80368 | 1607 | *answerp = 0; |
4ed5bcfb NB |
1608 | predicate = cpp_get_token (pfile); |
1609 | if (predicate->type == CPP_EOF) | |
ebef4e8c | 1610 | cpp_error (pfile, DL_ERROR, "assertion without predicate"); |
4ed5bcfb | 1611 | else if (predicate->type != CPP_NAME) |
ebef4e8c | 1612 | cpp_error (pfile, DL_ERROR, "predicate must be an identifier"); |
93c80368 NB |
1613 | else if (parse_answer (pfile, answerp, type) == 0) |
1614 | { | |
4ed5bcfb | 1615 | unsigned int len = NODE_LEN (predicate->val.node); |
93c80368 | 1616 | unsigned char *sym = alloca (len + 1); |
041c3194 | 1617 | |
93c80368 NB |
1618 | /* Prefix '#' to get it out of macro namespace. */ |
1619 | sym[0] = '#'; | |
4ed5bcfb | 1620 | memcpy (sym + 1, NODE_NAME (predicate->val.node), len); |
93c80368 NB |
1621 | result = cpp_lookup (pfile, sym, len + 1); |
1622 | } | |
7061aa5a | 1623 | |
93c80368 NB |
1624 | pfile->state.prevent_expansion--; |
1625 | return result; | |
7f2935c7 | 1626 | } |
7061aa5a | 1627 | |
5d8ebbd8 | 1628 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
041c3194 | 1629 | or a pointer to NULL if the answer is not in the chain. */ |
93c80368 NB |
1630 | static struct answer ** |
1631 | find_answer (node, candidate) | |
041c3194 | 1632 | cpp_hashnode *node; |
93c80368 | 1633 | const struct answer *candidate; |
7f2935c7 | 1634 | { |
93c80368 | 1635 | unsigned int i; |
041c3194 | 1636 | struct answer **result; |
7f2935c7 | 1637 | |
041c3194 | 1638 | for (result = &node->value.answers; *result; result = &(*result)->next) |
93c80368 NB |
1639 | { |
1640 | struct answer *answer = *result; | |
1641 | ||
1642 | if (answer->count == candidate->count) | |
1643 | { | |
1644 | for (i = 0; i < answer->count; i++) | |
1645 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
1646 | break; | |
1647 | ||
1648 | if (i == answer->count) | |
1649 | break; | |
1650 | } | |
1651 | } | |
ff2b53ef | 1652 | |
041c3194 ZW |
1653 | return result; |
1654 | } | |
15dad1d9 | 1655 | |
93c80368 NB |
1656 | /* Test an assertion within a preprocessor conditional. Returns |
1657 | non-zero on failure, zero on success. On success, the result of | |
1658 | the test is written into VALUE. */ | |
1659 | int | |
1660 | _cpp_test_assertion (pfile, value) | |
1661 | cpp_reader *pfile; | |
60284a59 | 1662 | unsigned int *value; |
93c80368 NB |
1663 | { |
1664 | struct answer *answer; | |
1665 | cpp_hashnode *node; | |
1666 | ||
1667 | node = parse_assertion (pfile, &answer, T_IF); | |
1668 | if (node) | |
1669 | *value = (node->type == NT_ASSERTION && | |
1670 | (answer == 0 || *find_answer (node, answer) != 0)); | |
91318908 NB |
1671 | else if (pfile->cur_token[-1].type == CPP_EOF) |
1672 | _cpp_backup_tokens (pfile, 1); | |
93c80368 NB |
1673 | |
1674 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1675 | return node == 0; | |
1676 | } | |
1677 | ||
5d8ebbd8 | 1678 | /* Handle #assert. */ |
711b8824 | 1679 | static void |
041c3194 ZW |
1680 | do_assert (pfile) |
1681 | cpp_reader *pfile; | |
1682 | { | |
1683 | struct answer *new_answer; | |
1684 | cpp_hashnode *node; | |
df383483 | 1685 | |
93c80368 | 1686 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
041c3194 | 1687 | if (node) |
ff2b53ef | 1688 | { |
93c80368 NB |
1689 | /* Place the new answer in the answer list. First check there |
1690 | is not a duplicate. */ | |
041c3194 | 1691 | new_answer->next = 0; |
93c80368 | 1692 | if (node->type == NT_ASSERTION) |
041c3194 | 1693 | { |
93c80368 NB |
1694 | if (*find_answer (node, new_answer)) |
1695 | { | |
ebef4e8c NB |
1696 | cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted", |
1697 | NODE_NAME (node) + 1); | |
93c80368 NB |
1698 | return; |
1699 | } | |
041c3194 ZW |
1700 | new_answer->next = node->value.answers; |
1701 | } | |
8c3b2693 | 1702 | |
93c80368 | 1703 | node->type = NT_ASSERTION; |
041c3194 | 1704 | node->value.answers = new_answer; |
8c3b2693 NB |
1705 | BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer) |
1706 | + (new_answer->count - 1) | |
1707 | * sizeof (cpp_token)); | |
1708 | check_eol (pfile); | |
ff2b53ef | 1709 | } |
041c3194 | 1710 | } |
15dad1d9 | 1711 | |
5d8ebbd8 | 1712 | /* Handle #unassert. */ |
711b8824 | 1713 | static void |
041c3194 ZW |
1714 | do_unassert (pfile) |
1715 | cpp_reader *pfile; | |
1716 | { | |
1717 | cpp_hashnode *node; | |
93c80368 | 1718 | struct answer *answer; |
df383483 | 1719 | |
93c80368 NB |
1720 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
1721 | /* It isn't an error to #unassert something that isn't asserted. */ | |
1722 | if (node && node->type == NT_ASSERTION) | |
7061aa5a | 1723 | { |
93c80368 | 1724 | if (answer) |
15dad1d9 | 1725 | { |
93c80368 | 1726 | struct answer **p = find_answer (node, answer), *temp; |
041c3194 | 1727 | |
93c80368 NB |
1728 | /* Remove the answer from the list. */ |
1729 | temp = *p; | |
1730 | if (temp) | |
1731 | *p = temp->next; | |
a7abcbbf | 1732 | |
93c80368 NB |
1733 | /* Did we free the last answer? */ |
1734 | if (node->value.answers == 0) | |
1735 | node->type = NT_VOID; | |
8c3b2693 NB |
1736 | |
1737 | check_eol (pfile); | |
93c80368 NB |
1738 | } |
1739 | else | |
1740 | _cpp_free_definition (node); | |
041c3194 | 1741 | } |
93c80368 NB |
1742 | |
1743 | /* We don't commit the memory for the answer - it's temporary only. */ | |
7f2935c7 | 1744 | } |
7f2935c7 | 1745 | |
45b966db ZW |
1746 | /* These are for -D, -U, -A. */ |
1747 | ||
1748 | /* Process the string STR as if it appeared as the body of a #define. | |
1749 | If STR is just an identifier, define it with value 1. | |
1750 | If STR has anything after the identifier, then it should | |
ec5c56db | 1751 | be identifier=definition. */ |
0b22d65c | 1752 | void |
45b966db | 1753 | cpp_define (pfile, str) |
0b22d65c | 1754 | cpp_reader *pfile; |
7ceb3598 | 1755 | const char *str; |
0b22d65c | 1756 | { |
45b966db ZW |
1757 | char *buf, *p; |
1758 | size_t count; | |
1759 | ||
df383483 | 1760 | /* Copy the entire option so we can modify it. |
45b966db | 1761 | Change the first "=" in the string to a space. If there is none, |
86368122 NB |
1762 | tack " 1" on the end. */ |
1763 | ||
86368122 | 1764 | count = strlen (str); |
4d6baafa | 1765 | buf = (char *) alloca (count + 3); |
86368122 NB |
1766 | memcpy (buf, str, count); |
1767 | ||
1768 | p = strchr (str, '='); | |
45b966db | 1769 | if (p) |
86368122 | 1770 | buf[p - str] = ' '; |
45b966db ZW |
1771 | else |
1772 | { | |
86368122 NB |
1773 | buf[count++] = ' '; |
1774 | buf[count++] = '1'; | |
0b22d65c | 1775 | } |
4d6baafa | 1776 | buf[count] = '\0'; |
cf4ed945 | 1777 | |
29401c30 | 1778 | run_directive (pfile, T_DEFINE, buf, count); |
2c8f0515 ZW |
1779 | } |
1780 | ||
ad2a084d | 1781 | /* Slight variant of the above for use by initialize_builtins. */ |
2c8f0515 ZW |
1782 | void |
1783 | _cpp_define_builtin (pfile, str) | |
1784 | cpp_reader *pfile; | |
1785 | const char *str; | |
1786 | { | |
29401c30 | 1787 | run_directive (pfile, T_DEFINE, str, strlen (str)); |
45b966db | 1788 | } |
0f41302f | 1789 | |
45b966db ZW |
1790 | /* Process MACRO as if it appeared as the body of an #undef. */ |
1791 | void | |
1792 | cpp_undef (pfile, macro) | |
7f2935c7 | 1793 | cpp_reader *pfile; |
45b966db | 1794 | const char *macro; |
7f2935c7 | 1795 | { |
29401c30 | 1796 | run_directive (pfile, T_UNDEF, macro, strlen (macro)); |
7f2935c7 PB |
1797 | } |
1798 | ||
ec5c56db | 1799 | /* Process the string STR as if it appeared as the body of a #assert. */ |
45b966db ZW |
1800 | void |
1801 | cpp_assert (pfile, str) | |
1802 | cpp_reader *pfile; | |
1803 | const char *str; | |
1804 | { | |
86368122 | 1805 | handle_assertion (pfile, str, T_ASSERT); |
45b966db | 1806 | } |
7f2935c7 | 1807 | |
ec5c56db | 1808 | /* Process STR as if it appeared as the body of an #unassert. */ |
45b966db ZW |
1809 | void |
1810 | cpp_unassert (pfile, str) | |
7f2935c7 | 1811 | cpp_reader *pfile; |
45b966db | 1812 | const char *str; |
7f2935c7 | 1813 | { |
86368122 | 1814 | handle_assertion (pfile, str, T_UNASSERT); |
df383483 | 1815 | } |
3fdc651f | 1816 | |
86368122 NB |
1817 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
1818 | static void | |
1819 | handle_assertion (pfile, str, type) | |
1820 | cpp_reader *pfile; | |
1821 | const char *str; | |
1822 | int type; | |
1823 | { | |
1824 | size_t count = strlen (str); | |
1825 | const char *p = strchr (str, '='); | |
1826 | ||
1827 | if (p) | |
1828 | { | |
1829 | /* Copy the entire option so we can modify it. Change the first | |
1830 | "=" in the string to a '(', and tack a ')' on the end. */ | |
4d6baafa | 1831 | char *buf = (char *) alloca (count + 2); |
86368122 NB |
1832 | |
1833 | memcpy (buf, str, count); | |
1834 | buf[p - str] = '('; | |
1835 | buf[count++] = ')'; | |
4d6baafa | 1836 | buf[count] = '\0'; |
86368122 NB |
1837 | str = buf; |
1838 | } | |
1839 | ||
29401c30 | 1840 | run_directive (pfile, type, str, count); |
86368122 NB |
1841 | } |
1842 | ||
7e96d768 NB |
1843 | /* The number of errors for a given reader. */ |
1844 | unsigned int | |
1845 | cpp_errors (pfile) | |
1846 | cpp_reader *pfile; | |
1847 | { | |
1848 | return pfile->errors; | |
1849 | } | |
1850 | ||
1851 | /* The options structure. */ | |
1852 | cpp_options * | |
1853 | cpp_get_options (pfile) | |
1854 | cpp_reader *pfile; | |
1855 | { | |
1856 | return &pfile->opts; | |
1857 | } | |
1858 | ||
1859 | /* The callbacks structure. */ | |
1860 | cpp_callbacks * | |
1861 | cpp_get_callbacks (pfile) | |
1862 | cpp_reader *pfile; | |
1863 | { | |
1864 | return &pfile->cb; | |
1865 | } | |
1866 | ||
d82fc108 | 1867 | /* The line map set. */ |
47d89cf3 | 1868 | const struct line_maps * |
d82fc108 NB |
1869 | cpp_get_line_maps (pfile) |
1870 | cpp_reader *pfile; | |
1871 | { | |
1872 | return &pfile->line_maps; | |
1873 | } | |
1874 | ||
7e96d768 NB |
1875 | /* Copy the given callbacks structure to our own. */ |
1876 | void | |
1877 | cpp_set_callbacks (pfile, cb) | |
1878 | cpp_reader *pfile; | |
1879 | cpp_callbacks *cb; | |
1880 | { | |
1881 | pfile->cb = *cb; | |
1882 | } | |
1883 | ||
eb1f4d9d NB |
1884 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
1885 | doesn't fail. It does not generate a file change call back; that | |
1886 | is the responsibility of the caller. */ | |
c71f835b | 1887 | cpp_buffer * |
29401c30 | 1888 | cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof) |
c71f835b | 1889 | cpp_reader *pfile; |
562a5c27 | 1890 | const uchar *buffer; |
3cf3593f | 1891 | size_t len; |
29401c30 | 1892 | int from_stage3; |
ef6e958a | 1893 | int return_at_eof; |
c71f835b | 1894 | { |
2a967f3d | 1895 | cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer); |
93c80368 | 1896 | |
fde84349 NB |
1897 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
1898 | memset (new, 0, sizeof (cpp_buffer)); | |
1899 | ||
1900 | new->line_base = new->buf = new->cur = buffer; | |
1901 | new->rlimit = buffer + len; | |
43612ffb | 1902 | new->from_stage3 = from_stage3 || CPP_OPTION (pfile, traditional); |
3cf3593f | 1903 | new->prev = pfile->buffer; |
ef6e958a | 1904 | new->return_at_eof = return_at_eof; |
bdcbe496 | 1905 | new->saved_flags = BOL; |
0bda4760 | 1906 | |
3cf3593f | 1907 | pfile->buffer = new; |
0bda4760 | 1908 | |
c71f835b ZW |
1909 | return new; |
1910 | } | |
1911 | ||
af0d16cd NB |
1912 | /* Pops a single buffer, with a file change call-back if appropriate. |
1913 | Then pushes the next -include file, if any remain. */ | |
ef6e958a NB |
1914 | void |
1915 | _cpp_pop_buffer (pfile) | |
c71f835b ZW |
1916 | cpp_reader *pfile; |
1917 | { | |
fde84349 | 1918 | cpp_buffer *buffer = pfile->buffer; |
af0d16cd | 1919 | struct include_file *inc = buffer->inc; |
ad2a084d | 1920 | struct if_stack *ifs; |
c71f835b | 1921 | |
fde84349 NB |
1922 | /* Walk back up the conditional stack till we reach its level at |
1923 | entry to this file, issuing error messages. */ | |
1924 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
ebef4e8c | 1925 | cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0, |
fde84349 | 1926 | "unterminated #%s", dtable[ifs->type].name); |
eb1f4d9d | 1927 | |
97293897 | 1928 | /* In case of a missing #endif. */ |
67821e3a | 1929 | pfile->state.skipping = 0; |
29401c30 | 1930 | |
af0d16cd | 1931 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
29401c30 NB |
1932 | pfile->buffer = buffer->prev; |
1933 | ||
af0d16cd NB |
1934 | /* Free the buffer object now; we may want to push a new buffer |
1935 | in _cpp_push_next_include_file. */ | |
1936 | obstack_free (&pfile->buffer_ob, buffer); | |
29401c30 | 1937 | |
af0d16cd NB |
1938 | if (inc) |
1939 | { | |
1940 | _cpp_pop_file_buffer (pfile, inc); | |
1941 | ||
1942 | /* Don't generate a callback for popping the main file. */ | |
1943 | if (pfile->buffer) | |
1944 | { | |
1945 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); | |
1946 | ||
1947 | /* If this is the main file, there may be some -include | |
1948 | files left to push. */ | |
1949 | if (!pfile->buffer->prev) | |
1950 | _cpp_maybe_push_include_file (pfile); | |
1951 | } | |
1952 | } | |
c71f835b ZW |
1953 | } |
1954 | ||
5d8ebbd8 | 1955 | /* Enter all recognised directives in the hash table. */ |
c71f835b | 1956 | void |
2a967f3d | 1957 | _cpp_init_directives (pfile) |
c71f835b ZW |
1958 | cpp_reader *pfile; |
1959 | { | |
766ee681 | 1960 | unsigned int i; |
93c80368 | 1961 | cpp_hashnode *node; |
bfb9dc7f | 1962 | |
37b8524c | 1963 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
93c80368 | 1964 | { |
766ee681 NB |
1965 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
1966 | node->directive_index = i + 1; | |
93c80368 | 1967 | } |
c71f835b | 1968 | } |