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