1169695Skan/* CPP Library. 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 "localedir.h" 28169695Skan 29169695Skanstatic void init_library (void); 30169695Skanstatic void mark_named_operators (cpp_reader *); 31169695Skanstatic void read_original_filename (cpp_reader *); 32169695Skanstatic void read_original_directory (cpp_reader *); 33169695Skanstatic void post_options (cpp_reader *); 34169695Skan 35169695Skan/* If we have designated initializers (GCC >2.7) these tables can be 36169695Skan initialized, constant data. Otherwise, they have to be filled in at 37169695Skan runtime. */ 38169695Skan#if HAVE_DESIGNATED_INITIALIZERS 39169695Skan 40169695Skan#define init_trigraph_map() /* Nothing. */ 41169695Skan#define TRIGRAPH_MAP \ 42169695Skan__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 43169695Skan 44169695Skan#define END }; 45169695Skan#define s(p, v) [p] = v, 46169695Skan 47169695Skan#else 48169695Skan 49169695Skan#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \ 50169695Skan static void init_trigraph_map (void) { \ 51169695Skan unsigned char *x = _cpp_trigraph_map; 52169695Skan 53169695Skan#define END } 54169695Skan#define s(p, v) x[p] = v; 55169695Skan 56169695Skan#endif 57169695Skan 58169695SkanTRIGRAPH_MAP 59169695Skan s('=', '#') s(')', ']') s('!', '|') 60169695Skan s('(', '[') s('\'', '^') s('>', '}') 61169695Skan s('/', '\\') s('<', '{') s('-', '~') 62169695SkanEND 63169695Skan 64169695Skan#undef s 65169695Skan#undef END 66169695Skan#undef TRIGRAPH_MAP 67169695Skan 68169695Skan/* A set of booleans indicating what CPP features each source language 69169695Skan requires. */ 70169695Skanstruct lang_flags 71169695Skan{ 72169695Skan char c99; 73169695Skan char cplusplus; 74169695Skan char extended_numbers; 75169695Skan char extended_identifiers; 76169695Skan char std; 77169695Skan char cplusplus_comments; 78169695Skan char digraphs; 79169695Skan}; 80169695Skan 81169695Skanstatic const struct lang_flags lang_defaults[] = 82169695Skan{ /* c99 c++ xnum xid std // digr */ 83169695Skan /* GNUC89 */ { 0, 0, 1, 0, 0, 1, 1 }, 84169695Skan /* GNUC99 */ { 1, 0, 1, 0, 0, 1, 1 }, 85169695Skan /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0 }, 86169695Skan /* STDC94 */ { 0, 0, 0, 0, 1, 0, 1 }, 87169695Skan /* STDC99 */ { 1, 0, 1, 0, 1, 1, 1 }, 88169695Skan /* GNUCXX */ { 0, 1, 1, 0, 0, 1, 1 }, 89169695Skan /* CXX98 */ { 0, 1, 1, 0, 1, 1, 1 }, 90169695Skan /* ASM */ { 0, 0, 1, 0, 0, 1, 0 } 91169695Skan /* xid should be 1 for GNUC99, STDC99, GNUCXX and CXX98 when no 92169695Skan longer experimental (when all uses of identifiers in the compiler 93169695Skan have been audited for correct handling of extended 94169695Skan identifiers). */ 95169695Skan}; 96169695Skan 97169695Skan/* Sets internal flags correctly for a given language. */ 98169695Skanvoid 99169695Skancpp_set_lang (cpp_reader *pfile, enum c_lang lang) 100169695Skan{ 101169695Skan const struct lang_flags *l = &lang_defaults[(int) lang]; 102169695Skan 103169695Skan CPP_OPTION (pfile, lang) = lang; 104169695Skan 105169695Skan CPP_OPTION (pfile, c99) = l->c99; 106169695Skan CPP_OPTION (pfile, cplusplus) = l->cplusplus; 107169695Skan CPP_OPTION (pfile, extended_numbers) = l->extended_numbers; 108169695Skan CPP_OPTION (pfile, extended_identifiers) = l->extended_identifiers; 109169695Skan CPP_OPTION (pfile, std) = l->std; 110169695Skan CPP_OPTION (pfile, trigraphs) = l->std; 111169695Skan CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments; 112169695Skan CPP_OPTION (pfile, digraphs) = l->digraphs; 113169695Skan} 114169695Skan 115169695Skan/* Initialize library global state. */ 116169695Skanstatic void 117169695Skaninit_library (void) 118169695Skan{ 119169695Skan static int initialized = 0; 120169695Skan 121169695Skan if (! initialized) 122169695Skan { 123169695Skan initialized = 1; 124169695Skan 125169695Skan /* Set up the trigraph map. This doesn't need to do anything if 126169695Skan we were compiled with a compiler that supports C99 designated 127169695Skan initializers. */ 128169695Skan init_trigraph_map (); 129169695Skan 130169695Skan#ifdef ENABLE_NLS 131169695Skan (void) bindtextdomain (PACKAGE, LOCALEDIR); 132169695Skan#endif 133169695Skan } 134169695Skan} 135169695Skan 136169695Skan/* Initialize a cpp_reader structure. */ 137169695Skancpp_reader * 138169695Skancpp_create_reader (enum c_lang lang, hash_table *table, 139169695Skan struct line_maps *line_table) 140169695Skan{ 141169695Skan cpp_reader *pfile; 142169695Skan 143169695Skan /* Initialize this instance of the library if it hasn't been already. */ 144169695Skan init_library (); 145169695Skan 146169695Skan pfile = XCNEW (cpp_reader); 147169695Skan 148169695Skan cpp_set_lang (pfile, lang); 149169695Skan CPP_OPTION (pfile, warn_multichar) = 1; 150169695Skan CPP_OPTION (pfile, discard_comments) = 1; 151169695Skan CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1; 152169695Skan CPP_OPTION (pfile, show_column) = 1; 153169695Skan CPP_OPTION (pfile, tabstop) = 8; 154169695Skan CPP_OPTION (pfile, operator_names) = 1; 155169695Skan CPP_OPTION (pfile, warn_trigraphs) = 2; 156169695Skan CPP_OPTION (pfile, warn_endif_labels) = 1; 157169695Skan CPP_OPTION (pfile, warn_deprecated) = 1; 158169695Skan CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99); 159169695Skan CPP_OPTION (pfile, dollars_in_ident) = 1; 160169695Skan CPP_OPTION (pfile, warn_dollars) = 1; 161169695Skan CPP_OPTION (pfile, warn_variadic_macros) = 1; 162169695Skan CPP_OPTION (pfile, warn_normalize) = normalized_C; 163169695Skan 164169695Skan /* Default CPP arithmetic to something sensible for the host for the 165169695Skan benefit of dumb users like fix-header. */ 166169695Skan CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long); 167169695Skan CPP_OPTION (pfile, char_precision) = CHAR_BIT; 168169695Skan CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int); 169169695Skan CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int); 170169695Skan CPP_OPTION (pfile, unsigned_char) = 0; 171169695Skan CPP_OPTION (pfile, unsigned_wchar) = 1; 172169695Skan CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */ 173169695Skan 174169695Skan /* Default to no charset conversion. */ 175169695Skan CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding (); 176169695Skan CPP_OPTION (pfile, wide_charset) = 0; 177169695Skan 178169695Skan /* Default the input character set to UTF-8. */ 179169695Skan CPP_OPTION (pfile, input_charset) = _cpp_default_encoding (); 180169695Skan 181169695Skan /* A fake empty "directory" used as the starting point for files 182169695Skan looked up without a search path. Name cannot be '/' because we 183169695Skan don't want to prepend anything at all to filenames using it. All 184169695Skan other entries are correct zero-initialized. */ 185169695Skan pfile->no_search_path.name = (char *) ""; 186169695Skan 187169695Skan /* Initialize the line map. */ 188169695Skan pfile->line_table = line_table; 189169695Skan 190169695Skan /* Initialize lexer state. */ 191169695Skan pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); 192169695Skan 193169695Skan /* Set up static tokens. */ 194169695Skan pfile->avoid_paste.type = CPP_PADDING; 195169695Skan pfile->avoid_paste.val.source = NULL; 196169695Skan pfile->eof.type = CPP_EOF; 197169695Skan pfile->eof.flags = 0; 198169695Skan 199169695Skan /* Create a token buffer for the lexer. */ 200169695Skan _cpp_init_tokenrun (&pfile->base_run, 250); 201169695Skan pfile->cur_run = &pfile->base_run; 202169695Skan pfile->cur_token = pfile->base_run.base; 203169695Skan 204169695Skan /* Initialize the base context. */ 205169695Skan pfile->context = &pfile->base_context; 206169695Skan pfile->base_context.macro = 0; 207169695Skan pfile->base_context.prev = pfile->base_context.next = 0; 208169695Skan 209169695Skan /* Aligned and unaligned storage. */ 210169695Skan pfile->a_buff = _cpp_get_buff (pfile, 0); 211169695Skan pfile->u_buff = _cpp_get_buff (pfile, 0); 212169695Skan 213169695Skan /* The expression parser stack. */ 214169695Skan _cpp_expand_op_stack (pfile); 215169695Skan 216169695Skan /* Initialize the buffer obstack. */ 217169695Skan _obstack_begin (&pfile->buffer_ob, 0, 0, 218169695Skan (void *(*) (long)) xmalloc, 219169695Skan (void (*) (void *)) free); 220169695Skan 221169695Skan _cpp_init_files (pfile); 222169695Skan 223169695Skan _cpp_init_hashtable (pfile, table); 224169695Skan 225169695Skan return pfile; 226169695Skan} 227169695Skan 228169695Skan/* Free resources used by PFILE. Accessing PFILE after this function 229169695Skan returns leads to undefined behavior. Returns the error count. */ 230169695Skanvoid 231169695Skancpp_destroy (cpp_reader *pfile) 232169695Skan{ 233169695Skan cpp_context *context, *contextn; 234169695Skan tokenrun *run, *runn; 235169695Skan 236169695Skan free (pfile->op_stack); 237169695Skan 238169695Skan while (CPP_BUFFER (pfile) != NULL) 239169695Skan _cpp_pop_buffer (pfile); 240169695Skan 241169695Skan if (pfile->out.base) 242169695Skan free (pfile->out.base); 243169695Skan 244169695Skan if (pfile->macro_buffer) 245169695Skan { 246169695Skan free (pfile->macro_buffer); 247169695Skan pfile->macro_buffer = NULL; 248169695Skan pfile->macro_buffer_len = 0; 249169695Skan } 250169695Skan 251169695Skan if (pfile->deps) 252169695Skan deps_free (pfile->deps); 253169695Skan obstack_free (&pfile->buffer_ob, 0); 254169695Skan 255169695Skan _cpp_destroy_hashtable (pfile); 256169695Skan _cpp_cleanup_files (pfile); 257169695Skan _cpp_destroy_iconv (pfile); 258169695Skan 259169695Skan _cpp_free_buff (pfile->a_buff); 260169695Skan _cpp_free_buff (pfile->u_buff); 261169695Skan _cpp_free_buff (pfile->free_buffs); 262169695Skan 263169695Skan for (run = &pfile->base_run; run; run = runn) 264169695Skan { 265169695Skan runn = run->next; 266169695Skan free (run->base); 267169695Skan if (run != &pfile->base_run) 268169695Skan free (run); 269169695Skan } 270169695Skan 271169695Skan for (context = pfile->base_context.next; context; context = contextn) 272169695Skan { 273169695Skan contextn = context->next; 274169695Skan free (context); 275169695Skan } 276169695Skan 277169695Skan free (pfile); 278169695Skan} 279169695Skan 280169695Skan/* This structure defines one built-in identifier. A node will be 281169695Skan entered in the hash table under the name NAME, with value VALUE. 282169695Skan 283169695Skan There are two tables of these. builtin_array holds all the 284169695Skan "builtin" macros: these are handled by builtin_macro() in 285169695Skan macro.c. Builtin is somewhat of a misnomer -- the property of 286169695Skan interest is that these macros require special code to compute their 287169695Skan expansions. The value is a "builtin_type" enumerator. 288169695Skan 289169695Skan operator_array holds the C++ named operators. These are keywords 290169695Skan which act as aliases for punctuators. In C++, they cannot be 291169695Skan altered through #define, and #if recognizes them as operators. In 292169695Skan C, these are not entered into the hash table at all (but see 293169695Skan <iso646.h>). The value is a token-type enumerator. */ 294169695Skanstruct builtin 295169695Skan{ 296169695Skan const uchar *name; 297169695Skan unsigned short len; 298169695Skan unsigned short value; 299169695Skan}; 300169695Skan 301169695Skan#define B(n, t) { DSC(n), t } 302169695Skanstatic const struct builtin builtin_array[] = 303169695Skan{ 304169695Skan B("__TIMESTAMP__", BT_TIMESTAMP), 305169695Skan B("__TIME__", BT_TIME), 306169695Skan B("__DATE__", BT_DATE), 307169695Skan B("__FILE__", BT_FILE), 308169695Skan B("__BASE_FILE__", BT_BASE_FILE), 309169695Skan B("__LINE__", BT_SPECLINE), 310169695Skan B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL), 311228474Sed B("__COUNTER__", BT_COUNTER), 312169695Skan /* Keep builtins not used for -traditional-cpp at the end, and 313169695Skan update init_builtins() if any more are added. */ 314169695Skan B("_Pragma", BT_PRAGMA), 315169695Skan B("__STDC__", BT_STDC), 316169695Skan}; 317169695Skan 318169695Skanstatic const struct builtin operator_array[] = 319169695Skan{ 320169695Skan B("and", CPP_AND_AND), 321169695Skan B("and_eq", CPP_AND_EQ), 322169695Skan B("bitand", CPP_AND), 323169695Skan B("bitor", CPP_OR), 324169695Skan B("compl", CPP_COMPL), 325169695Skan B("not", CPP_NOT), 326169695Skan B("not_eq", CPP_NOT_EQ), 327169695Skan B("or", CPP_OR_OR), 328169695Skan B("or_eq", CPP_OR_EQ), 329169695Skan B("xor", CPP_XOR), 330169695Skan B("xor_eq", CPP_XOR_EQ) 331169695Skan}; 332169695Skan#undef B 333169695Skan 334169695Skan/* Mark the C++ named operators in the hash table. */ 335169695Skanstatic void 336169695Skanmark_named_operators (cpp_reader *pfile) 337169695Skan{ 338169695Skan const struct builtin *b; 339169695Skan 340169695Skan for (b = operator_array; 341169695Skan b < (operator_array + ARRAY_SIZE (operator_array)); 342169695Skan b++) 343169695Skan { 344169695Skan cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len); 345169695Skan hp->flags |= NODE_OPERATOR; 346169695Skan hp->is_directive = 0; 347169695Skan hp->directive_index = b->value; 348169695Skan } 349169695Skan} 350169695Skan 351169695Skan/* Read the builtins table above and enter them, and language-specific 352169695Skan macros, into the hash table. HOSTED is true if this is a hosted 353169695Skan environment. */ 354169695Skanvoid 355169695Skancpp_init_builtins (cpp_reader *pfile, int hosted) 356169695Skan{ 357169695Skan const struct builtin *b; 358169695Skan size_t n = ARRAY_SIZE (builtin_array); 359169695Skan 360169695Skan if (CPP_OPTION (pfile, traditional)) 361169695Skan n -= 2; 362169695Skan else if (! CPP_OPTION (pfile, stdc_0_in_system_headers) 363169695Skan || CPP_OPTION (pfile, std)) 364169695Skan { 365169695Skan n--; 366169695Skan _cpp_define_builtin (pfile, "__STDC__ 1"); 367169695Skan } 368169695Skan 369169695Skan for (b = builtin_array; b < builtin_array + n; b++) 370169695Skan { 371169695Skan cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len); 372169695Skan hp->type = NT_MACRO; 373169695Skan hp->flags |= NODE_BUILTIN | NODE_WARN; 374169695Skan hp->value.builtin = (enum builtin_type) b->value; 375169695Skan } 376169695Skan 377169695Skan if (CPP_OPTION (pfile, cplusplus)) 378169695Skan _cpp_define_builtin (pfile, "__cplusplus 1"); 379169695Skan else if (CPP_OPTION (pfile, lang) == CLK_ASM) 380169695Skan _cpp_define_builtin (pfile, "__ASSEMBLER__ 1"); 381169695Skan else if (CPP_OPTION (pfile, lang) == CLK_STDC94) 382169695Skan _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L"); 383169695Skan else if (CPP_OPTION (pfile, c99)) 384169695Skan _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L"); 385169695Skan 386169695Skan if (hosted) 387169695Skan _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1"); 388169695Skan else 389169695Skan _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0"); 390169695Skan 391169695Skan if (CPP_OPTION (pfile, objc)) 392169695Skan _cpp_define_builtin (pfile, "__OBJC__ 1"); 393169695Skan} 394169695Skan 395169695Skan/* Sanity-checks are dependent on command-line options, so it is 396169695Skan called as a subroutine of cpp_read_main_file (). */ 397169695Skan#if ENABLE_CHECKING 398169695Skanstatic void sanity_checks (cpp_reader *); 399169695Skanstatic void sanity_checks (cpp_reader *pfile) 400169695Skan{ 401169695Skan cppchar_t test = 0; 402169695Skan size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part); 403169695Skan 404169695Skan /* Sanity checks for assumptions about CPP arithmetic and target 405169695Skan type precisions made by cpplib. */ 406169695Skan test--; 407169695Skan if (test < 1) 408169695Skan cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type"); 409169695Skan 410169695Skan if (CPP_OPTION (pfile, precision) > max_precision) 411169695Skan cpp_error (pfile, CPP_DL_ICE, 412169695Skan "preprocessor arithmetic has maximum precision of %lu bits;" 413169695Skan " target requires %lu bits", 414169695Skan (unsigned long) max_precision, 415169695Skan (unsigned long) CPP_OPTION (pfile, precision)); 416169695Skan 417169695Skan if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision)) 418169695Skan cpp_error (pfile, CPP_DL_ICE, 419169695Skan "CPP arithmetic must be at least as precise as a target int"); 420169695Skan 421169695Skan if (CPP_OPTION (pfile, char_precision) < 8) 422169695Skan cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide"); 423169695Skan 424169695Skan if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision)) 425169695Skan cpp_error (pfile, CPP_DL_ICE, 426169695Skan "target wchar_t is narrower than target char"); 427169695Skan 428169695Skan if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision)) 429169695Skan cpp_error (pfile, CPP_DL_ICE, 430169695Skan "target int is narrower than target char"); 431169695Skan 432169695Skan /* This is assumed in eval_token() and could be fixed if necessary. */ 433169695Skan if (sizeof (cppchar_t) > sizeof (cpp_num_part)) 434169695Skan cpp_error (pfile, CPP_DL_ICE, 435169695Skan "CPP half-integer narrower than CPP character"); 436169695Skan 437169695Skan if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T) 438169695Skan cpp_error (pfile, CPP_DL_ICE, 439169695Skan "CPP on this host cannot handle wide character constants over" 440169695Skan " %lu bits, but the target requires %lu bits", 441169695Skan (unsigned long) BITS_PER_CPPCHAR_T, 442169695Skan (unsigned long) CPP_OPTION (pfile, wchar_precision)); 443169695Skan} 444169695Skan#else 445169695Skan# define sanity_checks(PFILE) 446169695Skan#endif 447169695Skan 448169695Skan/* This is called after options have been parsed, and partially 449169695Skan processed. */ 450169695Skanvoid 451169695Skancpp_post_options (cpp_reader *pfile) 452169695Skan{ 453169695Skan sanity_checks (pfile); 454169695Skan 455169695Skan post_options (pfile); 456169695Skan 457169695Skan /* Mark named operators before handling command line macros. */ 458169695Skan if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names)) 459169695Skan mark_named_operators (pfile); 460169695Skan} 461169695Skan 462169695Skan/* Setup for processing input from the file named FNAME, or stdin if 463169695Skan it is the empty string. Return the original filename 464169695Skan on success (e.g. foo.i->foo.c), or NULL on failure. */ 465169695Skanconst char * 466169695Skancpp_read_main_file (cpp_reader *pfile, const char *fname) 467169695Skan{ 468169695Skan if (CPP_OPTION (pfile, deps.style) != DEPS_NONE) 469169695Skan { 470169695Skan if (!pfile->deps) 471169695Skan pfile->deps = deps_init (); 472169695Skan 473169695Skan /* Set the default target (if there is none already). */ 474169695Skan deps_add_default_target (pfile->deps, fname); 475169695Skan } 476169695Skan 477169695Skan pfile->main_file 478169695Skan = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0); 479169695Skan if (_cpp_find_failed (pfile->main_file)) 480169695Skan return NULL; 481169695Skan 482169695Skan _cpp_stack_file (pfile, pfile->main_file, false); 483169695Skan 484169695Skan /* For foo.i, read the original filename foo.c now, for the benefit 485169695Skan of the front ends. */ 486169695Skan if (CPP_OPTION (pfile, preprocessed)) 487169695Skan { 488169695Skan read_original_filename (pfile); 489169695Skan fname = pfile->line_table->maps[pfile->line_table->used-1].to_file; 490169695Skan } 491169695Skan return fname; 492169695Skan} 493169695Skan 494169695Skan/* For preprocessed files, if the first tokens are of the form # NUM. 495169695Skan handle the directive so we know the original file name. This will 496169695Skan generate file_change callbacks, which the front ends must handle 497169695Skan appropriately given their state of initialization. */ 498169695Skanstatic void 499169695Skanread_original_filename (cpp_reader *pfile) 500169695Skan{ 501169695Skan const cpp_token *token, *token1; 502169695Skan 503169695Skan /* Lex ahead; if the first tokens are of the form # NUM, then 504169695Skan process the directive, otherwise back up. */ 505169695Skan token = _cpp_lex_direct (pfile); 506169695Skan if (token->type == CPP_HASH) 507169695Skan { 508169695Skan pfile->state.in_directive = 1; 509169695Skan token1 = _cpp_lex_direct (pfile); 510169695Skan _cpp_backup_tokens (pfile, 1); 511169695Skan pfile->state.in_directive = 0; 512169695Skan 513169695Skan /* If it's a #line directive, handle it. */ 514169695Skan if (token1->type == CPP_NUMBER) 515169695Skan { 516169695Skan _cpp_handle_directive (pfile, token->flags & PREV_WHITE); 517169695Skan read_original_directory (pfile); 518169695Skan return; 519169695Skan } 520169695Skan } 521169695Skan 522169695Skan /* Backup as if nothing happened. */ 523169695Skan _cpp_backup_tokens (pfile, 1); 524169695Skan} 525169695Skan 526169695Skan/* For preprocessed files, if the tokens following the first filename 527169695Skan line is of the form # <line> "/path/name//", handle the 528169695Skan directive so we know the original current directory. */ 529169695Skanstatic void 530169695Skanread_original_directory (cpp_reader *pfile) 531169695Skan{ 532169695Skan const cpp_token *hash, *token; 533169695Skan 534169695Skan /* Lex ahead; if the first tokens are of the form # NUM, then 535169695Skan process the directive, otherwise back up. */ 536169695Skan hash = _cpp_lex_direct (pfile); 537169695Skan if (hash->type != CPP_HASH) 538169695Skan { 539169695Skan _cpp_backup_tokens (pfile, 1); 540169695Skan return; 541169695Skan } 542169695Skan 543169695Skan token = _cpp_lex_direct (pfile); 544169695Skan 545169695Skan if (token->type != CPP_NUMBER) 546169695Skan { 547169695Skan _cpp_backup_tokens (pfile, 2); 548169695Skan return; 549169695Skan } 550169695Skan 551169695Skan token = _cpp_lex_direct (pfile); 552169695Skan 553169695Skan if (token->type != CPP_STRING 554169695Skan || ! (token->val.str.len >= 5 555169695Skan && token->val.str.text[token->val.str.len-2] == '/' 556169695Skan && token->val.str.text[token->val.str.len-3] == '/')) 557169695Skan { 558169695Skan _cpp_backup_tokens (pfile, 3); 559169695Skan return; 560169695Skan } 561169695Skan 562169695Skan if (pfile->cb.dir_change) 563169695Skan { 564169695Skan char *debugdir = (char *) alloca (token->val.str.len - 3); 565169695Skan 566169695Skan memcpy (debugdir, (const char *) token->val.str.text + 1, 567169695Skan token->val.str.len - 4); 568169695Skan debugdir[token->val.str.len - 4] = '\0'; 569169695Skan 570169695Skan pfile->cb.dir_change (pfile, debugdir); 571169695Skan } 572169695Skan} 573169695Skan 574169695Skan/* This is called at the end of preprocessing. It pops the last 575169695Skan buffer and writes dependency output, and returns the number of 576169695Skan errors. 577169695Skan 578169695Skan Maybe it should also reset state, such that you could call 579169695Skan cpp_start_read with a new filename to restart processing. */ 580169695Skanint 581169695Skancpp_finish (cpp_reader *pfile, FILE *deps_stream) 582169695Skan{ 583169695Skan /* Warn about unused macros before popping the final buffer. */ 584169695Skan if (CPP_OPTION (pfile, warn_unused_macros)) 585169695Skan cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL); 586169695Skan 587169695Skan /* lex.c leaves the final buffer on the stack. This it so that 588169695Skan it returns an unending stream of CPP_EOFs to the client. If we 589169695Skan popped the buffer, we'd dereference a NULL buffer pointer and 590169695Skan segfault. It's nice to allow the client to do worry-free excess 591169695Skan cpp_get_token calls. */ 592169695Skan while (pfile->buffer) 593169695Skan _cpp_pop_buffer (pfile); 594169695Skan 595169695Skan /* Don't write the deps file if there are errors. */ 596169695Skan if (CPP_OPTION (pfile, deps.style) != DEPS_NONE 597169695Skan && deps_stream && pfile->errors == 0) 598169695Skan { 599169695Skan deps_write (pfile->deps, deps_stream, 72); 600169695Skan 601169695Skan if (CPP_OPTION (pfile, deps.phony_targets)) 602169695Skan deps_phony_targets (pfile->deps, deps_stream); 603169695Skan } 604169695Skan 605169695Skan /* Report on headers that could use multiple include guards. */ 606169695Skan if (CPP_OPTION (pfile, print_include_names)) 607169695Skan _cpp_report_missing_guards (pfile); 608169695Skan 609169695Skan return pfile->errors; 610169695Skan} 611169695Skan 612169695Skanstatic void 613169695Skanpost_options (cpp_reader *pfile) 614169695Skan{ 615169695Skan /* -Wtraditional is not useful in C++ mode. */ 616169695Skan if (CPP_OPTION (pfile, cplusplus)) 617169695Skan CPP_OPTION (pfile, warn_traditional) = 0; 618169695Skan 619169695Skan /* Permanently disable macro expansion if we are rescanning 620169695Skan preprocessed text. Read preprocesed source in ISO mode. */ 621169695Skan if (CPP_OPTION (pfile, preprocessed)) 622169695Skan { 623169695Skan pfile->state.prevent_expansion = 1; 624169695Skan CPP_OPTION (pfile, traditional) = 0; 625169695Skan } 626169695Skan 627169695Skan if (CPP_OPTION (pfile, warn_trigraphs) == 2) 628169695Skan CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs); 629169695Skan 630169695Skan if (CPP_OPTION (pfile, traditional)) 631169695Skan { 632169695Skan CPP_OPTION (pfile, cplusplus_comments) = 0; 633169695Skan 634169695Skan /* Traditional CPP does not accurately track column information. */ 635169695Skan CPP_OPTION (pfile, show_column) = 0; 636169695Skan CPP_OPTION (pfile, trigraphs) = 0; 637169695Skan CPP_OPTION (pfile, warn_trigraphs) = 0; 638169695Skan } 639169695Skan} 640