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