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