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