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