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