]>
Commit | Line | Data |
---|---|---|
a949941c | 1 | /* CPP Library. (Directive handling.) |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
705e2d28 | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
33ae4837 | 4 | 2007, 2008 Free Software Foundation, Inc. |
4c8cc616 | 5 | Contributed by Per Bothner, 1994-95. |
d8bfa78c | 6 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
7 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
8 | ||
9 | This program is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2, or (at your option) any | |
12 | later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
200031d1 | 21 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
7f2935c7 | 22 | |
956d6950 | 23 | #include "config.h" |
b04cd507 | 24 | #include "system.h" |
956d6950 | 25 | #include "cpplib.h" |
4f4e53dd | 26 | #include "internal.h" |
c6e83800 | 27 | #include "mkdeps.h" |
c71f835b | 28 | #include "obstack.h" |
7f2935c7 | 29 | |
88ae23e7 ZW |
30 | /* Stack of conditionals currently in progress |
31 | (including both successful and failing conditionals). */ | |
88ae23e7 ZW |
32 | struct if_stack |
33 | { | |
34 | struct if_stack *next; | |
50410426 | 35 | unsigned int line; /* Line where condition started. */ |
93c80368 | 36 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
cef0d199 NB |
37 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
38 | bool was_skipping; /* If were skipping on entry. */ | |
6cf87ca4 | 39 | int type; /* Most recent conditional for diagnostics. */ |
88ae23e7 | 40 | }; |
88ae23e7 | 41 | |
a5da89c6 | 42 | /* Contains a registered pragma or pragma namespace. */ |
6cf87ca4 | 43 | typedef void (*pragma_cb) (cpp_reader *); |
a5da89c6 NB |
44 | struct pragma_entry |
45 | { | |
46 | struct pragma_entry *next; | |
4b115ff0 | 47 | const cpp_hashnode *pragma; /* Name and length. */ |
21b11495 ZW |
48 | bool is_nspace; |
49 | bool is_internal; | |
bc4071dd RH |
50 | bool is_deferred; |
51 | bool allow_expansion; | |
a5da89c6 NB |
52 | union { |
53 | pragma_cb handler; | |
54 | struct pragma_entry *space; | |
bc4071dd | 55 | unsigned int ident; |
a5da89c6 NB |
56 | } u; |
57 | }; | |
58 | ||
93c80368 NB |
59 | /* Values for the origin field of struct directive. KANDR directives |
60 | come from traditional (K&R) C. STDC89 directives come from the | |
61 | 1989 C standard. EXTENSION directives are extensions. */ | |
62 | #define KANDR 0 | |
63 | #define STDC89 1 | |
64 | #define EXTENSION 2 | |
65 | ||
66 | /* Values for the flags field of struct directive. COND indicates a | |
67 | conditional; IF_COND an opening conditional. INCL means to treat | |
68 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
69 | means this directive should be handled even if -fpreprocessed is in | |
1a76916c NB |
70 | effect (these are the directives with callback hooks). |
71 | ||
d97371e0 | 72 | EXPAND is set on directives that are always macro-expanded. */ |
93c80368 NB |
73 | #define COND (1 << 0) |
74 | #define IF_COND (1 << 1) | |
75 | #define INCL (1 << 2) | |
76 | #define IN_I (1 << 3) | |
1a76916c | 77 | #define EXPAND (1 << 4) |
93c80368 NB |
78 | |
79 | /* Defines one #-directive, including how to handle it. */ | |
6cf87ca4 | 80 | typedef void (*directive_handler) (cpp_reader *); |
93c80368 NB |
81 | typedef struct directive directive; |
82 | struct directive | |
83 | { | |
84 | directive_handler handler; /* Function to handle directive. */ | |
562a5c27 | 85 | const uchar *name; /* Name of directive. */ |
93c80368 NB |
86 | unsigned short length; /* Length of name. */ |
87 | unsigned char origin; /* Origin of directive. */ | |
88 | unsigned char flags; /* Flags describing this directive. */ | |
89 | }; | |
90 | ||
1316f1f7 ZW |
91 | /* Forward declarations. */ |
92 | ||
6cf87ca4 ZW |
93 | static void skip_rest_of_line (cpp_reader *); |
94 | static void check_eol (cpp_reader *); | |
95 | static void start_directive (cpp_reader *); | |
96 | static void prepare_directive_trad (cpp_reader *); | |
97 | static void end_directive (cpp_reader *, int); | |
98 | static void directive_diagnostics (cpp_reader *, const directive *, int); | |
99 | static void run_directive (cpp_reader *, int, const char *, size_t); | |
100 | static char *glue_header_name (cpp_reader *); | |
cbc43ae0 | 101 | static const char *parse_include (cpp_reader *, int *, const cpp_token ***); |
6cf87ca4 ZW |
102 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); |
103 | static unsigned int read_flag (cpp_reader *, unsigned int); | |
6cf87ca4 ZW |
104 | static int strtoul_for_line (const uchar *, unsigned int, unsigned long *); |
105 | static void do_diagnostic (cpp_reader *, int, int); | |
ee1c2a10 | 106 | static cpp_hashnode *lex_macro_node (cpp_reader *, bool); |
d1bd0ded | 107 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
6cf87ca4 ZW |
108 | static void do_include_common (cpp_reader *, enum include_type); |
109 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, | |
110 | const cpp_hashnode *); | |
6cf87ca4 ZW |
111 | static int count_registered_pragmas (struct pragma_entry *); |
112 | static char ** save_registered_pragmas (struct pragma_entry *, char **); | |
113 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, | |
114 | char **); | |
115 | static void do_pragma_once (cpp_reader *); | |
116 | static void do_pragma_poison (cpp_reader *); | |
117 | static void do_pragma_system_header (cpp_reader *); | |
118 | static void do_pragma_dependency (cpp_reader *); | |
119 | static void do_linemarker (cpp_reader *); | |
120 | static const cpp_token *get_token_no_padding (cpp_reader *); | |
121 | static const cpp_token *get__Pragma_string (cpp_reader *); | |
122 | static void destringize_and_run (cpp_reader *, const cpp_string *); | |
123 | static int parse_answer (cpp_reader *, struct answer **, int); | |
124 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); | |
125 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); | |
126 | static void handle_assertion (cpp_reader *, const char *, int); | |
d481b69b | 127 | |
168d3732 ZW |
128 | /* This is the table of directive handlers. It is ordered by |
129 | frequency of occurrence; the numbers at the end are directive | |
130 | counts from all the source code I have lying around (egcs and libc | |
131 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
93c80368 NB |
132 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
133 | is now O(1). All extensions other than #warning and #include_next | |
134 | are deprecated. The name is where the extension appears to have | |
135 | come from. */ | |
168d3732 | 136 | |
93c80368 NB |
137 | #define DIRECTIVE_TABLE \ |
138 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
d97371e0 | 139 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
93c80368 NB |
140 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
141 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
1a76916c | 142 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
93c80368 NB |
143 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
144 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
145 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
1a76916c NB |
146 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
147 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ | |
93c80368 NB |
148 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
149 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
150 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
d97371e0 | 151 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
93c80368 | 152 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
d97371e0 | 153 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
93c80368 NB |
154 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
155 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ | |
1ed17cd5 ZW |
156 | D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ |
157 | ||
158 | /* #sccs is synonymous with #ident. */ | |
159 | #define do_sccs do_ident | |
07aa0b04 | 160 | |
168d3732 ZW |
161 | /* Use the table to generate a series of prototypes, an enum for the |
162 | directive names, and an array of directive handlers. */ | |
163 | ||
6cf87ca4 | 164 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
168d3732 ZW |
165 | DIRECTIVE_TABLE |
166 | #undef D | |
167 | ||
041c3194 | 168 | #define D(n, tag, o, f) tag, |
168d3732 ZW |
169 | enum |
170 | { | |
171 | DIRECTIVE_TABLE | |
172 | N_DIRECTIVES | |
7f2935c7 | 173 | }; |
168d3732 ZW |
174 | #undef D |
175 | ||
041c3194 | 176 | #define D(name, t, origin, flags) \ |
9a238586 KG |
177 | { do_##name, (const uchar *) #name, \ |
178 | sizeof #name - 1, origin, flags }, | |
93c80368 | 179 | static const directive dtable[] = |
168d3732 ZW |
180 | { |
181 | DIRECTIVE_TABLE | |
182 | }; | |
183 | #undef D | |
184 | #undef DIRECTIVE_TABLE | |
7f2935c7 | 185 | |
dcc229e5 ZW |
186 | /* Wrapper struct directive for linemarkers. |
187 | The origin is more or less true - the original K+R cpp | |
188 | did use this notation in its preprocessed output. */ | |
189 | static const directive linemarker_dir = | |
190 | { | |
b6baa67d | 191 | do_linemarker, UC"#", 1, KANDR, IN_I |
dcc229e5 ZW |
192 | }; |
193 | ||
1a76916c | 194 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
67821e3a | 195 | |
93c80368 NB |
196 | /* Skip any remaining tokens in a directive. */ |
197 | static void | |
6cf87ca4 | 198 | skip_rest_of_line (cpp_reader *pfile) |
c5a04734 | 199 | { |
93c80368 | 200 | /* Discard all stacked contexts. */ |
8128cccf | 201 | while (pfile->context->prev) |
93c80368 NB |
202 | _cpp_pop_context (pfile); |
203 | ||
b528a07e | 204 | /* Sweep up all tokens remaining on the line. */ |
345894b4 NB |
205 | if (! SEEN_EOL ()) |
206 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
207 | ; | |
93c80368 | 208 | } |
c5a04734 | 209 | |
93c80368 NB |
210 | /* Ensure there are no stray tokens at the end of a directive. */ |
211 | static void | |
6cf87ca4 | 212 | check_eol (cpp_reader *pfile) |
93c80368 | 213 | { |
345894b4 | 214 | if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) |
0527bc4e | 215 | cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", |
ebef4e8c | 216 | pfile->directive->name); |
93c80368 NB |
217 | } |
218 | ||
cbc43ae0 ILT |
219 | /* Ensure there are no stray tokens other than comments at the end of |
220 | a directive, and gather the comments. */ | |
221 | static const cpp_token ** | |
222 | check_eol_return_comments (cpp_reader *pfile) | |
223 | { | |
224 | size_t c; | |
225 | size_t capacity = 8; | |
226 | const cpp_token **buf; | |
227 | ||
228 | buf = XNEWVEC (const cpp_token *, capacity); | |
229 | c = 0; | |
230 | if (! SEEN_EOL ()) | |
231 | { | |
232 | while (1) | |
233 | { | |
234 | const cpp_token *tok; | |
235 | ||
236 | tok = _cpp_lex_token (pfile); | |
237 | if (tok->type == CPP_EOF) | |
238 | break; | |
239 | if (tok->type != CPP_COMMENT) | |
240 | cpp_error (pfile, CPP_DL_PEDWARN, | |
241 | "extra tokens at end of #%s directive", | |
242 | pfile->directive->name); | |
243 | else | |
244 | { | |
245 | if (c + 1 >= capacity) | |
246 | { | |
247 | capacity *= 2; | |
248 | buf = XRESIZEVEC (const cpp_token *, buf, capacity); | |
249 | } | |
250 | buf[c] = tok; | |
251 | ++c; | |
252 | } | |
253 | } | |
254 | } | |
255 | buf[c] = NULL; | |
256 | return buf; | |
257 | } | |
258 | ||
fe6c2db9 NB |
259 | /* Called when entering a directive, _Pragma or command-line directive. */ |
260 | static void | |
6cf87ca4 | 261 | start_directive (cpp_reader *pfile) |
93c80368 | 262 | { |
7f2f1a66 NB |
263 | /* Setup in-directive state. */ |
264 | pfile->state.in_directive = 1; | |
265 | pfile->state.save_comments = 0; | |
21b11495 | 266 | pfile->directive_result.type = CPP_PADDING; |
7f2f1a66 | 267 | |
93c80368 | 268 | /* Some handlers need the position of the # for diagnostics. */ |
500bee0a | 269 | pfile->directive_line = pfile->line_table->highest_line; |
fe6c2db9 NB |
270 | } |
271 | ||
272 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
273 | static void | |
6cf87ca4 | 274 | end_directive (cpp_reader *pfile, int skip_line) |
fe6c2db9 | 275 | { |
bc4071dd RH |
276 | if (pfile->state.in_deferred_pragma) |
277 | ; | |
278 | else if (CPP_OPTION (pfile, traditional)) | |
1a76916c | 279 | { |
d97371e0 NB |
280 | /* Revert change of prepare_directive_trad. */ |
281 | pfile->state.prevent_expansion--; | |
282 | ||
b66377c1 | 283 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c NB |
284 | _cpp_remove_overlay (pfile); |
285 | } | |
fe6c2db9 | 286 | /* We don't skip for an assembler #. */ |
b66377c1 | 287 | else if (skip_line) |
67821e3a NB |
288 | { |
289 | skip_rest_of_line (pfile); | |
bdcbe496 NB |
290 | if (!pfile->keep_tokens) |
291 | { | |
292 | pfile->cur_run = &pfile->base_run; | |
293 | pfile->cur_token = pfile->base_run.base; | |
294 | } | |
67821e3a | 295 | } |
fe6c2db9 NB |
296 | |
297 | /* Restore state. */ | |
fe6c2db9 NB |
298 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
299 | pfile->state.in_directive = 0; | |
d97371e0 | 300 | pfile->state.in_expression = 0; |
fe6c2db9 NB |
301 | pfile->state.angled_headers = 0; |
302 | pfile->directive = 0; | |
303 | } | |
304 | ||
1a76916c NB |
305 | /* Prepare to handle the directive in pfile->directive. */ |
306 | static void | |
6cf87ca4 | 307 | prepare_directive_trad (cpp_reader *pfile) |
1a76916c | 308 | { |
951a0766 | 309 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c | 310 | { |
b66377c1 NB |
311 | bool no_expand = (pfile->directive |
312 | && ! (pfile->directive->flags & EXPAND)); | |
974c43f1 | 313 | bool was_skipping = pfile->state.skipping; |
1a76916c | 314 | |
d97371e0 NB |
315 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
316 | || pfile->directive == &dtable[T_ELIF]); | |
45f2492c NB |
317 | if (pfile->state.in_expression) |
318 | pfile->state.skipping = false; | |
319 | ||
1a76916c NB |
320 | if (no_expand) |
321 | pfile->state.prevent_expansion++; | |
43839642 | 322 | _cpp_scan_out_logical_line (pfile, NULL); |
1a76916c NB |
323 | if (no_expand) |
324 | pfile->state.prevent_expansion--; | |
45f2492c | 325 | |
974c43f1 | 326 | pfile->state.skipping = was_skipping; |
1a76916c NB |
327 | _cpp_overlay_buffer (pfile, pfile->out.base, |
328 | pfile->out.cur - pfile->out.base); | |
329 | } | |
d97371e0 NB |
330 | |
331 | /* Stop ISO C from expanding anything. */ | |
332 | pfile->state.prevent_expansion++; | |
1a76916c NB |
333 | } |
334 | ||
da7d8304 | 335 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
18a9d8ff | 336 | the '#' was indented. */ |
18a9d8ff | 337 | static void |
6cf87ca4 | 338 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
18a9d8ff | 339 | { |
dcc229e5 ZW |
340 | /* Issue -pedantic warnings for extensions. */ |
341 | if (CPP_PEDANTIC (pfile) | |
342 | && ! pfile->state.skipping | |
343 | && dir->origin == EXTENSION) | |
0527bc4e | 344 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); |
dcc229e5 ZW |
345 | |
346 | /* Traditionally, a directive is ignored unless its # is in | |
347 | column 1. Therefore in code intended to work with K+R | |
348 | compilers, directives added by C89 must have their # | |
349 | indented, and directives present in traditional C must not. | |
350 | This is true even of directives in skipped conditional | |
351 | blocks. #elif cannot be used at all. */ | |
352 | if (CPP_WTRADITIONAL (pfile)) | |
18a9d8ff | 353 | { |
dcc229e5 | 354 | if (dir == &dtable[T_ELIF]) |
0527bc4e | 355 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 356 | "suggest not using #elif in traditional C"); |
dcc229e5 | 357 | else if (indented && dir->origin == KANDR) |
0527bc4e | 358 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c NB |
359 | "traditional C ignores #%s with the # indented", |
360 | dir->name); | |
dcc229e5 | 361 | else if (!indented && dir->origin != KANDR) |
0527bc4e | 362 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c NB |
363 | "suggest hiding #%s from traditional C with an indented #", |
364 | dir->name); | |
18a9d8ff NB |
365 | } |
366 | } | |
367 | ||
da7d8304 | 368 | /* Check if we have a known directive. INDENTED is nonzero if the |
18a9d8ff | 369 | '#' of the directive was indented. This function is in this file |
a2566ae9 | 370 | to save unnecessarily exporting dtable etc. to lex.c. Returns |
da7d8304 | 371 | nonzero if the line of tokens has been handled, zero if we should |
18a9d8ff | 372 | continue processing the line. */ |
fe6c2db9 | 373 | int |
6cf87ca4 | 374 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
fe6c2db9 | 375 | { |
fe6c2db9 | 376 | const directive *dir = 0; |
345894b4 | 377 | const cpp_token *dname; |
e808ec9c | 378 | bool was_parsing_args = pfile->state.parsing_args; |
c6e83800 | 379 | bool was_discarding_output = pfile->state.discarding_output; |
fe6c2db9 NB |
380 | int skip = 1; |
381 | ||
c6e83800 ZW |
382 | if (was_discarding_output) |
383 | pfile->state.prevent_expansion = 0; | |
384 | ||
e808ec9c NB |
385 | if (was_parsing_args) |
386 | { | |
387 | if (CPP_OPTION (pfile, pedantic)) | |
0527bc4e | 388 | cpp_error (pfile, CPP_DL_PEDWARN, |
e808ec9c NB |
389 | "embedding a directive within macro arguments is not portable"); |
390 | pfile->state.parsing_args = 0; | |
391 | pfile->state.prevent_expansion = 0; | |
392 | } | |
fe6c2db9 | 393 | start_directive (pfile); |
345894b4 | 394 | dname = _cpp_lex_token (pfile); |
0d9f234d | 395 | |
345894b4 | 396 | if (dname->type == CPP_NAME) |
0d9f234d | 397 | { |
4977bab6 ZW |
398 | if (dname->val.node->is_directive) |
399 | dir = &dtable[dname->val.node->directive_index]; | |
0d9f234d | 400 | } |
05713b80 | 401 | /* We do not recognize the # followed by a number extension in |
18a9d8ff | 402 | assembler code. */ |
345894b4 | 403 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
0d9f234d | 404 | { |
dcc229e5 ZW |
405 | dir = &linemarker_dir; |
406 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
407 | && ! pfile->state.skipping) | |
0527bc4e | 408 | cpp_error (pfile, CPP_DL_PEDWARN, |
ebef4e8c | 409 | "style of line directive is a GCC extension"); |
93c80368 | 410 | } |
0d9f234d | 411 | |
93c80368 NB |
412 | if (dir) |
413 | { | |
18a9d8ff NB |
414 | /* If we have a directive that is not an opening conditional, |
415 | invalidate any control macro. */ | |
416 | if (! (dir->flags & IF_COND)) | |
417 | pfile->mi_valid = false; | |
418 | ||
419 | /* Kluge alert. In order to be sure that code like this | |
420 | ||
421 | #define HASH # | |
422 | HASH define foo bar | |
423 | ||
424 | does not cause '#define foo bar' to get executed when | |
425 | compiled with -save-temps, we recognize directives in | |
a2566ae9 | 426 | -fpreprocessed mode only if the # is in column 1. macro.c |
ccfc4c91 OW |
427 | puts a space in front of any '#' at the start of a macro. |
428 | ||
429 | We exclude the -fdirectives-only case because macro expansion | |
430 | has not been performed yet, and block comments can cause spaces | |
431 | to preceed the directive. */ | |
18a9d8ff | 432 | if (CPP_OPTION (pfile, preprocessed) |
ccfc4c91 | 433 | && !CPP_OPTION (pfile, directives_only) |
18a9d8ff | 434 | && (indented || !(dir->flags & IN_I))) |
6d4587f7 | 435 | { |
18a9d8ff NB |
436 | skip = 0; |
437 | dir = 0; | |
6d4587f7 ZW |
438 | } |
439 | else | |
93c80368 | 440 | { |
18a9d8ff NB |
441 | /* In failed conditional groups, all non-conditional |
442 | directives are ignored. Before doing that, whether | |
443 | skipping or not, we should lex angle-bracketed headers | |
444 | correctly, and maybe output some diagnostics. */ | |
445 | pfile->state.angled_headers = dir->flags & INCL; | |
a8d0ddaf | 446 | pfile->state.directive_wants_padding = dir->flags & INCL; |
18a9d8ff NB |
447 | if (! CPP_OPTION (pfile, preprocessed)) |
448 | directive_diagnostics (pfile, dir, indented); | |
449 | if (pfile->state.skipping && !(dir->flags & COND)) | |
450 | dir = 0; | |
93c80368 NB |
451 | } |
452 | } | |
345894b4 | 453 | else if (dname->type == CPP_EOF) |
18a9d8ff NB |
454 | ; /* CPP_EOF is the "null directive". */ |
455 | else | |
93c80368 NB |
456 | { |
457 | /* An unknown directive. Don't complain about it in assembly | |
458 | source: we don't know where the comments are, and # may | |
459 | introduce assembler pseudo-ops. Don't complain about invalid | |
460 | directives in skipped conditional groups (6.10 p4). */ | |
bdb05a7b | 461 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
18a9d8ff NB |
462 | skip = 0; |
463 | else if (!pfile->state.skipping) | |
0527bc4e | 464 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
345894b4 | 465 | cpp_token_as_text (pfile, dname)); |
0d9f234d NB |
466 | } |
467 | ||
d1a58688 NB |
468 | pfile->directive = dir; |
469 | if (CPP_OPTION (pfile, traditional)) | |
470 | prepare_directive_trad (pfile); | |
471 | ||
18a9d8ff | 472 | if (dir) |
6cf87ca4 | 473 | pfile->directive->handler (pfile); |
18a9d8ff NB |
474 | else if (skip == 0) |
475 | _cpp_backup_tokens (pfile, 1); | |
476 | ||
477 | end_directive (pfile, skip); | |
765d600a | 478 | if (was_parsing_args && !pfile->state.in_deferred_pragma) |
e808ec9c NB |
479 | { |
480 | /* Restore state when within macro args. */ | |
481 | pfile->state.parsing_args = 2; | |
482 | pfile->state.prevent_expansion = 1; | |
e808ec9c | 483 | } |
c6e83800 ZW |
484 | if (was_discarding_output) |
485 | pfile->state.prevent_expansion = 1; | |
fe6c2db9 | 486 | return skip; |
041c3194 | 487 | } |
7f2935c7 | 488 | |
93c80368 | 489 | /* Directive handler wrapper used by the command line option |
26aea073 | 490 | processor. BUF is \n terminated. */ |
93c80368 | 491 | static void |
6cf87ca4 | 492 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
3fdc651f | 493 | { |
562a5c27 | 494 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
40de9f76 | 495 | /* from_stage3 */ true); |
0bda4760 | 496 | start_directive (pfile); |
26aea073 NB |
497 | |
498 | /* This is a short-term fix to prevent a leading '#' being | |
499 | interpreted as a directive. */ | |
500 | _cpp_clean_line (pfile); | |
501 | ||
f71aebba | 502 | pfile->directive = &dtable[dir_no]; |
1a76916c NB |
503 | if (CPP_OPTION (pfile, traditional)) |
504 | prepare_directive_trad (pfile); | |
6cf87ca4 | 505 | pfile->directive->handler (pfile); |
0bda4760 | 506 | end_directive (pfile, 1); |
ef6e958a | 507 | _cpp_pop_buffer (pfile); |
93c80368 NB |
508 | } |
509 | ||
510 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
ee1c2a10 TT |
511 | #ifndef directives. IS_DEF_OR_UNDEF is true if this call is |
512 | processing a #define or #undefine directive, and false | |
513 | otherwise. */ | |
041c3194 | 514 | static cpp_hashnode * |
ee1c2a10 | 515 | lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) |
041c3194 | 516 | { |
1a76916c | 517 | const cpp_token *token = _cpp_lex_token (pfile); |
ea4a453b | 518 | |
92936ecf | 519 | /* The token immediately after #define must be an identifier. That |
b8363a24 ZW |
520 | identifier may not be "defined", per C99 6.10.8p4. |
521 | In C++, it may not be any of the "named operators" either, | |
522 | per C++98 [lex.digraph], [lex.key]. | |
523 | Finally, the identifier may not have been poisoned. (In that case | |
1d63a28a | 524 | the lexer has issued the error message for us.) */ |
cbc69f84 | 525 | |
1a76916c NB |
526 | if (token->type == CPP_NAME) |
527 | { | |
528 | cpp_hashnode *node = token->val.node; | |
92936ecf | 529 | |
ee1c2a10 | 530 | if (is_def_or_undef && node == pfile->spec_nodes.n_defined) |
0527bc4e | 531 | cpp_error (pfile, CPP_DL_ERROR, |
1a76916c NB |
532 | "\"defined\" cannot be used as a macro name"); |
533 | else if (! (node->flags & NODE_POISONED)) | |
534 | return node; | |
ba89d661 | 535 | } |
1a76916c | 536 | else if (token->flags & NAMED_OP) |
0527bc4e | 537 | cpp_error (pfile, CPP_DL_ERROR, |
cbc69f84 | 538 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
1a76916c NB |
539 | NODE_NAME (token->val.node)); |
540 | else if (token->type == CPP_EOF) | |
0527bc4e | 541 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
1a76916c NB |
542 | pfile->directive->name); |
543 | else | |
0527bc4e | 544 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
07aa0b04 | 545 | |
cbc69f84 | 546 | return NULL; |
7f2935c7 | 547 | } |
7f2935c7 | 548 | |
a2566ae9 | 549 | /* Process a #define directive. Most work is done in macro.c. */ |
711b8824 | 550 | static void |
6cf87ca4 | 551 | do_define (cpp_reader *pfile) |
7f2935c7 | 552 | { |
ee1c2a10 | 553 | cpp_hashnode *node = lex_macro_node (pfile, true); |
ff2b53ef | 554 | |
93c80368 NB |
555 | if (node) |
556 | { | |
1d63a28a NB |
557 | /* If we have been requested to expand comments into macros, |
558 | then re-enable saving of comments. */ | |
559 | pfile->state.save_comments = | |
560 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
561 | ||
93d45d9e JM |
562 | if (pfile->cb.before_define) |
563 | pfile->cb.before_define (pfile); | |
564 | ||
93c80368 NB |
565 | if (_cpp_create_definition (pfile, node)) |
566 | if (pfile->cb.define) | |
6cf87ca4 | 567 | pfile->cb.define (pfile, pfile->directive_line, node); |
93d45d9e JM |
568 | |
569 | node->flags &= ~NODE_USED; | |
93c80368 | 570 | } |
041c3194 ZW |
571 | } |
572 | ||
5d8ebbd8 | 573 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
711b8824 | 574 | static void |
6cf87ca4 | 575 | do_undef (cpp_reader *pfile) |
041c3194 | 576 | { |
ee1c2a10 | 577 | cpp_hashnode *node = lex_macro_node (pfile, true); |
9e62c811 | 578 | |
45f2492c | 579 | if (node) |
9e62c811 | 580 | { |
93d45d9e JM |
581 | if (pfile->cb.before_define) |
582 | pfile->cb.before_define (pfile); | |
583 | ||
58fea6af | 584 | if (pfile->cb.undef) |
6cf87ca4 | 585 | pfile->cb.undef (pfile, pfile->directive_line, node); |
9e62c811 | 586 | |
45f2492c NB |
587 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
588 | identifier is not currently defined as a macro name. */ | |
589 | if (node->type == NT_MACRO) | |
590 | { | |
591 | if (node->flags & NODE_WARN) | |
592 | cpp_error (pfile, CPP_DL_WARNING, | |
593 | "undefining \"%s\"", NODE_NAME (node)); | |
9e62c811 | 594 | |
45f2492c NB |
595 | if (CPP_OPTION (pfile, warn_unused_macros)) |
596 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
a69cbaac | 597 | |
45f2492c NB |
598 | _cpp_free_definition (node); |
599 | } | |
9e62c811 | 600 | } |
45f2492c | 601 | |
93c80368 | 602 | check_eol (pfile); |
7f2935c7 PB |
603 | } |
604 | ||
d1bd0ded GK |
605 | /* Undefine a single macro/assertion/whatever. */ |
606 | ||
607 | static int | |
c6e83800 | 608 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
d1bd0ded GK |
609 | void *data_p ATTRIBUTE_UNUSED) |
610 | { | |
c6e83800 ZW |
611 | /* Body of _cpp_free_definition inlined here for speed. |
612 | Macros and assertions no longer have anything to free. */ | |
613 | h->type = NT_VOID; | |
93d45d9e | 614 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED); |
d1bd0ded GK |
615 | return 1; |
616 | } | |
617 | ||
618 | /* Undefine all macros and assertions. */ | |
619 | ||
620 | void | |
621 | cpp_undef_all (cpp_reader *pfile) | |
622 | { | |
623 | cpp_forall_identifiers (pfile, undefine_macros, NULL); | |
624 | } | |
625 | ||
626 | ||
93c80368 NB |
627 | /* Helper routine used by parse_include. Reinterpret the current line |
628 | as an h-char-sequence (< ... >); we are looking at the first token | |
74eb4b3e NB |
629 | after the <. Returns a malloced filename. */ |
630 | static char * | |
6cf87ca4 | 631 | glue_header_name (cpp_reader *pfile) |
93c80368 | 632 | { |
4ed5bcfb | 633 | const cpp_token *token; |
74eb4b3e | 634 | char *buffer; |
2450e0b8 | 635 | size_t len, total_len = 0, capacity = 1024; |
93c80368 NB |
636 | |
637 | /* To avoid lexed tokens overwriting our glued name, we can only | |
638 | allocate from the string pool once we've lexed everything. */ | |
c3f829c1 | 639 | buffer = XNEWVEC (char, capacity); |
93c80368 NB |
640 | for (;;) |
641 | { | |
a8d0ddaf | 642 | token = get_token_no_padding (pfile); |
93c80368 | 643 | |
74eb4b3e | 644 | if (token->type == CPP_GREATER) |
93c80368 | 645 | break; |
74eb4b3e NB |
646 | if (token->type == CPP_EOF) |
647 | { | |
0527bc4e | 648 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
74eb4b3e NB |
649 | break; |
650 | } | |
93c80368 | 651 | |
59325650 | 652 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
2450e0b8 | 653 | if (total_len + len > capacity) |
93c80368 | 654 | { |
2450e0b8 | 655 | capacity = (capacity + len) * 2; |
c3f829c1 | 656 | buffer = XRESIZEVEC (char, buffer, capacity); |
93c80368 NB |
657 | } |
658 | ||
4ed5bcfb | 659 | if (token->flags & PREV_WHITE) |
2450e0b8 | 660 | buffer[total_len++] = ' '; |
93c80368 | 661 | |
47e20491 GK |
662 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
663 | true) | |
74eb4b3e | 664 | - (uchar *) buffer); |
93c80368 | 665 | } |
041c3194 | 666 | |
74eb4b3e NB |
667 | buffer[total_len] = '\0'; |
668 | return buffer; | |
93c80368 | 669 | } |
7f2935c7 | 670 | |
74eb4b3e NB |
671 | /* Returns the file name of #include, #include_next, #import and |
672 | #pragma dependency. The string is malloced and the caller should | |
673 | free it. Returns NULL on error. */ | |
674 | static const char * | |
cbc43ae0 ILT |
675 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
676 | const cpp_token ***buf) | |
7f2935c7 | 677 | { |
74eb4b3e | 678 | char *fname; |
4ed5bcfb | 679 | const cpp_token *header; |
7f2935c7 | 680 | |
93c80368 | 681 | /* Allow macro expansion. */ |
a8d0ddaf | 682 | header = get_token_no_padding (pfile); |
74eb4b3e | 683 | if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME) |
7f2935c7 | 684 | { |
c3f829c1 | 685 | fname = XNEWVEC (char, header->val.str.len - 1); |
6338b358 NB |
686 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
687 | fname[header->val.str.len - 2] = '\0'; | |
74eb4b3e | 688 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
7f2935c7 | 689 | } |
74eb4b3e | 690 | else if (header->type == CPP_LESS) |
7f2935c7 | 691 | { |
74eb4b3e NB |
692 | fname = glue_header_name (pfile); |
693 | *pangle_brackets = 1; | |
694 | } | |
695 | else | |
696 | { | |
697 | const unsigned char *dir; | |
698 | ||
699 | if (pfile->directive == &dtable[T_PRAGMA]) | |
b6baa67d | 700 | dir = UC"pragma dependency"; |
74eb4b3e NB |
701 | else |
702 | dir = pfile->directive->name; | |
0527bc4e | 703 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
74eb4b3e NB |
704 | dir); |
705 | ||
4ed5bcfb | 706 | return NULL; |
7f2935c7 | 707 | } |
7f2935c7 | 708 | |
cda5e672 TT |
709 | if (pfile->directive == &dtable[T_PRAGMA]) |
710 | { | |
711 | /* This pragma allows extra tokens after the file name. */ | |
712 | } | |
713 | else if (buf == NULL || CPP_OPTION (pfile, discard_comments)) | |
cbc43ae0 ILT |
714 | check_eol (pfile); |
715 | else | |
716 | { | |
717 | /* If we are not discarding comments, then gather them while | |
718 | doing the eol check. */ | |
719 | *buf = check_eol_return_comments (pfile); | |
720 | } | |
721 | ||
74eb4b3e | 722 | return fname; |
168d3732 | 723 | } |
3caee4a8 | 724 | |
ba133c96 | 725 | /* Handle #include, #include_next and #import. */ |
711b8824 | 726 | static void |
6cf87ca4 | 727 | do_include_common (cpp_reader *pfile, enum include_type type) |
168d3732 | 728 | { |
74eb4b3e NB |
729 | const char *fname; |
730 | int angle_brackets; | |
cbc43ae0 ILT |
731 | const cpp_token **buf = NULL; |
732 | ||
733 | /* Re-enable saving of comments if requested, so that the include | |
734 | callback can dump comments which follow #include. */ | |
735 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); | |
74eb4b3e | 736 | |
cbc43ae0 | 737 | fname = parse_include (pfile, &angle_brackets, &buf); |
74eb4b3e | 738 | if (!fname) |
cbc43ae0 ILT |
739 | { |
740 | if (buf) | |
741 | XDELETEVEC (buf); | |
742 | return; | |
743 | } | |
7f2935c7 | 744 | |
28303828 NN |
745 | if (!*fname) |
746 | { | |
747 | cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s", | |
748 | pfile->directive->name); | |
cbc43ae0 ILT |
749 | XDELETEVEC (fname); |
750 | if (buf) | |
751 | XDELETEVEC (buf); | |
28303828 NN |
752 | return; |
753 | } | |
754 | ||
3963c2e0 | 755 | /* Prevent #include recursion. */ |
50f59cd7 | 756 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
0527bc4e | 757 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
74eb4b3e | 758 | else |
09b82253 | 759 | { |
74eb4b3e NB |
760 | /* Get out of macro context, if we are. */ |
761 | skip_rest_of_line (pfile); | |
09b82253 | 762 | |
74eb4b3e | 763 | if (pfile->cb.include) |
6cf87ca4 | 764 | pfile->cb.include (pfile, pfile->directive_line, |
cbc43ae0 ILT |
765 | pfile->directive->name, fname, angle_brackets, |
766 | buf); | |
3963c2e0 | 767 | |
8f9b4009 | 768 | _cpp_stack_include (pfile, fname, angle_brackets, type); |
74eb4b3e | 769 | } |
3963c2e0 | 770 | |
cbc43ae0 ILT |
771 | XDELETEVEC (fname); |
772 | if (buf) | |
773 | XDELETEVEC (buf); | |
168d3732 | 774 | } |
e8037d57 | 775 | |
711b8824 | 776 | static void |
6cf87ca4 | 777 | do_include (cpp_reader *pfile) |
168d3732 | 778 | { |
ba133c96 NB |
779 | do_include_common (pfile, IT_INCLUDE); |
780 | } | |
168d3732 | 781 | |
ba133c96 | 782 | static void |
6cf87ca4 | 783 | do_import (cpp_reader *pfile) |
ba133c96 | 784 | { |
ba133c96 | 785 | do_include_common (pfile, IT_IMPORT); |
168d3732 | 786 | } |
7f2935c7 | 787 | |
711b8824 | 788 | static void |
6cf87ca4 | 789 | do_include_next (cpp_reader *pfile) |
168d3732 | 790 | { |
3963c2e0 ZW |
791 | enum include_type type = IT_INCLUDE_NEXT; |
792 | ||
793 | /* If this is the primary source file, warn and use the normal | |
794 | search logic. */ | |
705e2d28 | 795 | if (cpp_in_primary_file (pfile)) |
3963c2e0 | 796 | { |
0527bc4e | 797 | cpp_error (pfile, CPP_DL_WARNING, |
3963c2e0 ZW |
798 | "#include_next in primary source file"); |
799 | type = IT_INCLUDE; | |
800 | } | |
801 | do_include_common (pfile, type); | |
7f2935c7 | 802 | } |
7f2935c7 | 803 | |
dcc229e5 ZW |
804 | /* Subroutine of do_linemarker. Read possible flags after file name. |
805 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
806 | flag if it is valid, 0 at the end of the directive. Otherwise | |
807 | complain. */ | |
642ce434 | 808 | static unsigned int |
6cf87ca4 | 809 | read_flag (cpp_reader *pfile, unsigned int last) |
d3a34a0a | 810 | { |
345894b4 | 811 | const cpp_token *token = _cpp_lex_token (pfile); |
d3a34a0a | 812 | |
345894b4 | 813 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
d3a34a0a | 814 | { |
345894b4 | 815 | unsigned int flag = token->val.str.text[0] - '0'; |
28e0f040 NB |
816 | |
817 | if (flag > last && flag <= 4 | |
818 | && (flag != 4 || last == 3) | |
819 | && (flag != 2 || last == 0)) | |
820 | return flag; | |
d3a34a0a | 821 | } |
93c80368 | 822 | |
345894b4 | 823 | if (token->type != CPP_EOF) |
0527bc4e | 824 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
345894b4 | 825 | cpp_token_as_text (pfile, token)); |
93c80368 | 826 | return 0; |
d3a34a0a JM |
827 | } |
828 | ||
dcc229e5 ZW |
829 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
830 | of length LEN, to binary; store it in NUMP, and return 0 if the | |
831 | number was well-formed, 1 if not. Temporary, hopefully. */ | |
041c3194 | 832 | static int |
6cf87ca4 | 833 | strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump) |
041c3194 ZW |
834 | { |
835 | unsigned long reg = 0; | |
562a5c27 | 836 | uchar c; |
041c3194 ZW |
837 | while (len--) |
838 | { | |
839 | c = *str++; | |
840 | if (!ISDIGIT (c)) | |
841 | return 1; | |
842 | reg *= 10; | |
843 | reg += c - '0'; | |
844 | } | |
845 | *nump = reg; | |
846 | return 0; | |
847 | } | |
848 | ||
6de1e2a9 | 849 | /* Interpret #line command. |
dcc229e5 ZW |
850 | Note that the filename string (if any) is a true string constant |
851 | (escapes are interpreted), unlike in #line. */ | |
711b8824 | 852 | static void |
6cf87ca4 | 853 | do_line (cpp_reader *pfile) |
7f2935c7 | 854 | { |
500bee0a PB |
855 | const struct line_maps *line_table = pfile->line_table; |
856 | const struct line_map *map = &line_table->maps[line_table->used - 1]; | |
2203a881 DP |
857 | |
858 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down | |
859 | sysp right now. */ | |
860 | ||
861 | unsigned char map_sysp = map->sysp; | |
4ed5bcfb | 862 | const cpp_token *token; |
12f9df4e | 863 | const char *new_file = map->to_file; |
bb74c963 | 864 | unsigned long new_lineno; |
93c80368 | 865 | |
27e2564a | 866 | /* C99 raised the minimum limit on #line numbers. */ |
dcc229e5 | 867 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
18a9d8ff | 868 | |
93c80368 | 869 | /* #line commands expand macros. */ |
4ed5bcfb NB |
870 | token = cpp_get_token (pfile); |
871 | if (token->type != CPP_NUMBER | |
872 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
873 | &new_lineno)) | |
7f2935c7 | 874 | { |
33ae4837 TT |
875 | if (token->type == CPP_EOF) |
876 | cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line"); | |
877 | else | |
878 | cpp_error (pfile, CPP_DL_ERROR, | |
879 | "\"%s\" after #line is not a positive integer", | |
880 | cpp_token_as_text (pfile, token)); | |
9ec7291f | 881 | return; |
df383483 | 882 | } |
7f2935c7 | 883 | |
dcc229e5 | 884 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
0527bc4e | 885 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
7f2935c7 | 886 | |
4ed5bcfb NB |
887 | token = cpp_get_token (pfile); |
888 | if (token->type == CPP_STRING) | |
5538ada6 | 889 | { |
e6cc3a24 | 890 | cpp_string s = { 0, 0 }; |
423e95e2 EC |
891 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
892 | &s, false)) | |
e6cc3a24 | 893 | new_file = (const char *)s.text; |
dcc229e5 ZW |
894 | check_eol (pfile); |
895 | } | |
896 | else if (token->type != CPP_EOF) | |
897 | { | |
0527bc4e | 898 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
dcc229e5 ZW |
899 | cpp_token_as_text (pfile, token)); |
900 | return; | |
901 | } | |
902 | ||
903 | skip_rest_of_line (pfile); | |
904 | _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, | |
2203a881 | 905 | map_sysp); |
dcc229e5 ZW |
906 | } |
907 | ||
908 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
909 | different syntax and semantics from #line: Flags are allowed, | |
910 | and we never complain about the line number being too big. */ | |
911 | static void | |
6cf87ca4 | 912 | do_linemarker (cpp_reader *pfile) |
dcc229e5 | 913 | { |
500bee0a PB |
914 | const struct line_maps *line_table = pfile->line_table; |
915 | const struct line_map *map = &line_table->maps[line_table->used - 1]; | |
dcc229e5 | 916 | const cpp_token *token; |
12f9df4e | 917 | const char *new_file = map->to_file; |
dcc229e5 | 918 | unsigned long new_lineno; |
12f9df4e | 919 | unsigned int new_sysp = map->sysp; |
dcc229e5 ZW |
920 | enum lc_reason reason = LC_RENAME; |
921 | int flag; | |
922 | ||
923 | /* Back up so we can get the number again. Putting this in | |
924 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
925 | some circumstances, which can segfault. */ | |
926 | _cpp_backup_tokens (pfile, 1); | |
927 | ||
928 | /* #line commands expand macros. */ | |
929 | token = cpp_get_token (pfile); | |
930 | if (token->type != CPP_NUMBER | |
931 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
932 | &new_lineno)) | |
933 | { | |
33ae4837 TT |
934 | /* Unlike #line, there does not seem to be a way to get an EOF |
935 | here. So, it should be safe to always spell the token. */ | |
0527bc4e JDA |
936 | cpp_error (pfile, CPP_DL_ERROR, |
937 | "\"%s\" after # is not a positive integer", | |
dcc229e5 ZW |
938 | cpp_token_as_text (pfile, token)); |
939 | return; | |
df383483 | 940 | } |
941e09b6 | 941 | |
dcc229e5 ZW |
942 | token = cpp_get_token (pfile); |
943 | if (token->type == CPP_STRING) | |
944 | { | |
e6cc3a24 | 945 | cpp_string s = { 0, 0 }; |
423e95e2 EC |
946 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
947 | 1, &s, false)) | |
e6cc3a24 | 948 | new_file = (const char *)s.text; |
cf551fba | 949 | |
dcc229e5 ZW |
950 | new_sysp = 0; |
951 | flag = read_flag (pfile, 0); | |
952 | if (flag == 1) | |
953 | { | |
954 | reason = LC_ENTER; | |
955 | /* Fake an include for cpp_included (). */ | |
956 | _cpp_fake_include (pfile, new_file); | |
957 | flag = read_flag (pfile, flag); | |
958 | } | |
959 | else if (flag == 2) | |
960 | { | |
961 | reason = LC_LEAVE; | |
962 | flag = read_flag (pfile, flag); | |
963 | } | |
964 | if (flag == 3) | |
93c80368 | 965 | { |
dcc229e5 ZW |
966 | new_sysp = 1; |
967 | flag = read_flag (pfile, flag); | |
968 | if (flag == 4) | |
969 | new_sysp = 2; | |
93c80368 | 970 | } |
9d30f270 | 971 | pfile->buffer->sysp = new_sysp; |
dcc229e5 | 972 | |
fde84349 | 973 | check_eol (pfile); |
041c3194 | 974 | } |
4ed5bcfb | 975 | else if (token->type != CPP_EOF) |
27e2564a | 976 | { |
0527bc4e | 977 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
4ed5bcfb | 978 | cpp_token_as_text (pfile, token)); |
27e2564a NB |
979 | return; |
980 | } | |
7f2935c7 | 981 | |
bdcbe496 | 982 | skip_rest_of_line (pfile); |
bb74c963 | 983 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
27e2564a NB |
984 | } |
985 | ||
67821e3a | 986 | /* Arrange the file_change callback. pfile->line has changed to |
47d89cf3 | 987 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
a1f300c0 | 988 | header, 2 for a system header that needs to be extern "C" protected, |
47d89cf3 | 989 | and zero otherwise. */ |
eb1f4d9d | 990 | void |
6cf87ca4 ZW |
991 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
992 | const char *to_file, unsigned int file_line, | |
993 | unsigned int sysp) | |
27e2564a | 994 | { |
12f9df4e PB |
995 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
996 | to_file, file_line); | |
500bee0a PB |
997 | if (map != NULL) |
998 | linemap_line_start (pfile->line_table, map->to_line, 127); | |
d82fc108 | 999 | |
eb1f4d9d | 1000 | if (pfile->cb.file_change) |
12f9df4e | 1001 | pfile->cb.file_change (pfile, map); |
7f2935c7 | 1002 | } |
941e09b6 | 1003 | |
5d8ebbd8 NB |
1004 | /* Report a warning or error detected by the program we are |
1005 | processing. Use the directive's tokens in the error message. */ | |
711b8824 | 1006 | static void |
6cf87ca4 | 1007 | do_diagnostic (cpp_reader *pfile, int code, int print_dir) |
7f2935c7 | 1008 | { |
12f9df4e | 1009 | if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0)) |
58fea6af | 1010 | { |
29b10746 NB |
1011 | if (print_dir) |
1012 | fprintf (stderr, "#%s ", pfile->directive->name); | |
93c80368 NB |
1013 | pfile->state.prevent_expansion++; |
1014 | cpp_output_line (pfile, stderr); | |
1015 | pfile->state.prevent_expansion--; | |
58fea6af | 1016 | } |
7f2935c7 PB |
1017 | } |
1018 | ||
838f313b | 1019 | static void |
6cf87ca4 | 1020 | do_error (cpp_reader *pfile) |
838f313b | 1021 | { |
0527bc4e | 1022 | do_diagnostic (pfile, CPP_DL_ERROR, 1); |
838f313b | 1023 | } |
7f2935c7 | 1024 | |
711b8824 | 1025 | static void |
6cf87ca4 | 1026 | do_warning (cpp_reader *pfile) |
7f2935c7 | 1027 | { |
2f878973 | 1028 | /* We want #warning diagnostics to be emitted in system headers too. */ |
0527bc4e | 1029 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1); |
7f2935c7 PB |
1030 | } |
1031 | ||
a2a76ce7 | 1032 | /* Report program identification. */ |
711b8824 | 1033 | static void |
6cf87ca4 | 1034 | do_ident (cpp_reader *pfile) |
7f2935c7 | 1035 | { |
4ed5bcfb | 1036 | const cpp_token *str = cpp_get_token (pfile); |
58fea6af | 1037 | |
4ed5bcfb | 1038 | if (str->type != CPP_STRING) |
1ed17cd5 ZW |
1039 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
1040 | pfile->directive->name); | |
93c80368 | 1041 | else if (pfile->cb.ident) |
6cf87ca4 | 1042 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
a2a76ce7 | 1043 | |
93c80368 | 1044 | check_eol (pfile); |
7f2935c7 PB |
1045 | } |
1046 | ||
a5da89c6 NB |
1047 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
1048 | matching entry, or NULL if none is found. The returned entry could | |
1049 | be the start of a namespace chain, or a pragma. */ | |
1050 | static struct pragma_entry * | |
6cf87ca4 | 1051 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
82443371 | 1052 | { |
4b115ff0 NB |
1053 | while (chain && chain->pragma != pragma) |
1054 | chain = chain->next; | |
a5da89c6 NB |
1055 | |
1056 | return chain; | |
1057 | } | |
1058 | ||
bc4071dd RH |
1059 | /* Create and insert a blank pragma entry at the beginning of a |
1060 | singly-linked CHAIN. */ | |
a5da89c6 | 1061 | static struct pragma_entry * |
bc4071dd | 1062 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
82443371 | 1063 | { |
c3f829c1 | 1064 | struct pragma_entry *new_entry; |
58fea6af | 1065 | |
c3f829c1 | 1066 | new_entry = (struct pragma_entry *) |
8c3b2693 | 1067 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
58fea6af | 1068 | |
bc4071dd | 1069 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
c3f829c1 | 1070 | new_entry->next = *chain; |
bc4071dd | 1071 | |
c3f829c1 GDR |
1072 | *chain = new_entry; |
1073 | return new_entry; | |
58fea6af | 1074 | } |
82443371 | 1075 | |
a5da89c6 | 1076 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
bc4071dd RH |
1077 | goes in the global namespace. */ |
1078 | static struct pragma_entry * | |
1079 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, | |
1080 | bool allow_name_expansion) | |
82443371 | 1081 | { |
a5da89c6 NB |
1082 | struct pragma_entry **chain = &pfile->pragmas; |
1083 | struct pragma_entry *entry; | |
4b115ff0 | 1084 | const cpp_hashnode *node; |
a5da89c6 | 1085 | |
a5da89c6 | 1086 | if (space) |
58fea6af | 1087 | { |
b6baa67d | 1088 | node = cpp_lookup (pfile, UC space, strlen (space)); |
4b115ff0 | 1089 | entry = lookup_pragma_entry (*chain, node); |
a5da89c6 | 1090 | if (!entry) |
bc4071dd RH |
1091 | { |
1092 | entry = new_pragma_entry (pfile, chain); | |
1093 | entry->pragma = node; | |
1094 | entry->is_nspace = true; | |
1095 | entry->allow_expansion = allow_name_expansion; | |
1096 | } | |
a5da89c6 NB |
1097 | else if (!entry->is_nspace) |
1098 | goto clash; | |
bc4071dd RH |
1099 | else if (entry->allow_expansion != allow_name_expansion) |
1100 | { | |
1101 | cpp_error (pfile, CPP_DL_ICE, | |
1102 | "registering pragmas in namespace \"%s\" with mismatched " | |
1103 | "name expansion", space); | |
1104 | return NULL; | |
1105 | } | |
a5da89c6 | 1106 | chain = &entry->u.space; |
58fea6af | 1107 | } |
bc4071dd RH |
1108 | else if (allow_name_expansion) |
1109 | { | |
1110 | cpp_error (pfile, CPP_DL_ICE, | |
1111 | "registering pragma \"%s\" with name expansion " | |
1112 | "and no namespace", name); | |
1113 | return NULL; | |
1114 | } | |
58fea6af | 1115 | |
a5da89c6 | 1116 | /* Check for duplicates. */ |
b6baa67d | 1117 | node = cpp_lookup (pfile, UC name, strlen (name)); |
4b115ff0 | 1118 | entry = lookup_pragma_entry (*chain, node); |
bc4071dd | 1119 | if (entry == NULL) |
a5da89c6 | 1120 | { |
bc4071dd RH |
1121 | entry = new_pragma_entry (pfile, chain); |
1122 | entry->pragma = node; | |
1123 | return entry; | |
a5da89c6 | 1124 | } |
bc4071dd RH |
1125 | |
1126 | if (entry->is_nspace) | |
1127 | clash: | |
1128 | cpp_error (pfile, CPP_DL_ICE, | |
1129 | "registering \"%s\" as both a pragma and a pragma namespace", | |
1130 | NODE_NAME (node)); | |
1131 | else if (space) | |
1132 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", | |
1133 | space, name); | |
a5da89c6 | 1134 | else |
bc4071dd RH |
1135 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
1136 | ||
1137 | return NULL; | |
1138 | } | |
1139 | ||
1140 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ | |
1141 | static void | |
1142 | register_pragma_internal (cpp_reader *pfile, const char *space, | |
1143 | const char *name, pragma_cb handler) | |
1144 | { | |
1145 | struct pragma_entry *entry; | |
1146 | ||
1147 | entry = register_pragma_1 (pfile, space, name, false); | |
1148 | entry->is_internal = true; | |
1149 | entry->u.handler = handler; | |
21b11495 ZW |
1150 | } |
1151 | ||
1152 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it | |
1153 | goes in the global namespace. HANDLER is the handler it will call, | |
b5b3e36a DJ |
1154 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
1155 | expansion while parsing pragma NAME. This function is exported | |
1156 | from libcpp. */ | |
21b11495 ZW |
1157 | void |
1158 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, | |
b5b3e36a | 1159 | pragma_cb handler, bool allow_expansion) |
21b11495 | 1160 | { |
bc4071dd RH |
1161 | struct pragma_entry *entry; |
1162 | ||
1163 | if (!handler) | |
1164 | { | |
1165 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); | |
1166 | return; | |
1167 | } | |
1168 | ||
1169 | entry = register_pragma_1 (pfile, space, name, false); | |
1170 | if (entry) | |
1171 | { | |
1172 | entry->allow_expansion = allow_expansion; | |
1173 | entry->u.handler = handler; | |
1174 | } | |
58fea6af | 1175 | } |
a5da89c6 | 1176 | |
bc4071dd RH |
1177 | /* Similarly, but create mark the pragma for deferred processing. |
1178 | When found, a CPP_PRAGMA token will be insertted into the stream | |
1179 | with IDENT in the token->u.pragma slot. */ | |
1180 | void | |
1181 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, | |
1182 | const char *name, unsigned int ident, | |
1183 | bool allow_expansion, bool allow_name_expansion) | |
1184 | { | |
1185 | struct pragma_entry *entry; | |
1186 | ||
1187 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); | |
1188 | if (entry) | |
1189 | { | |
1190 | entry->is_deferred = true; | |
1191 | entry->allow_expansion = allow_expansion; | |
1192 | entry->u.ident = ident; | |
1193 | } | |
1194 | } | |
1195 | ||
a5da89c6 | 1196 | /* Register the pragmas the preprocessor itself handles. */ |
58fea6af | 1197 | void |
6cf87ca4 | 1198 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
58fea6af | 1199 | { |
a5da89c6 | 1200 | /* Pragmas in the global namespace. */ |
bc4071dd | 1201 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
58fea6af | 1202 | |
a5da89c6 | 1203 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
bc4071dd RH |
1204 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
1205 | register_pragma_internal (pfile, "GCC", "system_header", | |
1206 | do_pragma_system_header); | |
1207 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); | |
82443371 | 1208 | } |
7f2935c7 | 1209 | |
17211ab5 GK |
1210 | /* Return the number of registered pragmas in PE. */ |
1211 | ||
1212 | static int | |
6cf87ca4 | 1213 | count_registered_pragmas (struct pragma_entry *pe) |
17211ab5 GK |
1214 | { |
1215 | int ct = 0; | |
1216 | for (; pe != NULL; pe = pe->next) | |
1217 | { | |
1218 | if (pe->is_nspace) | |
1219 | ct += count_registered_pragmas (pe->u.space); | |
1220 | ct++; | |
1221 | } | |
1222 | return ct; | |
1223 | } | |
1224 | ||
1225 | /* Save into SD the names of the registered pragmas referenced by PE, | |
1226 | and return a pointer to the next free space in SD. */ | |
1227 | ||
1228 | static char ** | |
6cf87ca4 | 1229 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
17211ab5 GK |
1230 | { |
1231 | for (; pe != NULL; pe = pe->next) | |
1232 | { | |
1233 | if (pe->is_nspace) | |
1234 | sd = save_registered_pragmas (pe->u.space, sd); | |
c3f829c1 GDR |
1235 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
1236 | HT_LEN (&pe->pragma->ident), | |
1237 | HT_LEN (&pe->pragma->ident) + 1); | |
17211ab5 GK |
1238 | } |
1239 | return sd; | |
1240 | } | |
1241 | ||
1242 | /* Return a newly-allocated array which saves the names of the | |
1243 | registered pragmas. */ | |
1244 | ||
1245 | char ** | |
6cf87ca4 | 1246 | _cpp_save_pragma_names (cpp_reader *pfile) |
17211ab5 GK |
1247 | { |
1248 | int ct = count_registered_pragmas (pfile->pragmas); | |
72bb2c39 | 1249 | char **result = XNEWVEC (char *, ct); |
17211ab5 GK |
1250 | (void) save_registered_pragmas (pfile->pragmas, result); |
1251 | return result; | |
1252 | } | |
1253 | ||
1254 | /* Restore from SD the names of the registered pragmas referenced by PE, | |
1255 | and return a pointer to the next unused name in SD. */ | |
1256 | ||
1257 | static char ** | |
6cf87ca4 ZW |
1258 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
1259 | char **sd) | |
17211ab5 GK |
1260 | { |
1261 | for (; pe != NULL; pe = pe->next) | |
1262 | { | |
1263 | if (pe->is_nspace) | |
1264 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); | |
b6baa67d | 1265 | pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd)); |
17211ab5 GK |
1266 | free (*sd); |
1267 | sd++; | |
1268 | } | |
1269 | return sd; | |
1270 | } | |
1271 | ||
1272 | /* Restore the names of the registered pragmas from SAVED. */ | |
1273 | ||
1274 | void | |
6cf87ca4 | 1275 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
17211ab5 GK |
1276 | { |
1277 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); | |
1278 | free (saved); | |
1279 | } | |
1280 | ||
a5da89c6 NB |
1281 | /* Pragmata handling. We handle some, and pass the rest on to the |
1282 | front end. C99 defines three pragmas and says that no macro | |
1283 | expansion is to be performed on them; whether or not macro | |
1284 | expansion happens for other pragmas is implementation defined. | |
bc4071dd RH |
1285 | This implementation allows for a mix of both, since GCC did not |
1286 | traditionally macro expand its (few) pragmas, whereas OpenMP | |
1287 | specifies that macro expansion should happen. */ | |
711b8824 | 1288 | static void |
6cf87ca4 | 1289 | do_pragma (cpp_reader *pfile) |
7f2935c7 | 1290 | { |
a5da89c6 | 1291 | const struct pragma_entry *p = NULL; |
e2e1fa50 | 1292 | const cpp_token *token, *pragma_token = pfile->cur_token; |
1c90c6f9 | 1293 | cpp_token ns_token; |
a5da89c6 | 1294 | unsigned int count = 1; |
add7091b | 1295 | |
93c80368 | 1296 | pfile->state.prevent_expansion++; |
58fea6af | 1297 | |
4ed5bcfb | 1298 | token = cpp_get_token (pfile); |
1c90c6f9 | 1299 | ns_token = *token; |
4ed5bcfb | 1300 | if (token->type == CPP_NAME) |
0172e2bc | 1301 | { |
4b115ff0 | 1302 | p = lookup_pragma_entry (pfile->pragmas, token->val.node); |
a5da89c6 | 1303 | if (p && p->is_nspace) |
58fea6af | 1304 | { |
bc4071dd RH |
1305 | bool allow_name_expansion = p->allow_expansion; |
1306 | if (allow_name_expansion) | |
1307 | pfile->state.prevent_expansion--; | |
a5da89c6 NB |
1308 | token = cpp_get_token (pfile); |
1309 | if (token->type == CPP_NAME) | |
4b115ff0 | 1310 | p = lookup_pragma_entry (p->u.space, token->val.node); |
a5da89c6 NB |
1311 | else |
1312 | p = NULL; | |
bc4071dd RH |
1313 | if (allow_name_expansion) |
1314 | pfile->state.prevent_expansion++; | |
1315 | count = 2; | |
58fea6af | 1316 | } |
58fea6af | 1317 | } |
041c3194 | 1318 | |
3da3d587 | 1319 | if (p) |
e2e1fa50 | 1320 | { |
bc4071dd RH |
1321 | if (p->is_deferred) |
1322 | { | |
1323 | pfile->directive_result.src_loc = pragma_token->src_loc; | |
1324 | pfile->directive_result.type = CPP_PRAGMA; | |
1325 | pfile->directive_result.flags = pragma_token->flags; | |
1326 | pfile->directive_result.val.pragma = p->u.ident; | |
1327 | pfile->state.in_deferred_pragma = true; | |
1328 | pfile->state.pragma_allow_expansion = p->allow_expansion; | |
1329 | if (!p->allow_expansion) | |
1330 | pfile->state.prevent_expansion++; | |
1331 | } | |
1332 | else | |
3da3d587 | 1333 | { |
bc4071dd RH |
1334 | /* Since the handler below doesn't get the line number, that |
1335 | it might need for diagnostics, make sure it has the right | |
3da3d587 ZW |
1336 | numbers in place. */ |
1337 | if (pfile->cb.line_change) | |
1338 | (*pfile->cb.line_change) (pfile, pragma_token, false); | |
bc4071dd | 1339 | if (p->allow_expansion) |
b5b3e36a | 1340 | pfile->state.prevent_expansion--; |
3da3d587 | 1341 | (*p->u.handler) (pfile); |
bc4071dd | 1342 | if (p->allow_expansion) |
b5b3e36a | 1343 | pfile->state.prevent_expansion++; |
3da3d587 | 1344 | } |
21b11495 | 1345 | } |
d82fc108 | 1346 | else if (pfile->cb.def_pragma) |
bdcbe496 | 1347 | { |
1c90c6f9 JJ |
1348 | if (count == 1 || pfile->context->prev == NULL) |
1349 | _cpp_backup_tokens (pfile, count); | |
1350 | else | |
1351 | { | |
1352 | /* Invalid name comes from macro expansion, _cpp_backup_tokens | |
1353 | won't allow backing 2 tokens. */ | |
1354 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook | |
1355 | reads both tokens, we could perhaps free it, but if it doesn't, | |
1356 | we don't know the exact lifespan. */ | |
1357 | cpp_token *toks = XNEWVEC (cpp_token, 2); | |
1358 | toks[0] = ns_token; | |
1359 | toks[0].flags |= NO_EXPAND; | |
1360 | toks[1] = *token; | |
1361 | toks[1].flags |= NO_EXPAND; | |
1362 | _cpp_push_token_context (pfile, NULL, toks, 2); | |
1363 | } | |
6cf87ca4 | 1364 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
bdcbe496 | 1365 | } |
a5da89c6 | 1366 | |
97293897 | 1367 | pfile->state.prevent_expansion--; |
82443371 NS |
1368 | } |
1369 | ||
5d8ebbd8 | 1370 | /* Handle #pragma once. */ |
58fea6af | 1371 | static void |
6cf87ca4 | 1372 | do_pragma_once (cpp_reader *pfile) |
a2a76ce7 | 1373 | { |
705e2d28 | 1374 | if (cpp_in_primary_file (pfile)) |
0527bc4e | 1375 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
93c80368 NB |
1376 | |
1377 | check_eol (pfile); | |
49634b3a | 1378 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
a2a76ce7 | 1379 | } |
fc009f96 | 1380 | |
c3bf3e6e NB |
1381 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1382 | that the lexer produces a hard error for each subsequent usage. */ | |
58fea6af | 1383 | static void |
6cf87ca4 | 1384 | do_pragma_poison (cpp_reader *pfile) |
a2a76ce7 | 1385 | { |
345894b4 | 1386 | const cpp_token *tok; |
f8f769ea | 1387 | cpp_hashnode *hp; |
a2a76ce7 | 1388 | |
93c80368 | 1389 | pfile->state.poisoned_ok = 1; |
a2a76ce7 ZW |
1390 | for (;;) |
1391 | { | |
345894b4 NB |
1392 | tok = _cpp_lex_token (pfile); |
1393 | if (tok->type == CPP_EOF) | |
a2a76ce7 | 1394 | break; |
345894b4 | 1395 | if (tok->type != CPP_NAME) |
fc009f96 | 1396 | { |
0527bc4e JDA |
1397 | cpp_error (pfile, CPP_DL_ERROR, |
1398 | "invalid #pragma GCC poison directive"); | |
93c80368 | 1399 | break; |
fc009f96 GK |
1400 | } |
1401 | ||
345894b4 | 1402 | hp = tok->val.node; |
93c80368 NB |
1403 | if (hp->flags & NODE_POISONED) |
1404 | continue; | |
1405 | ||
1406 | if (hp->type == NT_MACRO) | |
0527bc4e | 1407 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
ebef4e8c | 1408 | NODE_NAME (hp)); |
93c80368 NB |
1409 | _cpp_free_definition (hp); |
1410 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
fc009f96 | 1411 | } |
93c80368 | 1412 | pfile->state.poisoned_ok = 0; |
7f2935c7 | 1413 | } |
2c0b35cb ZW |
1414 | |
1415 | /* Mark the current header as a system header. This will suppress | |
1416 | some categories of warnings (notably those from -pedantic). It is | |
1417 | intended for use in system libraries that cannot be implemented in | |
1418 | conforming C, but cannot be certain that their headers appear in a | |
1419 | system include directory. To prevent abuse, it is rejected in the | |
1420 | primary source file. */ | |
58fea6af | 1421 | static void |
6cf87ca4 | 1422 | do_pragma_system_header (cpp_reader *pfile) |
2c0b35cb | 1423 | { |
705e2d28 | 1424 | if (cpp_in_primary_file (pfile)) |
0527bc4e | 1425 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 1426 | "#pragma system_header ignored outside include file"); |
2c0b35cb | 1427 | else |
d82fc108 NB |
1428 | { |
1429 | check_eol (pfile); | |
bdcbe496 | 1430 | skip_rest_of_line (pfile); |
d82fc108 NB |
1431 | cpp_make_system_header (pfile, 1, 0); |
1432 | } | |
2c0b35cb | 1433 | } |
f3f751ad NS |
1434 | |
1435 | /* Check the modified date of the current include file against a specified | |
1436 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1437 | determine if a fixed header should be refixed. */ | |
58fea6af | 1438 | static void |
6cf87ca4 | 1439 | do_pragma_dependency (cpp_reader *pfile) |
f3f751ad | 1440 | { |
74eb4b3e NB |
1441 | const char *fname; |
1442 | int angle_brackets, ordering; | |
df383483 | 1443 | |
cbc43ae0 | 1444 | fname = parse_include (pfile, &angle_brackets, NULL); |
74eb4b3e | 1445 | if (!fname) |
58fea6af | 1446 | return; |
041c3194 | 1447 | |
74eb4b3e | 1448 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
f3f751ad | 1449 | if (ordering < 0) |
0527bc4e | 1450 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
f3f751ad NS |
1451 | else if (ordering > 0) |
1452 | { | |
0527bc4e JDA |
1453 | cpp_error (pfile, CPP_DL_WARNING, |
1454 | "current file is older than %s", fname); | |
4ed5bcfb | 1455 | if (cpp_get_token (pfile)->type != CPP_EOF) |
bdcbe496 NB |
1456 | { |
1457 | _cpp_backup_tokens (pfile, 1); | |
0527bc4e | 1458 | do_diagnostic (pfile, CPP_DL_WARNING, 0); |
bdcbe496 | 1459 | } |
f3f751ad | 1460 | } |
74eb4b3e | 1461 | |
fad205ff | 1462 | free ((void *) fname); |
f3f751ad NS |
1463 | } |
1464 | ||
4ed5bcfb NB |
1465 | /* Get a token but skip padding. */ |
1466 | static const cpp_token * | |
6cf87ca4 | 1467 | get_token_no_padding (cpp_reader *pfile) |
a5c3cccd | 1468 | { |
4ed5bcfb NB |
1469 | for (;;) |
1470 | { | |
1471 | const cpp_token *result = cpp_get_token (pfile); | |
1472 | if (result->type != CPP_PADDING) | |
1473 | return result; | |
1474 | } | |
1475 | } | |
a5c3cccd | 1476 | |
4ed5bcfb NB |
1477 | /* Check syntax is "(string-literal)". Returns the string on success, |
1478 | or NULL on failure. */ | |
1479 | static const cpp_token * | |
6cf87ca4 | 1480 | get__Pragma_string (cpp_reader *pfile) |
4ed5bcfb NB |
1481 | { |
1482 | const cpp_token *string; | |
5b9a40df | 1483 | const cpp_token *paren; |
a5c3cccd | 1484 | |
5b9a40df TT |
1485 | paren = get_token_no_padding (pfile); |
1486 | if (paren->type == CPP_EOF) | |
1487 | _cpp_backup_tokens (pfile, 1); | |
1488 | if (paren->type != CPP_OPEN_PAREN) | |
4ed5bcfb NB |
1489 | return NULL; |
1490 | ||
1491 | string = get_token_no_padding (pfile); | |
5b9a40df TT |
1492 | if (string->type == CPP_EOF) |
1493 | _cpp_backup_tokens (pfile, 1); | |
b6baa67d KVH |
1494 | if (string->type != CPP_STRING && string->type != CPP_WSTRING |
1495 | && string->type != CPP_STRING32 && string->type != CPP_STRING16) | |
4ed5bcfb NB |
1496 | return NULL; |
1497 | ||
5b9a40df TT |
1498 | paren = get_token_no_padding (pfile); |
1499 | if (paren->type == CPP_EOF) | |
1500 | _cpp_backup_tokens (pfile, 1); | |
1501 | if (paren->type != CPP_CLOSE_PAREN) | |
4ed5bcfb | 1502 | return NULL; |
a5c3cccd | 1503 | |
4ed5bcfb | 1504 | return string; |
a5c3cccd NB |
1505 | } |
1506 | ||
87062813 NB |
1507 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1508 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1509 | static void | |
6cf87ca4 | 1510 | destringize_and_run (cpp_reader *pfile, const cpp_string *in) |
a5c3cccd NB |
1511 | { |
1512 | const unsigned char *src, *limit; | |
87062813 | 1513 | char *dest, *result; |
bc4071dd RH |
1514 | cpp_context *saved_context; |
1515 | cpp_token *saved_cur_token; | |
1516 | tokenrun *saved_cur_run; | |
1517 | cpp_token *toks; | |
1518 | int count; | |
14ccf800 | 1519 | const struct directive *save_directive; |
a5c3cccd | 1520 | |
c3f829c1 | 1521 | dest = result = (char *) alloca (in->len - 1); |
6338b358 NB |
1522 | src = in->text + 1 + (in->text[0] == 'L'); |
1523 | limit = in->text + in->len - 1; | |
1524 | while (src < limit) | |
a5c3cccd NB |
1525 | { |
1526 | /* We know there is a character following the backslash. */ | |
1527 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1528 | src++; | |
1529 | *dest++ = *src++; | |
1530 | } | |
26aea073 | 1531 | *dest = '\n'; |
a5c3cccd | 1532 | |
8128cccf NB |
1533 | /* Ugh; an awful kludge. We are really not set up to be lexing |
1534 | tokens when in the middle of a macro expansion. Use a new | |
1535 | context to force cpp_get_token to lex, and so skip_rest_of_line | |
1536 | doesn't go beyond the end of the text. Also, remember the | |
1537 | current lexing position so we can return to it later. | |
1538 | ||
1539 | Something like line-at-a-time lexing should remove the need for | |
1540 | this. */ | |
bc4071dd RH |
1541 | saved_context = pfile->context; |
1542 | saved_cur_token = pfile->cur_token; | |
1543 | saved_cur_run = pfile->cur_run; | |
79ba5e3b | 1544 | |
bc4071dd RH |
1545 | pfile->context = XNEW (cpp_context); |
1546 | pfile->context->macro = 0; | |
1547 | pfile->context->prev = 0; | |
1c90c6f9 | 1548 | pfile->context->next = 0; |
79ba5e3b | 1549 | |
bc4071dd RH |
1550 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
1551 | until we've read all of the tokens that we want. */ | |
1552 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, | |
1553 | /* from_stage3 */ true); | |
1554 | /* ??? Antique Disgusting Hack. What does this do? */ | |
1555 | if (pfile->buffer->prev) | |
1556 | pfile->buffer->file = pfile->buffer->prev->file; | |
79ba5e3b | 1557 | |
bc4071dd RH |
1558 | start_directive (pfile); |
1559 | _cpp_clean_line (pfile); | |
14ccf800 TT |
1560 | save_directive = pfile->directive; |
1561 | pfile->directive = &dtable[T_PRAGMA]; | |
bc4071dd RH |
1562 | do_pragma (pfile); |
1563 | end_directive (pfile, 1); | |
14ccf800 | 1564 | pfile->directive = save_directive; |
79ba5e3b | 1565 | |
bc4071dd RH |
1566 | /* We always insert at least one token, the directive result. It'll |
1567 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we | |
1568 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ | |
1569 | ||
1570 | /* If we're not handling the pragma internally, read all of the tokens from | |
1571 | the string buffer now, while the string buffer is still installed. */ | |
1572 | /* ??? Note that the token buffer allocated here is leaked. It's not clear | |
1573 | to me what the true lifespan of the tokens are. It would appear that | |
1574 | the lifespan is the entire parse of the main input stream, in which case | |
1575 | this may not be wrong. */ | |
1576 | if (pfile->directive_result.type == CPP_PRAGMA) | |
1577 | { | |
1578 | int maxcount; | |
1579 | ||
1580 | count = 1; | |
1581 | maxcount = 50; | |
1582 | toks = XNEWVEC (cpp_token, maxcount); | |
1583 | toks[0] = pfile->directive_result; | |
79ba5e3b | 1584 | |
bc4071dd RH |
1585 | do |
1586 | { | |
1587 | if (count == maxcount) | |
1588 | { | |
1589 | maxcount = maxcount * 3 / 2; | |
1590 | toks = XRESIZEVEC (cpp_token, toks, maxcount); | |
1591 | } | |
1c90c6f9 JJ |
1592 | toks[count] = *cpp_get_token (pfile); |
1593 | /* Macros have been already expanded by cpp_get_token | |
1594 | if the pragma allowed expansion. */ | |
1595 | toks[count++].flags |= NO_EXPAND; | |
bc4071dd RH |
1596 | } |
1597 | while (toks[count-1].type != CPP_PRAGMA_EOL); | |
1598 | } | |
1599 | else | |
1600 | { | |
1601 | count = 1; | |
1602 | toks = XNEW (cpp_token); | |
1603 | toks[0] = pfile->directive_result; | |
1604 | ||
1605 | /* If we handled the entire pragma internally, make sure we get the | |
1606 | line number correct for the next token. */ | |
1607 | if (pfile->cb.line_change) | |
1608 | pfile->cb.line_change (pfile, pfile->cur_token, false); | |
1609 | } | |
1610 | ||
1611 | /* Finish inlining run_directive. */ | |
1612 | pfile->buffer->file = NULL; | |
1613 | _cpp_pop_buffer (pfile); | |
1614 | ||
1615 | /* Reset the old macro state before ... */ | |
1616 | XDELETE (pfile->context); | |
1617 | pfile->context = saved_context; | |
1618 | pfile->cur_token = saved_cur_token; | |
1619 | pfile->cur_run = saved_cur_run; | |
1620 | ||
1621 | /* ... inserting the new tokens we collected. */ | |
1622 | _cpp_push_token_context (pfile, NULL, toks, count); | |
a5c3cccd NB |
1623 | } |
1624 | ||
5b9a40df TT |
1625 | /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ |
1626 | int | |
6cf87ca4 | 1627 | _cpp_do__Pragma (cpp_reader *pfile) |
a5c3cccd | 1628 | { |
4ed5bcfb | 1629 | const cpp_token *string = get__Pragma_string (pfile); |
21b11495 | 1630 | pfile->directive_result.type = CPP_PADDING; |
a5c3cccd | 1631 | |
79ba5e3b | 1632 | if (string) |
5b9a40df TT |
1633 | { |
1634 | destringize_and_run (pfile, &string->val.str); | |
1635 | return 1; | |
1636 | } | |
1637 | cpp_error (pfile, CPP_DL_ERROR, | |
1638 | "_Pragma takes a parenthesized string literal"); | |
1639 | return 0; | |
a5c3cccd | 1640 | } |
21b11495 | 1641 | |
5d8ebbd8 | 1642 | /* Handle #ifdef. */ |
711b8824 | 1643 | static void |
6cf87ca4 | 1644 | do_ifdef (cpp_reader *pfile) |
168d3732 | 1645 | { |
93c80368 | 1646 | int skip = 1; |
041c3194 | 1647 | |
cef0d199 | 1648 | if (! pfile->state.skipping) |
93c80368 | 1649 | { |
93d45d9e | 1650 | cpp_hashnode *node = lex_macro_node (pfile, false); |
041c3194 | 1651 | |
93c80368 | 1652 | if (node) |
a69cbaac NB |
1653 | { |
1654 | skip = node->type != NT_MACRO; | |
1655 | _cpp_mark_macro_used (node); | |
93d45d9e JM |
1656 | if (!(node->flags & NODE_USED)) |
1657 | { | |
1658 | node->flags |= NODE_USED; | |
1659 | if (node->type == NT_MACRO) | |
1660 | { | |
1661 | if (pfile->cb.used_define) | |
1662 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1663 | } | |
1664 | else | |
1665 | { | |
1666 | if (pfile->cb.used_undef) | |
1667 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
1668 | } | |
1669 | } | |
a69cbaac NB |
1670 | check_eol (pfile); |
1671 | } | |
93c80368 | 1672 | } |
168d3732 | 1673 | |
93c80368 NB |
1674 | push_conditional (pfile, skip, T_IFDEF, 0); |
1675 | } | |
168d3732 | 1676 | |
5d8ebbd8 | 1677 | /* Handle #ifndef. */ |
711b8824 | 1678 | static void |
6cf87ca4 | 1679 | do_ifndef (cpp_reader *pfile) |
168d3732 | 1680 | { |
93c80368 | 1681 | int skip = 1; |
93d45d9e | 1682 | cpp_hashnode *node = 0; |
168d3732 | 1683 | |
cef0d199 | 1684 | if (! pfile->state.skipping) |
5af7e2c2 | 1685 | { |
ee1c2a10 | 1686 | node = lex_macro_node (pfile, false); |
b43db0b3 GK |
1687 | |
1688 | if (node) | |
a69cbaac NB |
1689 | { |
1690 | skip = node->type == NT_MACRO; | |
1691 | _cpp_mark_macro_used (node); | |
93d45d9e JM |
1692 | if (!(node->flags & NODE_USED)) |
1693 | { | |
1694 | node->flags |= NODE_USED; | |
1695 | if (node->type == NT_MACRO) | |
1696 | { | |
1697 | if (pfile->cb.used_define) | |
1698 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1699 | } | |
1700 | else | |
1701 | { | |
1702 | if (pfile->cb.used_undef) | |
1703 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
1704 | } | |
1705 | } | |
a69cbaac NB |
1706 | check_eol (pfile); |
1707 | } | |
5af7e2c2 | 1708 | } |
041c3194 | 1709 | |
93c80368 | 1710 | push_conditional (pfile, skip, T_IFNDEF, node); |
7f2935c7 PB |
1711 | } |
1712 | ||
6d18adbc NB |
1713 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
1714 | pfile->mi_ind_cmacro so we can handle multiple-include | |
6356f892 | 1715 | optimizations. If macro expansion occurs in the expression, we |
6d18adbc NB |
1716 | cannot treat it as a controlling conditional, since the expansion |
1717 | could change in the future. That is handled by cpp_get_token. */ | |
711b8824 | 1718 | static void |
6cf87ca4 | 1719 | do_if (cpp_reader *pfile) |
7f2935c7 | 1720 | { |
93c80368 | 1721 | int skip = 1; |
7f2935c7 | 1722 | |
cef0d199 | 1723 | if (! pfile->state.skipping) |
87ed109f | 1724 | skip = _cpp_parse_expr (pfile) == false; |
93c80368 | 1725 | |
6d18adbc | 1726 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
7f2935c7 PB |
1727 | } |
1728 | ||
b528a07e | 1729 | /* Flip skipping state if appropriate and continue without changing |
ea4a453b ZW |
1730 | if_stack; this is so that the error message for missing #endif's |
1731 | etc. will point to the original #if. */ | |
711b8824 | 1732 | static void |
6cf87ca4 | 1733 | do_else (cpp_reader *pfile) |
ed705a82 | 1734 | { |
b528a07e NB |
1735 | cpp_buffer *buffer = pfile->buffer; |
1736 | struct if_stack *ifs = buffer->if_stack; | |
ff2b53ef | 1737 | |
ea4a453b | 1738 | if (ifs == NULL) |
0527bc4e | 1739 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
93c80368 | 1740 | else |
ff2b53ef | 1741 | { |
93c80368 NB |
1742 | if (ifs->type == T_ELSE) |
1743 | { | |
0527bc4e JDA |
1744 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
1745 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
93c80368 NB |
1746 | "the conditional began here"); |
1747 | } | |
b528a07e NB |
1748 | ifs->type = T_ELSE; |
1749 | ||
cef0d199 NB |
1750 | /* Skip any future (erroneous) #elses or #elifs. */ |
1751 | pfile->state.skipping = ifs->skip_elses; | |
1752 | ifs->skip_elses = true; | |
7f2935c7 | 1753 | |
93c80368 NB |
1754 | /* Invalidate any controlling macro. */ |
1755 | ifs->mi_cmacro = 0; | |
93c80368 | 1756 | |
cef0d199 | 1757 | /* Only check EOL if was not originally skipping. */ |
909de5da | 1758 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
cef0d199 NB |
1759 | check_eol (pfile); |
1760 | } | |
7f2935c7 PB |
1761 | } |
1762 | ||
5d8ebbd8 | 1763 | /* Handle a #elif directive by not changing if_stack either. See the |
93c80368 | 1764 | comment above do_else. */ |
711b8824 | 1765 | static void |
6cf87ca4 | 1766 | do_elif (cpp_reader *pfile) |
7f2935c7 | 1767 | { |
b528a07e NB |
1768 | cpp_buffer *buffer = pfile->buffer; |
1769 | struct if_stack *ifs = buffer->if_stack; | |
7f2935c7 | 1770 | |
ea4a453b | 1771 | if (ifs == NULL) |
0527bc4e | 1772 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
b528a07e | 1773 | else |
40ea76de | 1774 | { |
b528a07e NB |
1775 | if (ifs->type == T_ELSE) |
1776 | { | |
0527bc4e JDA |
1777 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
1778 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
b528a07e NB |
1779 | "the conditional began here"); |
1780 | } | |
1781 | ifs->type = T_ELIF; | |
93c80368 | 1782 | |
cef0d199 NB |
1783 | /* Only evaluate this if we aren't skipping elses. During |
1784 | evaluation, set skipping to false to get lexer warnings. */ | |
1785 | if (ifs->skip_elses) | |
1786 | pfile->state.skipping = 1; | |
1787 | else | |
b528a07e | 1788 | { |
cef0d199 NB |
1789 | pfile->state.skipping = 0; |
1790 | pfile->state.skipping = ! _cpp_parse_expr (pfile); | |
1791 | ifs->skip_elses = ! pfile->state.skipping; | |
b528a07e | 1792 | } |
cef0d199 NB |
1793 | |
1794 | /* Invalidate any controlling macro. */ | |
1795 | ifs->mi_cmacro = 0; | |
40ea76de | 1796 | } |
7f2935c7 PB |
1797 | } |
1798 | ||
cef0d199 | 1799 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
711b8824 | 1800 | static void |
6cf87ca4 | 1801 | do_endif (cpp_reader *pfile) |
7f2935c7 | 1802 | { |
b528a07e NB |
1803 | cpp_buffer *buffer = pfile->buffer; |
1804 | struct if_stack *ifs = buffer->if_stack; | |
ea4a453b | 1805 | |
ea4a453b | 1806 | if (ifs == NULL) |
0527bc4e | 1807 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
7f2935c7 PB |
1808 | else |
1809 | { | |
cef0d199 | 1810 | /* Only check EOL if was not originally skipping. */ |
909de5da | 1811 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
cef0d199 NB |
1812 | check_eol (pfile); |
1813 | ||
93c80368 NB |
1814 | /* If potential control macro, we go back outside again. */ |
1815 | if (ifs->next == 0 && ifs->mi_cmacro) | |
1816 | { | |
6d18adbc | 1817 | pfile->mi_valid = true; |
93c80368 NB |
1818 | pfile->mi_cmacro = ifs->mi_cmacro; |
1819 | } | |
1820 | ||
b528a07e | 1821 | buffer->if_stack = ifs->next; |
cef0d199 | 1822 | pfile->state.skipping = ifs->was_skipping; |
2a967f3d | 1823 | obstack_free (&pfile->buffer_ob, ifs); |
7f2935c7 | 1824 | } |
93c80368 | 1825 | } |
041c3194 | 1826 | |
5d8ebbd8 NB |
1827 | /* Push an if_stack entry for a preprocessor conditional, and set |
1828 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
1829 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
1830 | we need to check here that we are at the top of the file. */ | |
ea4a453b | 1831 | static void |
6cf87ca4 ZW |
1832 | push_conditional (cpp_reader *pfile, int skip, int type, |
1833 | const cpp_hashnode *cmacro) | |
ea4a453b ZW |
1834 | { |
1835 | struct if_stack *ifs; | |
b528a07e | 1836 | cpp_buffer *buffer = pfile->buffer; |
ea4a453b | 1837 | |
72bb2c39 | 1838 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
50410426 | 1839 | ifs->line = pfile->directive_line; |
b528a07e | 1840 | ifs->next = buffer->if_stack; |
cef0d199 NB |
1841 | ifs->skip_elses = pfile->state.skipping || !skip; |
1842 | ifs->was_skipping = pfile->state.skipping; | |
ea4a453b | 1843 | ifs->type = type; |
6d18adbc NB |
1844 | /* This condition is effectively a test for top-of-file. */ |
1845 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
93c80368 NB |
1846 | ifs->mi_cmacro = cmacro; |
1847 | else | |
1848 | ifs->mi_cmacro = 0; | |
ea4a453b | 1849 | |
cef0d199 | 1850 | pfile->state.skipping = skip; |
b528a07e | 1851 | buffer->if_stack = ifs; |
782331f4 | 1852 | } |
7061aa5a | 1853 | |
5d8ebbd8 NB |
1854 | /* Read the tokens of the answer into the macro pool, in a directive |
1855 | of type TYPE. Only commit the memory if we intend it as permanent | |
1856 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
1857 | ANSWERP to point to the answer. */ | |
93c80368 | 1858 | static int |
6cf87ca4 | 1859 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type) |
7f2935c7 | 1860 | { |
4ed5bcfb | 1861 | const cpp_token *paren; |
93c80368 | 1862 | struct answer *answer; |
8c3b2693 | 1863 | unsigned int acount; |
93c80368 NB |
1864 | |
1865 | /* In a conditional, it is legal to not have an open paren. We | |
1866 | should save the following token in this case. */ | |
4ed5bcfb | 1867 | paren = cpp_get_token (pfile); |
93c80368 NB |
1868 | |
1869 | /* If not a paren, see if we're OK. */ | |
4ed5bcfb | 1870 | if (paren->type != CPP_OPEN_PAREN) |
041c3194 | 1871 | { |
93c80368 NB |
1872 | /* In a conditional no answer is a test for any answer. It |
1873 | could be followed by any token. */ | |
1874 | if (type == T_IF) | |
bdcbe496 NB |
1875 | { |
1876 | _cpp_backup_tokens (pfile, 1); | |
1877 | return 0; | |
1878 | } | |
93c80368 NB |
1879 | |
1880 | /* #unassert with no answer is valid - it removes all answers. */ | |
4ed5bcfb | 1881 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
93c80368 | 1882 | return 0; |
15dad1d9 | 1883 | |
0527bc4e | 1884 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate"); |
93c80368 | 1885 | return 1; |
041c3194 | 1886 | } |
7061aa5a | 1887 | |
8c3b2693 | 1888 | for (acount = 0;; acount++) |
041c3194 | 1889 | { |
8c3b2693 NB |
1890 | size_t room_needed; |
1891 | const cpp_token *token = cpp_get_token (pfile); | |
1892 | cpp_token *dest; | |
93c80368 | 1893 | |
041c3194 ZW |
1894 | if (token->type == CPP_CLOSE_PAREN) |
1895 | break; | |
a7abcbbf | 1896 | |
93c80368 | 1897 | if (token->type == CPP_EOF) |
041c3194 | 1898 | { |
0527bc4e | 1899 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
93c80368 | 1900 | return 1; |
041c3194 | 1901 | } |
8c3b2693 NB |
1902 | |
1903 | /* struct answer includes the space for one token. */ | |
1904 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
1905 | ||
1906 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
1907 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
1908 | ||
1909 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
1910 | *dest = *token; | |
1911 | ||
1912 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
1913 | if (acount == 0) | |
1914 | dest->flags &= ~PREV_WHITE; | |
041c3194 | 1915 | } |
15dad1d9 | 1916 | |
8c3b2693 | 1917 | if (acount == 0) |
7061aa5a | 1918 | { |
0527bc4e | 1919 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
93c80368 | 1920 | return 1; |
7f2935c7 | 1921 | } |
041c3194 | 1922 | |
8c3b2693 NB |
1923 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
1924 | answer->count = acount; | |
1925 | answer->next = NULL; | |
93c80368 | 1926 | *answerp = answer; |
041c3194 | 1927 | |
93c80368 NB |
1928 | return 0; |
1929 | } | |
041c3194 | 1930 | |
5d8ebbd8 NB |
1931 | /* Parses an assertion directive of type TYPE, returning a pointer to |
1932 | the hash node of the predicate, or 0 on error. If an answer was | |
1933 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
93c80368 | 1934 | static cpp_hashnode * |
6cf87ca4 | 1935 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
93c80368 NB |
1936 | { |
1937 | cpp_hashnode *result = 0; | |
4ed5bcfb | 1938 | const cpp_token *predicate; |
93c80368 NB |
1939 | |
1940 | /* We don't expand predicates or answers. */ | |
1941 | pfile->state.prevent_expansion++; | |
1942 | ||
93c80368 | 1943 | *answerp = 0; |
4ed5bcfb NB |
1944 | predicate = cpp_get_token (pfile); |
1945 | if (predicate->type == CPP_EOF) | |
0527bc4e | 1946 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
4ed5bcfb | 1947 | else if (predicate->type != CPP_NAME) |
0527bc4e | 1948 | cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier"); |
93c80368 NB |
1949 | else if (parse_answer (pfile, answerp, type) == 0) |
1950 | { | |
4ed5bcfb | 1951 | unsigned int len = NODE_LEN (predicate->val.node); |
c3f829c1 | 1952 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
041c3194 | 1953 | |
93c80368 NB |
1954 | /* Prefix '#' to get it out of macro namespace. */ |
1955 | sym[0] = '#'; | |
4ed5bcfb | 1956 | memcpy (sym + 1, NODE_NAME (predicate->val.node), len); |
93c80368 NB |
1957 | result = cpp_lookup (pfile, sym, len + 1); |
1958 | } | |
7061aa5a | 1959 | |
93c80368 NB |
1960 | pfile->state.prevent_expansion--; |
1961 | return result; | |
7f2935c7 | 1962 | } |
7061aa5a | 1963 | |
5d8ebbd8 | 1964 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
041c3194 | 1965 | or a pointer to NULL if the answer is not in the chain. */ |
93c80368 | 1966 | static struct answer ** |
6cf87ca4 | 1967 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
7f2935c7 | 1968 | { |
93c80368 | 1969 | unsigned int i; |
041c3194 | 1970 | struct answer **result; |
7f2935c7 | 1971 | |
041c3194 | 1972 | for (result = &node->value.answers; *result; result = &(*result)->next) |
93c80368 NB |
1973 | { |
1974 | struct answer *answer = *result; | |
1975 | ||
1976 | if (answer->count == candidate->count) | |
1977 | { | |
1978 | for (i = 0; i < answer->count; i++) | |
1979 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
1980 | break; | |
1981 | ||
1982 | if (i == answer->count) | |
1983 | break; | |
1984 | } | |
1985 | } | |
ff2b53ef | 1986 | |
041c3194 ZW |
1987 | return result; |
1988 | } | |
15dad1d9 | 1989 | |
93c80368 | 1990 | /* Test an assertion within a preprocessor conditional. Returns |
da7d8304 | 1991 | nonzero on failure, zero on success. On success, the result of |
2402645b | 1992 | the test is written into VALUE, otherwise the value 0. */ |
93c80368 | 1993 | int |
6cf87ca4 | 1994 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
93c80368 NB |
1995 | { |
1996 | struct answer *answer; | |
1997 | cpp_hashnode *node; | |
1998 | ||
1999 | node = parse_assertion (pfile, &answer, T_IF); | |
2402645b HPN |
2000 | |
2001 | /* For recovery, an erroneous assertion expression is handled as a | |
2002 | failing assertion. */ | |
2003 | *value = 0; | |
2004 | ||
93c80368 NB |
2005 | if (node) |
2006 | *value = (node->type == NT_ASSERTION && | |
2007 | (answer == 0 || *find_answer (node, answer) != 0)); | |
91318908 NB |
2008 | else if (pfile->cur_token[-1].type == CPP_EOF) |
2009 | _cpp_backup_tokens (pfile, 1); | |
93c80368 NB |
2010 | |
2011 | /* We don't commit the memory for the answer - it's temporary only. */ | |
2012 | return node == 0; | |
2013 | } | |
2014 | ||
5d8ebbd8 | 2015 | /* Handle #assert. */ |
711b8824 | 2016 | static void |
6cf87ca4 | 2017 | do_assert (cpp_reader *pfile) |
041c3194 ZW |
2018 | { |
2019 | struct answer *new_answer; | |
2020 | cpp_hashnode *node; | |
df383483 | 2021 | |
93c80368 | 2022 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
041c3194 | 2023 | if (node) |
ff2b53ef | 2024 | { |
d8044160 GK |
2025 | size_t answer_size; |
2026 | ||
93c80368 NB |
2027 | /* Place the new answer in the answer list. First check there |
2028 | is not a duplicate. */ | |
041c3194 | 2029 | new_answer->next = 0; |
93c80368 | 2030 | if (node->type == NT_ASSERTION) |
041c3194 | 2031 | { |
93c80368 NB |
2032 | if (*find_answer (node, new_answer)) |
2033 | { | |
0527bc4e | 2034 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
ebef4e8c | 2035 | NODE_NAME (node) + 1); |
93c80368 NB |
2036 | return; |
2037 | } | |
041c3194 ZW |
2038 | new_answer->next = node->value.answers; |
2039 | } | |
8c3b2693 | 2040 | |
d8044160 GK |
2041 | answer_size = sizeof (struct answer) + ((new_answer->count - 1) |
2042 | * sizeof (cpp_token)); | |
2043 | /* Commit or allocate storage for the object. */ | |
2044 | if (pfile->hash_table->alloc_subobject) | |
2045 | { | |
2046 | struct answer *temp_answer = new_answer; | |
c3f829c1 GDR |
2047 | new_answer = (struct answer *) pfile->hash_table->alloc_subobject |
2048 | (answer_size); | |
d8044160 GK |
2049 | memcpy (new_answer, temp_answer, answer_size); |
2050 | } | |
2051 | else | |
2052 | BUFF_FRONT (pfile->a_buff) += answer_size; | |
2053 | ||
93c80368 | 2054 | node->type = NT_ASSERTION; |
041c3194 | 2055 | node->value.answers = new_answer; |
8c3b2693 | 2056 | check_eol (pfile); |
ff2b53ef | 2057 | } |
041c3194 | 2058 | } |
15dad1d9 | 2059 | |
5d8ebbd8 | 2060 | /* Handle #unassert. */ |
711b8824 | 2061 | static void |
6cf87ca4 | 2062 | do_unassert (cpp_reader *pfile) |
041c3194 ZW |
2063 | { |
2064 | cpp_hashnode *node; | |
93c80368 | 2065 | struct answer *answer; |
df383483 | 2066 | |
93c80368 NB |
2067 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
2068 | /* It isn't an error to #unassert something that isn't asserted. */ | |
2069 | if (node && node->type == NT_ASSERTION) | |
7061aa5a | 2070 | { |
93c80368 | 2071 | if (answer) |
15dad1d9 | 2072 | { |
93c80368 | 2073 | struct answer **p = find_answer (node, answer), *temp; |
041c3194 | 2074 | |
93c80368 NB |
2075 | /* Remove the answer from the list. */ |
2076 | temp = *p; | |
2077 | if (temp) | |
2078 | *p = temp->next; | |
a7abcbbf | 2079 | |
93c80368 NB |
2080 | /* Did we free the last answer? */ |
2081 | if (node->value.answers == 0) | |
2082 | node->type = NT_VOID; | |
8c3b2693 NB |
2083 | |
2084 | check_eol (pfile); | |
93c80368 NB |
2085 | } |
2086 | else | |
2087 | _cpp_free_definition (node); | |
041c3194 | 2088 | } |
93c80368 NB |
2089 | |
2090 | /* We don't commit the memory for the answer - it's temporary only. */ | |
7f2935c7 | 2091 | } |
7f2935c7 | 2092 | |
45b966db ZW |
2093 | /* These are for -D, -U, -A. */ |
2094 | ||
2095 | /* Process the string STR as if it appeared as the body of a #define. | |
2096 | If STR is just an identifier, define it with value 1. | |
2097 | If STR has anything after the identifier, then it should | |
ec5c56db | 2098 | be identifier=definition. */ |
0b22d65c | 2099 | void |
6cf87ca4 | 2100 | cpp_define (cpp_reader *pfile, const char *str) |
0b22d65c | 2101 | { |
45b966db ZW |
2102 | char *buf, *p; |
2103 | size_t count; | |
2104 | ||
df383483 | 2105 | /* Copy the entire option so we can modify it. |
45b966db | 2106 | Change the first "=" in the string to a space. If there is none, |
86368122 NB |
2107 | tack " 1" on the end. */ |
2108 | ||
86368122 | 2109 | count = strlen (str); |
c3f829c1 | 2110 | buf = (char *) alloca (count + 3); |
86368122 NB |
2111 | memcpy (buf, str, count); |
2112 | ||
2113 | p = strchr (str, '='); | |
45b966db | 2114 | if (p) |
86368122 | 2115 | buf[p - str] = ' '; |
45b966db ZW |
2116 | else |
2117 | { | |
86368122 NB |
2118 | buf[count++] = ' '; |
2119 | buf[count++] = '1'; | |
0b22d65c | 2120 | } |
26aea073 | 2121 | buf[count] = '\n'; |
cf4ed945 | 2122 | |
29401c30 | 2123 | run_directive (pfile, T_DEFINE, buf, count); |
2c8f0515 ZW |
2124 | } |
2125 | ||
28f68625 DF |
2126 | |
2127 | /* Use to build macros to be run through cpp_define() as | |
2128 | described above. | |
2129 | Example: cpp_define_formatted (pfile, "MACRO=%d", value); */ | |
2130 | ||
2131 | void | |
2132 | cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...) | |
2133 | { | |
2134 | char *ptr = NULL; | |
2135 | ||
2136 | va_list ap; | |
2137 | va_start (ap, fmt); | |
2138 | vasprintf (&ptr, fmt, ap); | |
2139 | va_end (ap); | |
2140 | ||
2141 | cpp_define (pfile, ptr); | |
2142 | free (ptr); | |
2143 | } | |
2144 | ||
2145 | ||
ad2a084d | 2146 | /* Slight variant of the above for use by initialize_builtins. */ |
2c8f0515 | 2147 | void |
6cf87ca4 | 2148 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
2c8f0515 | 2149 | { |
26aea073 | 2150 | size_t len = strlen (str); |
c3f829c1 | 2151 | char *buf = (char *) alloca (len + 1); |
26aea073 NB |
2152 | memcpy (buf, str, len); |
2153 | buf[len] = '\n'; | |
2154 | run_directive (pfile, T_DEFINE, buf, len); | |
45b966db | 2155 | } |
0f41302f | 2156 | |
45b966db ZW |
2157 | /* Process MACRO as if it appeared as the body of an #undef. */ |
2158 | void | |
6cf87ca4 | 2159 | cpp_undef (cpp_reader *pfile, const char *macro) |
7f2935c7 | 2160 | { |
26aea073 | 2161 | size_t len = strlen (macro); |
c3f829c1 | 2162 | char *buf = (char *) alloca (len + 1); |
26aea073 NB |
2163 | memcpy (buf, macro, len); |
2164 | buf[len] = '\n'; | |
2165 | run_directive (pfile, T_UNDEF, buf, len); | |
7f2935c7 PB |
2166 | } |
2167 | ||
121de39f RH |
2168 | /* Like lex_macro_node, but read the input from STR. */ |
2169 | static cpp_hashnode * | |
2170 | lex_macro_node_from_str (cpp_reader *pfile, const char *str) | |
2171 | { | |
2172 | size_t len = strlen (str); | |
4cd97075 | 2173 | uchar *buf = (uchar *) alloca (len + 1); |
121de39f RH |
2174 | cpp_hashnode *node; |
2175 | ||
2176 | memcpy (buf, str, len); | |
2177 | buf[len] = '\n'; | |
2178 | cpp_push_buffer (pfile, buf, len, true); | |
2179 | node = lex_macro_node (pfile, true); | |
2180 | _cpp_pop_buffer (pfile); | |
2181 | ||
2182 | return node; | |
2183 | } | |
2184 | ||
2185 | /* If STR is a defined macro, return its definition node, else return NULL. */ | |
2186 | cpp_macro * | |
2187 | cpp_push_definition (cpp_reader *pfile, const char *str) | |
2188 | { | |
2189 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); | |
2190 | if (node && node->type == NT_MACRO) | |
2191 | return node->value.macro; | |
2192 | else | |
2193 | return NULL; | |
2194 | } | |
2195 | ||
2196 | /* Replace a previous definition DFN of the macro STR. If DFN is NULL, | |
2197 | then the macro should be undefined. */ | |
2198 | void | |
2199 | cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn) | |
2200 | { | |
2201 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); | |
2202 | if (node == NULL) | |
2203 | return; | |
2204 | ||
93d45d9e JM |
2205 | if (pfile->cb.before_define) |
2206 | pfile->cb.before_define (pfile); | |
2207 | ||
121de39f RH |
2208 | if (node->type == NT_MACRO) |
2209 | { | |
2210 | if (pfile->cb.undef) | |
2211 | pfile->cb.undef (pfile, pfile->directive_line, node); | |
2212 | if (CPP_OPTION (pfile, warn_unused_macros)) | |
2213 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
2214 | } | |
2215 | if (node->type != NT_VOID) | |
2216 | _cpp_free_definition (node); | |
2217 | ||
2218 | if (dfn) | |
2219 | { | |
2220 | node->type = NT_MACRO; | |
2221 | node->value.macro = dfn; | |
2222 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))) | |
2223 | node->flags |= NODE_WARN; | |
2224 | ||
2225 | if (pfile->cb.define) | |
2226 | pfile->cb.define (pfile, pfile->directive_line, node); | |
2227 | } | |
2228 | } | |
2229 | ||
ec5c56db | 2230 | /* Process the string STR as if it appeared as the body of a #assert. */ |
45b966db | 2231 | void |
6cf87ca4 | 2232 | cpp_assert (cpp_reader *pfile, const char *str) |
45b966db | 2233 | { |
86368122 | 2234 | handle_assertion (pfile, str, T_ASSERT); |
45b966db | 2235 | } |
7f2935c7 | 2236 | |
ec5c56db | 2237 | /* Process STR as if it appeared as the body of an #unassert. */ |
45b966db | 2238 | void |
6cf87ca4 | 2239 | cpp_unassert (cpp_reader *pfile, const char *str) |
7f2935c7 | 2240 | { |
86368122 | 2241 | handle_assertion (pfile, str, T_UNASSERT); |
df383483 | 2242 | } |
3fdc651f | 2243 | |
86368122 NB |
2244 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
2245 | static void | |
6cf87ca4 | 2246 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
86368122 NB |
2247 | { |
2248 | size_t count = strlen (str); | |
2249 | const char *p = strchr (str, '='); | |
2250 | ||
26aea073 NB |
2251 | /* Copy the entire option so we can modify it. Change the first |
2252 | "=" in the string to a '(', and tack a ')' on the end. */ | |
c3f829c1 | 2253 | char *buf = (char *) alloca (count + 2); |
26aea073 NB |
2254 | |
2255 | memcpy (buf, str, count); | |
86368122 NB |
2256 | if (p) |
2257 | { | |
86368122 NB |
2258 | buf[p - str] = '('; |
2259 | buf[count++] = ')'; | |
86368122 | 2260 | } |
26aea073 NB |
2261 | buf[count] = '\n'; |
2262 | str = buf; | |
86368122 | 2263 | |
29401c30 | 2264 | run_directive (pfile, type, str, count); |
86368122 NB |
2265 | } |
2266 | ||
7e96d768 NB |
2267 | /* The number of errors for a given reader. */ |
2268 | unsigned int | |
6cf87ca4 | 2269 | cpp_errors (cpp_reader *pfile) |
7e96d768 NB |
2270 | { |
2271 | return pfile->errors; | |
2272 | } | |
2273 | ||
2274 | /* The options structure. */ | |
2275 | cpp_options * | |
6cf87ca4 | 2276 | cpp_get_options (cpp_reader *pfile) |
7e96d768 NB |
2277 | { |
2278 | return &pfile->opts; | |
2279 | } | |
2280 | ||
2281 | /* The callbacks structure. */ | |
2282 | cpp_callbacks * | |
6cf87ca4 | 2283 | cpp_get_callbacks (cpp_reader *pfile) |
7e96d768 NB |
2284 | { |
2285 | return &pfile->cb; | |
2286 | } | |
2287 | ||
2288 | /* Copy the given callbacks structure to our own. */ | |
2289 | void | |
6cf87ca4 | 2290 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
7e96d768 NB |
2291 | { |
2292 | pfile->cb = *cb; | |
2293 | } | |
2294 | ||
c6e83800 ZW |
2295 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
2296 | struct deps * | |
2297 | cpp_get_deps (cpp_reader *pfile) | |
2298 | { | |
2299 | if (!pfile->deps) | |
2300 | pfile->deps = deps_init (); | |
2301 | return pfile->deps; | |
2302 | } | |
2303 | ||
eb1f4d9d NB |
2304 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
2305 | doesn't fail. It does not generate a file change call back; that | |
2306 | is the responsibility of the caller. */ | |
c71f835b | 2307 | cpp_buffer * |
6cf87ca4 | 2308 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
40de9f76 | 2309 | int from_stage3) |
c71f835b | 2310 | { |
c3f829c1 | 2311 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
93c80368 | 2312 | |
fde84349 | 2313 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
c3f829c1 | 2314 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
fde84349 | 2315 | |
c3f829c1 GDR |
2316 | new_buffer->next_line = new_buffer->buf = buffer; |
2317 | new_buffer->rlimit = buffer + len; | |
2318 | new_buffer->from_stage3 = from_stage3; | |
2319 | new_buffer->prev = pfile->buffer; | |
2320 | new_buffer->need_line = true; | |
0bda4760 | 2321 | |
c3f829c1 | 2322 | pfile->buffer = new_buffer; |
cf551fba | 2323 | |
c3f829c1 | 2324 | return new_buffer; |
c71f835b ZW |
2325 | } |
2326 | ||
af0d16cd NB |
2327 | /* Pops a single buffer, with a file change call-back if appropriate. |
2328 | Then pushes the next -include file, if any remain. */ | |
ef6e958a | 2329 | void |
6cf87ca4 | 2330 | _cpp_pop_buffer (cpp_reader *pfile) |
c71f835b | 2331 | { |
fde84349 | 2332 | cpp_buffer *buffer = pfile->buffer; |
8f9b4009 | 2333 | struct _cpp_file *inc = buffer->file; |
ad2a084d | 2334 | struct if_stack *ifs; |
c71f835b | 2335 | |
fde84349 NB |
2336 | /* Walk back up the conditional stack till we reach its level at |
2337 | entry to this file, issuing error messages. */ | |
2338 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
0527bc4e | 2339 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
fde84349 | 2340 | "unterminated #%s", dtable[ifs->type].name); |
eb1f4d9d | 2341 | |
97293897 | 2342 | /* In case of a missing #endif. */ |
67821e3a | 2343 | pfile->state.skipping = 0; |
29401c30 | 2344 | |
af0d16cd | 2345 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
29401c30 NB |
2346 | pfile->buffer = buffer->prev; |
2347 | ||
26aea073 NB |
2348 | free (buffer->notes); |
2349 | ||
af0d16cd NB |
2350 | /* Free the buffer object now; we may want to push a new buffer |
2351 | in _cpp_push_next_include_file. */ | |
2352 | obstack_free (&pfile->buffer_ob, buffer); | |
29401c30 | 2353 | |
af0d16cd NB |
2354 | if (inc) |
2355 | { | |
2356 | _cpp_pop_file_buffer (pfile, inc); | |
2357 | ||
40de9f76 | 2358 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
af0d16cd | 2359 | } |
c71f835b ZW |
2360 | } |
2361 | ||
05713b80 | 2362 | /* Enter all recognized directives in the hash table. */ |
c71f835b | 2363 | void |
6cf87ca4 | 2364 | _cpp_init_directives (cpp_reader *pfile) |
c71f835b | 2365 | { |
766ee681 | 2366 | unsigned int i; |
93c80368 | 2367 | cpp_hashnode *node; |
bfb9dc7f | 2368 | |
37b8524c | 2369 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
93c80368 | 2370 | { |
766ee681 | 2371 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
4977bab6 ZW |
2372 | node->is_directive = 1; |
2373 | node->directive_index = i; | |
93c80368 | 2374 | } |
c71f835b | 2375 | } |