directives.c revision 258501
1169695Skan/* CPP Library. (Directive handling.) 2169695Skan Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3169695Skan 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 4169695Skan Contributed by Per Bothner, 1994-95. 5169695Skan Based on CCCP program by Paul Rubin, June 1986 6169695Skan Adapted to ANSI C, Richard Stallman, Jan 1987 7169695Skan 8169695SkanThis program is free software; you can redistribute it and/or modify it 9169695Skanunder the terms of the GNU General Public License as published by the 10169695SkanFree Software Foundation; either version 2, or (at your option) any 11169695Skanlater version. 12169695Skan 13169695SkanThis program is distributed in the hope that it will be useful, 14169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 15169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16169695SkanGNU General Public License for more details. 17169695Skan 18169695SkanYou should have received a copy of the GNU General Public License 19169695Skanalong with this program; if not, write to the Free Software 20169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 21169695Skan 22169695Skan#include "config.h" 23169695Skan#include "system.h" 24169695Skan#include "cpplib.h" 25169695Skan#include "internal.h" 26169695Skan#include "mkdeps.h" 27169695Skan#include "obstack.h" 28169695Skan 29169695Skan/* Stack of conditionals currently in progress 30169695Skan (including both successful and failing conditionals). */ 31169695Skanstruct if_stack 32169695Skan{ 33169695Skan struct if_stack *next; 34169695Skan unsigned int line; /* Line where condition started. */ 35169695Skan const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ 36169695Skan bool skip_elses; /* Can future #else / #elif be skipped? */ 37169695Skan bool was_skipping; /* If were skipping on entry. */ 38169695Skan int type; /* Most recent conditional for diagnostics. */ 39169695Skan}; 40169695Skan 41169695Skan/* Contains a registered pragma or pragma namespace. */ 42169695Skantypedef void (*pragma_cb) (cpp_reader *); 43169695Skanstruct pragma_entry 44169695Skan{ 45169695Skan struct pragma_entry *next; 46169695Skan const cpp_hashnode *pragma; /* Name and length. */ 47169695Skan bool is_nspace; 48169695Skan bool is_internal; 49169695Skan bool is_deferred; 50169695Skan bool allow_expansion; 51169695Skan union { 52169695Skan pragma_cb handler; 53169695Skan struct pragma_entry *space; 54169695Skan unsigned int ident; 55169695Skan } u; 56169695Skan}; 57169695Skan 58169695Skan/* Values for the origin field of struct directive. KANDR directives 59169695Skan come from traditional (K&R) C. STDC89 directives come from the 60169695Skan 1989 C standard. EXTENSION directives are extensions. */ 61169695Skan#define KANDR 0 62169695Skan#define STDC89 1 63169695Skan#define EXTENSION 2 64169695Skan 65169695Skan/* Values for the flags field of struct directive. COND indicates a 66169695Skan conditional; IF_COND an opening conditional. INCL means to treat 67169695Skan "..." and <...> as q-char and h-char sequences respectively. IN_I 68169695Skan means this directive should be handled even if -fpreprocessed is in 69169695Skan effect (these are the directives with callback hooks). 70169695Skan 71169695Skan EXPAND is set on directives that are always macro-expanded. */ 72169695Skan#define COND (1 << 0) 73169695Skan#define IF_COND (1 << 1) 74169695Skan#define INCL (1 << 2) 75169695Skan#define IN_I (1 << 3) 76169695Skan#define EXPAND (1 << 4) 77169695Skan 78169695Skan/* Defines one #-directive, including how to handle it. */ 79169695Skantypedef void (*directive_handler) (cpp_reader *); 80169695Skantypedef struct directive directive; 81169695Skanstruct directive 82169695Skan{ 83169695Skan directive_handler handler; /* Function to handle directive. */ 84169695Skan const uchar *name; /* Name of directive. */ 85169695Skan unsigned short length; /* Length of name. */ 86169695Skan unsigned char origin; /* Origin of directive. */ 87169695Skan unsigned char flags; /* Flags describing this directive. */ 88169695Skan}; 89169695Skan 90169695Skan/* Forward declarations. */ 91169695Skan 92169695Skanstatic void skip_rest_of_line (cpp_reader *); 93169695Skanstatic void check_eol (cpp_reader *); 94169695Skanstatic void start_directive (cpp_reader *); 95169695Skanstatic void prepare_directive_trad (cpp_reader *); 96169695Skanstatic void end_directive (cpp_reader *, int); 97169695Skanstatic void directive_diagnostics (cpp_reader *, const directive *, int); 98169695Skanstatic void run_directive (cpp_reader *, int, const char *, size_t); 99169695Skanstatic char *glue_header_name (cpp_reader *); 100169695Skanstatic const char *parse_include (cpp_reader *, int *, const cpp_token ***); 101169695Skanstatic void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); 102169695Skanstatic unsigned int read_flag (cpp_reader *, unsigned int); 103169695Skanstatic int strtoul_for_line (const uchar *, unsigned int, unsigned long *); 104169695Skanstatic void do_diagnostic (cpp_reader *, int, int); 105169695Skanstatic cpp_hashnode *lex_macro_node (cpp_reader *); 106169695Skanstatic int undefine_macros (cpp_reader *, cpp_hashnode *, void *); 107169695Skanstatic void do_include_common (cpp_reader *, enum include_type); 108169695Skanstatic struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, 109169695Skan const cpp_hashnode *); 110169695Skanstatic int count_registered_pragmas (struct pragma_entry *); 111169695Skanstatic char ** save_registered_pragmas (struct pragma_entry *, char **); 112169695Skanstatic char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, 113169695Skan char **); 114169695Skanstatic void do_pragma_once (cpp_reader *); 115169695Skanstatic void do_pragma_poison (cpp_reader *); 116169695Skanstatic void do_pragma_system_header (cpp_reader *); 117169695Skanstatic void do_pragma_dependency (cpp_reader *); 118169695Skanstatic void do_linemarker (cpp_reader *); 119169695Skanstatic const cpp_token *get_token_no_padding (cpp_reader *); 120169695Skanstatic const cpp_token *get__Pragma_string (cpp_reader *); 121169695Skanstatic void destringize_and_run (cpp_reader *, const cpp_string *); 122169695Skanstatic int parse_answer (cpp_reader *, struct answer **, int); 123169695Skanstatic cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); 124169695Skanstatic struct answer ** find_answer (cpp_hashnode *, const struct answer *); 125169695Skanstatic void handle_assertion (cpp_reader *, const char *, int); 126169695Skan 127169695Skan/* This is the table of directive handlers. It is ordered by 128169695Skan frequency of occurrence; the numbers at the end are directive 129169695Skan counts from all the source code I have lying around (egcs and libc 130169695Skan CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and 131169695Skan pcmcia-cs-3.0.9). This is no longer important as directive lookup 132169695Skan is now O(1). All extensions other than #warning and #include_next 133169695Skan are deprecated. The name is where the extension appears to have 134169695Skan come from. */ 135169695Skan 136169695Skan#define DIRECTIVE_TABLE \ 137169695SkanD(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ 138169695SkanD(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ 139169695SkanD(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ 140169695SkanD(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ 141169695SkanD(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ 142169695SkanD(else, T_ELSE, KANDR, COND) /* 9863 */ \ 143169695SkanD(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ 144169695SkanD(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ 145169695SkanD(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ 146169695SkanD(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ 147169695SkanD(error, T_ERROR, STDC89, 0) /* 475 */ \ 148169695SkanD(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ 149169695SkanD(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ 150169695SkanD(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ 151169695SkanD(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ 152169695SkanD(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ 153169695SkanD(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ 154169695SkanD(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ 155169695SkanD(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ 156169695Skan 157169695Skan/* #sccs is synonymous with #ident. */ 158169695Skan#define do_sccs do_ident 159169695Skan 160169695Skan/* Use the table to generate a series of prototypes, an enum for the 161169695Skan directive names, and an array of directive handlers. */ 162169695Skan 163169695Skan#define D(name, t, o, f) static void do_##name (cpp_reader *); 164169695SkanDIRECTIVE_TABLE 165169695Skan#undef D 166169695Skan 167169695Skan#define D(n, tag, o, f) tag, 168169695Skanenum 169169695Skan{ 170169695Skan DIRECTIVE_TABLE 171169695Skan N_DIRECTIVES 172169695Skan}; 173169695Skan#undef D 174169695Skan 175169695Skan#define D(name, t, origin, flags) \ 176169695Skan{ do_##name, (const uchar *) #name, \ 177169695Skan sizeof #name - 1, origin, flags }, 178169695Skanstatic const directive dtable[] = 179169695Skan{ 180169695SkanDIRECTIVE_TABLE 181169695Skan}; 182169695Skan#undef D 183169695Skan#undef DIRECTIVE_TABLE 184169695Skan 185169695Skan/* Wrapper struct directive for linemarkers. 186169695Skan The origin is more or less true - the original K+R cpp 187169695Skan did use this notation in its preprocessed output. */ 188169695Skanstatic const directive linemarker_dir = 189169695Skan{ 190169695Skan do_linemarker, U"#", 1, KANDR, IN_I 191169695Skan}; 192169695Skan 193169695Skan#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) 194169695Skan 195169695Skan/* Skip any remaining tokens in a directive. */ 196169695Skanstatic void 197169695Skanskip_rest_of_line (cpp_reader *pfile) 198169695Skan{ 199169695Skan /* Discard all stacked contexts. */ 200169695Skan while (pfile->context->prev) 201169695Skan _cpp_pop_context (pfile); 202169695Skan 203169695Skan /* Sweep up all tokens remaining on the line. */ 204169695Skan if (! SEEN_EOL ()) 205169695Skan while (_cpp_lex_token (pfile)->type != CPP_EOF) 206169695Skan ; 207169695Skan} 208169695Skan 209169695Skan/* Ensure there are no stray tokens at the end of a directive. */ 210169695Skanstatic void 211169695Skancheck_eol (cpp_reader *pfile) 212169695Skan{ 213169695Skan if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) 214169695Skan cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", 215169695Skan pfile->directive->name); 216169695Skan} 217169695Skan 218169695Skan/* Ensure there are no stray tokens other than comments at the end of 219169695Skan a directive, and gather the comments. */ 220169695Skanstatic const cpp_token ** 221169695Skancheck_eol_return_comments (cpp_reader *pfile) 222169695Skan{ 223169695Skan size_t c; 224169695Skan size_t capacity = 8; 225169695Skan const cpp_token **buf; 226169695Skan 227169695Skan buf = XNEWVEC (const cpp_token *, capacity); 228169695Skan c = 0; 229169695Skan if (! SEEN_EOL ()) 230169695Skan { 231169695Skan while (1) 232169695Skan { 233169695Skan const cpp_token *tok; 234169695Skan 235169695Skan tok = _cpp_lex_token (pfile); 236169695Skan if (tok->type == CPP_EOF) 237169695Skan break; 238169695Skan if (tok->type != CPP_COMMENT) 239169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 240169695Skan "extra tokens at end of #%s directive", 241169695Skan pfile->directive->name); 242169695Skan else 243169695Skan { 244169695Skan if (c + 1 >= capacity) 245169695Skan { 246169695Skan capacity *= 2; 247169695Skan buf = XRESIZEVEC (const cpp_token *, buf, capacity); 248169695Skan } 249169695Skan buf[c] = tok; 250169695Skan ++c; 251169695Skan } 252169695Skan } 253169695Skan } 254169695Skan buf[c] = NULL; 255169695Skan return buf; 256169695Skan} 257169695Skan 258169695Skan/* Called when entering a directive, _Pragma or command-line directive. */ 259169695Skanstatic void 260169695Skanstart_directive (cpp_reader *pfile) 261169695Skan{ 262169695Skan /* Setup in-directive state. */ 263169695Skan pfile->state.in_directive = 1; 264169695Skan pfile->state.save_comments = 0; 265169695Skan pfile->directive_result.type = CPP_PADDING; 266169695Skan 267169695Skan /* Some handlers need the position of the # for diagnostics. */ 268169695Skan pfile->directive_line = pfile->line_table->highest_line; 269169695Skan} 270169695Skan 271169695Skan/* Called when leaving a directive, _Pragma or command-line directive. */ 272169695Skanstatic void 273169695Skanend_directive (cpp_reader *pfile, int skip_line) 274169695Skan{ 275169695Skan if (pfile->state.in_deferred_pragma) 276169695Skan ; 277169695Skan else if (CPP_OPTION (pfile, traditional)) 278169695Skan { 279169695Skan /* Revert change of prepare_directive_trad. */ 280169695Skan pfile->state.prevent_expansion--; 281169695Skan 282169695Skan if (pfile->directive != &dtable[T_DEFINE]) 283169695Skan _cpp_remove_overlay (pfile); 284169695Skan } 285169695Skan /* We don't skip for an assembler #. */ 286169695Skan else if (skip_line) 287169695Skan { 288169695Skan skip_rest_of_line (pfile); 289169695Skan if (!pfile->keep_tokens) 290169695Skan { 291169695Skan pfile->cur_run = &pfile->base_run; 292169695Skan pfile->cur_token = pfile->base_run.base; 293169695Skan } 294169695Skan } 295169695Skan 296169695Skan /* Restore state. */ 297169695Skan pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); 298169695Skan pfile->state.in_directive = 0; 299169695Skan pfile->state.in_expression = 0; 300169695Skan pfile->state.angled_headers = 0; 301169695Skan pfile->directive = 0; 302169695Skan} 303169695Skan 304169695Skan/* Prepare to handle the directive in pfile->directive. */ 305169695Skanstatic void 306169695Skanprepare_directive_trad (cpp_reader *pfile) 307169695Skan{ 308169695Skan if (pfile->directive != &dtable[T_DEFINE]) 309169695Skan { 310169695Skan bool no_expand = (pfile->directive 311169695Skan && ! (pfile->directive->flags & EXPAND)); 312169695Skan bool was_skipping = pfile->state.skipping; 313169695Skan 314169695Skan pfile->state.in_expression = (pfile->directive == &dtable[T_IF] 315169695Skan || pfile->directive == &dtable[T_ELIF]); 316169695Skan if (pfile->state.in_expression) 317169695Skan pfile->state.skipping = false; 318169695Skan 319169695Skan if (no_expand) 320169695Skan pfile->state.prevent_expansion++; 321169695Skan _cpp_scan_out_logical_line (pfile, NULL); 322169695Skan if (no_expand) 323169695Skan pfile->state.prevent_expansion--; 324169695Skan 325169695Skan pfile->state.skipping = was_skipping; 326169695Skan _cpp_overlay_buffer (pfile, pfile->out.base, 327169695Skan pfile->out.cur - pfile->out.base); 328169695Skan } 329169695Skan 330169695Skan /* Stop ISO C from expanding anything. */ 331169695Skan pfile->state.prevent_expansion++; 332169695Skan} 333169695Skan 334169695Skan/* Output diagnostics for a directive DIR. INDENTED is nonzero if 335169695Skan the '#' was indented. */ 336169695Skanstatic void 337169695Skandirective_diagnostics (cpp_reader *pfile, const directive *dir, int indented) 338169695Skan{ 339169695Skan /* Issue -pedantic warnings for extensions. */ 340169695Skan if (CPP_PEDANTIC (pfile) 341169695Skan && ! pfile->state.skipping 342169695Skan && dir->origin == EXTENSION) 343169695Skan cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); 344169695Skan 345169695Skan /* Traditionally, a directive is ignored unless its # is in 346169695Skan column 1. Therefore in code intended to work with K+R 347169695Skan compilers, directives added by C89 must have their # 348169695Skan indented, and directives present in traditional C must not. 349169695Skan This is true even of directives in skipped conditional 350169695Skan blocks. #elif cannot be used at all. */ 351169695Skan if (CPP_WTRADITIONAL (pfile)) 352169695Skan { 353169695Skan if (dir == &dtable[T_ELIF]) 354169695Skan cpp_error (pfile, CPP_DL_WARNING, 355169695Skan "suggest not using #elif in traditional C"); 356169695Skan else if (indented && dir->origin == KANDR) 357169695Skan cpp_error (pfile, CPP_DL_WARNING, 358169695Skan "traditional C ignores #%s with the # indented", 359169695Skan dir->name); 360169695Skan else if (!indented && dir->origin != KANDR) 361169695Skan cpp_error (pfile, CPP_DL_WARNING, 362169695Skan "suggest hiding #%s from traditional C with an indented #", 363169695Skan dir->name); 364169695Skan } 365169695Skan} 366169695Skan 367169695Skan/* Check if we have a known directive. INDENTED is nonzero if the 368169695Skan '#' of the directive was indented. This function is in this file 369169695Skan to save unnecessarily exporting dtable etc. to lex.c. Returns 370169695Skan nonzero if the line of tokens has been handled, zero if we should 371169695Skan continue processing the line. */ 372169695Skanint 373169695Skan_cpp_handle_directive (cpp_reader *pfile, int indented) 374169695Skan{ 375169695Skan const directive *dir = 0; 376169695Skan const cpp_token *dname; 377169695Skan bool was_parsing_args = pfile->state.parsing_args; 378169695Skan bool was_discarding_output = pfile->state.discarding_output; 379169695Skan int skip = 1; 380169695Skan 381169695Skan if (was_discarding_output) 382169695Skan pfile->state.prevent_expansion = 0; 383169695Skan 384169695Skan if (was_parsing_args) 385169695Skan { 386169695Skan if (CPP_OPTION (pfile, pedantic)) 387169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 388169695Skan "embedding a directive within macro arguments is not portable"); 389169695Skan pfile->state.parsing_args = 0; 390169695Skan pfile->state.prevent_expansion = 0; 391169695Skan } 392169695Skan start_directive (pfile); 393169695Skan dname = _cpp_lex_token (pfile); 394169695Skan 395169695Skan if (dname->type == CPP_NAME) 396169695Skan { 397169695Skan if (dname->val.node->is_directive) 398169695Skan dir = &dtable[dname->val.node->directive_index]; 399169695Skan } 400169695Skan /* We do not recognize the # followed by a number extension in 401169695Skan assembler code. */ 402169695Skan else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) 403169695Skan { 404169695Skan dir = &linemarker_dir; 405169695Skan if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) 406169695Skan && ! pfile->state.skipping) 407169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 408169695Skan "style of line directive is a GCC extension"); 409169695Skan } 410169695Skan 411169695Skan if (dir) 412169695Skan { 413169695Skan /* If we have a directive that is not an opening conditional, 414169695Skan invalidate any control macro. */ 415169695Skan if (! (dir->flags & IF_COND)) 416169695Skan pfile->mi_valid = false; 417169695Skan 418169695Skan /* Kluge alert. In order to be sure that code like this 419169695Skan 420169695Skan #define HASH # 421169695Skan HASH define foo bar 422169695Skan 423169695Skan does not cause '#define foo bar' to get executed when 424169695Skan compiled with -save-temps, we recognize directives in 425169695Skan -fpreprocessed mode only if the # is in column 1. macro.c 426258501Spfg puts a space in front of any '#' at the start of a macro. 427258501Spfg 428258501Spfg We exclude the -fdirectives-only case because macro expansion 429258501Spfg has not been performed yet, and block comments can cause spaces 430258501Spfg to preceed the directive. */ 431169695Skan if (CPP_OPTION (pfile, preprocessed) 432258501Spfg && !CPP_OPTION (pfile, directives_only) 433169695Skan && (indented || !(dir->flags & IN_I))) 434169695Skan { 435169695Skan skip = 0; 436169695Skan dir = 0; 437169695Skan } 438169695Skan else 439169695Skan { 440169695Skan /* In failed conditional groups, all non-conditional 441169695Skan directives are ignored. Before doing that, whether 442169695Skan skipping or not, we should lex angle-bracketed headers 443169695Skan correctly, and maybe output some diagnostics. */ 444169695Skan pfile->state.angled_headers = dir->flags & INCL; 445169695Skan pfile->state.directive_wants_padding = dir->flags & INCL; 446169695Skan if (! CPP_OPTION (pfile, preprocessed)) 447169695Skan directive_diagnostics (pfile, dir, indented); 448169695Skan if (pfile->state.skipping && !(dir->flags & COND)) 449169695Skan dir = 0; 450169695Skan } 451169695Skan } 452169695Skan else if (dname->type == CPP_EOF) 453169695Skan ; /* CPP_EOF is the "null directive". */ 454169695Skan else 455169695Skan { 456169695Skan /* An unknown directive. Don't complain about it in assembly 457169695Skan source: we don't know where the comments are, and # may 458169695Skan introduce assembler pseudo-ops. Don't complain about invalid 459169695Skan directives in skipped conditional groups (6.10 p4). */ 460169695Skan if (CPP_OPTION (pfile, lang) == CLK_ASM) 461169695Skan skip = 0; 462169695Skan else if (!pfile->state.skipping) 463169695Skan cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", 464169695Skan cpp_token_as_text (pfile, dname)); 465169695Skan } 466169695Skan 467169695Skan pfile->directive = dir; 468169695Skan if (CPP_OPTION (pfile, traditional)) 469169695Skan prepare_directive_trad (pfile); 470169695Skan 471169695Skan if (dir) 472169695Skan pfile->directive->handler (pfile); 473169695Skan else if (skip == 0) 474169695Skan _cpp_backup_tokens (pfile, 1); 475169695Skan 476169695Skan end_directive (pfile, skip); 477169695Skan if (was_parsing_args) 478169695Skan { 479169695Skan /* Restore state when within macro args. */ 480169695Skan pfile->state.parsing_args = 2; 481169695Skan pfile->state.prevent_expansion = 1; 482169695Skan } 483169695Skan if (was_discarding_output) 484169695Skan pfile->state.prevent_expansion = 1; 485169695Skan return skip; 486169695Skan} 487169695Skan 488169695Skan/* Directive handler wrapper used by the command line option 489169695Skan processor. BUF is \n terminated. */ 490169695Skanstatic void 491169695Skanrun_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) 492169695Skan{ 493169695Skan cpp_push_buffer (pfile, (const uchar *) buf, count, 494169695Skan /* from_stage3 */ true); 495169695Skan start_directive (pfile); 496169695Skan 497169695Skan /* This is a short-term fix to prevent a leading '#' being 498169695Skan interpreted as a directive. */ 499169695Skan _cpp_clean_line (pfile); 500169695Skan 501169695Skan pfile->directive = &dtable[dir_no]; 502169695Skan if (CPP_OPTION (pfile, traditional)) 503169695Skan prepare_directive_trad (pfile); 504169695Skan pfile->directive->handler (pfile); 505169695Skan end_directive (pfile, 1); 506169695Skan _cpp_pop_buffer (pfile); 507169695Skan} 508169695Skan 509169695Skan/* Checks for validity the macro name in #define, #undef, #ifdef and 510169695Skan #ifndef directives. */ 511169695Skanstatic cpp_hashnode * 512169695Skanlex_macro_node (cpp_reader *pfile) 513169695Skan{ 514169695Skan const cpp_token *token = _cpp_lex_token (pfile); 515169695Skan 516169695Skan /* The token immediately after #define must be an identifier. That 517169695Skan identifier may not be "defined", per C99 6.10.8p4. 518169695Skan In C++, it may not be any of the "named operators" either, 519169695Skan per C++98 [lex.digraph], [lex.key]. 520169695Skan Finally, the identifier may not have been poisoned. (In that case 521169695Skan the lexer has issued the error message for us.) */ 522169695Skan 523169695Skan if (token->type == CPP_NAME) 524169695Skan { 525169695Skan cpp_hashnode *node = token->val.node; 526169695Skan 527169695Skan if (node == pfile->spec_nodes.n_defined) 528169695Skan cpp_error (pfile, CPP_DL_ERROR, 529169695Skan "\"defined\" cannot be used as a macro name"); 530169695Skan else if (! (node->flags & NODE_POISONED)) 531169695Skan return node; 532169695Skan } 533169695Skan else if (token->flags & NAMED_OP) 534169695Skan cpp_error (pfile, CPP_DL_ERROR, 535169695Skan "\"%s\" cannot be used as a macro name as it is an operator in C++", 536169695Skan NODE_NAME (token->val.node)); 537169695Skan else if (token->type == CPP_EOF) 538169695Skan cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", 539169695Skan pfile->directive->name); 540169695Skan else 541169695Skan cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); 542169695Skan 543169695Skan return NULL; 544169695Skan} 545169695Skan 546169695Skan/* Process a #define directive. Most work is done in macro.c. */ 547169695Skanstatic void 548169695Skando_define (cpp_reader *pfile) 549169695Skan{ 550169695Skan cpp_hashnode *node = lex_macro_node (pfile); 551169695Skan 552169695Skan if (node) 553169695Skan { 554169695Skan /* If we have been requested to expand comments into macros, 555169695Skan then re-enable saving of comments. */ 556169695Skan pfile->state.save_comments = 557169695Skan ! CPP_OPTION (pfile, discard_comments_in_macro_exp); 558169695Skan 559169695Skan if (_cpp_create_definition (pfile, node)) 560169695Skan if (pfile->cb.define) 561169695Skan pfile->cb.define (pfile, pfile->directive_line, node); 562169695Skan } 563169695Skan} 564169695Skan 565169695Skan/* Handle #undef. Mark the identifier NT_VOID in the hash table. */ 566169695Skanstatic void 567169695Skando_undef (cpp_reader *pfile) 568169695Skan{ 569169695Skan cpp_hashnode *node = lex_macro_node (pfile); 570169695Skan 571169695Skan if (node) 572169695Skan { 573169695Skan if (pfile->cb.undef) 574169695Skan pfile->cb.undef (pfile, pfile->directive_line, node); 575169695Skan 576169695Skan /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified 577169695Skan identifier is not currently defined as a macro name. */ 578169695Skan if (node->type == NT_MACRO) 579169695Skan { 580169695Skan if (node->flags & NODE_WARN) 581169695Skan cpp_error (pfile, CPP_DL_WARNING, 582169695Skan "undefining \"%s\"", NODE_NAME (node)); 583169695Skan 584169695Skan if (CPP_OPTION (pfile, warn_unused_macros)) 585169695Skan _cpp_warn_if_unused_macro (pfile, node, NULL); 586169695Skan 587169695Skan _cpp_free_definition (node); 588169695Skan } 589169695Skan } 590169695Skan 591169695Skan check_eol (pfile); 592169695Skan} 593169695Skan 594169695Skan/* Undefine a single macro/assertion/whatever. */ 595169695Skan 596169695Skanstatic int 597169695Skanundefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, 598169695Skan void *data_p ATTRIBUTE_UNUSED) 599169695Skan{ 600169695Skan /* Body of _cpp_free_definition inlined here for speed. 601169695Skan Macros and assertions no longer have anything to free. */ 602169695Skan h->type = NT_VOID; 603169695Skan h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED); 604169695Skan return 1; 605169695Skan} 606169695Skan 607169695Skan/* Undefine all macros and assertions. */ 608169695Skan 609169695Skanvoid 610169695Skancpp_undef_all (cpp_reader *pfile) 611169695Skan{ 612169695Skan cpp_forall_identifiers (pfile, undefine_macros, NULL); 613169695Skan} 614169695Skan 615169695Skan 616169695Skan/* Helper routine used by parse_include. Reinterpret the current line 617169695Skan as an h-char-sequence (< ... >); we are looking at the first token 618169695Skan after the <. Returns a malloced filename. */ 619169695Skanstatic char * 620169695Skanglue_header_name (cpp_reader *pfile) 621169695Skan{ 622169695Skan const cpp_token *token; 623169695Skan char *buffer; 624169695Skan size_t len, total_len = 0, capacity = 1024; 625169695Skan 626169695Skan /* To avoid lexed tokens overwriting our glued name, we can only 627169695Skan allocate from the string pool once we've lexed everything. */ 628169695Skan buffer = XNEWVEC (char, capacity); 629169695Skan for (;;) 630169695Skan { 631169695Skan token = get_token_no_padding (pfile); 632169695Skan 633169695Skan if (token->type == CPP_GREATER) 634169695Skan break; 635169695Skan if (token->type == CPP_EOF) 636169695Skan { 637169695Skan cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); 638169695Skan break; 639169695Skan } 640169695Skan 641169695Skan len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ 642169695Skan if (total_len + len > capacity) 643169695Skan { 644169695Skan capacity = (capacity + len) * 2; 645169695Skan buffer = XRESIZEVEC (char, buffer, capacity); 646169695Skan } 647169695Skan 648169695Skan if (token->flags & PREV_WHITE) 649169695Skan buffer[total_len++] = ' '; 650169695Skan 651169695Skan total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], 652169695Skan true) 653169695Skan - (uchar *) buffer); 654169695Skan } 655169695Skan 656169695Skan buffer[total_len] = '\0'; 657169695Skan return buffer; 658169695Skan} 659169695Skan 660169695Skan/* Returns the file name of #include, #include_next, #import and 661169695Skan #pragma dependency. The string is malloced and the caller should 662169695Skan free it. Returns NULL on error. */ 663169695Skanstatic const char * 664169695Skanparse_include (cpp_reader *pfile, int *pangle_brackets, 665169695Skan const cpp_token ***buf) 666169695Skan{ 667169695Skan char *fname; 668169695Skan const cpp_token *header; 669169695Skan 670169695Skan /* Allow macro expansion. */ 671169695Skan header = get_token_no_padding (pfile); 672169695Skan if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME) 673169695Skan { 674169695Skan fname = XNEWVEC (char, header->val.str.len - 1); 675169695Skan memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); 676169695Skan fname[header->val.str.len - 2] = '\0'; 677169695Skan *pangle_brackets = header->type == CPP_HEADER_NAME; 678169695Skan } 679169695Skan else if (header->type == CPP_LESS) 680169695Skan { 681169695Skan fname = glue_header_name (pfile); 682169695Skan *pangle_brackets = 1; 683169695Skan } 684169695Skan else 685169695Skan { 686169695Skan const unsigned char *dir; 687169695Skan 688169695Skan if (pfile->directive == &dtable[T_PRAGMA]) 689169695Skan dir = U"pragma dependency"; 690169695Skan else 691169695Skan dir = pfile->directive->name; 692169695Skan cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", 693169695Skan dir); 694169695Skan 695169695Skan return NULL; 696169695Skan } 697169695Skan 698169695Skan if (buf == NULL || CPP_OPTION (pfile, discard_comments)) 699169695Skan check_eol (pfile); 700169695Skan else 701169695Skan { 702169695Skan /* If we are not discarding comments, then gather them while 703169695Skan doing the eol check. */ 704169695Skan *buf = check_eol_return_comments (pfile); 705169695Skan } 706169695Skan 707169695Skan return fname; 708169695Skan} 709169695Skan 710169695Skan/* Handle #include, #include_next and #import. */ 711169695Skanstatic void 712169695Skando_include_common (cpp_reader *pfile, enum include_type type) 713169695Skan{ 714169695Skan const char *fname; 715169695Skan int angle_brackets; 716169695Skan const cpp_token **buf = NULL; 717169695Skan 718169695Skan /* Re-enable saving of comments if requested, so that the include 719169695Skan callback can dump comments which follow #include. */ 720169695Skan pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); 721169695Skan 722169695Skan fname = parse_include (pfile, &angle_brackets, &buf); 723169695Skan if (!fname) 724169695Skan { 725169695Skan if (buf) 726169695Skan XDELETEVEC (buf); 727169695Skan return; 728169695Skan } 729169695Skan 730169695Skan if (!*fname) 731169695Skan { 732169695Skan cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s", 733169695Skan pfile->directive->name); 734169695Skan XDELETEVEC (fname); 735169695Skan if (buf) 736169695Skan XDELETEVEC (buf); 737169695Skan return; 738169695Skan } 739169695Skan 740169695Skan /* Prevent #include recursion. */ 741169695Skan if (pfile->line_table->depth >= CPP_STACK_MAX) 742169695Skan cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); 743169695Skan else 744169695Skan { 745169695Skan /* Get out of macro context, if we are. */ 746169695Skan skip_rest_of_line (pfile); 747169695Skan 748169695Skan if (pfile->cb.include) 749169695Skan pfile->cb.include (pfile, pfile->directive_line, 750169695Skan pfile->directive->name, fname, angle_brackets, 751169695Skan buf); 752169695Skan 753169695Skan _cpp_stack_include (pfile, fname, angle_brackets, type); 754169695Skan } 755169695Skan 756169695Skan XDELETEVEC (fname); 757169695Skan if (buf) 758169695Skan XDELETEVEC (buf); 759169695Skan} 760169695Skan 761169695Skanstatic void 762169695Skando_include (cpp_reader *pfile) 763169695Skan{ 764169695Skan do_include_common (pfile, IT_INCLUDE); 765169695Skan} 766169695Skan 767169695Skanstatic void 768169695Skando_import (cpp_reader *pfile) 769169695Skan{ 770169695Skan do_include_common (pfile, IT_IMPORT); 771169695Skan} 772169695Skan 773169695Skanstatic void 774169695Skando_include_next (cpp_reader *pfile) 775169695Skan{ 776169695Skan enum include_type type = IT_INCLUDE_NEXT; 777169695Skan 778169695Skan /* If this is the primary source file, warn and use the normal 779169695Skan search logic. */ 780169695Skan if (! pfile->buffer->prev) 781169695Skan { 782169695Skan cpp_error (pfile, CPP_DL_WARNING, 783169695Skan "#include_next in primary source file"); 784169695Skan type = IT_INCLUDE; 785169695Skan } 786169695Skan do_include_common (pfile, type); 787169695Skan} 788169695Skan 789169695Skan/* Subroutine of do_linemarker. Read possible flags after file name. 790169695Skan LAST is the last flag seen; 0 if this is the first flag. Return the 791169695Skan flag if it is valid, 0 at the end of the directive. Otherwise 792169695Skan complain. */ 793169695Skanstatic unsigned int 794169695Skanread_flag (cpp_reader *pfile, unsigned int last) 795169695Skan{ 796169695Skan const cpp_token *token = _cpp_lex_token (pfile); 797169695Skan 798169695Skan if (token->type == CPP_NUMBER && token->val.str.len == 1) 799169695Skan { 800169695Skan unsigned int flag = token->val.str.text[0] - '0'; 801169695Skan 802169695Skan if (flag > last && flag <= 4 803169695Skan && (flag != 4 || last == 3) 804169695Skan && (flag != 2 || last == 0)) 805169695Skan return flag; 806169695Skan } 807169695Skan 808169695Skan if (token->type != CPP_EOF) 809169695Skan cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", 810169695Skan cpp_token_as_text (pfile, token)); 811169695Skan return 0; 812169695Skan} 813169695Skan 814169695Skan/* Subroutine of do_line and do_linemarker. Convert a number in STR, 815169695Skan of length LEN, to binary; store it in NUMP, and return 0 if the 816169695Skan number was well-formed, 1 if not. Temporary, hopefully. */ 817169695Skanstatic int 818169695Skanstrtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump) 819169695Skan{ 820169695Skan unsigned long reg = 0; 821169695Skan uchar c; 822169695Skan while (len--) 823169695Skan { 824169695Skan c = *str++; 825169695Skan if (!ISDIGIT (c)) 826169695Skan return 1; 827169695Skan reg *= 10; 828169695Skan reg += c - '0'; 829169695Skan } 830169695Skan *nump = reg; 831169695Skan return 0; 832169695Skan} 833169695Skan 834169695Skan/* Interpret #line command. 835169695Skan Note that the filename string (if any) is a true string constant 836169695Skan (escapes are interpreted), unlike in #line. */ 837169695Skanstatic void 838169695Skando_line (cpp_reader *pfile) 839169695Skan{ 840169695Skan const struct line_maps *line_table = pfile->line_table; 841169695Skan const struct line_map *map = &line_table->maps[line_table->used - 1]; 842169695Skan 843169695Skan /* skip_rest_of_line() may cause line table to be realloc()ed so note down 844169695Skan sysp right now. */ 845169695Skan 846169695Skan unsigned char map_sysp = map->sysp; 847169695Skan const cpp_token *token; 848169695Skan const char *new_file = map->to_file; 849169695Skan unsigned long new_lineno; 850169695Skan 851169695Skan /* C99 raised the minimum limit on #line numbers. */ 852169695Skan unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; 853169695Skan 854169695Skan /* #line commands expand macros. */ 855169695Skan token = cpp_get_token (pfile); 856169695Skan if (token->type != CPP_NUMBER 857169695Skan || strtoul_for_line (token->val.str.text, token->val.str.len, 858169695Skan &new_lineno)) 859169695Skan { 860169695Skan cpp_error (pfile, CPP_DL_ERROR, 861169695Skan "\"%s\" after #line is not a positive integer", 862169695Skan cpp_token_as_text (pfile, token)); 863169695Skan return; 864169695Skan } 865169695Skan 866169695Skan if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) 867169695Skan cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); 868169695Skan 869169695Skan token = cpp_get_token (pfile); 870169695Skan if (token->type == CPP_STRING) 871169695Skan { 872169695Skan cpp_string s = { 0, 0 }; 873169695Skan if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, 874169695Skan &s, false)) 875169695Skan new_file = (const char *)s.text; 876169695Skan check_eol (pfile); 877169695Skan } 878169695Skan else if (token->type != CPP_EOF) 879169695Skan { 880169695Skan cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", 881169695Skan cpp_token_as_text (pfile, token)); 882169695Skan return; 883169695Skan } 884169695Skan 885169695Skan skip_rest_of_line (pfile); 886169695Skan _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, 887169695Skan map_sysp); 888169695Skan} 889169695Skan 890169695Skan/* Interpret the # 44 "file" [flags] notation, which has slightly 891169695Skan different syntax and semantics from #line: Flags are allowed, 892169695Skan and we never complain about the line number being too big. */ 893169695Skanstatic void 894169695Skando_linemarker (cpp_reader *pfile) 895169695Skan{ 896169695Skan const struct line_maps *line_table = pfile->line_table; 897169695Skan const struct line_map *map = &line_table->maps[line_table->used - 1]; 898169695Skan const cpp_token *token; 899169695Skan const char *new_file = map->to_file; 900169695Skan unsigned long new_lineno; 901169695Skan unsigned int new_sysp = map->sysp; 902169695Skan enum lc_reason reason = LC_RENAME; 903169695Skan int flag; 904169695Skan 905169695Skan /* Back up so we can get the number again. Putting this in 906169695Skan _cpp_handle_directive risks two calls to _cpp_backup_tokens in 907169695Skan some circumstances, which can segfault. */ 908169695Skan _cpp_backup_tokens (pfile, 1); 909169695Skan 910169695Skan /* #line commands expand macros. */ 911169695Skan token = cpp_get_token (pfile); 912169695Skan if (token->type != CPP_NUMBER 913169695Skan || strtoul_for_line (token->val.str.text, token->val.str.len, 914169695Skan &new_lineno)) 915169695Skan { 916169695Skan cpp_error (pfile, CPP_DL_ERROR, 917169695Skan "\"%s\" after # is not a positive integer", 918169695Skan cpp_token_as_text (pfile, token)); 919169695Skan return; 920169695Skan } 921169695Skan 922169695Skan token = cpp_get_token (pfile); 923169695Skan if (token->type == CPP_STRING) 924169695Skan { 925169695Skan cpp_string s = { 0, 0 }; 926169695Skan if (cpp_interpret_string_notranslate (pfile, &token->val.str, 927169695Skan 1, &s, false)) 928169695Skan new_file = (const char *)s.text; 929169695Skan 930169695Skan new_sysp = 0; 931169695Skan flag = read_flag (pfile, 0); 932169695Skan if (flag == 1) 933169695Skan { 934169695Skan reason = LC_ENTER; 935169695Skan /* Fake an include for cpp_included (). */ 936169695Skan _cpp_fake_include (pfile, new_file); 937169695Skan flag = read_flag (pfile, flag); 938169695Skan } 939169695Skan else if (flag == 2) 940169695Skan { 941169695Skan reason = LC_LEAVE; 942169695Skan flag = read_flag (pfile, flag); 943169695Skan } 944169695Skan if (flag == 3) 945169695Skan { 946169695Skan new_sysp = 1; 947169695Skan flag = read_flag (pfile, flag); 948169695Skan if (flag == 4) 949169695Skan new_sysp = 2; 950169695Skan } 951169695Skan pfile->buffer->sysp = new_sysp; 952169695Skan 953169695Skan check_eol (pfile); 954169695Skan } 955169695Skan else if (token->type != CPP_EOF) 956169695Skan { 957169695Skan cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", 958169695Skan cpp_token_as_text (pfile, token)); 959169695Skan return; 960169695Skan } 961169695Skan 962169695Skan skip_rest_of_line (pfile); 963169695Skan _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); 964169695Skan} 965169695Skan 966169695Skan/* Arrange the file_change callback. pfile->line has changed to 967169695Skan FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system 968169695Skan header, 2 for a system header that needs to be extern "C" protected, 969169695Skan and zero otherwise. */ 970169695Skanvoid 971169695Skan_cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, 972169695Skan const char *to_file, unsigned int file_line, 973169695Skan unsigned int sysp) 974169695Skan{ 975169695Skan const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, 976169695Skan to_file, file_line); 977169695Skan if (map != NULL) 978169695Skan linemap_line_start (pfile->line_table, map->to_line, 127); 979169695Skan 980169695Skan if (pfile->cb.file_change) 981169695Skan pfile->cb.file_change (pfile, map); 982169695Skan} 983169695Skan 984169695Skan/* Report a warning or error detected by the program we are 985169695Skan processing. Use the directive's tokens in the error message. */ 986169695Skanstatic void 987169695Skando_diagnostic (cpp_reader *pfile, int code, int print_dir) 988169695Skan{ 989169695Skan if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0)) 990169695Skan { 991169695Skan if (print_dir) 992169695Skan fprintf (stderr, "#%s ", pfile->directive->name); 993169695Skan pfile->state.prevent_expansion++; 994169695Skan cpp_output_line (pfile, stderr); 995169695Skan pfile->state.prevent_expansion--; 996169695Skan } 997169695Skan} 998169695Skan 999169695Skanstatic void 1000169695Skando_error (cpp_reader *pfile) 1001169695Skan{ 1002169695Skan do_diagnostic (pfile, CPP_DL_ERROR, 1); 1003169695Skan} 1004169695Skan 1005169695Skanstatic void 1006169695Skando_warning (cpp_reader *pfile) 1007169695Skan{ 1008169695Skan /* We want #warning diagnostics to be emitted in system headers too. */ 1009169695Skan do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1); 1010169695Skan} 1011169695Skan 1012169695Skan/* Report program identification. */ 1013169695Skanstatic void 1014169695Skando_ident (cpp_reader *pfile) 1015169695Skan{ 1016169695Skan const cpp_token *str = cpp_get_token (pfile); 1017169695Skan 1018169695Skan if (str->type != CPP_STRING) 1019169695Skan cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", 1020169695Skan pfile->directive->name); 1021169695Skan else if (pfile->cb.ident) 1022169695Skan pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); 1023169695Skan 1024169695Skan check_eol (pfile); 1025169695Skan} 1026169695Skan 1027169695Skan/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the 1028169695Skan matching entry, or NULL if none is found. The returned entry could 1029169695Skan be the start of a namespace chain, or a pragma. */ 1030169695Skanstatic struct pragma_entry * 1031169695Skanlookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) 1032169695Skan{ 1033169695Skan while (chain && chain->pragma != pragma) 1034169695Skan chain = chain->next; 1035169695Skan 1036169695Skan return chain; 1037169695Skan} 1038169695Skan 1039169695Skan/* Create and insert a blank pragma entry at the beginning of a 1040169695Skan singly-linked CHAIN. */ 1041169695Skanstatic struct pragma_entry * 1042169695Skannew_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) 1043169695Skan{ 1044169695Skan struct pragma_entry *new_entry; 1045169695Skan 1046169695Skan new_entry = (struct pragma_entry *) 1047169695Skan _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); 1048169695Skan 1049169695Skan memset (new_entry, 0, sizeof (struct pragma_entry)); 1050169695Skan new_entry->next = *chain; 1051169695Skan 1052169695Skan *chain = new_entry; 1053169695Skan return new_entry; 1054169695Skan} 1055169695Skan 1056169695Skan/* Register a pragma NAME in namespace SPACE. If SPACE is null, it 1057169695Skan goes in the global namespace. */ 1058169695Skanstatic struct pragma_entry * 1059169695Skanregister_pragma_1 (cpp_reader *pfile, const char *space, const char *name, 1060169695Skan bool allow_name_expansion) 1061169695Skan{ 1062169695Skan struct pragma_entry **chain = &pfile->pragmas; 1063169695Skan struct pragma_entry *entry; 1064169695Skan const cpp_hashnode *node; 1065169695Skan 1066169695Skan if (space) 1067169695Skan { 1068169695Skan node = cpp_lookup (pfile, U space, strlen (space)); 1069169695Skan entry = lookup_pragma_entry (*chain, node); 1070169695Skan if (!entry) 1071169695Skan { 1072169695Skan entry = new_pragma_entry (pfile, chain); 1073169695Skan entry->pragma = node; 1074169695Skan entry->is_nspace = true; 1075169695Skan entry->allow_expansion = allow_name_expansion; 1076169695Skan } 1077169695Skan else if (!entry->is_nspace) 1078169695Skan goto clash; 1079169695Skan else if (entry->allow_expansion != allow_name_expansion) 1080169695Skan { 1081169695Skan cpp_error (pfile, CPP_DL_ICE, 1082169695Skan "registering pragmas in namespace \"%s\" with mismatched " 1083169695Skan "name expansion", space); 1084169695Skan return NULL; 1085169695Skan } 1086169695Skan chain = &entry->u.space; 1087169695Skan } 1088169695Skan else if (allow_name_expansion) 1089169695Skan { 1090169695Skan cpp_error (pfile, CPP_DL_ICE, 1091169695Skan "registering pragma \"%s\" with name expansion " 1092169695Skan "and no namespace", name); 1093169695Skan return NULL; 1094169695Skan } 1095169695Skan 1096169695Skan /* Check for duplicates. */ 1097169695Skan node = cpp_lookup (pfile, U name, strlen (name)); 1098169695Skan entry = lookup_pragma_entry (*chain, node); 1099169695Skan if (entry == NULL) 1100169695Skan { 1101169695Skan entry = new_pragma_entry (pfile, chain); 1102169695Skan entry->pragma = node; 1103169695Skan return entry; 1104169695Skan } 1105169695Skan 1106169695Skan if (entry->is_nspace) 1107169695Skan clash: 1108169695Skan cpp_error (pfile, CPP_DL_ICE, 1109169695Skan "registering \"%s\" as both a pragma and a pragma namespace", 1110169695Skan NODE_NAME (node)); 1111169695Skan else if (space) 1112169695Skan cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", 1113169695Skan space, name); 1114169695Skan else 1115169695Skan cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); 1116169695Skan 1117169695Skan return NULL; 1118169695Skan} 1119169695Skan 1120169695Skan/* Register a cpplib internal pragma SPACE NAME with HANDLER. */ 1121169695Skanstatic void 1122169695Skanregister_pragma_internal (cpp_reader *pfile, const char *space, 1123169695Skan const char *name, pragma_cb handler) 1124169695Skan{ 1125169695Skan struct pragma_entry *entry; 1126169695Skan 1127169695Skan entry = register_pragma_1 (pfile, space, name, false); 1128169695Skan entry->is_internal = true; 1129169695Skan entry->u.handler = handler; 1130169695Skan} 1131169695Skan 1132169695Skan/* Register a pragma NAME in namespace SPACE. If SPACE is null, it 1133169695Skan goes in the global namespace. HANDLER is the handler it will call, 1134169695Skan which must be non-NULL. If ALLOW_EXPANSION is set, allow macro 1135169695Skan expansion while parsing pragma NAME. This function is exported 1136169695Skan from libcpp. */ 1137169695Skanvoid 1138169695Skancpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, 1139169695Skan pragma_cb handler, bool allow_expansion) 1140169695Skan{ 1141169695Skan struct pragma_entry *entry; 1142169695Skan 1143169695Skan if (!handler) 1144169695Skan { 1145169695Skan cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); 1146169695Skan return; 1147169695Skan } 1148169695Skan 1149169695Skan entry = register_pragma_1 (pfile, space, name, false); 1150169695Skan if (entry) 1151169695Skan { 1152169695Skan entry->allow_expansion = allow_expansion; 1153169695Skan entry->u.handler = handler; 1154169695Skan } 1155169695Skan} 1156169695Skan 1157169695Skan/* Similarly, but create mark the pragma for deferred processing. 1158169695Skan When found, a CPP_PRAGMA token will be insertted into the stream 1159169695Skan with IDENT in the token->u.pragma slot. */ 1160169695Skanvoid 1161169695Skancpp_register_deferred_pragma (cpp_reader *pfile, const char *space, 1162169695Skan const char *name, unsigned int ident, 1163169695Skan bool allow_expansion, bool allow_name_expansion) 1164169695Skan{ 1165169695Skan struct pragma_entry *entry; 1166169695Skan 1167169695Skan entry = register_pragma_1 (pfile, space, name, allow_name_expansion); 1168169695Skan if (entry) 1169169695Skan { 1170169695Skan entry->is_deferred = true; 1171169695Skan entry->allow_expansion = allow_expansion; 1172169695Skan entry->u.ident = ident; 1173169695Skan } 1174169695Skan} 1175169695Skan 1176169695Skan/* Register the pragmas the preprocessor itself handles. */ 1177169695Skanvoid 1178169695Skan_cpp_init_internal_pragmas (cpp_reader *pfile) 1179169695Skan{ 1180169695Skan /* Pragmas in the global namespace. */ 1181169695Skan register_pragma_internal (pfile, 0, "once", do_pragma_once); 1182169695Skan 1183169695Skan /* New GCC-specific pragmas should be put in the GCC namespace. */ 1184169695Skan register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); 1185169695Skan register_pragma_internal (pfile, "GCC", "system_header", 1186169695Skan do_pragma_system_header); 1187169695Skan register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); 1188169695Skan} 1189169695Skan 1190169695Skan/* Return the number of registered pragmas in PE. */ 1191169695Skan 1192169695Skanstatic int 1193169695Skancount_registered_pragmas (struct pragma_entry *pe) 1194169695Skan{ 1195169695Skan int ct = 0; 1196169695Skan for (; pe != NULL; pe = pe->next) 1197169695Skan { 1198169695Skan if (pe->is_nspace) 1199169695Skan ct += count_registered_pragmas (pe->u.space); 1200169695Skan ct++; 1201169695Skan } 1202169695Skan return ct; 1203169695Skan} 1204169695Skan 1205169695Skan/* Save into SD the names of the registered pragmas referenced by PE, 1206169695Skan and return a pointer to the next free space in SD. */ 1207169695Skan 1208169695Skanstatic char ** 1209169695Skansave_registered_pragmas (struct pragma_entry *pe, char **sd) 1210169695Skan{ 1211169695Skan for (; pe != NULL; pe = pe->next) 1212169695Skan { 1213169695Skan if (pe->is_nspace) 1214169695Skan sd = save_registered_pragmas (pe->u.space, sd); 1215169695Skan *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), 1216169695Skan HT_LEN (&pe->pragma->ident), 1217169695Skan HT_LEN (&pe->pragma->ident) + 1); 1218169695Skan } 1219169695Skan return sd; 1220169695Skan} 1221169695Skan 1222169695Skan/* Return a newly-allocated array which saves the names of the 1223169695Skan registered pragmas. */ 1224169695Skan 1225169695Skanchar ** 1226169695Skan_cpp_save_pragma_names (cpp_reader *pfile) 1227169695Skan{ 1228169695Skan int ct = count_registered_pragmas (pfile->pragmas); 1229169695Skan char **result = XNEWVEC (char *, ct); 1230169695Skan (void) save_registered_pragmas (pfile->pragmas, result); 1231169695Skan return result; 1232169695Skan} 1233169695Skan 1234169695Skan/* Restore from SD the names of the registered pragmas referenced by PE, 1235169695Skan and return a pointer to the next unused name in SD. */ 1236169695Skan 1237169695Skanstatic char ** 1238169695Skanrestore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, 1239169695Skan char **sd) 1240169695Skan{ 1241169695Skan for (; pe != NULL; pe = pe->next) 1242169695Skan { 1243169695Skan if (pe->is_nspace) 1244169695Skan sd = restore_registered_pragmas (pfile, pe->u.space, sd); 1245169695Skan pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd)); 1246169695Skan free (*sd); 1247169695Skan sd++; 1248169695Skan } 1249169695Skan return sd; 1250169695Skan} 1251169695Skan 1252169695Skan/* Restore the names of the registered pragmas from SAVED. */ 1253169695Skan 1254169695Skanvoid 1255169695Skan_cpp_restore_pragma_names (cpp_reader *pfile, char **saved) 1256169695Skan{ 1257169695Skan (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); 1258169695Skan free (saved); 1259169695Skan} 1260169695Skan 1261169695Skan/* Pragmata handling. We handle some, and pass the rest on to the 1262169695Skan front end. C99 defines three pragmas and says that no macro 1263169695Skan expansion is to be performed on them; whether or not macro 1264169695Skan expansion happens for other pragmas is implementation defined. 1265169695Skan This implementation allows for a mix of both, since GCC did not 1266169695Skan traditionally macro expand its (few) pragmas, whereas OpenMP 1267169695Skan specifies that macro expansion should happen. */ 1268169695Skanstatic void 1269169695Skando_pragma (cpp_reader *pfile) 1270169695Skan{ 1271169695Skan const struct pragma_entry *p = NULL; 1272169695Skan const cpp_token *token, *pragma_token = pfile->cur_token; 1273169695Skan cpp_token ns_token; 1274169695Skan unsigned int count = 1; 1275169695Skan 1276169695Skan pfile->state.prevent_expansion++; 1277169695Skan 1278169695Skan token = cpp_get_token (pfile); 1279169695Skan ns_token = *token; 1280169695Skan if (token->type == CPP_NAME) 1281169695Skan { 1282169695Skan p = lookup_pragma_entry (pfile->pragmas, token->val.node); 1283169695Skan if (p && p->is_nspace) 1284169695Skan { 1285169695Skan bool allow_name_expansion = p->allow_expansion; 1286169695Skan if (allow_name_expansion) 1287169695Skan pfile->state.prevent_expansion--; 1288169695Skan token = cpp_get_token (pfile); 1289169695Skan if (token->type == CPP_NAME) 1290169695Skan p = lookup_pragma_entry (p->u.space, token->val.node); 1291169695Skan else 1292169695Skan p = NULL; 1293169695Skan if (allow_name_expansion) 1294169695Skan pfile->state.prevent_expansion++; 1295169695Skan count = 2; 1296169695Skan } 1297169695Skan } 1298169695Skan 1299169695Skan if (p) 1300169695Skan { 1301169695Skan if (p->is_deferred) 1302169695Skan { 1303169695Skan pfile->directive_result.src_loc = pragma_token->src_loc; 1304169695Skan pfile->directive_result.type = CPP_PRAGMA; 1305169695Skan pfile->directive_result.flags = pragma_token->flags; 1306169695Skan pfile->directive_result.val.pragma = p->u.ident; 1307169695Skan pfile->state.in_deferred_pragma = true; 1308169695Skan pfile->state.pragma_allow_expansion = p->allow_expansion; 1309169695Skan if (!p->allow_expansion) 1310169695Skan pfile->state.prevent_expansion++; 1311169695Skan } 1312169695Skan else 1313169695Skan { 1314169695Skan /* Since the handler below doesn't get the line number, that 1315169695Skan it might need for diagnostics, make sure it has the right 1316169695Skan numbers in place. */ 1317169695Skan if (pfile->cb.line_change) 1318169695Skan (*pfile->cb.line_change) (pfile, pragma_token, false); 1319169695Skan if (p->allow_expansion) 1320169695Skan pfile->state.prevent_expansion--; 1321169695Skan (*p->u.handler) (pfile); 1322169695Skan if (p->allow_expansion) 1323169695Skan pfile->state.prevent_expansion++; 1324169695Skan } 1325169695Skan } 1326169695Skan else if (pfile->cb.def_pragma) 1327169695Skan { 1328169695Skan if (count == 1 || pfile->context->prev == NULL) 1329169695Skan _cpp_backup_tokens (pfile, count); 1330169695Skan else 1331169695Skan { 1332169695Skan /* Invalid name comes from macro expansion, _cpp_backup_tokens 1333169695Skan won't allow backing 2 tokens. */ 1334169695Skan /* ??? The token buffer is leaked. Perhaps if def_pragma hook 1335169695Skan reads both tokens, we could perhaps free it, but if it doesn't, 1336169695Skan we don't know the exact lifespan. */ 1337169695Skan cpp_token *toks = XNEWVEC (cpp_token, 2); 1338169695Skan toks[0] = ns_token; 1339169695Skan toks[0].flags |= NO_EXPAND; 1340169695Skan toks[1] = *token; 1341169695Skan toks[1].flags |= NO_EXPAND; 1342169695Skan _cpp_push_token_context (pfile, NULL, toks, 2); 1343169695Skan } 1344169695Skan pfile->cb.def_pragma (pfile, pfile->directive_line); 1345169695Skan } 1346169695Skan 1347169695Skan pfile->state.prevent_expansion--; 1348169695Skan} 1349169695Skan 1350169695Skan/* Handle #pragma once. */ 1351169695Skanstatic void 1352169695Skando_pragma_once (cpp_reader *pfile) 1353169695Skan{ 1354169695Skan if (pfile->buffer->prev == NULL) 1355169695Skan cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); 1356169695Skan 1357169695Skan check_eol (pfile); 1358169695Skan _cpp_mark_file_once_only (pfile, pfile->buffer->file); 1359169695Skan} 1360169695Skan 1361169695Skan/* Handle #pragma GCC poison, to poison one or more identifiers so 1362169695Skan that the lexer produces a hard error for each subsequent usage. */ 1363169695Skanstatic void 1364169695Skando_pragma_poison (cpp_reader *pfile) 1365169695Skan{ 1366169695Skan const cpp_token *tok; 1367169695Skan cpp_hashnode *hp; 1368169695Skan 1369169695Skan pfile->state.poisoned_ok = 1; 1370169695Skan for (;;) 1371169695Skan { 1372169695Skan tok = _cpp_lex_token (pfile); 1373169695Skan if (tok->type == CPP_EOF) 1374169695Skan break; 1375169695Skan if (tok->type != CPP_NAME) 1376169695Skan { 1377169695Skan cpp_error (pfile, CPP_DL_ERROR, 1378169695Skan "invalid #pragma GCC poison directive"); 1379169695Skan break; 1380169695Skan } 1381169695Skan 1382169695Skan hp = tok->val.node; 1383169695Skan if (hp->flags & NODE_POISONED) 1384169695Skan continue; 1385169695Skan 1386169695Skan if (hp->type == NT_MACRO) 1387169695Skan cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", 1388169695Skan NODE_NAME (hp)); 1389169695Skan _cpp_free_definition (hp); 1390169695Skan hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; 1391169695Skan } 1392169695Skan pfile->state.poisoned_ok = 0; 1393169695Skan} 1394169695Skan 1395169695Skan/* Mark the current header as a system header. This will suppress 1396169695Skan some categories of warnings (notably those from -pedantic). It is 1397169695Skan intended for use in system libraries that cannot be implemented in 1398169695Skan conforming C, but cannot be certain that their headers appear in a 1399169695Skan system include directory. To prevent abuse, it is rejected in the 1400169695Skan primary source file. */ 1401169695Skanstatic void 1402169695Skando_pragma_system_header (cpp_reader *pfile) 1403169695Skan{ 1404169695Skan cpp_buffer *buffer = pfile->buffer; 1405169695Skan 1406169695Skan if (buffer->prev == 0) 1407169695Skan cpp_error (pfile, CPP_DL_WARNING, 1408169695Skan "#pragma system_header ignored outside include file"); 1409169695Skan else 1410169695Skan { 1411169695Skan check_eol (pfile); 1412169695Skan skip_rest_of_line (pfile); 1413169695Skan cpp_make_system_header (pfile, 1, 0); 1414169695Skan } 1415169695Skan} 1416169695Skan 1417169695Skan/* Check the modified date of the current include file against a specified 1418169695Skan file. Issue a diagnostic, if the specified file is newer. We use this to 1419169695Skan determine if a fixed header should be refixed. */ 1420169695Skanstatic void 1421169695Skando_pragma_dependency (cpp_reader *pfile) 1422169695Skan{ 1423169695Skan const char *fname; 1424169695Skan int angle_brackets, ordering; 1425169695Skan 1426169695Skan fname = parse_include (pfile, &angle_brackets, NULL); 1427169695Skan if (!fname) 1428169695Skan return; 1429169695Skan 1430169695Skan ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); 1431169695Skan if (ordering < 0) 1432169695Skan cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); 1433169695Skan else if (ordering > 0) 1434169695Skan { 1435169695Skan cpp_error (pfile, CPP_DL_WARNING, 1436169695Skan "current file is older than %s", fname); 1437169695Skan if (cpp_get_token (pfile)->type != CPP_EOF) 1438169695Skan { 1439169695Skan _cpp_backup_tokens (pfile, 1); 1440169695Skan do_diagnostic (pfile, CPP_DL_WARNING, 0); 1441169695Skan } 1442169695Skan } 1443169695Skan 1444169695Skan free ((void *) fname); 1445169695Skan} 1446169695Skan 1447169695Skan/* Get a token but skip padding. */ 1448169695Skanstatic const cpp_token * 1449169695Skanget_token_no_padding (cpp_reader *pfile) 1450169695Skan{ 1451169695Skan for (;;) 1452169695Skan { 1453169695Skan const cpp_token *result = cpp_get_token (pfile); 1454169695Skan if (result->type != CPP_PADDING) 1455169695Skan return result; 1456169695Skan } 1457169695Skan} 1458169695Skan 1459169695Skan/* Check syntax is "(string-literal)". Returns the string on success, 1460169695Skan or NULL on failure. */ 1461169695Skanstatic const cpp_token * 1462169695Skanget__Pragma_string (cpp_reader *pfile) 1463169695Skan{ 1464169695Skan const cpp_token *string; 1465169695Skan 1466169695Skan if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN) 1467169695Skan return NULL; 1468169695Skan 1469169695Skan string = get_token_no_padding (pfile); 1470169695Skan if (string->type != CPP_STRING && string->type != CPP_WSTRING) 1471169695Skan return NULL; 1472169695Skan 1473169695Skan if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) 1474169695Skan return NULL; 1475169695Skan 1476169695Skan return string; 1477169695Skan} 1478169695Skan 1479169695Skan/* Destringize IN into a temporary buffer, by removing the first \ of 1480169695Skan \" and \\ sequences, and process the result as a #pragma directive. */ 1481169695Skanstatic void 1482169695Skandestringize_and_run (cpp_reader *pfile, const cpp_string *in) 1483169695Skan{ 1484169695Skan const unsigned char *src, *limit; 1485169695Skan char *dest, *result; 1486169695Skan cpp_context *saved_context; 1487169695Skan cpp_token *saved_cur_token; 1488169695Skan tokenrun *saved_cur_run; 1489169695Skan cpp_token *toks; 1490169695Skan int count; 1491169695Skan 1492169695Skan dest = result = (char *) alloca (in->len - 1); 1493169695Skan src = in->text + 1 + (in->text[0] == 'L'); 1494169695Skan limit = in->text + in->len - 1; 1495169695Skan while (src < limit) 1496169695Skan { 1497169695Skan /* We know there is a character following the backslash. */ 1498169695Skan if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) 1499169695Skan src++; 1500169695Skan *dest++ = *src++; 1501169695Skan } 1502169695Skan *dest = '\n'; 1503169695Skan 1504169695Skan /* Ugh; an awful kludge. We are really not set up to be lexing 1505169695Skan tokens when in the middle of a macro expansion. Use a new 1506169695Skan context to force cpp_get_token to lex, and so skip_rest_of_line 1507169695Skan doesn't go beyond the end of the text. Also, remember the 1508169695Skan current lexing position so we can return to it later. 1509169695Skan 1510169695Skan Something like line-at-a-time lexing should remove the need for 1511169695Skan this. */ 1512169695Skan saved_context = pfile->context; 1513169695Skan saved_cur_token = pfile->cur_token; 1514169695Skan saved_cur_run = pfile->cur_run; 1515169695Skan 1516169695Skan pfile->context = XNEW (cpp_context); 1517169695Skan pfile->context->macro = 0; 1518169695Skan pfile->context->prev = 0; 1519169695Skan pfile->context->next = 0; 1520169695Skan 1521169695Skan /* Inline run_directive, since we need to delay the _cpp_pop_buffer 1522169695Skan until we've read all of the tokens that we want. */ 1523169695Skan cpp_push_buffer (pfile, (const uchar *) result, dest - result, 1524169695Skan /* from_stage3 */ true); 1525169695Skan /* ??? Antique Disgusting Hack. What does this do? */ 1526169695Skan if (pfile->buffer->prev) 1527169695Skan pfile->buffer->file = pfile->buffer->prev->file; 1528169695Skan 1529169695Skan start_directive (pfile); 1530169695Skan _cpp_clean_line (pfile); 1531169695Skan do_pragma (pfile); 1532169695Skan end_directive (pfile, 1); 1533169695Skan 1534169695Skan /* We always insert at least one token, the directive result. It'll 1535169695Skan either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we 1536169695Skan need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ 1537169695Skan 1538169695Skan /* If we're not handling the pragma internally, read all of the tokens from 1539169695Skan the string buffer now, while the string buffer is still installed. */ 1540169695Skan /* ??? Note that the token buffer allocated here is leaked. It's not clear 1541169695Skan to me what the true lifespan of the tokens are. It would appear that 1542169695Skan the lifespan is the entire parse of the main input stream, in which case 1543169695Skan this may not be wrong. */ 1544169695Skan if (pfile->directive_result.type == CPP_PRAGMA) 1545169695Skan { 1546169695Skan int maxcount; 1547169695Skan 1548169695Skan count = 1; 1549169695Skan maxcount = 50; 1550169695Skan toks = XNEWVEC (cpp_token, maxcount); 1551169695Skan toks[0] = pfile->directive_result; 1552169695Skan 1553169695Skan do 1554169695Skan { 1555169695Skan if (count == maxcount) 1556169695Skan { 1557169695Skan maxcount = maxcount * 3 / 2; 1558169695Skan toks = XRESIZEVEC (cpp_token, toks, maxcount); 1559169695Skan } 1560169695Skan toks[count] = *cpp_get_token (pfile); 1561169695Skan /* Macros have been already expanded by cpp_get_token 1562169695Skan if the pragma allowed expansion. */ 1563169695Skan toks[count++].flags |= NO_EXPAND; 1564169695Skan } 1565169695Skan while (toks[count-1].type != CPP_PRAGMA_EOL); 1566169695Skan } 1567169695Skan else 1568169695Skan { 1569169695Skan count = 1; 1570169695Skan toks = XNEW (cpp_token); 1571169695Skan toks[0] = pfile->directive_result; 1572169695Skan 1573169695Skan /* If we handled the entire pragma internally, make sure we get the 1574169695Skan line number correct for the next token. */ 1575169695Skan if (pfile->cb.line_change) 1576169695Skan pfile->cb.line_change (pfile, pfile->cur_token, false); 1577169695Skan } 1578169695Skan 1579169695Skan /* Finish inlining run_directive. */ 1580169695Skan pfile->buffer->file = NULL; 1581169695Skan _cpp_pop_buffer (pfile); 1582169695Skan 1583169695Skan /* Reset the old macro state before ... */ 1584169695Skan XDELETE (pfile->context); 1585169695Skan pfile->context = saved_context; 1586169695Skan pfile->cur_token = saved_cur_token; 1587169695Skan pfile->cur_run = saved_cur_run; 1588169695Skan 1589169695Skan /* ... inserting the new tokens we collected. */ 1590169695Skan _cpp_push_token_context (pfile, NULL, toks, count); 1591169695Skan} 1592169695Skan 1593169695Skan/* Handle the _Pragma operator. */ 1594169695Skanvoid 1595169695Skan_cpp_do__Pragma (cpp_reader *pfile) 1596169695Skan{ 1597169695Skan const cpp_token *string = get__Pragma_string (pfile); 1598169695Skan pfile->directive_result.type = CPP_PADDING; 1599169695Skan 1600169695Skan if (string) 1601169695Skan destringize_and_run (pfile, &string->val.str); 1602169695Skan else 1603169695Skan cpp_error (pfile, CPP_DL_ERROR, 1604169695Skan "_Pragma takes a parenthesized string literal"); 1605169695Skan} 1606169695Skan 1607169695Skan/* Handle #ifdef. */ 1608169695Skanstatic void 1609169695Skando_ifdef (cpp_reader *pfile) 1610169695Skan{ 1611169695Skan int skip = 1; 1612169695Skan 1613169695Skan if (! pfile->state.skipping) 1614169695Skan { 1615169695Skan const cpp_hashnode *node = lex_macro_node (pfile); 1616169695Skan 1617169695Skan if (node) 1618169695Skan { 1619169695Skan skip = node->type != NT_MACRO; 1620169695Skan _cpp_mark_macro_used (node); 1621169695Skan check_eol (pfile); 1622169695Skan } 1623169695Skan } 1624169695Skan 1625169695Skan push_conditional (pfile, skip, T_IFDEF, 0); 1626169695Skan} 1627169695Skan 1628169695Skan/* Handle #ifndef. */ 1629169695Skanstatic void 1630169695Skando_ifndef (cpp_reader *pfile) 1631169695Skan{ 1632169695Skan int skip = 1; 1633169695Skan const cpp_hashnode *node = 0; 1634169695Skan 1635169695Skan if (! pfile->state.skipping) 1636169695Skan { 1637169695Skan node = lex_macro_node (pfile); 1638169695Skan 1639169695Skan if (node) 1640169695Skan { 1641169695Skan skip = node->type == NT_MACRO; 1642169695Skan _cpp_mark_macro_used (node); 1643169695Skan check_eol (pfile); 1644169695Skan } 1645169695Skan } 1646169695Skan 1647169695Skan push_conditional (pfile, skip, T_IFNDEF, node); 1648169695Skan} 1649169695Skan 1650169695Skan/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in 1651169695Skan pfile->mi_ind_cmacro so we can handle multiple-include 1652169695Skan optimizations. If macro expansion occurs in the expression, we 1653169695Skan cannot treat it as a controlling conditional, since the expansion 1654169695Skan could change in the future. That is handled by cpp_get_token. */ 1655169695Skanstatic void 1656169695Skando_if (cpp_reader *pfile) 1657169695Skan{ 1658169695Skan int skip = 1; 1659169695Skan 1660169695Skan if (! pfile->state.skipping) 1661169695Skan skip = _cpp_parse_expr (pfile) == false; 1662169695Skan 1663169695Skan push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); 1664169695Skan} 1665169695Skan 1666169695Skan/* Flip skipping state if appropriate and continue without changing 1667169695Skan if_stack; this is so that the error message for missing #endif's 1668169695Skan etc. will point to the original #if. */ 1669169695Skanstatic void 1670169695Skando_else (cpp_reader *pfile) 1671169695Skan{ 1672169695Skan cpp_buffer *buffer = pfile->buffer; 1673169695Skan struct if_stack *ifs = buffer->if_stack; 1674169695Skan 1675169695Skan if (ifs == NULL) 1676169695Skan cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); 1677169695Skan else 1678169695Skan { 1679169695Skan if (ifs->type == T_ELSE) 1680169695Skan { 1681169695Skan cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); 1682169695Skan cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, 1683169695Skan "the conditional began here"); 1684169695Skan } 1685169695Skan ifs->type = T_ELSE; 1686169695Skan 1687169695Skan /* Skip any future (erroneous) #elses or #elifs. */ 1688169695Skan pfile->state.skipping = ifs->skip_elses; 1689169695Skan ifs->skip_elses = true; 1690169695Skan 1691169695Skan /* Invalidate any controlling macro. */ 1692169695Skan ifs->mi_cmacro = 0; 1693169695Skan 1694169695Skan /* Only check EOL if was not originally skipping. */ 1695169695Skan if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) 1696169695Skan check_eol (pfile); 1697169695Skan } 1698169695Skan} 1699169695Skan 1700169695Skan/* Handle a #elif directive by not changing if_stack either. See the 1701169695Skan comment above do_else. */ 1702169695Skanstatic void 1703169695Skando_elif (cpp_reader *pfile) 1704169695Skan{ 1705169695Skan cpp_buffer *buffer = pfile->buffer; 1706169695Skan struct if_stack *ifs = buffer->if_stack; 1707169695Skan 1708169695Skan if (ifs == NULL) 1709169695Skan cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); 1710169695Skan else 1711169695Skan { 1712169695Skan if (ifs->type == T_ELSE) 1713169695Skan { 1714169695Skan cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); 1715169695Skan cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, 1716169695Skan "the conditional began here"); 1717169695Skan } 1718169695Skan ifs->type = T_ELIF; 1719169695Skan 1720169695Skan /* Only evaluate this if we aren't skipping elses. During 1721169695Skan evaluation, set skipping to false to get lexer warnings. */ 1722169695Skan if (ifs->skip_elses) 1723169695Skan pfile->state.skipping = 1; 1724169695Skan else 1725169695Skan { 1726169695Skan pfile->state.skipping = 0; 1727169695Skan pfile->state.skipping = ! _cpp_parse_expr (pfile); 1728169695Skan ifs->skip_elses = ! pfile->state.skipping; 1729169695Skan } 1730169695Skan 1731169695Skan /* Invalidate any controlling macro. */ 1732169695Skan ifs->mi_cmacro = 0; 1733169695Skan } 1734169695Skan} 1735169695Skan 1736169695Skan/* #endif pops the if stack and resets pfile->state.skipping. */ 1737169695Skanstatic void 1738169695Skando_endif (cpp_reader *pfile) 1739169695Skan{ 1740169695Skan cpp_buffer *buffer = pfile->buffer; 1741169695Skan struct if_stack *ifs = buffer->if_stack; 1742169695Skan 1743169695Skan if (ifs == NULL) 1744169695Skan cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); 1745169695Skan else 1746169695Skan { 1747169695Skan /* Only check EOL if was not originally skipping. */ 1748169695Skan if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) 1749169695Skan check_eol (pfile); 1750169695Skan 1751169695Skan /* If potential control macro, we go back outside again. */ 1752169695Skan if (ifs->next == 0 && ifs->mi_cmacro) 1753169695Skan { 1754169695Skan pfile->mi_valid = true; 1755169695Skan pfile->mi_cmacro = ifs->mi_cmacro; 1756169695Skan } 1757169695Skan 1758169695Skan buffer->if_stack = ifs->next; 1759169695Skan pfile->state.skipping = ifs->was_skipping; 1760169695Skan obstack_free (&pfile->buffer_ob, ifs); 1761169695Skan } 1762169695Skan} 1763169695Skan 1764169695Skan/* Push an if_stack entry for a preprocessor conditional, and set 1765169695Skan pfile->state.skipping to SKIP. If TYPE indicates the conditional 1766169695Skan is #if or #ifndef, CMACRO is a potentially controlling macro, and 1767169695Skan we need to check here that we are at the top of the file. */ 1768169695Skanstatic void 1769169695Skanpush_conditional (cpp_reader *pfile, int skip, int type, 1770169695Skan const cpp_hashnode *cmacro) 1771169695Skan{ 1772169695Skan struct if_stack *ifs; 1773169695Skan cpp_buffer *buffer = pfile->buffer; 1774169695Skan 1775169695Skan ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); 1776169695Skan ifs->line = pfile->directive_line; 1777169695Skan ifs->next = buffer->if_stack; 1778169695Skan ifs->skip_elses = pfile->state.skipping || !skip; 1779169695Skan ifs->was_skipping = pfile->state.skipping; 1780169695Skan ifs->type = type; 1781169695Skan /* This condition is effectively a test for top-of-file. */ 1782169695Skan if (pfile->mi_valid && pfile->mi_cmacro == 0) 1783169695Skan ifs->mi_cmacro = cmacro; 1784169695Skan else 1785169695Skan ifs->mi_cmacro = 0; 1786169695Skan 1787169695Skan pfile->state.skipping = skip; 1788169695Skan buffer->if_stack = ifs; 1789169695Skan} 1790169695Skan 1791169695Skan/* Read the tokens of the answer into the macro pool, in a directive 1792169695Skan of type TYPE. Only commit the memory if we intend it as permanent 1793169695Skan storage, i.e. the #assert case. Returns 0 on success, and sets 1794169695Skan ANSWERP to point to the answer. */ 1795169695Skanstatic int 1796169695Skanparse_answer (cpp_reader *pfile, struct answer **answerp, int type) 1797169695Skan{ 1798169695Skan const cpp_token *paren; 1799169695Skan struct answer *answer; 1800169695Skan unsigned int acount; 1801169695Skan 1802169695Skan /* In a conditional, it is legal to not have an open paren. We 1803169695Skan should save the following token in this case. */ 1804169695Skan paren = cpp_get_token (pfile); 1805169695Skan 1806169695Skan /* If not a paren, see if we're OK. */ 1807169695Skan if (paren->type != CPP_OPEN_PAREN) 1808169695Skan { 1809169695Skan /* In a conditional no answer is a test for any answer. It 1810169695Skan could be followed by any token. */ 1811169695Skan if (type == T_IF) 1812169695Skan { 1813169695Skan _cpp_backup_tokens (pfile, 1); 1814169695Skan return 0; 1815169695Skan } 1816169695Skan 1817169695Skan /* #unassert with no answer is valid - it removes all answers. */ 1818169695Skan if (type == T_UNASSERT && paren->type == CPP_EOF) 1819169695Skan return 0; 1820169695Skan 1821169695Skan cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate"); 1822169695Skan return 1; 1823169695Skan } 1824169695Skan 1825169695Skan for (acount = 0;; acount++) 1826169695Skan { 1827169695Skan size_t room_needed; 1828169695Skan const cpp_token *token = cpp_get_token (pfile); 1829169695Skan cpp_token *dest; 1830169695Skan 1831169695Skan if (token->type == CPP_CLOSE_PAREN) 1832169695Skan break; 1833169695Skan 1834169695Skan if (token->type == CPP_EOF) 1835169695Skan { 1836169695Skan cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); 1837169695Skan return 1; 1838169695Skan } 1839169695Skan 1840169695Skan /* struct answer includes the space for one token. */ 1841169695Skan room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); 1842169695Skan 1843169695Skan if (BUFF_ROOM (pfile->a_buff) < room_needed) 1844169695Skan _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); 1845169695Skan 1846169695Skan dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; 1847169695Skan *dest = *token; 1848169695Skan 1849169695Skan /* Drop whitespace at start, for answer equivalence purposes. */ 1850169695Skan if (acount == 0) 1851169695Skan dest->flags &= ~PREV_WHITE; 1852169695Skan } 1853169695Skan 1854169695Skan if (acount == 0) 1855169695Skan { 1856169695Skan cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); 1857169695Skan return 1; 1858169695Skan } 1859169695Skan 1860169695Skan answer = (struct answer *) BUFF_FRONT (pfile->a_buff); 1861169695Skan answer->count = acount; 1862169695Skan answer->next = NULL; 1863169695Skan *answerp = answer; 1864169695Skan 1865169695Skan return 0; 1866169695Skan} 1867169695Skan 1868169695Skan/* Parses an assertion directive of type TYPE, returning a pointer to 1869169695Skan the hash node of the predicate, or 0 on error. If an answer was 1870169695Skan supplied, it is placed in ANSWERP, otherwise it is set to 0. */ 1871169695Skanstatic cpp_hashnode * 1872169695Skanparse_assertion (cpp_reader *pfile, struct answer **answerp, int type) 1873169695Skan{ 1874169695Skan cpp_hashnode *result = 0; 1875169695Skan const cpp_token *predicate; 1876169695Skan 1877169695Skan /* We don't expand predicates or answers. */ 1878169695Skan pfile->state.prevent_expansion++; 1879169695Skan 1880169695Skan *answerp = 0; 1881169695Skan predicate = cpp_get_token (pfile); 1882169695Skan if (predicate->type == CPP_EOF) 1883169695Skan cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); 1884169695Skan else if (predicate->type != CPP_NAME) 1885169695Skan cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier"); 1886169695Skan else if (parse_answer (pfile, answerp, type) == 0) 1887169695Skan { 1888169695Skan unsigned int len = NODE_LEN (predicate->val.node); 1889169695Skan unsigned char *sym = (unsigned char *) alloca (len + 1); 1890169695Skan 1891169695Skan /* Prefix '#' to get it out of macro namespace. */ 1892169695Skan sym[0] = '#'; 1893169695Skan memcpy (sym + 1, NODE_NAME (predicate->val.node), len); 1894169695Skan result = cpp_lookup (pfile, sym, len + 1); 1895169695Skan } 1896169695Skan 1897169695Skan pfile->state.prevent_expansion--; 1898169695Skan return result; 1899169695Skan} 1900169695Skan 1901169695Skan/* Returns a pointer to the pointer to CANDIDATE in the answer chain, 1902169695Skan or a pointer to NULL if the answer is not in the chain. */ 1903169695Skanstatic struct answer ** 1904169695Skanfind_answer (cpp_hashnode *node, const struct answer *candidate) 1905169695Skan{ 1906169695Skan unsigned int i; 1907169695Skan struct answer **result; 1908169695Skan 1909169695Skan for (result = &node->value.answers; *result; result = &(*result)->next) 1910169695Skan { 1911169695Skan struct answer *answer = *result; 1912169695Skan 1913169695Skan if (answer->count == candidate->count) 1914169695Skan { 1915169695Skan for (i = 0; i < answer->count; i++) 1916169695Skan if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) 1917169695Skan break; 1918169695Skan 1919169695Skan if (i == answer->count) 1920169695Skan break; 1921169695Skan } 1922169695Skan } 1923169695Skan 1924169695Skan return result; 1925169695Skan} 1926169695Skan 1927169695Skan/* Test an assertion within a preprocessor conditional. Returns 1928169695Skan nonzero on failure, zero on success. On success, the result of 1929169695Skan the test is written into VALUE, otherwise the value 0. */ 1930169695Skanint 1931169695Skan_cpp_test_assertion (cpp_reader *pfile, unsigned int *value) 1932169695Skan{ 1933169695Skan struct answer *answer; 1934169695Skan cpp_hashnode *node; 1935169695Skan 1936169695Skan node = parse_assertion (pfile, &answer, T_IF); 1937169695Skan 1938169695Skan /* For recovery, an erroneous assertion expression is handled as a 1939169695Skan failing assertion. */ 1940169695Skan *value = 0; 1941169695Skan 1942169695Skan if (node) 1943169695Skan *value = (node->type == NT_ASSERTION && 1944169695Skan (answer == 0 || *find_answer (node, answer) != 0)); 1945169695Skan else if (pfile->cur_token[-1].type == CPP_EOF) 1946169695Skan _cpp_backup_tokens (pfile, 1); 1947169695Skan 1948169695Skan /* We don't commit the memory for the answer - it's temporary only. */ 1949169695Skan return node == 0; 1950169695Skan} 1951169695Skan 1952169695Skan/* Handle #assert. */ 1953169695Skanstatic void 1954169695Skando_assert (cpp_reader *pfile) 1955169695Skan{ 1956169695Skan struct answer *new_answer; 1957169695Skan cpp_hashnode *node; 1958169695Skan 1959169695Skan node = parse_assertion (pfile, &new_answer, T_ASSERT); 1960169695Skan if (node) 1961169695Skan { 1962169695Skan size_t answer_size; 1963169695Skan 1964169695Skan /* Place the new answer in the answer list. First check there 1965169695Skan is not a duplicate. */ 1966169695Skan new_answer->next = 0; 1967169695Skan if (node->type == NT_ASSERTION) 1968169695Skan { 1969169695Skan if (*find_answer (node, new_answer)) 1970169695Skan { 1971169695Skan cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", 1972169695Skan NODE_NAME (node) + 1); 1973169695Skan return; 1974169695Skan } 1975169695Skan new_answer->next = node->value.answers; 1976169695Skan } 1977169695Skan 1978169695Skan answer_size = sizeof (struct answer) + ((new_answer->count - 1) 1979169695Skan * sizeof (cpp_token)); 1980169695Skan /* Commit or allocate storage for the object. */ 1981169695Skan if (pfile->hash_table->alloc_subobject) 1982169695Skan { 1983169695Skan struct answer *temp_answer = new_answer; 1984169695Skan new_answer = (struct answer *) pfile->hash_table->alloc_subobject 1985169695Skan (answer_size); 1986169695Skan memcpy (new_answer, temp_answer, answer_size); 1987169695Skan } 1988169695Skan else 1989169695Skan BUFF_FRONT (pfile->a_buff) += answer_size; 1990169695Skan 1991169695Skan node->type = NT_ASSERTION; 1992169695Skan node->value.answers = new_answer; 1993169695Skan check_eol (pfile); 1994169695Skan } 1995169695Skan} 1996169695Skan 1997169695Skan/* Handle #unassert. */ 1998169695Skanstatic void 1999169695Skando_unassert (cpp_reader *pfile) 2000169695Skan{ 2001169695Skan cpp_hashnode *node; 2002169695Skan struct answer *answer; 2003169695Skan 2004169695Skan node = parse_assertion (pfile, &answer, T_UNASSERT); 2005169695Skan /* It isn't an error to #unassert something that isn't asserted. */ 2006169695Skan if (node && node->type == NT_ASSERTION) 2007169695Skan { 2008169695Skan if (answer) 2009169695Skan { 2010169695Skan struct answer **p = find_answer (node, answer), *temp; 2011169695Skan 2012169695Skan /* Remove the answer from the list. */ 2013169695Skan temp = *p; 2014169695Skan if (temp) 2015169695Skan *p = temp->next; 2016169695Skan 2017169695Skan /* Did we free the last answer? */ 2018169695Skan if (node->value.answers == 0) 2019169695Skan node->type = NT_VOID; 2020169695Skan 2021169695Skan check_eol (pfile); 2022169695Skan } 2023169695Skan else 2024169695Skan _cpp_free_definition (node); 2025169695Skan } 2026169695Skan 2027169695Skan /* We don't commit the memory for the answer - it's temporary only. */ 2028169695Skan} 2029169695Skan 2030169695Skan/* These are for -D, -U, -A. */ 2031169695Skan 2032169695Skan/* Process the string STR as if it appeared as the body of a #define. 2033169695Skan If STR is just an identifier, define it with value 1. 2034169695Skan If STR has anything after the identifier, then it should 2035169695Skan be identifier=definition. */ 2036169695Skanvoid 2037169695Skancpp_define (cpp_reader *pfile, const char *str) 2038169695Skan{ 2039169695Skan char *buf, *p; 2040169695Skan size_t count; 2041169695Skan 2042169695Skan /* Copy the entire option so we can modify it. 2043169695Skan Change the first "=" in the string to a space. If there is none, 2044169695Skan tack " 1" on the end. */ 2045169695Skan 2046169695Skan count = strlen (str); 2047169695Skan buf = (char *) alloca (count + 3); 2048169695Skan memcpy (buf, str, count); 2049169695Skan 2050169695Skan p = strchr (str, '='); 2051169695Skan if (p) 2052169695Skan buf[p - str] = ' '; 2053169695Skan else 2054169695Skan { 2055169695Skan buf[count++] = ' '; 2056169695Skan buf[count++] = '1'; 2057169695Skan } 2058169695Skan buf[count] = '\n'; 2059169695Skan 2060169695Skan run_directive (pfile, T_DEFINE, buf, count); 2061169695Skan} 2062169695Skan 2063169695Skan/* Slight variant of the above for use by initialize_builtins. */ 2064169695Skanvoid 2065169695Skan_cpp_define_builtin (cpp_reader *pfile, const char *str) 2066169695Skan{ 2067169695Skan size_t len = strlen (str); 2068169695Skan char *buf = (char *) alloca (len + 1); 2069169695Skan memcpy (buf, str, len); 2070169695Skan buf[len] = '\n'; 2071169695Skan run_directive (pfile, T_DEFINE, buf, len); 2072169695Skan} 2073169695Skan 2074169695Skan/* Process MACRO as if it appeared as the body of an #undef. */ 2075169695Skanvoid 2076169695Skancpp_undef (cpp_reader *pfile, const char *macro) 2077169695Skan{ 2078169695Skan size_t len = strlen (macro); 2079169695Skan char *buf = (char *) alloca (len + 1); 2080169695Skan memcpy (buf, macro, len); 2081169695Skan buf[len] = '\n'; 2082169695Skan run_directive (pfile, T_UNDEF, buf, len); 2083169695Skan} 2084169695Skan 2085169695Skan/* Process the string STR as if it appeared as the body of a #assert. */ 2086169695Skanvoid 2087169695Skancpp_assert (cpp_reader *pfile, const char *str) 2088169695Skan{ 2089169695Skan handle_assertion (pfile, str, T_ASSERT); 2090169695Skan} 2091169695Skan 2092169695Skan/* Process STR as if it appeared as the body of an #unassert. */ 2093169695Skanvoid 2094169695Skancpp_unassert (cpp_reader *pfile, const char *str) 2095169695Skan{ 2096169695Skan handle_assertion (pfile, str, T_UNASSERT); 2097169695Skan} 2098169695Skan 2099169695Skan/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ 2100169695Skanstatic void 2101169695Skanhandle_assertion (cpp_reader *pfile, const char *str, int type) 2102169695Skan{ 2103169695Skan size_t count = strlen (str); 2104169695Skan const char *p = strchr (str, '='); 2105169695Skan 2106169695Skan /* Copy the entire option so we can modify it. Change the first 2107169695Skan "=" in the string to a '(', and tack a ')' on the end. */ 2108169695Skan char *buf = (char *) alloca (count + 2); 2109169695Skan 2110169695Skan memcpy (buf, str, count); 2111169695Skan if (p) 2112169695Skan { 2113169695Skan buf[p - str] = '('; 2114169695Skan buf[count++] = ')'; 2115169695Skan } 2116169695Skan buf[count] = '\n'; 2117169695Skan str = buf; 2118169695Skan 2119169695Skan run_directive (pfile, type, str, count); 2120169695Skan} 2121169695Skan 2122169695Skan/* The number of errors for a given reader. */ 2123169695Skanunsigned int 2124169695Skancpp_errors (cpp_reader *pfile) 2125169695Skan{ 2126169695Skan return pfile->errors; 2127169695Skan} 2128169695Skan 2129169695Skan/* The options structure. */ 2130169695Skancpp_options * 2131169695Skancpp_get_options (cpp_reader *pfile) 2132169695Skan{ 2133169695Skan return &pfile->opts; 2134169695Skan} 2135169695Skan 2136169695Skan/* The callbacks structure. */ 2137169695Skancpp_callbacks * 2138169695Skancpp_get_callbacks (cpp_reader *pfile) 2139169695Skan{ 2140169695Skan return &pfile->cb; 2141169695Skan} 2142169695Skan 2143169695Skan/* Copy the given callbacks structure to our own. */ 2144169695Skanvoid 2145169695Skancpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) 2146169695Skan{ 2147169695Skan pfile->cb = *cb; 2148169695Skan} 2149169695Skan 2150169695Skan/* The dependencies structure. (Creates one if it hasn't already been.) */ 2151169695Skanstruct deps * 2152169695Skancpp_get_deps (cpp_reader *pfile) 2153169695Skan{ 2154169695Skan if (!pfile->deps) 2155169695Skan pfile->deps = deps_init (); 2156169695Skan return pfile->deps; 2157169695Skan} 2158169695Skan 2159169695Skan/* Push a new buffer on the buffer stack. Returns the new buffer; it 2160169695Skan doesn't fail. It does not generate a file change call back; that 2161169695Skan is the responsibility of the caller. */ 2162169695Skancpp_buffer * 2163169695Skancpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, 2164169695Skan int from_stage3) 2165169695Skan{ 2166169695Skan cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); 2167169695Skan 2168169695Skan /* Clears, amongst other things, if_stack and mi_cmacro. */ 2169169695Skan memset (new_buffer, 0, sizeof (cpp_buffer)); 2170169695Skan 2171169695Skan new_buffer->next_line = new_buffer->buf = buffer; 2172169695Skan new_buffer->rlimit = buffer + len; 2173169695Skan new_buffer->from_stage3 = from_stage3; 2174169695Skan new_buffer->prev = pfile->buffer; 2175169695Skan new_buffer->need_line = true; 2176169695Skan 2177169695Skan pfile->buffer = new_buffer; 2178169695Skan 2179169695Skan return new_buffer; 2180169695Skan} 2181169695Skan 2182169695Skan/* Pops a single buffer, with a file change call-back if appropriate. 2183169695Skan Then pushes the next -include file, if any remain. */ 2184169695Skanvoid 2185169695Skan_cpp_pop_buffer (cpp_reader *pfile) 2186169695Skan{ 2187169695Skan cpp_buffer *buffer = pfile->buffer; 2188169695Skan struct _cpp_file *inc = buffer->file; 2189169695Skan struct if_stack *ifs; 2190169695Skan 2191169695Skan /* Walk back up the conditional stack till we reach its level at 2192169695Skan entry to this file, issuing error messages. */ 2193169695Skan for (ifs = buffer->if_stack; ifs; ifs = ifs->next) 2194169695Skan cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, 2195169695Skan "unterminated #%s", dtable[ifs->type].name); 2196169695Skan 2197169695Skan /* In case of a missing #endif. */ 2198169695Skan pfile->state.skipping = 0; 2199169695Skan 2200169695Skan /* _cpp_do_file_change expects pfile->buffer to be the new one. */ 2201169695Skan pfile->buffer = buffer->prev; 2202169695Skan 2203169695Skan free (buffer->notes); 2204169695Skan 2205169695Skan /* Free the buffer object now; we may want to push a new buffer 2206169695Skan in _cpp_push_next_include_file. */ 2207169695Skan obstack_free (&pfile->buffer_ob, buffer); 2208169695Skan 2209169695Skan if (inc) 2210169695Skan { 2211169695Skan _cpp_pop_file_buffer (pfile, inc); 2212169695Skan 2213169695Skan _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); 2214169695Skan } 2215169695Skan} 2216169695Skan 2217169695Skan/* Enter all recognized directives in the hash table. */ 2218169695Skanvoid 2219169695Skan_cpp_init_directives (cpp_reader *pfile) 2220169695Skan{ 2221169695Skan unsigned int i; 2222169695Skan cpp_hashnode *node; 2223169695Skan 2224169695Skan for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) 2225169695Skan { 2226169695Skan node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); 2227169695Skan node->is_directive = 1; 2228169695Skan node->directive_index = i; 2229169695Skan } 2230169695Skan} 2231