1 /* Part of CPP library. File handling. 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 4 Free Software Foundation, Inc. 5 Written by Per Bothner, 1994. 6 Based on CCCP program by Paul Rubin, June 1986 7 Adapted to ANSI C, Richard Stallman, Jan 1987 8 Split out of cpplib.c, Zack Weinberg, Oct 1998 9 Reimplemented, Neil Booth, Jul 2003 10 11 This program is free software; you can redistribute it and/or modify it 12 under the terms of the GNU General Public License as published by the 13 Free Software Foundation; either version 3, or (at your option) any 14 later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; see the file COPYING3. If not see 23 <http://www.gnu.org/licenses/>. */ 24 25 #include "config.h" 26 #include "system.h" 27 #include "cpplib.h" 28 #include "internal.h" 29 #include "mkdeps.h" 30 #include "obstack.h" 31 #include "hashtab.h" 32 #include "md5.h" 33 #include "../gcc/defaults.h" 34 #include <dirent.h> 35 36 /* Variable length record files on VMS will have a stat size that includes 37 record control characters that won't be included in the read size. */ 38 #ifdef VMS 39 # define FAB_C_VAR 2 /* variable length records (see Starlet fabdef.h) */ 40 # define STAT_SIZE_RELIABLE(ST) ((ST).st_fab_rfm != FAB_C_VAR) 41 #else 42 # define STAT_SIZE_RELIABLE(ST) true 43 #endif 44 45 #ifdef __DJGPP__ 46 #include <io.h> 47 /* For DJGPP redirected input is opened in text mode. */ 48 # define set_stdin_to_binary_mode() \ 49 if (! isatty (0)) setmode (0, O_BINARY) 50 #else 51 # define set_stdin_to_binary_mode() /* Nothing */ 52 #endif 53 54 /* This structure represents a file searched for by CPP, whether it 55 exists or not. An instance may be pointed to by more than one 56 file_hash_entry; at present no reference count is kept. */ 57 struct _cpp_file 58 { 59 /* Filename as given to #include or command line switch. */ 60 const char *name; 61 62 /* The full path used to find the file. */ 63 const char *path; 64 65 /* The full path of the pch file. */ 66 const char *pchname; 67 68 /* The file's path with the basename stripped. NULL if it hasn't 69 been calculated yet. */ 70 const char *dir_name; 71 72 /* Chain through all files. */ 73 struct _cpp_file *next_file; 74 75 /* The contents of NAME after calling read_file(). */ 76 const uchar *buffer; 77 78 /* Pointer to the real start of BUFFER. read_file() might increment 79 BUFFER; when freeing, this this pointer must be used instead. */ 80 const uchar *buffer_start; 81 82 /* The macro, if any, preventing re-inclusion. */ 83 const cpp_hashnode *cmacro; 84 85 /* The directory in the search path where FILE was found. Used for 86 #include_next and determining whether a header is a system 87 header. */ 88 cpp_dir *dir; 89 90 /* As filled in by stat(2) for the file. */ 91 struct stat st; 92 93 /* File descriptor. Invalid if -1, otherwise open. */ 94 int fd; 95 96 /* Zero if this file was successfully opened and stat()-ed, 97 otherwise errno obtained from failure. */ 98 int err_no; 99 100 /* Number of times the file has been stacked for preprocessing. */ 101 unsigned short stack_count; 102 103 /* If opened with #import or contains #pragma once. */ 104 bool once_only; 105 106 /* If read() failed before. */ 107 bool dont_read; 108 109 /* If this file is the main file. */ 110 bool main_file; 111 112 /* If BUFFER above contains the true contents of the file. */ 113 bool buffer_valid; 114 }; 115 116 /* A singly-linked list for all searches for a given file name, with 117 its head pointed to by a slot in FILE_HASH. The file name is what 118 appeared between the quotes in a #include directive; it can be 119 determined implicitly from the hash table location or explicitly 120 from FILE->name. 121 122 FILE is a structure containing details about the file that was 123 found with that search, or details of how the search failed. 124 125 START_DIR is the starting location of the search in the include 126 chain. The current directories for "" includes are also hashed in 127 the hash table and therefore unique. Files that are looked up 128 without using a search path, such as absolute filenames and file 129 names from the command line share a special starting directory so 130 they don't cause cache hits with normal include-chain lookups. 131 132 If START_DIR is NULL then the entry is for a directory, not a file, 133 and the directory is in DIR. Since the starting point in a file 134 lookup chain is never NULL, this means that simple pointer 135 comparisons against START_DIR can be made to determine cache hits 136 in file lookups. 137 138 If a cache lookup fails because of e.g. an extra "./" in the path, 139 then nothing will break. It is just less efficient as CPP will 140 have to do more work re-preprocessing the file, and/or comparing 141 its contents against earlier once-only files. 142 */ 143 struct file_hash_entry 144 { 145 struct file_hash_entry *next; 146 cpp_dir *start_dir; 147 source_location location; 148 union 149 { 150 _cpp_file *file; 151 cpp_dir *dir; 152 } u; 153 }; 154 155 /* Number of entries to put in a file_hash_entry pool. */ 156 #define FILE_HASH_POOL_SIZE 127 157 158 /* A file hash entry pool. We allocate file_hash_entry object from 159 one of these. */ 160 struct file_hash_entry_pool 161 { 162 /* Number of entries used from this pool. */ 163 unsigned int file_hash_entries_used; 164 /* Next pool in the chain; used when freeing. */ 165 struct file_hash_entry_pool *next; 166 /* The memory pool. */ 167 struct file_hash_entry pool[FILE_HASH_POOL_SIZE]; 168 }; 169 170 static bool open_file (_cpp_file *file); 171 static bool pch_open_file (cpp_reader *pfile, _cpp_file *file, 172 bool *invalid_pch); 173 static bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file, 174 bool *invalid_pch); 175 static bool read_file_guts (cpp_reader *pfile, _cpp_file *file); 176 static bool read_file (cpp_reader *pfile, _cpp_file *file); 177 static bool should_stack_file (cpp_reader *, _cpp_file *file, bool import); 178 static struct cpp_dir *search_path_head (cpp_reader *, const char *fname, 179 int angle_brackets, enum include_type); 180 static const char *dir_name_of_file (_cpp_file *file); 181 static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int); 182 static struct file_hash_entry *search_cache (struct file_hash_entry *head, 183 const cpp_dir *start_dir); 184 static _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname); 185 static void destroy_cpp_file (_cpp_file *); 186 static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp); 187 static void allocate_file_hash_entries (cpp_reader *pfile); 188 static struct file_hash_entry *new_file_hash_entry (cpp_reader *pfile); 189 static int report_missing_guard (void **slot, void *b); 190 static hashval_t file_hash_hash (const void *p); 191 static int file_hash_eq (const void *p, const void *q); 192 static char *read_filename_string (int ch, FILE *f); 193 static void read_name_map (cpp_dir *dir); 194 static char *remap_filename (cpp_reader *pfile, _cpp_file *file); 195 static char *append_file_to_dir (const char *fname, cpp_dir *dir); 196 static bool validate_pch (cpp_reader *, _cpp_file *file, const char *pchname); 197 static int pchf_save_compare (const void *e1, const void *e2); 198 static int pchf_compare (const void *d_p, const void *e_p); 199 static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool); 200 201 /* Given a filename in FILE->PATH, with the empty string interpreted 202 as <stdin>, open it. 203 204 On success FILE contains an open file descriptor and stat 205 information for the file. On failure the file descriptor is -1 and 206 the appropriate errno is also stored in FILE. Returns TRUE iff 207 successful. 208 209 We used to open files in nonblocking mode, but that caused more 210 problems than it solved. Do take care not to acquire a controlling 211 terminal by mistake (this can't happen on sane systems, but 212 paranoia is a virtue). 213 214 Use the three-argument form of open even though we aren't 215 specifying O_CREAT, to defend against broken system headers. 216 217 O_BINARY tells some runtime libraries (notably DJGPP) not to do 218 newline translation; we can handle DOS line breaks just fine 219 ourselves. */ 220 static bool 221 open_file (_cpp_file *file) 222 { 223 const char *cpp_restricted; 224 225 GET_ENVIRONMENT(cpp_restricted, "CPP_RESTRICTED"); 226 227 if (file->path[0] == '\0') 228 { 229 file->fd = 0; 230 set_stdin_to_binary_mode (); 231 } 232 else 233 file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY 234 | (cpp_restricted != NULL) ? O_NONBLOCK : 0, 0666); 235 236 237 if (file->fd != -1) 238 { 239 if (fstat (file->fd, &file->st) == 0) 240 { 241 if (!S_ISDIR (file->st.st_mode)) 242 if (cpp_restricted != NULL 243 ? S_ISREG (file->st.st_mode) : !S_ISDIR (file->st.st_mode)) 244 245 { 246 if (cpp_restricted) 247 fcntl(file->fd, F_SETFL, 248 fcntl(file->fd, F_GETFL, 0) & ~O_NONBLOCK); 249 file->err_no = 0; 250 return true; 251 } 252 253 /* Ignore a directory and continue the search. The file we're 254 looking for may be elsewhere in the search path. */ 255 errno = ENOENT; 256 } 257 258 close (file->fd); 259 file->fd = -1; 260 } 261 #if defined(_WIN32) && !defined(__CYGWIN__) 262 else if (errno == EACCES) 263 { 264 /* On most UNIX systems, open succeeds on a directory. Above, 265 we check if we have opened a directory and if so, set errno 266 to ENOENT. However, on Windows, opening a directory 267 fails with EACCES. We want to return ENOENT in that 268 case too. */ 269 if (stat (file->path, &file->st) == 0 270 && S_ISDIR (file->st.st_mode)) 271 errno = ENOENT; 272 else 273 /* The call to stat may have reset errno. */ 274 errno = EACCES; 275 } 276 #endif 277 else if (errno == ENOTDIR) 278 errno = ENOENT; 279 280 file->err_no = errno; 281 282 return false; 283 } 284 285 /* Temporary PCH intercept of opening a file. Try to find a PCH file 286 based on FILE->name and FILE->dir, and test those found for 287 validity using PFILE->cb.valid_pch. Return true iff a valid file is 288 found. Set *INVALID_PCH if a PCH file is found but wasn't valid. */ 289 290 static bool 291 pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch) 292 { 293 static const char extension[] = ".gch"; 294 const char *path = file->path; 295 size_t len, flen; 296 char *pchname; 297 struct stat st; 298 bool valid = false; 299 300 /* No PCH on <stdin> or if not requested. */ 301 if (file->name[0] == '\0' || !pfile->cb.valid_pch) 302 return false; 303 304 /* If the file is not included as first include from either the toplevel 305 file or the command-line it is not a valid use of PCH. */ 306 if (pfile->all_files 307 && pfile->all_files->next_file) 308 return false; 309 310 flen = strlen (path); 311 len = flen + sizeof (extension); 312 pchname = XNEWVEC (char, len); 313 memcpy (pchname, path, flen); 314 memcpy (pchname + flen, extension, sizeof (extension)); 315 316 if (stat (pchname, &st) == 0) 317 { 318 DIR *pchdir; 319 struct dirent *d; 320 size_t dlen, plen = len; 321 322 if (!S_ISDIR (st.st_mode)) 323 valid = validate_pch (pfile, file, pchname); 324 else if ((pchdir = opendir (pchname)) != NULL) 325 { 326 pchname[plen - 1] = '/'; 327 while ((d = readdir (pchdir)) != NULL) 328 { 329 dlen = strlen (d->d_name) + 1; 330 if ((strcmp (d->d_name, ".") == 0) 331 || (strcmp (d->d_name, "..") == 0)) 332 continue; 333 if (dlen + plen > len) 334 { 335 len += dlen + 64; 336 pchname = XRESIZEVEC (char, pchname, len); 337 } 338 memcpy (pchname + plen, d->d_name, dlen); 339 valid = validate_pch (pfile, file, pchname); 340 if (valid) 341 break; 342 } 343 closedir (pchdir); 344 } 345 if (!valid) 346 *invalid_pch = true; 347 } 348 349 if (valid) 350 file->pchname = pchname; 351 else 352 free (pchname); 353 354 return valid; 355 } 356 357 /* Try to open the path FILE->name appended to FILE->dir. This is 358 where remap and PCH intercept the file lookup process. Return true 359 if the file was found, whether or not the open was successful. 360 Set *INVALID_PCH to true if a PCH file is found but wasn't valid. */ 361 362 static bool 363 find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch) 364 { 365 char *path; 366 367 if (CPP_OPTION (pfile, remap) && (path = remap_filename (pfile, file))) 368 ; 369 else 370 if (file->dir->construct) 371 path = file->dir->construct (file->name, file->dir); 372 else 373 path = append_file_to_dir (file->name, file->dir); 374 375 if (path) 376 { 377 hashval_t hv = htab_hash_string (path); 378 char *copy; 379 void **pp; 380 381 if (htab_find_with_hash (pfile->nonexistent_file_hash, path, hv) != NULL) 382 { 383 file->err_no = ENOENT; 384 return false; 385 } 386 387 file->path = path; 388 if (pch_open_file (pfile, file, invalid_pch)) 389 return true; 390 391 if (open_file (file)) 392 return true; 393 394 if (file->err_no != ENOENT) 395 { 396 open_file_failed (pfile, file, 0); 397 return true; 398 } 399 400 /* We copy the path name onto an obstack partly so that we don't 401 leak the memory, but mostly so that we don't fragment the 402 heap. */ 403 copy = (char *) obstack_copy0 (&pfile->nonexistent_file_ob, path, 404 strlen (path)); 405 free (path); 406 pp = htab_find_slot_with_hash (pfile->nonexistent_file_hash, 407 copy, hv, INSERT); 408 *pp = copy; 409 410 file->path = file->name; 411 } 412 else 413 { 414 file->err_no = ENOENT; 415 file->path = NULL; 416 } 417 418 return false; 419 } 420 421 /* Return tue iff the missing_header callback found the given HEADER. */ 422 static bool 423 search_path_exhausted (cpp_reader *pfile, const char *header, _cpp_file *file) 424 { 425 missing_header_cb func = pfile->cb.missing_header; 426 427 /* When the regular search path doesn't work, try context dependent 428 headers search paths. */ 429 if (func 430 && file->dir == NULL) 431 { 432 if ((file->path = func (pfile, header, &file->dir)) != NULL) 433 { 434 if (open_file (file)) 435 return true; 436 free ((void *)file->path); 437 } 438 file->path = file->name; 439 } 440 441 return false; 442 } 443 444 bool 445 _cpp_find_failed (_cpp_file *file) 446 { 447 return file->err_no != 0; 448 } 449 450 /* Given a filename FNAME search for such a file in the include path 451 starting from START_DIR. If FNAME is the empty string it is 452 interpreted as STDIN if START_DIR is PFILE->no_search_path. 453 454 If the file is not found in the file cache fall back to the O/S and 455 add the result to our cache. 456 457 If the file was not found in the filesystem, or there was an error 458 opening it, then ERR_NO is nonzero and FD is -1. If the file was 459 found, then ERR_NO is zero and FD could be -1 or an open file 460 descriptor. FD can be -1 if the file was found in the cache and 461 had previously been closed. To open it again pass the return value 462 to open_file(). 463 */ 464 _cpp_file * 465 _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake, int angle_brackets) 466 { 467 struct file_hash_entry *entry, **hash_slot; 468 _cpp_file *file; 469 bool invalid_pch = false; 470 bool saw_bracket_include = false; 471 bool saw_quote_include = false; 472 struct cpp_dir *found_in_cache = NULL; 473 474 /* Ensure we get no confusion between cached files and directories. */ 475 if (start_dir == NULL) 476 cpp_error (pfile, CPP_DL_ICE, "NULL directory in find_file"); 477 478 hash_slot = (struct file_hash_entry **) 479 htab_find_slot_with_hash (pfile->file_hash, fname, 480 htab_hash_string (fname), 481 INSERT); 482 483 /* First check the cache before we resort to memory allocation. */ 484 entry = search_cache (*hash_slot, start_dir); 485 if (entry) 486 return entry->u.file; 487 488 file = make_cpp_file (pfile, start_dir, fname); 489 490 /* Try each path in the include chain. */ 491 for (; !fake ;) 492 { 493 if (find_file_in_dir (pfile, file, &invalid_pch)) 494 break; 495 496 file->dir = file->dir->next; 497 if (file->dir == NULL) 498 { 499 if (search_path_exhausted (pfile, fname, file)) 500 { 501 /* Although this file must not go in the cache, because 502 the file found might depend on things (like the current file) 503 that aren't represented in the cache, it still has to go in 504 the list of all files so that #import works. */ 505 file->next_file = pfile->all_files; 506 pfile->all_files = file; 507 return file; 508 } 509 510 if (invalid_pch) 511 { 512 cpp_error (pfile, CPP_DL_ERROR, 513 "one or more PCH files were found, but they were invalid"); 514 if (!cpp_get_options (pfile)->warn_invalid_pch) 515 cpp_error (pfile, CPP_DL_ERROR, 516 "use -Winvalid-pch for more information"); 517 } 518 open_file_failed (pfile, file, angle_brackets); 519 break; 520 } 521 522 /* Only check the cache for the starting location (done above) 523 and the quote and bracket chain heads because there are no 524 other possible starting points for searches. */ 525 if (file->dir == pfile->bracket_include) 526 saw_bracket_include = true; 527 else if (file->dir == pfile->quote_include) 528 saw_quote_include = true; 529 else 530 continue; 531 532 entry = search_cache (*hash_slot, file->dir); 533 if (entry) 534 { 535 found_in_cache = file->dir; 536 break; 537 } 538 } 539 540 if (entry) 541 { 542 /* Cache for START_DIR too, sharing the _cpp_file structure. */ 543 free ((char *) file->name); 544 free (file); 545 file = entry->u.file; 546 } 547 else 548 { 549 /* This is a new file; put it in the list. */ 550 file->next_file = pfile->all_files; 551 pfile->all_files = file; 552 } 553 554 /* Store this new result in the hash table. */ 555 entry = new_file_hash_entry (pfile); 556 entry->next = *hash_slot; 557 entry->start_dir = start_dir; 558 entry->location = pfile->line_table->highest_location; 559 entry->u.file = file; 560 *hash_slot = entry; 561 562 /* If we passed the quote or bracket chain heads, cache them also. 563 This speeds up processing if there are lots of -I options. */ 564 if (saw_bracket_include 565 && pfile->bracket_include != start_dir 566 && found_in_cache != pfile->bracket_include) 567 { 568 entry = new_file_hash_entry (pfile); 569 entry->next = *hash_slot; 570 entry->start_dir = pfile->bracket_include; 571 entry->location = pfile->line_table->highest_location; 572 entry->u.file = file; 573 *hash_slot = entry; 574 } 575 if (saw_quote_include 576 && pfile->quote_include != start_dir 577 && found_in_cache != pfile->quote_include) 578 { 579 entry = new_file_hash_entry (pfile); 580 entry->next = *hash_slot; 581 entry->start_dir = pfile->quote_include; 582 entry->location = pfile->line_table->highest_location; 583 entry->u.file = file; 584 *hash_slot = entry; 585 } 586 587 return file; 588 } 589 590 /* Read a file into FILE->buffer, returning true on success. 591 592 If FILE->fd is something weird, like a block device, we don't want 593 to read it at all. Don't even try to figure out what something is, 594 except for plain files and block devices, since there is no 595 reliable portable way of doing this. 596 597 FIXME: Flush file cache and try again if we run out of memory. */ 598 static bool 599 read_file_guts (cpp_reader *pfile, _cpp_file *file) 600 { 601 ssize_t size, total, count; 602 uchar *buf; 603 bool regular; 604 605 if (S_ISBLK (file->st.st_mode)) 606 { 607 cpp_error (pfile, CPP_DL_ERROR, "%s is a block device", file->path); 608 return false; 609 } 610 611 regular = S_ISREG (file->st.st_mode); 612 if (regular) 613 { 614 /* off_t might have a wider range than ssize_t - in other words, 615 the max size of a file might be bigger than the address 616 space. We can't handle a file that large. (Anyone with 617 a single source file bigger than 2GB needs to rethink 618 their coding style.) Some systems (e.g. AIX 4.1) define 619 SSIZE_MAX to be much smaller than the actual range of the 620 type. Use INTTYPE_MAXIMUM unconditionally to ensure this 621 does not bite us. */ 622 if (file->st.st_size > INTTYPE_MAXIMUM (ssize_t)) 623 { 624 cpp_error (pfile, CPP_DL_ERROR, "%s is too large", file->path); 625 return false; 626 } 627 628 size = file->st.st_size; 629 } 630 else 631 /* 8 kilobytes is a sensible starting size. It ought to be bigger 632 than the kernel pipe buffer, and it's definitely bigger than 633 the majority of C source files. */ 634 size = 8 * 1024; 635 636 buf = XNEWVEC (uchar, size + 1); 637 total = 0; 638 while ((count = read (file->fd, buf + total, size - total)) > 0) 639 { 640 total += count; 641 642 if (total == size) 643 { 644 if (regular) 645 break; 646 size *= 2; 647 buf = XRESIZEVEC (uchar, buf, size + 1); 648 } 649 } 650 651 if (count < 0) 652 { 653 cpp_errno (pfile, CPP_DL_ERROR, file->path); 654 return false; 655 } 656 657 if (regular && total != size && STAT_SIZE_RELIABLE (file->st)) 658 cpp_error (pfile, CPP_DL_WARNING, 659 "%s is shorter than expected", file->path); 660 661 file->buffer = _cpp_convert_input (pfile, 662 CPP_OPTION (pfile, input_charset), 663 buf, size, total, 664 &file->buffer_start, 665 &file->st.st_size); 666 file->buffer_valid = true; 667 668 return true; 669 } 670 671 /* Convenience wrapper around read_file_guts that opens the file if 672 necessary and closes the file descriptor after reading. FILE must 673 have been passed through find_file() at some stage. */ 674 static bool 675 read_file (cpp_reader *pfile, _cpp_file *file) 676 { 677 /* If we already have its contents in memory, succeed immediately. */ 678 if (file->buffer_valid) 679 return true; 680 681 /* If an earlier read failed for some reason don't try again. */ 682 if (file->dont_read || file->err_no) 683 return false; 684 685 if (file->fd == -1 && !open_file (file)) 686 { 687 open_file_failed (pfile, file, 0); 688 return false; 689 } 690 691 file->dont_read = !read_file_guts (pfile, file); 692 close (file->fd); 693 file->fd = -1; 694 695 return !file->dont_read; 696 } 697 698 /* Returns TRUE if FILE's contents have been successfully placed in 699 FILE->buffer and the file should be stacked, otherwise false. */ 700 static bool 701 should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) 702 { 703 _cpp_file *f; 704 705 /* Skip once-only files. */ 706 if (file->once_only) 707 return false; 708 709 /* We must mark the file once-only if #import now, before header 710 guard checks. Otherwise, undefining the header guard might 711 cause the file to be re-stacked. */ 712 if (import) 713 { 714 _cpp_mark_file_once_only (pfile, file); 715 716 /* Don't stack files that have been stacked before. */ 717 if (file->stack_count) 718 return false; 719 } 720 721 /* Skip if the file had a header guard and the macro is defined. 722 PCH relies on this appearing before the PCH handler below. */ 723 if (file->cmacro && file->cmacro->type == NT_MACRO) 724 return false; 725 726 /* Handle PCH files immediately; don't stack them. */ 727 if (file->pchname) 728 { 729 pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path); 730 file->fd = -1; 731 free ((void *) file->pchname); 732 file->pchname = NULL; 733 return false; 734 } 735 736 if (!read_file (pfile, file)) 737 return false; 738 739 /* Check the file against the PCH file. This is done before 740 checking against files we've already seen, since it may save on 741 I/O. */ 742 if (check_file_against_entries (pfile, file, import)) 743 { 744 /* If this isn't a #import, but yet we can't include the file, 745 that means that it was #import-ed in the PCH file, 746 so we can never include it again. */ 747 if (! import) 748 _cpp_mark_file_once_only (pfile, file); 749 return false; 750 } 751 752 /* Now we've read the file's contents, we can stack it if there 753 are no once-only files. */ 754 if (!pfile->seen_once_only) 755 return true; 756 757 /* We may have read the file under a different name. Look 758 for likely candidates and compare file contents to be sure. */ 759 for (f = pfile->all_files; f; f = f->next_file) 760 { 761 if (f == file) 762 continue; 763 764 if ((import || f->once_only) 765 && f->err_no == 0 766 && f->st.st_mtime == file->st.st_mtime 767 && f->st.st_size == file->st.st_size) 768 { 769 _cpp_file *ref_file; 770 bool same_file_p = false; 771 772 if (f->buffer && !f->buffer_valid) 773 { 774 /* We already have a buffer but it is not valid, because 775 the file is still stacked. Make a new one. */ 776 ref_file = make_cpp_file (pfile, f->dir, f->name); 777 ref_file->path = f->path; 778 } 779 else 780 /* The file is not stacked anymore. We can reuse it. */ 781 ref_file = f; 782 783 same_file_p = read_file (pfile, ref_file) 784 /* Size might have changed in read_file(). */ 785 && ref_file->st.st_size == file->st.st_size 786 && !memcmp (ref_file->buffer, 787 file->buffer, 788 file->st.st_size); 789 790 if (f->buffer && !f->buffer_valid) 791 { 792 ref_file->path = 0; 793 destroy_cpp_file (ref_file); 794 } 795 796 if (same_file_p) 797 break; 798 } 799 } 800 801 return f == NULL; 802 } 803 804 /* Place the file referenced by FILE into a new buffer on the buffer 805 stack if possible. IMPORT is true if this stacking attempt is 806 because of a #import directive. Returns true if a buffer is 807 stacked. */ 808 bool 809 _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) 810 { 811 cpp_buffer *buffer; 812 int sysp; 813 814 if (!should_stack_file (pfile, file, import)) 815 return false; 816 817 if (pfile->buffer == NULL || file->dir == NULL) 818 sysp = 0; 819 else 820 sysp = MAX (pfile->buffer->sysp, file->dir->sysp); 821 822 /* Add the file to the dependencies on its first inclusion. */ 823 if (CPP_OPTION (pfile, deps.style) > !!sysp && !file->stack_count) 824 { 825 if (!file->main_file || !CPP_OPTION (pfile, deps.ignore_main_file)) 826 deps_add_dep (pfile->deps, file->path); 827 } 828 829 /* Clear buffer_valid since _cpp_clean_line messes it up. */ 830 file->buffer_valid = false; 831 file->stack_count++; 832 833 /* Stack the buffer. */ 834 buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size, 835 CPP_OPTION (pfile, preprocessed) 836 && !CPP_OPTION (pfile, directives_only)); 837 buffer->file = file; 838 buffer->sysp = sysp; 839 840 /* Initialize controlling macro state. */ 841 pfile->mi_valid = true; 842 pfile->mi_cmacro = 0; 843 844 /* Generate the call back. */ 845 _cpp_do_file_change (pfile, LC_ENTER, file->path, 1, sysp); 846 847 return true; 848 } 849 850 /* Mark FILE to be included once only. */ 851 void 852 _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file) 853 { 854 pfile->seen_once_only = true; 855 file->once_only = true; 856 } 857 858 /* Return the directory from which searching for FNAME should start, 859 considering the directive TYPE and ANGLE_BRACKETS. If there is 860 nothing left in the path, returns NULL. */ 861 static struct cpp_dir * 862 search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, 863 enum include_type type) 864 { 865 cpp_dir *dir; 866 _cpp_file *file; 867 868 if (IS_ABSOLUTE_PATH (fname)) 869 return &pfile->no_search_path; 870 871 /* pfile->buffer is NULL when processing an -include command-line flag. */ 872 file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file; 873 874 /* For #include_next, skip in the search path past the dir in which 875 the current file was found, but if it was found via an absolute 876 path use the normal search logic. */ 877 if (type == IT_INCLUDE_NEXT && file->dir 878 && file->dir != &pfile->no_search_path) 879 dir = file->dir->next; 880 else if (angle_brackets) 881 dir = pfile->bracket_include; 882 else if (type == IT_CMDLINE) 883 /* -include and -imacros use the #include "" chain with the 884 preprocessor's cwd prepended. */ 885 return make_cpp_dir (pfile, "./", false); 886 else if (pfile->quote_ignores_source_dir) 887 dir = pfile->quote_include; 888 else 889 return make_cpp_dir (pfile, dir_name_of_file (file), 890 pfile->buffer ? pfile->buffer->sysp : 0); 891 892 if (dir == NULL) 893 cpp_error (pfile, CPP_DL_ERROR, 894 "no include path in which to search for %s", fname); 895 896 return dir; 897 } 898 899 /* Strip the basename from the file's path. It ends with a slash if 900 of nonzero length. Note that this procedure also works for 901 <stdin>, which is represented by the empty string. */ 902 static const char * 903 dir_name_of_file (_cpp_file *file) 904 { 905 if (!file->dir_name) 906 { 907 size_t len = lbasename (file->path) - file->path; 908 char *dir_name = XNEWVEC (char, len + 1); 909 910 memcpy (dir_name, file->path, len); 911 dir_name[len] = '\0'; 912 file->dir_name = dir_name; 913 } 914 915 return file->dir_name; 916 } 917 918 /* Handles #include-family directives (distinguished by TYPE), 919 including HEADER, and the command line -imacros and -include. 920 Returns true if a buffer was stacked. */ 921 bool 922 _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets, 923 enum include_type type) 924 { 925 struct cpp_dir *dir; 926 _cpp_file *file; 927 928 dir = search_path_head (pfile, fname, angle_brackets, type); 929 if (!dir) 930 return false; 931 932 file = _cpp_find_file (pfile, fname, dir, false, angle_brackets); 933 934 /* Compensate for the increment in linemap_add that occurs in 935 _cpp_stack_file. In the case of a normal #include, we're 936 currently at the start of the line *following* the #include. A 937 separate source_location for this location makes no sense (until 938 we do the LC_LEAVE), and complicates LAST_SOURCE_LINE_LOCATION. 939 This does not apply if we found a PCH file (in which case 940 linemap_add is not called) or we were included from the 941 command-line. */ 942 if (file->pchname == NULL && file->err_no == 0 && type != IT_CMDLINE) 943 pfile->line_table->highest_location--; 944 945 return _cpp_stack_file (pfile, file, type == IT_IMPORT); 946 } 947 948 /* Could not open FILE. The complication is dependency output. */ 949 static void 950 open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets) 951 { 952 int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0; 953 bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp); 954 955 errno = file->err_no; 956 if (print_dep && CPP_OPTION (pfile, deps.missing_files) && errno == ENOENT) 957 { 958 deps_add_dep (pfile->deps, file->name); 959 /* If the preprocessor output (other than dependency information) is 960 being used, we must also flag an error. */ 961 if (CPP_OPTION (pfile, deps.need_preprocessor_output)) 962 cpp_errno (pfile, CPP_DL_FATAL, file->path); 963 } 964 else 965 { 966 /* If we are not outputting dependencies, or if we are and dependencies 967 were requested for this file, or if preprocessor output is needed 968 in addition to dependency information, this is an error. 969 970 Otherwise (outputting dependencies but not for this file, and not 971 using the preprocessor output), we can still produce correct output 972 so it's only a warning. */ 973 if (CPP_OPTION (pfile, deps.style) == DEPS_NONE 974 || print_dep 975 || CPP_OPTION (pfile, deps.need_preprocessor_output)) 976 cpp_errno (pfile, CPP_DL_FATAL, file->path); 977 else 978 cpp_errno (pfile, CPP_DL_WARNING, file->path); 979 } 980 } 981 982 /* Search in the chain beginning at HEAD for a file whose search path 983 started at START_DIR != NULL. */ 984 static struct file_hash_entry * 985 search_cache (struct file_hash_entry *head, const cpp_dir *start_dir) 986 { 987 while (head && head->start_dir != start_dir) 988 head = head->next; 989 990 return head; 991 } 992 993 /* Allocate a new _cpp_file structure. */ 994 static _cpp_file * 995 make_cpp_file (cpp_reader *pfile, cpp_dir *dir, const char *fname) 996 { 997 _cpp_file *file; 998 999 file = XCNEW (_cpp_file); 1000 file->main_file = !pfile->buffer; 1001 file->fd = -1; 1002 file->dir = dir; 1003 file->name = xstrdup (fname); 1004 1005 return file; 1006 } 1007 1008 /* Release a _cpp_file structure. */ 1009 static void 1010 destroy_cpp_file (_cpp_file *file) 1011 { 1012 if (file->buffer_start) 1013 free ((void *) file->buffer_start); 1014 free ((void *) file->name); 1015 free (file); 1016 } 1017 1018 /* Release all the files allocated by this reader. */ 1019 static void 1020 destroy_all_cpp_files (cpp_reader *pfile) 1021 { 1022 _cpp_file *iter = pfile->all_files; 1023 while (iter) 1024 { 1025 _cpp_file *next = iter->next_file; 1026 destroy_cpp_file (iter); 1027 iter = next; 1028 } 1029 } 1030 1031 /* A hash of directory names. The directory names are the path names 1032 of files which contain a #include "", the included file name is 1033 appended to this directories. 1034 1035 To avoid duplicate entries we follow the convention that all 1036 non-empty directory names should end in a '/'. DIR_NAME must be 1037 stored in permanently allocated memory. */ 1038 static cpp_dir * 1039 make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp) 1040 { 1041 struct file_hash_entry *entry, **hash_slot; 1042 cpp_dir *dir; 1043 1044 hash_slot = (struct file_hash_entry **) 1045 htab_find_slot_with_hash (pfile->dir_hash, dir_name, 1046 htab_hash_string (dir_name), 1047 INSERT); 1048 1049 /* Have we already hashed this directory? */ 1050 for (entry = *hash_slot; entry; entry = entry->next) 1051 if (entry->start_dir == NULL) 1052 return entry->u.dir; 1053 1054 dir = XCNEW (cpp_dir); 1055 dir->next = pfile->quote_include; 1056 dir->name = (char *) dir_name; 1057 dir->len = strlen (dir_name); 1058 dir->sysp = sysp; 1059 dir->construct = 0; 1060 1061 /* Store this new result in the hash table. */ 1062 entry = new_file_hash_entry (pfile); 1063 entry->next = *hash_slot; 1064 entry->start_dir = NULL; 1065 entry->location = pfile->line_table->highest_location; 1066 entry->u.dir = dir; 1067 *hash_slot = entry; 1068 1069 return dir; 1070 } 1071 1072 /* Create a new block of memory for file hash entries. */ 1073 static void 1074 allocate_file_hash_entries (cpp_reader *pfile) 1075 { 1076 struct file_hash_entry_pool *pool = XNEW (struct file_hash_entry_pool); 1077 pool->file_hash_entries_used = 0; 1078 pool->next = pfile->file_hash_entries; 1079 pfile->file_hash_entries = pool; 1080 } 1081 1082 /* Return a new file hash entry. */ 1083 static struct file_hash_entry * 1084 new_file_hash_entry (cpp_reader *pfile) 1085 { 1086 unsigned int idx; 1087 if (pfile->file_hash_entries->file_hash_entries_used == FILE_HASH_POOL_SIZE) 1088 allocate_file_hash_entries (pfile); 1089 1090 idx = pfile->file_hash_entries->file_hash_entries_used++; 1091 return &pfile->file_hash_entries->pool[idx]; 1092 } 1093 1094 /* Free the file hash entry pools. */ 1095 static void 1096 free_file_hash_entries (cpp_reader *pfile) 1097 { 1098 struct file_hash_entry_pool *iter = pfile->file_hash_entries; 1099 while (iter) 1100 { 1101 struct file_hash_entry_pool *next = iter->next; 1102 free (iter); 1103 iter = next; 1104 } 1105 } 1106 1107 /* Returns TRUE if a file FNAME has ever been successfully opened. 1108 This routine is not intended to correctly handle filenames aliased 1109 by links or redundant . or .. traversals etc. */ 1110 bool 1111 cpp_included (cpp_reader *pfile, const char *fname) 1112 { 1113 struct file_hash_entry *entry; 1114 1115 entry = (struct file_hash_entry *) 1116 htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname)); 1117 1118 while (entry && (entry->start_dir == NULL || entry->u.file->err_no)) 1119 entry = entry->next; 1120 1121 return entry != NULL; 1122 } 1123 1124 /* Returns TRUE if a file FNAME has ever been successfully opened 1125 before LOCATION. This routine is not intended to correctly handle 1126 filenames aliased by links or redundant . or .. traversals etc. */ 1127 bool 1128 cpp_included_before (cpp_reader *pfile, const char *fname, 1129 source_location location) 1130 { 1131 struct file_hash_entry *entry; 1132 1133 entry = (struct file_hash_entry *) 1134 htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname)); 1135 1136 while (entry && (entry->start_dir == NULL || entry->u.file->err_no 1137 || entry->location > location)) 1138 entry = entry->next; 1139 1140 return entry != NULL; 1141 } 1142 1143 /* Calculate the hash value of a file hash entry P. */ 1144 1145 static hashval_t 1146 file_hash_hash (const void *p) 1147 { 1148 struct file_hash_entry *entry = (struct file_hash_entry *) p; 1149 const char *hname; 1150 if (entry->start_dir) 1151 hname = entry->u.file->name; 1152 else 1153 hname = entry->u.dir->name; 1154 1155 return htab_hash_string (hname); 1156 } 1157 1158 /* Compare a string Q against a file hash entry P. */ 1159 static int 1160 file_hash_eq (const void *p, const void *q) 1161 { 1162 struct file_hash_entry *entry = (struct file_hash_entry *) p; 1163 const char *fname = (const char *) q; 1164 const char *hname; 1165 1166 if (entry->start_dir) 1167 hname = entry->u.file->name; 1168 else 1169 hname = entry->u.dir->name; 1170 1171 return strcmp (hname, fname) == 0; 1172 } 1173 1174 /* Compare entries in the nonexistent file hash table. These are just 1175 strings. */ 1176 static int 1177 nonexistent_file_hash_eq (const void *p, const void *q) 1178 { 1179 return strcmp ((const char *) p, (const char *) q) == 0; 1180 } 1181 1182 /* Initialize everything in this source file. */ 1183 void 1184 _cpp_init_files (cpp_reader *pfile) 1185 { 1186 pfile->file_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq, 1187 NULL, xcalloc, free); 1188 pfile->dir_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq, 1189 NULL, xcalloc, free); 1190 allocate_file_hash_entries (pfile); 1191 pfile->nonexistent_file_hash = htab_create_alloc (127, htab_hash_string, 1192 nonexistent_file_hash_eq, 1193 NULL, xcalloc, free); 1194 _obstack_begin (&pfile->nonexistent_file_ob, 0, 0, 1195 (void *(*) (long)) xmalloc, 1196 (void (*) (void *)) free); 1197 } 1198 1199 /* Finalize everything in this source file. */ 1200 void 1201 _cpp_cleanup_files (cpp_reader *pfile) 1202 { 1203 htab_delete (pfile->file_hash); 1204 htab_delete (pfile->dir_hash); 1205 htab_delete (pfile->nonexistent_file_hash); 1206 obstack_free (&pfile->nonexistent_file_ob, 0); 1207 free_file_hash_entries (pfile); 1208 destroy_all_cpp_files (pfile); 1209 } 1210 1211 /* Make the parser forget about files it has seen. This can be useful 1212 for resetting the parser to start another run. */ 1213 void 1214 cpp_clear_file_cache (cpp_reader *pfile) 1215 { 1216 _cpp_cleanup_files (pfile); 1217 pfile->file_hash_entries = NULL; 1218 pfile->all_files = NULL; 1219 _cpp_init_files (pfile); 1220 } 1221 1222 /* Enter a file name in the hash for the sake of cpp_included. */ 1223 void 1224 _cpp_fake_include (cpp_reader *pfile, const char *fname) 1225 { 1226 _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0); 1227 } 1228 1229 /* Not everyone who wants to set system-header-ness on a buffer can 1230 see the details of a buffer. This is an exported interface because 1231 fix-header needs it. */ 1232 void 1233 cpp_make_system_header (cpp_reader *pfile, int syshdr, int externc) 1234 { 1235 int flags = 0; 1236 const struct line_maps *line_table = pfile->line_table; 1237 const struct line_map *map = &line_table->maps[line_table->used-1]; 1238 1239 /* 1 = system header, 2 = system header to be treated as C. */ 1240 if (syshdr) 1241 flags = 1 + (externc != 0); 1242 pfile->buffer->sysp = flags; 1243 _cpp_do_file_change (pfile, LC_RENAME, map->to_file, 1244 SOURCE_LINE (map, pfile->line_table->highest_line), flags); 1245 } 1246 1247 /* Allow the client to change the current file. Used by the front end 1248 to achieve pseudo-file names like <built-in>. 1249 If REASON is LC_LEAVE, then NEW_NAME must be NULL. */ 1250 void 1251 cpp_change_file (cpp_reader *pfile, enum lc_reason reason, 1252 const char *new_name) 1253 { 1254 _cpp_do_file_change (pfile, reason, new_name, 1, 0); 1255 } 1256 1257 struct report_missing_guard_data 1258 { 1259 const char **paths; 1260 size_t count; 1261 }; 1262 1263 /* Callback function for htab_traverse. */ 1264 static int 1265 report_missing_guard (void **slot, void *d) 1266 { 1267 struct file_hash_entry *entry = (struct file_hash_entry *) *slot; 1268 struct report_missing_guard_data *data 1269 = (struct report_missing_guard_data *) d; 1270 1271 /* Skip directories. */ 1272 if (entry->start_dir != NULL) 1273 { 1274 _cpp_file *file = entry->u.file; 1275 1276 /* We don't want MI guard advice for the main file. */ 1277 if (file->cmacro == NULL && file->stack_count == 1 && !file->main_file) 1278 { 1279 if (data->paths == NULL) 1280 { 1281 data->paths = XCNEWVEC (const char *, data->count); 1282 data->count = 0; 1283 } 1284 1285 data->paths[data->count++] = file->path; 1286 } 1287 } 1288 1289 /* Keep traversing the hash table. */ 1290 return 1; 1291 } 1292 1293 /* Comparison function for qsort. */ 1294 static int 1295 report_missing_guard_cmp (const void *p1, const void *p2) 1296 { 1297 return strcmp (*(const char *const *) p1, *(const char *const *) p2); 1298 } 1299 1300 /* Report on all files that might benefit from a multiple include guard. 1301 Triggered by -H. */ 1302 void 1303 _cpp_report_missing_guards (cpp_reader *pfile) 1304 { 1305 struct report_missing_guard_data data; 1306 1307 data.paths = NULL; 1308 data.count = htab_elements (pfile->file_hash); 1309 htab_traverse (pfile->file_hash, report_missing_guard, &data); 1310 1311 if (data.paths != NULL) 1312 { 1313 size_t i; 1314 1315 /* Sort the paths to avoid outputting them in hash table 1316 order. */ 1317 qsort (data.paths, data.count, sizeof (const char *), 1318 report_missing_guard_cmp); 1319 fputs (_("Multiple include guards may be useful for:\n"), 1320 stderr); 1321 for (i = 0; i < data.count; i++) 1322 { 1323 fputs (data.paths[i], stderr); 1324 putc ('\n', stderr); 1325 } 1326 free (data.paths); 1327 } 1328 } 1329 1330 /* Locate HEADER, and determine whether it is newer than the current 1331 file. If it cannot be located or dated, return -1, if it is 1332 newer, return 1, otherwise 0. */ 1333 int 1334 _cpp_compare_file_date (cpp_reader *pfile, const char *fname, 1335 int angle_brackets) 1336 { 1337 _cpp_file *file; 1338 struct cpp_dir *dir; 1339 1340 dir = search_path_head (pfile, fname, angle_brackets, IT_INCLUDE); 1341 if (!dir) 1342 return -1; 1343 1344 file = _cpp_find_file (pfile, fname, dir, false, angle_brackets); 1345 if (file->err_no) 1346 return -1; 1347 1348 if (file->fd != -1) 1349 { 1350 close (file->fd); 1351 file->fd = -1; 1352 } 1353 1354 return file->st.st_mtime > pfile->buffer->file->st.st_mtime; 1355 } 1356 1357 /* Pushes the given file onto the buffer stack. Returns nonzero if 1358 successful. */ 1359 bool 1360 cpp_push_include (cpp_reader *pfile, const char *fname) 1361 { 1362 return _cpp_stack_include (pfile, fname, false, IT_CMDLINE); 1363 } 1364 1365 /* Do appropriate cleanup when a file INC's buffer is popped off the 1366 input stack. */ 1367 void 1368 _cpp_pop_file_buffer (cpp_reader *pfile, _cpp_file *file) 1369 { 1370 /* Record the inclusion-preventing macro, which could be NULL 1371 meaning no controlling macro. */ 1372 if (pfile->mi_valid && file->cmacro == NULL) 1373 file->cmacro = pfile->mi_cmacro; 1374 1375 /* Invalidate control macros in the #including file. */ 1376 pfile->mi_valid = false; 1377 1378 if (file->buffer_start) 1379 { 1380 free ((void *) file->buffer_start); 1381 file->buffer_start = NULL; 1382 file->buffer = NULL; 1383 file->buffer_valid = false; 1384 } 1385 } 1386 1387 /* Inteface to file statistics record in _cpp_file structure. */ 1388 struct stat * 1389 _cpp_get_file_stat (_cpp_file *file) 1390 { 1391 return &file->st; 1392 } 1393 1394 /* Set the include chain for "" to QUOTE, for <> to BRACKET. If 1395 QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the 1396 directory of the including file. 1397 1398 If BRACKET does not lie in the QUOTE chain, it is set to QUOTE. */ 1399 void 1400 cpp_set_include_chains (cpp_reader *pfile, cpp_dir *quote, cpp_dir *bracket, 1401 int quote_ignores_source_dir) 1402 { 1403 pfile->quote_include = quote; 1404 pfile->bracket_include = quote; 1405 pfile->quote_ignores_source_dir = quote_ignores_source_dir; 1406 1407 for (; quote; quote = quote->next) 1408 { 1409 quote->name_map = NULL; 1410 quote->len = strlen (quote->name); 1411 if (quote == bracket) 1412 pfile->bracket_include = bracket; 1413 } 1414 } 1415 1416 /* Append the file name to the directory to create the path, but don't 1417 turn / into // or // into ///; // may be a namespace escape. */ 1418 static char * 1419 append_file_to_dir (const char *fname, cpp_dir *dir) 1420 { 1421 size_t dlen, flen; 1422 char *path; 1423 1424 dlen = dir->len; 1425 flen = strlen (fname); 1426 path = XNEWVEC (char, dlen + 1 + flen + 1); 1427 memcpy (path, dir->name, dlen); 1428 if (dlen && path[dlen - 1] != '/') 1429 path[dlen++] = '/'; 1430 memcpy (&path[dlen], fname, flen + 1); 1431 1432 return path; 1433 } 1434 1435 /* Read a space delimited string of unlimited length from a stdio 1436 file F. */ 1437 static char * 1438 read_filename_string (int ch, FILE *f) 1439 { 1440 char *alloc, *set; 1441 int len; 1442 1443 len = 20; 1444 set = alloc = XNEWVEC (char, len + 1); 1445 if (! is_space (ch)) 1446 { 1447 *set++ = ch; 1448 while ((ch = getc (f)) != EOF && ! is_space (ch)) 1449 { 1450 if (set - alloc == len) 1451 { 1452 len *= 2; 1453 alloc = XRESIZEVEC (char, alloc, len + 1); 1454 set = alloc + len / 2; 1455 } 1456 *set++ = ch; 1457 } 1458 } 1459 *set = '\0'; 1460 ungetc (ch, f); 1461 return alloc; 1462 } 1463 1464 /* Read the file name map file for DIR. */ 1465 static void 1466 read_name_map (cpp_dir *dir) 1467 { 1468 static const char FILE_NAME_MAP_FILE[] = "header.gcc"; 1469 char *name; 1470 FILE *f; 1471 size_t len, count = 0, room = 9; 1472 1473 len = dir->len; 1474 name = (char *) alloca (len + sizeof (FILE_NAME_MAP_FILE) + 1); 1475 memcpy (name, dir->name, len); 1476 if (len && name[len - 1] != '/') 1477 name[len++] = '/'; 1478 strcpy (name + len, FILE_NAME_MAP_FILE); 1479 f = fopen (name, "r"); 1480 1481 dir->name_map = XNEWVEC (const char *, room); 1482 1483 /* Silently return NULL if we cannot open. */ 1484 if (f) 1485 { 1486 int ch; 1487 1488 while ((ch = getc (f)) != EOF) 1489 { 1490 char *to; 1491 1492 if (is_space (ch)) 1493 continue; 1494 1495 if (count + 2 > room) 1496 { 1497 room += 8; 1498 dir->name_map = XRESIZEVEC (const char *, dir->name_map, room); 1499 } 1500 1501 dir->name_map[count] = read_filename_string (ch, f); 1502 while ((ch = getc (f)) != EOF && is_hspace (ch)) 1503 ; 1504 1505 to = read_filename_string (ch, f); 1506 if (IS_ABSOLUTE_PATH (to)) 1507 dir->name_map[count + 1] = to; 1508 else 1509 { 1510 dir->name_map[count + 1] = append_file_to_dir (to, dir); 1511 free (to); 1512 } 1513 1514 count += 2; 1515 while ((ch = getc (f)) != '\n') 1516 if (ch == EOF) 1517 break; 1518 } 1519 1520 fclose (f); 1521 } 1522 1523 /* Terminate the list of maps. */ 1524 dir->name_map[count] = NULL; 1525 } 1526 1527 /* Remap a FILE's name based on the file_name_map, if any, for 1528 FILE->dir. If the file name has any directory separators, 1529 recursively check those directories too. */ 1530 static char * 1531 remap_filename (cpp_reader *pfile, _cpp_file *file) 1532 { 1533 const char *fname, *p; 1534 char *new_dir; 1535 cpp_dir *dir; 1536 size_t index, len; 1537 1538 dir = file->dir; 1539 fname = file->name; 1540 1541 for (;;) 1542 { 1543 if (!dir->name_map) 1544 read_name_map (dir); 1545 1546 for (index = 0; dir->name_map[index]; index += 2) 1547 if (!strcmp (dir->name_map[index], fname)) 1548 return xstrdup (dir->name_map[index + 1]); 1549 1550 p = strchr (fname, '/'); 1551 if (!p || p == fname) 1552 return NULL; 1553 1554 len = dir->len + (p - fname + 1); 1555 new_dir = XNEWVEC (char, len + 1); 1556 memcpy (new_dir, dir->name, dir->len); 1557 memcpy (new_dir + dir->len, fname, p - fname + 1); 1558 new_dir[len] = '\0'; 1559 1560 dir = make_cpp_dir (pfile, new_dir, dir->sysp); 1561 fname = p + 1; 1562 } 1563 } 1564 1565 /* Returns true if PCHNAME is a valid PCH file for FILE. */ 1566 static bool 1567 validate_pch (cpp_reader *pfile, _cpp_file *file, const char *pchname) 1568 { 1569 const char *saved_path = file->path; 1570 bool valid = false; 1571 1572 file->path = pchname; 1573 if (open_file (file)) 1574 { 1575 valid = 1 & pfile->cb.valid_pch (pfile, pchname, file->fd); 1576 1577 if (!valid) 1578 { 1579 close (file->fd); 1580 file->fd = -1; 1581 } 1582 1583 if (CPP_OPTION (pfile, print_include_names)) 1584 { 1585 unsigned int i; 1586 for (i = 1; i < pfile->line_table->depth; i++) 1587 putc ('.', stderr); 1588 fprintf (stderr, "%c %s\n", 1589 valid ? '!' : 'x', pchname); 1590 } 1591 } 1592 1593 file->path = saved_path; 1594 return valid; 1595 } 1596 1597 /* Get the path associated with the _cpp_file F. The path includes 1598 the base name from the include directive and the directory it was 1599 found in via the search path. */ 1600 1601 const char * 1602 cpp_get_path (struct _cpp_file *f) 1603 { 1604 return f->path; 1605 } 1606 1607 /* Get the directory associated with the _cpp_file F. */ 1608 1609 cpp_dir * 1610 cpp_get_dir (struct _cpp_file *f) 1611 { 1612 return f->dir; 1613 } 1614 1615 /* Get the cpp_buffer currently associated with the cpp_reader 1616 PFILE. */ 1617 1618 cpp_buffer * 1619 cpp_get_buffer (cpp_reader *pfile) 1620 { 1621 return pfile->buffer; 1622 } 1623 1624 /* Get the _cpp_file associated with the cpp_buffer B. */ 1625 1626 _cpp_file * 1627 cpp_get_file (cpp_buffer *b) 1628 { 1629 return b->file; 1630 } 1631 1632 /* Get the previous cpp_buffer given a cpp_buffer B. The previous 1633 buffer is the buffer that included the given buffer. */ 1634 1635 cpp_buffer * 1636 cpp_get_prev (cpp_buffer *b) 1637 { 1638 return b->prev; 1639 } 1640 1641 /* This data structure holds the list of header files that were seen 1642 while the PCH was being built. The 'entries' field is kept sorted 1643 in memcmp() order; yes, this means that on little-endian systems, 1644 it's sorted initially by the least-significant byte of 'size', but 1645 that's OK. The code does rely on having entries with the same size 1646 next to each other. */ 1647 1648 struct pchf_entry { 1649 /* The size of this file. This is used to save running a MD5 checksum 1650 if the sizes don't match. */ 1651 off_t size; 1652 /* The MD5 checksum of this file. */ 1653 unsigned char sum[16]; 1654 /* Is this file to be included only once? */ 1655 bool once_only; 1656 }; 1657 1658 struct pchf_data { 1659 /* Number of pchf_entry structures. */ 1660 size_t count; 1661 1662 /* Are there any values with once_only set? 1663 This is used as an optimisation, it means we don't have to search 1664 the structure if we're processing a regular #include. */ 1665 bool have_once_only; 1666 1667 struct pchf_entry entries[1]; 1668 }; 1669 1670 static struct pchf_data *pchf; 1671 1672 /* A qsort ordering function for pchf_entry structures. */ 1673 1674 static int 1675 pchf_save_compare (const void *e1, const void *e2) 1676 { 1677 return memcmp (e1, e2, sizeof (struct pchf_entry)); 1678 } 1679 1680 /* Create and write to F a pchf_data structure. */ 1681 1682 bool 1683 _cpp_save_file_entries (cpp_reader *pfile, FILE *fp) 1684 { 1685 size_t count = 0; 1686 struct pchf_data *result; 1687 size_t result_size; 1688 _cpp_file *f; 1689 1690 for (f = pfile->all_files; f; f = f->next_file) 1691 ++count; 1692 1693 result_size = (sizeof (struct pchf_data) 1694 + sizeof (struct pchf_entry) * (count - 1)); 1695 result = XCNEWVAR (struct pchf_data, result_size); 1696 1697 result->count = 0; 1698 result->have_once_only = false; 1699 1700 for (f = pfile->all_files; f; f = f->next_file) 1701 { 1702 size_t count; 1703 1704 /* This should probably never happen, since if a read error occurred 1705 the PCH file shouldn't be written... */ 1706 if (f->dont_read || f->err_no) 1707 continue; 1708 1709 if (f->stack_count == 0) 1710 continue; 1711 1712 count = result->count++; 1713 1714 result->entries[count].once_only = f->once_only; 1715 /* |= is avoided in the next line because of an HP C compiler bug */ 1716 result->have_once_only = result->have_once_only | f->once_only; 1717 if (f->buffer_valid) 1718 md5_buffer ((const char *)f->buffer, 1719 f->st.st_size, result->entries[count].sum); 1720 else 1721 { 1722 FILE *ff; 1723 int oldfd = f->fd; 1724 1725 if (!open_file (f)) 1726 { 1727 open_file_failed (pfile, f, 0); 1728 return false; 1729 } 1730 ff = fdopen (f->fd, "rb"); 1731 md5_stream (ff, result->entries[count].sum); 1732 fclose (ff); 1733 f->fd = oldfd; 1734 } 1735 result->entries[count].size = f->st.st_size; 1736 } 1737 1738 result_size = (sizeof (struct pchf_data) 1739 + sizeof (struct pchf_entry) * (result->count - 1)); 1740 1741 qsort (result->entries, result->count, sizeof (struct pchf_entry), 1742 pchf_save_compare); 1743 1744 return fwrite (result, result_size, 1, fp) == 1; 1745 } 1746 1747 /* Read the pchf_data structure from F. */ 1748 1749 bool 1750 _cpp_read_file_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f) 1751 { 1752 struct pchf_data d; 1753 1754 if (fread (&d, sizeof (struct pchf_data) - sizeof (struct pchf_entry), 1, f) 1755 != 1) 1756 return false; 1757 1758 pchf = XNEWVAR (struct pchf_data, sizeof (struct pchf_data) 1759 + sizeof (struct pchf_entry) * (d.count - 1)); 1760 memcpy (pchf, &d, sizeof (struct pchf_data) - sizeof (struct pchf_entry)); 1761 if (fread (pchf->entries, sizeof (struct pchf_entry), d.count, f) 1762 != d.count) 1763 return false; 1764 return true; 1765 } 1766 1767 /* The parameters for pchf_compare. */ 1768 1769 struct pchf_compare_data 1770 { 1771 /* The size of the file we're looking for. */ 1772 off_t size; 1773 1774 /* The MD5 checksum of the file, if it's been computed. */ 1775 unsigned char sum[16]; 1776 1777 /* Is SUM valid? */ 1778 bool sum_computed; 1779 1780 /* Do we need to worry about entries that don't have ONCE_ONLY set? */ 1781 bool check_included; 1782 1783 /* The file that we're searching for. */ 1784 _cpp_file *f; 1785 }; 1786 1787 /* bsearch comparison function; look for D_P in E_P. */ 1788 1789 static int 1790 pchf_compare (const void *d_p, const void *e_p) 1791 { 1792 const struct pchf_entry *e = (const struct pchf_entry *)e_p; 1793 struct pchf_compare_data *d = (struct pchf_compare_data *)d_p; 1794 int result; 1795 1796 result = memcmp (&d->size, &e->size, sizeof (off_t)); 1797 if (result != 0) 1798 return result; 1799 1800 if (! d->sum_computed) 1801 { 1802 _cpp_file *const f = d->f; 1803 1804 md5_buffer ((const char *)f->buffer, f->st.st_size, d->sum); 1805 d->sum_computed = true; 1806 } 1807 1808 result = memcmp (d->sum, e->sum, 16); 1809 if (result != 0) 1810 return result; 1811 1812 if (d->check_included || e->once_only) 1813 return 0; 1814 else 1815 return 1; 1816 } 1817 1818 /* Check that F is not in a list read from a PCH file (if any). 1819 Assumes that f->buffer_valid is true. Return TRUE if the file 1820 should not be read. */ 1821 1822 static bool 1823 check_file_against_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, 1824 _cpp_file *f, 1825 bool check_included) 1826 { 1827 struct pchf_compare_data d; 1828 1829 if (pchf == NULL 1830 || (! check_included && ! pchf->have_once_only)) 1831 return false; 1832 1833 d.size = f->st.st_size; 1834 d.sum_computed = false; 1835 d.f = f; 1836 d.check_included = check_included; 1837 return bsearch (&d, pchf->entries, pchf->count, sizeof (struct pchf_entry), 1838 pchf_compare) != NULL; 1839 } 1840