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