| 1 |
/* Part of CPP library. File handling. |
| 2 |
Copyright (C) 1986-2025 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 |
/* Size for #embed, perhaps smaller than st.st_size. */ |
| 91 |
size_t limit; |
| 92 |
|
| 93 |
/* Offset for #embed. */ |
| 94 |
off_t offset; |
| 95 |
|
| 96 |
/* File descriptor. Invalid if -1, otherwise open. */ |
| 97 |
int fd; |
| 98 |
|
| 99 |
/* Zero if this file was successfully opened and stat()-ed, |
| 100 |
otherwise errno obtained from failure. */ |
| 101 |
int err_no; |
| 102 |
|
| 103 |
/* Number of times the file has been stacked for preprocessing. */ |
| 104 |
unsigned short stack_count; |
| 105 |
|
| 106 |
/* If opened with #import or contains #pragma once. */ |
| 107 |
bool once_only : 1; |
| 108 |
|
| 109 |
/* If read() failed before. */ |
| 110 |
bool dont_read : 1; |
| 111 |
|
| 112 |
/* If BUFFER above contains the true contents of the file. */ |
| 113 |
bool buffer_valid : 1; |
| 114 |
|
| 115 |
/* If this file is implicitly preincluded. */ |
| 116 |
bool implicit_preinclude : 1; |
| 117 |
|
| 118 |
/* Set if a header wasn't found with __has_include or __has_include_next |
| 119 |
and error should be emitted if it is included normally. */ |
| 120 |
bool deferred_error : 1; |
| 121 |
|
| 122 |
/* File loaded from #embed. */ |
| 123 |
bool embed : 1; |
| 124 |
|
| 125 |
/* > 0: Known C++ Module header unit, <0: known not. ==0, unknown */ |
| 126 |
int header_unit : 2; |
| 127 |
}; |
| 128 |
|
| 129 |
/* A singly-linked list for all searches for a given file name, with |
| 130 |
its head pointed to by a slot in FILE_HASH. The file name is what |
| 131 |
appeared between the quotes in a #include directive; it can be |
| 132 |
determined implicitly from the hash table location or explicitly |
| 133 |
from FILE->name. |
| 134 |
|
| 135 |
FILE is a structure containing details about the file that was |
| 136 |
found with that search, or details of how the search failed. |
| 137 |
|
| 138 |
START_DIR is the starting location of the search in the include |
| 139 |
chain. The current directories for "" includes are also hashed in |
| 140 |
the hash table and therefore unique. Files that are looked up |
| 141 |
without using a search path, such as absolute filenames and file |
| 142 |
names from the command line share a special starting directory so |
| 143 |
they don't cause cache hits with normal include-chain lookups. |
| 144 |
|
| 145 |
If START_DIR is NULL then the entry is for a directory, not a file, |
| 146 |
and the directory is in DIR. Since the starting point in a file |
| 147 |
lookup chain is never NULL, this means that simple pointer |
| 148 |
comparisons against START_DIR can be made to determine cache hits |
| 149 |
in file lookups. |
| 150 |
|
| 151 |
If a cache lookup fails because of e.g. an extra "./" in the path, |
| 152 |
then nothing will break. It is just less efficient as CPP will |
| 153 |
have to do more work re-preprocessing the file, and/or comparing |
| 154 |
its contents against earlier once-only files. |
| 155 |
*/ |
| 156 |
struct cpp_file_hash_entry |
| 157 |
{ |
| 158 |
struct cpp_file_hash_entry *next; |
| 159 |
cpp_dir *start_dir; |
| 160 |
location_t location; |
| 161 |
union |
| 162 |
{ |
| 163 |
_cpp_file *file; |
| 164 |
cpp_dir *dir; |
| 165 |
} u; |
| 166 |
}; |
| 167 |
|
| 168 |
/* Number of entries to put in a cpp_file_hash_entry pool. */ |
| 169 |
#define FILE_HASH_POOL_SIZE 127 |
| 170 |
|
| 171 |
/* A file hash entry pool. We allocate cpp_file_hash_entry object from |
| 172 |
one of these. */ |
| 173 |
struct file_hash_entry_pool |
| 174 |
{ |
| 175 |
/* Number of entries used from this pool. */ |
| 176 |
unsigned int file_hash_entries_used; |
| 177 |
/* Next pool in the chain; used when freeing. */ |
| 178 |
struct file_hash_entry_pool *next; |
| 179 |
/* The memory pool. */ |
| 180 |
struct cpp_file_hash_entry pool[FILE_HASH_POOL_SIZE]; |
| 181 |
}; |
| 182 |
|
| 183 |
static bool open_file (_cpp_file *file); |
| 184 |
static bool pch_open_file (cpp_reader *pfile, _cpp_file *file, |
| 185 |
bool *invalid_pch); |
| 186 |
static bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file, |
| 187 |
bool *invalid_pch, location_t loc); |
| 188 |
static bool read_file_guts (cpp_reader *pfile, _cpp_file *file, |
| 189 |
location_t loc, const char *input_charset); |
| 190 |
static bool read_file (cpp_reader *pfile, _cpp_file *file, |
| 191 |
location_t loc); |
| 192 |
static const char *dir_name_of_file (_cpp_file *file); |
| 193 |
static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int, |
| 194 |
location_t); |
| 195 |
static struct cpp_file_hash_entry *search_cache (struct cpp_file_hash_entry *head, |
| 196 |
const cpp_dir *start_dir, |
| 197 |
bool is_embed); |
| 198 |
static _cpp_file *make_cpp_file (cpp_dir *, const char *fname); |
| 199 |
static void destroy_cpp_file (_cpp_file *); |
| 200 |
static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp); |
| 201 |
static void allocate_file_hash_entries (cpp_reader *pfile); |
| 202 |
static struct cpp_file_hash_entry *new_file_hash_entry (cpp_reader *pfile); |
| 203 |
static int report_missing_guard (void **slot, void *b); |
| 204 |
static hashval_t file_hash_hash (const void *p); |
| 205 |
static int file_hash_eq (const void *p, const void *q); |
| 206 |
static char *read_filename_string (int ch, FILE *f); |
| 207 |
static void read_name_map (cpp_dir *dir); |
| 208 |
static char *remap_filename (cpp_reader *pfile, _cpp_file *file); |
| 209 |
static char *append_file_to_dir (const char *fname, cpp_dir *dir); |
| 210 |
static bool validate_pch (cpp_reader *, _cpp_file *file, const char *pchname); |
| 211 |
static int pchf_save_compare (const void *e1, const void *e2); |
| 212 |
static int pchf_compare (const void *d_p, const void *e_p); |
| 213 |
static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool); |
| 214 |
|
| 215 |
/* Given a filename in FILE->PATH, with the empty string interpreted |
| 216 |
as <stdin>, open it. |
| 217 |
|
| 218 |
On success FILE contains an open file descriptor and stat |
| 219 |
information for the file. On failure the file descriptor is -1 and |
| 220 |
the appropriate errno is also stored in FILE. Returns TRUE iff |
| 221 |
successful. |
| 222 |
|
| 223 |
We used to open files in nonblocking mode, but that caused more |
| 224 |
problems than it solved. Do take care not to acquire a controlling |
| 225 |
terminal by mistake (this can't happen on sane systems, but |
| 226 |
paranoia is a virtue). |
| 227 |
|
| 228 |
Use the three-argument form of open even though we aren't |
| 229 |
specifying O_CREAT, to defend against broken system headers. |
| 230 |
|
| 231 |
O_BINARY tells some runtime libraries (notably DJGPP) not to do |
| 232 |
newline translation; we can handle DOS line breaks just fine |
| 233 |
ourselves. */ |
| 234 |
static bool |
| 235 |
open_file (_cpp_file *file) |
| 236 |
{ |
| 237 |
if (file->path[0] == '\0') |
| 238 |
{ |
| 239 |
file->fd = 0; |
| 240 |
set_stdin_to_binary_mode (); |
| 241 |
} |
| 242 |
else |
| 243 |
file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY, 0666); |
| 244 |
|
| 245 |
if (file->fd != -1) |
| 246 |
{ |
| 247 |
if (fstat (file->fd, &file->st) == 0) |
| 248 |
{ |
| 249 |
if (!S_ISDIR (file->st.st_mode)) |
| 250 |
{ |
| 251 |
file->err_no = 0; |
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
/* Ignore a directory and continue the search. The file we're |
| 256 |
looking for may be elsewhere in the search path. */ |
| 257 |
errno = ENOENT; |
| 258 |
} |
| 259 |
|
| 260 |
close (file->fd); |
| 261 |
file->fd = -1; |
| 262 |
} |
| 263 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 264 |
else if (errno == EACCES) |
| 265 |
{ |
| 266 |
/* On most UNIX systems, open succeeds on a directory. Above, |
| 267 |
we check if we have opened a directory and if so, set errno |
| 268 |
to ENOENT. However, on Windows, opening a directory |
| 269 |
fails with EACCES. We want to return ENOENT in that |
| 270 |
case too. */ |
| 271 |
if (stat (file->path, &file->st) == 0 |
| 272 |
&& S_ISDIR (file->st.st_mode)) |
| 273 |
errno = ENOENT; |
| 274 |
else |
| 275 |
/* The call to stat may have reset errno. */ |
| 276 |
errno = EACCES; |
| 277 |
} |
| 278 |
#endif |
| 279 |
else if (errno == ENOTDIR) |
| 280 |
errno = ENOENT; |
| 281 |
|
| 282 |
file->err_no = errno; |
| 283 |
|
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
/* Temporary PCH intercept of opening a file. Try to find a PCH file |
| 288 |
based on FILE->name and FILE->dir, and test those found for |
| 289 |
validity using PFILE->cb.valid_pch. Return true iff a valid file is |
| 290 |
found. Set *INVALID_PCH if a PCH file is found but wasn't valid. */ |
| 291 |
|
| 292 |
static bool |
| 293 |
pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch) |
| 294 |
{ |
| 295 |
static const char extension[] = ".gch"; |
| 296 |
const char *path = file->path; |
| 297 |
size_t len, flen; |
| 298 |
char *pchname; |
| 299 |
struct stat st; |
| 300 |
bool valid = false; |
| 301 |
|
| 302 |
/* No PCH on <stdin> or if not requested. */ |
| 303 |
if (file->name[0] == '\0' || !pfile->cb.valid_pch) |
| 304 |
return false; |
| 305 |
|
| 306 |
/* If the file is not included as first include from either the toplevel |
| 307 |
file or the command-line it is not a valid use of PCH. */ |
| 308 |
for (_cpp_file *f = pfile->all_files; f; f = f->next_file) |
| 309 |
if (f->implicit_preinclude) |
| 310 |
continue; |
| 311 |
else if (pfile->main_file == f) |
| 312 |
break; |
| 313 |
else |
| 314 |
return false; |
| 315 |
|
| 316 |
flen = strlen (path); |
| 317 |
len = flen + sizeof (extension); |
| 318 |
pchname = XNEWVEC (char, len); |
| 319 |
memcpy (pchname, path, flen); |
| 320 |
memcpy (pchname + flen, extension, sizeof (extension)); |
| 321 |
|
| 322 |
if (stat (pchname, &st) == 0) |
| 323 |
{ |
| 324 |
DIR *pchdir; |
| 325 |
struct dirent *d; |
| 326 |
size_t dlen, plen = len; |
| 327 |
|
| 328 |
if (!S_ISDIR (st.st_mode)) |
| 329 |
valid = validate_pch (pfile, file, pchname); |
| 330 |
else if ((pchdir = opendir (pchname)) != NULL) |
| 331 |
{ |
| 332 |
pchname[plen - 1] = '/'; |
| 333 |
while ((d = readdir (pchdir)) != NULL) |
| 334 |
{ |
| 335 |
dlen = strlen (d->d_name) + 1; |
| 336 |
if ((strcmp (d->d_name, ".") == 0) |
| 337 |
|| (strcmp (d->d_name, "..") == 0)) |
| 338 |
continue; |
| 339 |
if (dlen + plen > len) |
| 340 |
{ |
| 341 |
len += dlen + 64; |
| 342 |
pchname = XRESIZEVEC (char, pchname, len); |
| 343 |
} |
| 344 |
memcpy (pchname + plen, d->d_name, dlen); |
| 345 |
valid = validate_pch (pfile, file, pchname); |
| 346 |
if (valid) |
| 347 |
break; |
| 348 |
} |
| 349 |
closedir (pchdir); |
| 350 |
} |
| 351 |
if (!valid) |
| 352 |
*invalid_pch = true; |
| 353 |
} |
| 354 |
|
| 355 |
if (valid) |
| 356 |
file->pchname = pchname; |
| 357 |
else |
| 358 |
free (pchname); |
| 359 |
|
| 360 |
return valid; |
| 361 |
} |
| 362 |
|
| 363 |
/* Canonicalize the path to FILE. Return the canonical form if it is |
| 364 |
shorter, otherwise return NULL. This function does NOT free the |
| 365 |
memory pointed by FILE. */ |
| 366 |
|
| 367 |
static char * |
| 368 |
maybe_shorter_path (const char * file) |
| 369 |
{ |
| 370 |
char * file2 = lrealpath (file); |
| 371 |
if (file2 && strlen (file2) < strlen (file)) |
| 372 |
{ |
| 373 |
return file2; |
| 374 |
} |
| 375 |
else |
| 376 |
{ |
| 377 |
free (file2); |
| 378 |
return NULL; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/* Try to open the path FILE->name appended to FILE->dir. This is |
| 383 |
where remap and PCH intercept the file lookup process. Return true |
| 384 |
if the file was found, whether or not the open was successful. |
| 385 |
Set *INVALID_PCH to true if a PCH file is found but wasn't valid. |
| 386 |
Use LOC when emitting any diagnostics. */ |
| 387 |
|
| 388 |
static bool |
| 389 |
find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch, |
| 390 |
location_t loc) |
| 391 |
{ |
| 392 |
char *path; |
| 393 |
|
| 394 |
if (CPP_OPTION (pfile, remap) && (path = remap_filename (pfile, file))) |
| 395 |
; |
| 396 |
else |
| 397 |
if (file->dir->construct) |
| 398 |
path = file->dir->construct (file->name, file->dir); |
| 399 |
else |
| 400 |
path = append_file_to_dir (file->name, file->dir); |
| 401 |
|
| 402 |
if (path) |
| 403 |
{ |
| 404 |
hashval_t hv; |
| 405 |
char *copy; |
| 406 |
void **pp; |
| 407 |
|
| 408 |
/* We try to canonicalize system headers. For DOS based file |
| 409 |
* system, we always try to shorten non-system headers, as DOS |
| 410 |
* has a tighter constraint on max path length. */ |
| 411 |
if ((CPP_OPTION (pfile, canonical_system_headers) && file->dir->sysp) |
| 412 |
#ifdef HAVE_DOS_BASED_FILE_SYSTEM |
| 413 |
|| !file->dir->sysp |
| 414 |
#endif |
| 415 |
) |
| 416 |
{ |
| 417 |
char * canonical_path = maybe_shorter_path (path); |
| 418 |
if (canonical_path) |
| 419 |
{ |
| 420 |
/* The canonical path was newly allocated. Let's free the |
| 421 |
non-canonical one. */ |
| 422 |
free (path); |
| 423 |
path = canonical_path; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
hv = htab_hash_string (path); |
| 428 |
if (htab_find_with_hash (pfile->nonexistent_file_hash, path, hv) != NULL) |
| 429 |
{ |
| 430 |
file->err_no = ENOENT; |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
file->path = path; |
| 435 |
if (!file->embed && pch_open_file (pfile, file, invalid_pch)) |
| 436 |
return true; |
| 437 |
|
| 438 |
if (open_file (file)) |
| 439 |
return true; |
| 440 |
|
| 441 |
if (file->err_no != ENOENT) |
| 442 |
{ |
| 443 |
open_file_failed (pfile, file, 0, loc); |
| 444 |
return true; |
| 445 |
} |
| 446 |
|
| 447 |
/* We copy the path name onto an obstack partly so that we don't |
| 448 |
leak the memory, but mostly so that we don't fragment the |
| 449 |
heap. */ |
| 450 |
copy = (char *) obstack_copy0 (&pfile->nonexistent_file_ob, path, |
| 451 |
strlen (path)); |
| 452 |
free (path); |
| 453 |
pp = htab_find_slot_with_hash (pfile->nonexistent_file_hash, |
| 454 |
copy, hv, INSERT); |
| 455 |
*pp = copy; |
| 456 |
|
| 457 |
file->path = file->name; |
| 458 |
} |
| 459 |
else |
| 460 |
{ |
| 461 |
file->err_no = ENOENT; |
| 462 |
file->path = NULL; |
| 463 |
} |
| 464 |
|
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
/* Return true iff the missing_header callback found the given HEADER. */ |
| 469 |
static bool |
| 470 |
search_path_exhausted (cpp_reader *pfile, const char *header, _cpp_file *file) |
| 471 |
{ |
| 472 |
missing_header_cb func = pfile->cb.missing_header; |
| 473 |
|
| 474 |
/* When the regular search path doesn't work, try context dependent |
| 475 |
headers search paths. */ |
| 476 |
if (func |
| 477 |
&& file->dir == NULL) |
| 478 |
{ |
| 479 |
if ((file->path = func (pfile, header, &file->dir)) != NULL) |
| 480 |
{ |
| 481 |
if (open_file (file)) |
| 482 |
return true; |
| 483 |
free ((void *)file->path); |
| 484 |
} |
| 485 |
file->path = file->name; |
| 486 |
} |
| 487 |
|
| 488 |
return false; |
| 489 |
} |
| 490 |
|
| 491 |
bool |
| 492 |
_cpp_find_failed (_cpp_file *file) |
| 493 |
{ |
| 494 |
return file->err_no != 0; |
| 495 |
} |
| 496 |
|
| 497 |
/* Given a filename FNAME search for such a file in the include path |
| 498 |
starting from START_DIR. If FNAME is the empty string it is |
| 499 |
interpreted as STDIN if START_DIR is PFILE->no_search_path. |
| 500 |
|
| 501 |
If the file is not found in the file cache fall back to the O/S and |
| 502 |
add the result to our cache. |
| 503 |
|
| 504 |
If the file was not found in the filesystem, or there was an error |
| 505 |
opening it, then ERR_NO is nonzero and FD is -1. If the file was |
| 506 |
found, then ERR_NO is zero and FD could be -1 or an open file |
| 507 |
descriptor. FD can be -1 if the file was found in the cache and |
| 508 |
had previously been closed. To open it again pass the return value |
| 509 |
to open_file(). |
| 510 |
|
| 511 |
If KIND is _cpp_FFK_PRE_INCLUDE then it is OK for the file to be |
| 512 |
missing. If present, it is OK for a precompiled header to be |
| 513 |
included after it. |
| 514 |
|
| 515 |
Use LOC as the location for any errors. */ |
| 516 |
|
| 517 |
_cpp_file * |
| 518 |
_cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, |
| 519 |
int angle_brackets, _cpp_find_file_kind kind, location_t loc) |
| 520 |
{ |
| 521 |
bool invalid_pch = false; |
| 522 |
bool saw_bracket_include = false; |
| 523 |
bool saw_quote_include = false; |
| 524 |
bool saw_embed_include = false; |
| 525 |
struct cpp_dir *found_in_cache = NULL; |
| 526 |
bool is_embed = kind == _cpp_FFK_EMBED || kind == _cpp_FFK_HAS_EMBED; |
| 527 |
|
| 528 |
/* Ensure we get no confusion between cached files and directories. */ |
| 529 |
if (start_dir == NULL) |
| 530 |
cpp_error_at (pfile, CPP_DL_ICE, loc, "NULL directory in %<find_file%>"); |
| 531 |
|
| 532 |
void **hash_slot |
| 533 |
= htab_find_slot_with_hash (pfile->file_hash, fname, |
| 534 |
htab_hash_string (fname), INSERT); |
| 535 |
|
| 536 |
/* First check the cache before we resort to memory allocation. */ |
| 537 |
cpp_file_hash_entry *entry |
| 538 |
= search_cache ((struct cpp_file_hash_entry *) *hash_slot, start_dir, |
| 539 |
is_embed); |
| 540 |
if (entry) |
| 541 |
{ |
| 542 |
if (entry->u.file->deferred_error |
| 543 |
&& (kind == _cpp_FFK_NORMAL || kind == _cpp_FFK_EMBED)) |
| 544 |
{ |
| 545 |
open_file_failed (pfile, entry->u.file, angle_brackets, loc); |
| 546 |
entry->u.file->deferred_error = false; |
| 547 |
} |
| 548 |
return entry->u.file; |
| 549 |
} |
| 550 |
|
| 551 |
_cpp_file *file = make_cpp_file (start_dir, fname); |
| 552 |
file->implicit_preinclude |
| 553 |
= (kind == _cpp_FFK_PRE_INCLUDE |
| 554 |
|| (pfile->buffer && pfile->buffer->file->implicit_preinclude)); |
| 555 |
file->embed = is_embed; |
| 556 |
|
| 557 |
if (kind == _cpp_FFK_FAKE) |
| 558 |
file->dont_read = true; |
| 559 |
else |
| 560 |
/* Try each path in the include chain. */ |
| 561 |
for (;;) |
| 562 |
{ |
| 563 |
if (find_file_in_dir (pfile, file, &invalid_pch, loc)) |
| 564 |
break; |
| 565 |
|
| 566 |
if (is_embed |
| 567 |
&& file->dir == start_dir |
| 568 |
&& start_dir != pfile->embed_include |
| 569 |
&& start_dir != &pfile->no_search_path) |
| 570 |
file->dir = pfile->embed_include; |
| 571 |
else |
| 572 |
file->dir = file->dir->next; |
| 573 |
if (file->dir == NULL) |
| 574 |
{ |
| 575 |
if (!is_embed |
| 576 |
&& search_path_exhausted (pfile, fname, file)) |
| 577 |
{ |
| 578 |
/* Although this file must not go in the cache, |
| 579 |
because the file found might depend on things (like |
| 580 |
the current file) that aren't represented in the |
| 581 |
cache, it still has to go in the list of all files |
| 582 |
so that #import works. */ |
| 583 |
file->next_file = pfile->all_files; |
| 584 |
pfile->all_files = file; |
| 585 |
if (*hash_slot == NULL) |
| 586 |
{ |
| 587 |
/* If *hash_slot is NULL, the above |
| 588 |
htab_find_slot_with_hash call just created the |
| 589 |
slot, but we aren't going to store there anything |
| 590 |
of use, so need to remove the newly created entry. |
| 591 |
htab_clear_slot requires that it is non-NULL, so |
| 592 |
store some non-NULL but valid pointer there, |
| 593 |
htab_clear_slot will immediately overwrite it. */ |
| 594 |
*hash_slot = file; |
| 595 |
htab_clear_slot (pfile->file_hash, hash_slot); |
| 596 |
} |
| 597 |
return file; |
| 598 |
} |
| 599 |
|
| 600 |
if (invalid_pch) |
| 601 |
{ |
| 602 |
cpp_error (pfile, CPP_DL_ERROR, |
| 603 |
"one or more PCH files were found," |
| 604 |
" but they were invalid"); |
| 605 |
if (!cpp_get_options (pfile)->warn_invalid_pch) |
| 606 |
cpp_error (pfile, CPP_DL_NOTE, |
| 607 |
"use %<-Winvalid-pch%> for more information"); |
| 608 |
} |
| 609 |
|
| 610 |
if (kind == _cpp_FFK_PRE_INCLUDE) |
| 611 |
{ |
| 612 |
free ((char *) file->name); |
| 613 |
free (file); |
| 614 |
if (*hash_slot == NULL) |
| 615 |
{ |
| 616 |
/* See comment on the above htab_clear_slot call. */ |
| 617 |
*hash_slot = &hash_slot; |
| 618 |
htab_clear_slot (pfile->file_hash, hash_slot); |
| 619 |
} |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
|
| 623 |
if (kind != _cpp_FFK_HAS_INCLUDE && kind != _cpp_FFK_HAS_EMBED) |
| 624 |
open_file_failed (pfile, file, angle_brackets, loc); |
| 625 |
else |
| 626 |
file->deferred_error = true; |
| 627 |
break; |
| 628 |
} |
| 629 |
|
| 630 |
/* Only check the cache for the starting location (done above) |
| 631 |
and the quote and bracket chain heads because there are no |
| 632 |
other possible starting points for searches. */ |
| 633 |
if (file->dir == pfile->bracket_include) |
| 634 |
saw_bracket_include = true; |
| 635 |
else if (file->dir == pfile->quote_include) |
| 636 |
saw_quote_include = true; |
| 637 |
else if (file->dir == pfile->embed_include) |
| 638 |
saw_embed_include = true; |
| 639 |
else |
| 640 |
continue; |
| 641 |
|
| 642 |
entry |
| 643 |
= search_cache ((struct cpp_file_hash_entry *) *hash_slot, |
| 644 |
file->dir, is_embed); |
| 645 |
if (entry) |
| 646 |
{ |
| 647 |
found_in_cache = file->dir; |
| 648 |
break; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if (entry) |
| 653 |
{ |
| 654 |
/* Cache for START_DIR too, sharing the _cpp_file structure. */ |
| 655 |
free ((char *) file->name); |
| 656 |
free (file); |
| 657 |
file = entry->u.file; |
| 658 |
} |
| 659 |
else |
| 660 |
{ |
| 661 |
/* This is a new file; put it in the list. */ |
| 662 |
file->next_file = pfile->all_files; |
| 663 |
pfile->all_files = file; |
| 664 |
} |
| 665 |
|
| 666 |
/* Store this new result in the hash table. */ |
| 667 |
entry = new_file_hash_entry (pfile); |
| 668 |
entry->next = (struct cpp_file_hash_entry *) *hash_slot; |
| 669 |
entry->start_dir = start_dir; |
| 670 |
entry->location = loc; |
| 671 |
entry->u.file = file; |
| 672 |
*hash_slot = (void *) entry; |
| 673 |
|
| 674 |
/* If we passed the quote or bracket chain heads, cache them also. |
| 675 |
This speeds up processing if there are lots of -I options. */ |
| 676 |
if (saw_bracket_include |
| 677 |
&& pfile->bracket_include != start_dir |
| 678 |
&& found_in_cache != pfile->bracket_include) |
| 679 |
{ |
| 680 |
entry = new_file_hash_entry (pfile); |
| 681 |
entry->next = (struct cpp_file_hash_entry *) *hash_slot; |
| 682 |
entry->start_dir = pfile->bracket_include; |
| 683 |
entry->location = loc; |
| 684 |
entry->u.file = file; |
| 685 |
*hash_slot = (void *) entry; |
| 686 |
} |
| 687 |
if (saw_quote_include |
| 688 |
&& pfile->quote_include != start_dir |
| 689 |
&& found_in_cache != pfile->quote_include) |
| 690 |
{ |
| 691 |
entry = new_file_hash_entry (pfile); |
| 692 |
entry->next = (struct cpp_file_hash_entry *) *hash_slot; |
| 693 |
entry->start_dir = pfile->quote_include; |
| 694 |
entry->location = loc; |
| 695 |
entry->u.file = file; |
| 696 |
*hash_slot = (void *) entry; |
| 697 |
} |
| 698 |
if (saw_embed_include |
| 699 |
&& pfile->embed_include != start_dir |
| 700 |
&& found_in_cache != pfile->embed_include) |
| 701 |
{ |
| 702 |
entry = new_file_hash_entry (pfile); |
| 703 |
entry->next = (struct cpp_file_hash_entry *) *hash_slot; |
| 704 |
entry->start_dir = pfile->embed_include; |
| 705 |
entry->location = loc; |
| 706 |
entry->u.file = file; |
| 707 |
*hash_slot = (void *) entry; |
| 708 |
} |
| 709 |
|
| 710 |
return file; |
| 711 |
} |
| 712 |
|
| 713 |
/* Read a file into FILE->buffer, returning true on success. |
| 714 |
|
| 715 |
If FILE->fd is something weird, like a block device, we don't want |
| 716 |
to read it at all. Don't even try to figure out what something is, |
| 717 |
except for plain files and block devices, since there is no |
| 718 |
reliable portable way of doing this. |
| 719 |
|
| 720 |
Use LOC for any diagnostics. |
| 721 |
|
| 722 |
PFILE may be NULL. In this case, no diagnostics are issued. |
| 723 |
|
| 724 |
FIXME: Flush file cache and try again if we run out of memory. */ |
| 725 |
static bool |
| 726 |
read_file_guts (cpp_reader *pfile, _cpp_file *file, location_t loc, |
| 727 |
const char *input_charset) |
| 728 |
{ |
| 729 |
ssize_t size, pad, total, count; |
| 730 |
uchar *buf; |
| 731 |
bool regular; |
| 732 |
|
| 733 |
if (S_ISBLK (file->st.st_mode)) |
| 734 |
{ |
| 735 |
if (pfile) |
| 736 |
cpp_error_at (pfile, CPP_DL_ERROR, loc, |
| 737 |
"%s is a block device", file->path); |
| 738 |
return false; |
| 739 |
} |
| 740 |
|
| 741 |
regular = S_ISREG (file->st.st_mode) != 0; |
| 742 |
if (regular) |
| 743 |
{ |
| 744 |
/* off_t might have a wider range than ssize_t - in other words, |
| 745 |
the max size of a file might be bigger than the address |
| 746 |
space. We can't handle a file that large. (Anyone with |
| 747 |
a single source file bigger than 2GB needs to rethink |
| 748 |
their coding style.) Some systems (e.g. AIX 4.1) define |
| 749 |
SSIZE_MAX to be much smaller than the actual range of the |
| 750 |
type. Use INTTYPE_MAXIMUM unconditionally to ensure this |
| 751 |
does not bite us. */ |
| 752 |
if (file->st.st_size > INTTYPE_MAXIMUM (ssize_t)) |
| 753 |
{ |
| 754 |
if (pfile) |
| 755 |
cpp_error_at (pfile, CPP_DL_ERROR, loc, |
| 756 |
"%s is too large", file->path); |
| 757 |
return false; |
| 758 |
} |
| 759 |
|
| 760 |
size = file->st.st_size; |
| 761 |
} |
| 762 |
else |
| 763 |
/* 8 kilobytes is a sensible starting size. It ought to be bigger |
| 764 |
than the kernel pipe buffer, and it's definitely bigger than |
| 765 |
the majority of C source files. */ |
| 766 |
size = 8 * 1024; |
| 767 |
|
| 768 |
pad = CPP_BUFFER_PADDING; |
| 769 |
/* The '+ PAD' here is space for the final '\n' and PAD-1 bytes of padding, |
| 770 |
allowing search_line_fast to use (possibly misaligned) vector loads. */ |
| 771 |
buf = XNEWVEC (uchar, size + pad); |
| 772 |
total = 0; |
| 773 |
while ((count = read (file->fd, buf + total, size - total)) > 0) |
| 774 |
{ |
| 775 |
total += count; |
| 776 |
|
| 777 |
if (total == size) |
| 778 |
{ |
| 779 |
if (regular) |
| 780 |
break; |
| 781 |
size *= 2; |
| 782 |
buf = XRESIZEVEC (uchar, buf, size + pad); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
if (count < 0) |
| 787 |
{ |
| 788 |
if (pfile) |
| 789 |
cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, loc); |
| 790 |
free (buf); |
| 791 |
return false; |
| 792 |
} |
| 793 |
|
| 794 |
if (pfile && regular && total != size && STAT_SIZE_RELIABLE (file->st)) |
| 795 |
cpp_error_at (pfile, CPP_DL_WARNING, loc, |
| 796 |
"%s is shorter than expected", file->path); |
| 797 |
|
| 798 |
file->buffer = _cpp_convert_input (pfile, |
| 799 |
input_charset, |
| 800 |
buf, size + pad, total, |
| 801 |
&file->buffer_start, |
| 802 |
&file->st.st_size); |
| 803 |
file->buffer_valid = file->buffer; |
| 804 |
return file->buffer_valid; |
| 805 |
} |
| 806 |
|
| 807 |
/* Convenience wrapper around read_file_guts that opens the file if |
| 808 |
necessary and closes the file descriptor after reading. FILE must |
| 809 |
have been passed through find_file() at some stage. Use LOC for |
| 810 |
any diagnostics. Unlike read_file_guts(), PFILE may not be NULL. */ |
| 811 |
static bool |
| 812 |
read_file (cpp_reader *pfile, _cpp_file *file, location_t loc) |
| 813 |
{ |
| 814 |
/* If we already have its contents in memory, succeed immediately. */ |
| 815 |
if (file->buffer_valid) |
| 816 |
return true; |
| 817 |
|
| 818 |
/* If an earlier read failed for some reason don't try again. */ |
| 819 |
if (file->dont_read || file->err_no) |
| 820 |
return false; |
| 821 |
|
| 822 |
if (file->fd == -1 && !open_file (file)) |
| 823 |
{ |
| 824 |
open_file_failed (pfile, file, 0, loc); |
| 825 |
return false; |
| 826 |
} |
| 827 |
|
| 828 |
file->dont_read = !read_file_guts (pfile, file, loc, |
| 829 |
CPP_OPTION (pfile, input_charset)); |
| 830 |
close (file->fd); |
| 831 |
file->fd = -1; |
| 832 |
|
| 833 |
return !file->dont_read; |
| 834 |
} |
| 835 |
|
| 836 |
/* Returns TRUE if FILE is already known to be idempotent, and should |
| 837 |
therefore not be read again. */ |
| 838 |
static bool |
| 839 |
is_known_idempotent_file (cpp_reader *pfile, _cpp_file *file, bool import) |
| 840 |
{ |
| 841 |
/* Skip once-only files. */ |
| 842 |
if (file->once_only) |
| 843 |
return true; |
| 844 |
|
| 845 |
/* We must mark the file once-only if #import now, before header |
| 846 |
guard checks. Otherwise, undefining the header guard might |
| 847 |
cause the file to be re-stacked. */ |
| 848 |
if (import) |
| 849 |
{ |
| 850 |
_cpp_mark_file_once_only (pfile, file); |
| 851 |
|
| 852 |
/* Don't stack files that have been stacked before. */ |
| 853 |
if (file->stack_count) |
| 854 |
return true; |
| 855 |
} |
| 856 |
|
| 857 |
/* Skip if the file had a header guard and the macro is defined. |
| 858 |
PCH relies on this appearing before the PCH handler below. */ |
| 859 |
if (file->cmacro && cpp_macro_p (file->cmacro)) |
| 860 |
return true; |
| 861 |
|
| 862 |
/* Handle PCH files immediately; don't stack them. */ |
| 863 |
if (file->pchname) |
| 864 |
{ |
| 865 |
pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path); |
| 866 |
file->fd = -1; |
| 867 |
free ((void *) file->pchname); |
| 868 |
file->pchname = NULL; |
| 869 |
return true; |
| 870 |
} |
| 871 |
|
| 872 |
return false; |
| 873 |
} |
| 874 |
|
| 875 |
/* Return TRUE if file has unique contents, so we should read process |
| 876 |
it. The file's contents must already have been read. */ |
| 877 |
|
| 878 |
static bool |
| 879 |
has_unique_contents (cpp_reader *pfile, _cpp_file *file, bool import, |
| 880 |
location_t loc) |
| 881 |
{ |
| 882 |
/* Check the file against the PCH file. This is done before |
| 883 |
checking against files we've already seen, since it may save on |
| 884 |
I/O. */ |
| 885 |
if (check_file_against_entries (pfile, file, import)) |
| 886 |
{ |
| 887 |
/* If this isn't a #import, but yet we can't include the file, |
| 888 |
that means that it was #import-ed in the PCH file, |
| 889 |
so we can never include it again. */ |
| 890 |
if (! import) |
| 891 |
_cpp_mark_file_once_only (pfile, file); |
| 892 |
return false; |
| 893 |
} |
| 894 |
|
| 895 |
/* Now we've read the file's contents, we can stack it if there |
| 896 |
are no once-only files. */ |
| 897 |
if (!pfile->seen_once_only) |
| 898 |
return true; |
| 899 |
|
| 900 |
/* We may have read the file under a different name. Look |
| 901 |
for likely candidates and compare file contents to be sure. */ |
| 902 |
for (_cpp_file *f = pfile->all_files; f; f = f->next_file) |
| 903 |
{ |
| 904 |
if (f == file) |
| 905 |
continue; /* It'sa me! */ |
| 906 |
|
| 907 |
if (f->embed) |
| 908 |
continue; |
| 909 |
|
| 910 |
if ((import || f->once_only) |
| 911 |
&& f->err_no == 0 |
| 912 |
&& f->st.st_mtime == file->st.st_mtime |
| 913 |
&& f->st.st_size == file->st.st_size) |
| 914 |
{ |
| 915 |
_cpp_file *ref_file; |
| 916 |
|
| 917 |
if (f->buffer && !f->buffer_valid) |
| 918 |
{ |
| 919 |
/* We already have a buffer but it is not valid, because |
| 920 |
the file is still stacked. Make a new one. */ |
| 921 |
ref_file = make_cpp_file (f->dir, f->name); |
| 922 |
ref_file->path = f->path; |
| 923 |
} |
| 924 |
else |
| 925 |
/* The file is not stacked anymore. We can reuse it. */ |
| 926 |
ref_file = f; |
| 927 |
|
| 928 |
bool same_file_p = (read_file (pfile, ref_file, loc) |
| 929 |
/* Size might have changed in read_file(). */ |
| 930 |
&& ref_file->st.st_size == file->st.st_size |
| 931 |
&& !memcmp (ref_file->buffer, file->buffer, |
| 932 |
file->st.st_size)); |
| 933 |
|
| 934 |
if (f->buffer && !f->buffer_valid) |
| 935 |
{ |
| 936 |
ref_file->path = 0; |
| 937 |
destroy_cpp_file (ref_file); |
| 938 |
} |
| 939 |
|
| 940 |
if (same_file_p) |
| 941 |
/* Already seen under a different name. */ |
| 942 |
return false; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
return true; |
| 947 |
} |
| 948 |
|
| 949 |
/* Place the file referenced by FILE into a new buffer on the buffer |
| 950 |
stack if possible. Returns true if a buffer is stacked. Use LOC |
| 951 |
for any diagnostics. */ |
| 952 |
|
| 953 |
bool |
| 954 |
_cpp_stack_file (cpp_reader *pfile, _cpp_file *file, include_type type, |
| 955 |
location_t loc) |
| 956 |
{ |
| 957 |
if (is_known_idempotent_file (pfile, file, type == IT_IMPORT)) |
| 958 |
return false; |
| 959 |
|
| 960 |
int sysp = 0; |
| 961 |
char *buf = nullptr; |
| 962 |
|
| 963 |
/* Check C++ module include translation. */ |
| 964 |
if (!file->header_unit && type < IT_HEADER_HWM |
| 965 |
/* Do not include translate include-next. */ |
| 966 |
&& type != IT_INCLUDE_NEXT |
| 967 |
&& pfile->cb.translate_include) |
| 968 |
buf = (pfile->cb.translate_include |
| 969 |
(pfile, pfile->line_table, loc, file->path)); |
| 970 |
|
| 971 |
if (buf) |
| 972 |
{ |
| 973 |
/* We don't increment the line number at the end of a buffer, |
| 974 |
because we don't usually need that location (we're popping an |
| 975 |
include file). However in this case we do want to do the |
| 976 |
increment. So push a writable buffer of two newlines to acheive |
| 977 |
that. (We also need an extra newline, so this looks like a regular |
| 978 |
file, which we do that to to make sure we don't fall off the end in the |
| 979 |
middle of a line. */ |
| 980 |
if (type != IT_CMDLINE) |
| 981 |
{ |
| 982 |
static uchar newlines[] = "\n\n\n"; |
| 983 |
cpp_push_buffer (pfile, newlines, 2, true); |
| 984 |
} |
| 985 |
|
| 986 |
size_t len = strlen (buf); |
| 987 |
buf[len] = '\n'; /* See above */ |
| 988 |
cpp_buffer *buffer |
| 989 |
= cpp_push_buffer (pfile, reinterpret_cast<unsigned char *> (buf), |
| 990 |
len, true); |
| 991 |
buffer->to_free = buffer->buf; |
| 992 |
if (type == IT_CMDLINE) |
| 993 |
/* Tell _cpp_pop_buffer to change files. */ |
| 994 |
buffer->file = file; |
| 995 |
|
| 996 |
file->header_unit = +1; |
| 997 |
_cpp_mark_file_once_only (pfile, file); |
| 998 |
} |
| 999 |
else |
| 1000 |
{ |
| 1001 |
/* Not a header unit, and we know it. */ |
| 1002 |
file->header_unit = -1; |
| 1003 |
|
| 1004 |
if (!read_file (pfile, file, loc)) |
| 1005 |
return false; |
| 1006 |
|
| 1007 |
if (!has_unique_contents (pfile, file, type == IT_IMPORT, loc)) |
| 1008 |
return false; |
| 1009 |
|
| 1010 |
if (pfile->buffer && file->dir) |
| 1011 |
sysp = MAX (pfile->buffer->sysp, file->dir->sysp); |
| 1012 |
|
| 1013 |
/* Add the file to the dependencies on its first inclusion. */ |
| 1014 |
if (CPP_OPTION (pfile, deps.style) > (sysp != 0) |
| 1015 |
&& !file->stack_count |
| 1016 |
&& !(pfile->main_file == file |
| 1017 |
&& CPP_OPTION (pfile, deps.ignore_main_file))) |
| 1018 |
deps_add_dep (pfile->deps, file->path); |
| 1019 |
|
| 1020 |
/* Clear buffer_valid since _cpp_clean_line messes it up. */ |
| 1021 |
file->buffer_valid = false; |
| 1022 |
file->stack_count++; |
| 1023 |
|
| 1024 |
/* Stack the buffer. */ |
| 1025 |
cpp_buffer *buffer |
| 1026 |
= cpp_push_buffer (pfile, file->buffer, file->st.st_size, |
| 1027 |
CPP_OPTION (pfile, preprocessed) |
| 1028 |
&& !CPP_OPTION (pfile, directives_only)); |
| 1029 |
buffer->file = file; |
| 1030 |
buffer->sysp = sysp; |
| 1031 |
buffer->to_free = file->buffer_start; |
| 1032 |
|
| 1033 |
/* Initialize controlling macro state. */ |
| 1034 |
pfile->mi_valid = true; |
| 1035 |
pfile->mi_cmacro = 0; |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* In the case of a normal #include, we're now at the start of the |
| 1039 |
line *following* the #include. A separate location_t for this |
| 1040 |
location makes no sense, until we do the LC_LEAVE. |
| 1041 |
|
| 1042 |
This does not apply if we found a PCH file, we're not a regular |
| 1043 |
include, or we ran out of locations. */ |
| 1044 |
bool decrement = (file->pchname == NULL |
| 1045 |
&& type < IT_DIRECTIVE_HWM |
| 1046 |
&& (pfile->line_table->highest_location |
| 1047 |
!= LINE_MAP_MAX_LOCATION - 1)); |
| 1048 |
|
| 1049 |
if (decrement) |
| 1050 |
pfile->line_table->highest_location--; |
| 1051 |
|
| 1052 |
/* Normally a header unit becomes an __import directive in the current file, |
| 1053 |
but with -include we need something to LC_LEAVE to trigger the file_change |
| 1054 |
hook and continue to the next -include or the main source file. */ |
| 1055 |
if (file->header_unit <= 0 || type == IT_CMDLINE) |
| 1056 |
/* Add line map and do callbacks. */ |
| 1057 |
_cpp_do_file_change (pfile, LC_ENTER, file->path, |
| 1058 |
/* With preamble injection, start on line zero, |
| 1059 |
so the preamble doesn't appear to have been |
| 1060 |
included from line 1. Likewise when |
| 1061 |
starting preprocessed, we expect an initial |
| 1062 |
locating line. */ |
| 1063 |
type == IT_PRE_MAIN ? 0 : 1, sysp); |
| 1064 |
else if (decrement) |
| 1065 |
{ |
| 1066 |
/* Adjust the line back one so we appear on the #include line itself. */ |
| 1067 |
const line_map_ordinary *map |
| 1068 |
= LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table); |
| 1069 |
linenum_type line = SOURCE_LINE (map, pfile->line_table->highest_line); |
| 1070 |
linemap_line_start (pfile->line_table, line - 1, 0); |
| 1071 |
} |
| 1072 |
|
| 1073 |
return true; |
| 1074 |
} |
| 1075 |
|
| 1076 |
/* Mark FILE to be included once only. */ |
| 1077 |
void |
| 1078 |
_cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file) |
| 1079 |
{ |
| 1080 |
pfile->seen_once_only = true; |
| 1081 |
file->once_only = true; |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* Return the directory from which searching for FNAME should start, |
| 1085 |
considering the directive TYPE and ANGLE_BRACKETS. If there is |
| 1086 |
nothing left in the path, returns NULL. */ |
| 1087 |
struct cpp_dir * |
| 1088 |
search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, |
| 1089 |
enum include_type type, bool suppress_diagnostic) |
| 1090 |
{ |
| 1091 |
cpp_dir *dir; |
| 1092 |
_cpp_file *file; |
| 1093 |
|
| 1094 |
if (IS_ABSOLUTE_PATH (fname) || *fname == '\0') |
| 1095 |
return &pfile->no_search_path; |
| 1096 |
|
| 1097 |
/* pfile->buffer is NULL when processing an -include command-line flag. */ |
| 1098 |
file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file; |
| 1099 |
|
| 1100 |
/* For #include_next, skip in the search path past the dir in which |
| 1101 |
the current file was found, but if it was found via an absolute |
| 1102 |
path use the normal search logic. */ |
| 1103 |
if (type == IT_INCLUDE_NEXT && file->dir |
| 1104 |
&& file->dir != &pfile->no_search_path) |
| 1105 |
dir = file->dir->next; |
| 1106 |
else if (angle_brackets) |
| 1107 |
dir = type == IT_EMBED ? pfile->embed_include : pfile->bracket_include; |
| 1108 |
else if (type == IT_CMDLINE) |
| 1109 |
/* -include and -imacros use the #include "" chain with the |
| 1110 |
preprocessor's cwd prepended. */ |
| 1111 |
return make_cpp_dir (pfile, "./", false); |
| 1112 |
else if (pfile->quote_ignores_source_dir && type != IT_EMBED) |
| 1113 |
dir = pfile->quote_include; |
| 1114 |
else |
| 1115 |
return make_cpp_dir (pfile, dir_name_of_file (file), |
| 1116 |
pfile->buffer ? pfile->buffer->sysp : 0); |
| 1117 |
|
| 1118 |
if (dir == NULL && !suppress_diagnostic) |
| 1119 |
cpp_error (pfile, CPP_DL_ERROR, |
| 1120 |
"no include path in which to search for %s", fname); |
| 1121 |
|
| 1122 |
return dir; |
| 1123 |
} |
| 1124 |
|
| 1125 |
/* Strip the basename from the file's path. It ends with a slash if |
| 1126 |
of nonzero length. Note that this procedure also works for |
| 1127 |
<stdin>, which is represented by the empty string. */ |
| 1128 |
static const char * |
| 1129 |
dir_name_of_file (_cpp_file *file) |
| 1130 |
{ |
| 1131 |
if (!file->dir_name) |
| 1132 |
{ |
| 1133 |
size_t len = lbasename (file->path) - file->path; |
| 1134 |
char *dir_name = XNEWVEC (char, len + 1); |
| 1135 |
|
| 1136 |
memcpy (dir_name, file->path, len); |
| 1137 |
dir_name[len] = '\0'; |
| 1138 |
file->dir_name = dir_name; |
| 1139 |
} |
| 1140 |
|
| 1141 |
return file->dir_name; |
| 1142 |
} |
| 1143 |
|
| 1144 |
/* Handles #include-family directives (distinguished by TYPE), |
| 1145 |
including HEADER, and the command line -imacros and -include. |
| 1146 |
Returns true if a buffer was stacked. */ |
| 1147 |
bool |
| 1148 |
_cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets, |
| 1149 |
enum include_type type, location_t loc) |
| 1150 |
{ |
| 1151 |
/* For -include command-line flags we have type == IT_CMDLINE. |
| 1152 |
When the first -include file is processed we have the case, where |
| 1153 |
pfile->cur_token == pfile->cur_run->base, we are directly called up |
| 1154 |
by the front end. However in the case of the second -include file, |
| 1155 |
we are called from _cpp_lex_token -> _cpp_get_fresh_line -> |
| 1156 |
cpp_push_include, with pfile->cur_token != pfile->cur_run->base, |
| 1157 |
and pfile->cur_token[-1].src_loc not (yet) initialized. |
| 1158 |
However, when the include file cannot be found, we need src_loc to |
| 1159 |
be initialized to some safe value: 0 means UNKNOWN_LOCATION. */ |
| 1160 |
if (type == IT_CMDLINE && pfile->cur_token != pfile->cur_run->base) |
| 1161 |
pfile->cur_token[-1].src_loc = 0; |
| 1162 |
|
| 1163 |
cpp_dir *dir = search_path_head (pfile, fname, angle_brackets, type); |
| 1164 |
if (!dir) |
| 1165 |
return false; |
| 1166 |
|
| 1167 |
_cpp_file *file = _cpp_find_file (pfile, fname, dir, angle_brackets, |
| 1168 |
type == IT_DEFAULT ? _cpp_FFK_PRE_INCLUDE |
| 1169 |
: _cpp_FFK_NORMAL, loc); |
| 1170 |
if (type == IT_DEFAULT && file == NULL) |
| 1171 |
return false; |
| 1172 |
|
| 1173 |
return _cpp_stack_file (pfile, file, type, loc); |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* NAME is a header file name, find the _cpp_file, if any. */ |
| 1177 |
|
| 1178 |
static _cpp_file * |
| 1179 |
test_header_unit (cpp_reader *pfile, const char *name, bool angle, |
| 1180 |
location_t loc) |
| 1181 |
{ |
| 1182 |
if (cpp_dir *dir = search_path_head (pfile, name, angle, IT_INCLUDE)) |
| 1183 |
return _cpp_find_file (pfile, name, dir, angle, _cpp_FFK_NORMAL, loc); |
| 1184 |
|
| 1185 |
return nullptr; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* NAME is a header file name, find the path we'll use to open it and infer that |
| 1189 |
it is a header-unit. */ |
| 1190 |
|
| 1191 |
const char * |
| 1192 |
_cpp_find_header_unit (cpp_reader *pfile, const char *name, bool angle, |
| 1193 |
location_t loc) |
| 1194 |
{ |
| 1195 |
if (_cpp_file *file = test_header_unit (pfile, name, angle, loc)) |
| 1196 |
{ |
| 1197 |
if (file->fd > 0) |
| 1198 |
{ |
| 1199 |
/* Don't leave it open. */ |
| 1200 |
close (file->fd); |
| 1201 |
file->fd = 0; |
| 1202 |
} |
| 1203 |
|
| 1204 |
file->header_unit = +1; |
| 1205 |
_cpp_mark_file_once_only (pfile, file); |
| 1206 |
|
| 1207 |
return file->path; |
| 1208 |
} |
| 1209 |
|
| 1210 |
return nullptr; |
| 1211 |
} |
| 1212 |
|
| 1213 |
/* NAME is a header file name, find the path we'll use to open it. But do not |
| 1214 |
infer it is a header unit. */ |
| 1215 |
|
| 1216 |
const char * |
| 1217 |
cpp_probe_header_unit (cpp_reader *pfile, const char *name, bool angle, |
| 1218 |
location_t loc) |
| 1219 |
{ |
| 1220 |
if (_cpp_file *file = test_header_unit (pfile, name, angle, loc)) |
| 1221 |
return file->path; |
| 1222 |
|
| 1223 |
return nullptr; |
| 1224 |
} |
| 1225 |
|
| 1226 |
/* Helper function for _cpp_stack_embed. Finish #embed/__has_embed processing |
| 1227 |
after a file is found and data loaded into buffer. */ |
| 1228 |
|
| 1229 |
static int |
| 1230 |
finish_embed (cpp_reader *pfile, _cpp_file *file, |
| 1231 |
struct cpp_embed_params *params) |
| 1232 |
{ |
| 1233 |
const uchar *buffer = file->buffer; |
| 1234 |
size_t limit = file->limit; |
| 1235 |
if (params->offset - file->offset > limit) |
| 1236 |
limit = 0; |
| 1237 |
else |
| 1238 |
{ |
| 1239 |
buffer += params->offset - file->offset; |
| 1240 |
limit -= params->offset - file->offset; |
| 1241 |
} |
| 1242 |
if (params->limit < limit) |
| 1243 |
limit = params->limit; |
| 1244 |
|
| 1245 |
size_t embed_tokens = 0; |
| 1246 |
if (CPP_OPTION (pfile, lang) != CLK_ASM |
| 1247 |
&& limit >= 64) |
| 1248 |
embed_tokens = ((limit - 2) / INT_MAX) + (((limit - 2) % INT_MAX) != 0); |
| 1249 |
|
| 1250 |
size_t max = INTTYPE_MAXIMUM (size_t) / sizeof (cpp_token); |
| 1251 |
if ((embed_tokens ? (embed_tokens > (max - 3) / 2) : (limit > max / 2)) |
| 1252 |
|| (limit |
| 1253 |
? (params->prefix.count > max |
| 1254 |
|| params->suffix.count > max |
| 1255 |
|| ((embed_tokens ? embed_tokens * 2 + 3 : limit * 2 - 1) |
| 1256 |
+ params->prefix.count |
| 1257 |
+ params->suffix.count > max)) |
| 1258 |
: params->if_empty.count > max)) |
| 1259 |
{ |
| 1260 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1261 |
"%s is too large", file->path); |
| 1262 |
return 0; |
| 1263 |
} |
| 1264 |
|
| 1265 |
size_t len = 0; |
| 1266 |
for (size_t i = 0; i < limit; ++i) |
| 1267 |
{ |
| 1268 |
if (buffer[i] < 10) |
| 1269 |
len += 2; |
| 1270 |
else if (buffer[i] < 100) |
| 1271 |
len += 3; |
| 1272 |
#if UCHAR_MAX == 255 |
| 1273 |
else |
| 1274 |
len += 4; |
| 1275 |
#else |
| 1276 |
else if (buffer[i] < 1000) |
| 1277 |
len += 4; |
| 1278 |
else |
| 1279 |
{ |
| 1280 |
char buf[64]; |
| 1281 |
len += sprintf (buf, "%d", buffer[i]) + 1; |
| 1282 |
} |
| 1283 |
#endif |
| 1284 |
if (len > INTTYPE_MAXIMUM (ssize_t)) |
| 1285 |
{ |
| 1286 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1287 |
"%s is too large", file->path); |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
if (embed_tokens && i == 0) |
| 1291 |
i = limit - 2; |
| 1292 |
} |
| 1293 |
uchar *s = len ? _cpp_unaligned_alloc (pfile, len) : NULL; |
| 1294 |
_cpp_buff *tok_buff = NULL; |
| 1295 |
cpp_token *tok = &pfile->directive_result, *toks = tok; |
| 1296 |
size_t count = 0; |
| 1297 |
if (limit) |
| 1298 |
count = (params->prefix.count |
| 1299 |
+ (embed_tokens ? embed_tokens * 2 + 3 : limit * 2 - 1) |
| 1300 |
+ params->suffix.count) - 1; |
| 1301 |
else if (params->if_empty.count) |
| 1302 |
count = params->if_empty.count - 1; |
| 1303 |
if (count) |
| 1304 |
{ |
| 1305 |
tok_buff = _cpp_get_buff (pfile, count * sizeof (cpp_token)); |
| 1306 |
toks = (cpp_token *) tok_buff->base; |
| 1307 |
} |
| 1308 |
cpp_embed_params_tokens *prefix |
| 1309 |
= limit ? ¶ms->prefix : ¶ms->if_empty; |
| 1310 |
if (prefix->count) |
| 1311 |
{ |
| 1312 |
*tok = *prefix->base_run.base; |
| 1313 |
tok = toks; |
| 1314 |
tokenrun *cur_run = &prefix->base_run; |
| 1315 |
while (cur_run) |
| 1316 |
{ |
| 1317 |
size_t cnt = (cur_run->next ? cur_run->limit |
| 1318 |
: prefix->cur_token) - cur_run->base; |
| 1319 |
cpp_token *t = cur_run->base; |
| 1320 |
if (cur_run == &prefix->base_run) |
| 1321 |
{ |
| 1322 |
t++; |
| 1323 |
cnt--; |
| 1324 |
} |
| 1325 |
memcpy (tok, t, cnt * sizeof (cpp_token)); |
| 1326 |
tok += cnt; |
| 1327 |
cur_run = cur_run->next; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
for (size_t i = 0; i < limit; ++i) |
| 1331 |
{ |
| 1332 |
tok->src_loc = params->loc; |
| 1333 |
tok->type = CPP_NUMBER; |
| 1334 |
tok->flags = NO_EXPAND; |
| 1335 |
if (i == 0) |
| 1336 |
tok->flags |= PREV_WHITE; |
| 1337 |
tok->val.str.text = s; |
| 1338 |
tok->val.str.len = sprintf ((char *) s, "%d", buffer[i]); |
| 1339 |
s += tok->val.str.len + 1; |
| 1340 |
if (tok == &pfile->directive_result) |
| 1341 |
tok = toks; |
| 1342 |
else |
| 1343 |
tok++; |
| 1344 |
if (i < limit - 1) |
| 1345 |
{ |
| 1346 |
tok->src_loc = params->loc; |
| 1347 |
tok->type = CPP_COMMA; |
| 1348 |
tok->flags = NO_EXPAND; |
| 1349 |
tok++; |
| 1350 |
} |
| 1351 |
if (i == 0 && embed_tokens) |
| 1352 |
{ |
| 1353 |
++i; |
| 1354 |
for (size_t j = 0; j < embed_tokens; ++j) |
| 1355 |
{ |
| 1356 |
tok->src_loc = params->loc; |
| 1357 |
tok->type = CPP_EMBED; |
| 1358 |
tok->flags = NO_EXPAND; |
| 1359 |
tok->val.str.text = &buffer[i]; |
| 1360 |
tok->val.str.len |
| 1361 |
= limit - 1 - i > INT_MAX ? INT_MAX : limit - 1 - i; |
| 1362 |
i += tok->val.str.len; |
| 1363 |
if (tok->val.str.len < 32 && j) |
| 1364 |
{ |
| 1365 |
/* Avoid CPP_EMBED with a fewer than 32 bytes, shrink the |
| 1366 |
previous CPP_EMBED by 64 and grow this one by 64. */ |
| 1367 |
tok[-2].val.str.len -= 64; |
| 1368 |
tok->val.str.text -= 64; |
| 1369 |
tok->val.str.len += 64; |
| 1370 |
} |
| 1371 |
tok++; |
| 1372 |
tok->src_loc = params->loc; |
| 1373 |
tok->type = CPP_COMMA; |
| 1374 |
tok->flags = NO_EXPAND; |
| 1375 |
tok++; |
| 1376 |
} |
| 1377 |
--i; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
if (limit && params->suffix.count) |
| 1381 |
{ |
| 1382 |
tokenrun *cur_run = ¶ms->suffix.base_run; |
| 1383 |
cpp_token *orig_tok = tok; |
| 1384 |
while (cur_run) |
| 1385 |
{ |
| 1386 |
size_t cnt = (cur_run->next ? cur_run->limit |
| 1387 |
: params->suffix.cur_token) - cur_run->base; |
| 1388 |
cpp_token *t = cur_run->base; |
| 1389 |
memcpy (tok, t, cnt * sizeof (cpp_token)); |
| 1390 |
tok += cnt; |
| 1391 |
cur_run = cur_run->next; |
| 1392 |
} |
| 1393 |
orig_tok->flags |= PREV_WHITE; |
| 1394 |
} |
| 1395 |
pfile->directive_result.flags |= PREV_WHITE; |
| 1396 |
if (count) |
| 1397 |
{ |
| 1398 |
_cpp_push_token_context (pfile, NULL, toks, count); |
| 1399 |
pfile->context->buff = tok_buff; |
| 1400 |
} |
| 1401 |
return limit ? 1 : 2; |
| 1402 |
} |
| 1403 |
|
| 1404 |
/* Helper function for initialization of base64_dec table. |
| 1405 |
Can't rely on ASCII compatibility, so check each letter |
| 1406 |
separately. */ |
| 1407 |
|
| 1408 |
constexpr signed char |
| 1409 |
base64_dec_fn (unsigned char c) |
| 1410 |
{ |
| 1411 |
return (c == 'A' ? 0 : c == 'B' ? 1 : c == 'C' ? 2 : c == 'D' ? 3 |
| 1412 |
: c == 'E' ? 4 : c == 'F' ? 5 : c == 'G' ? 6 : c == 'H' ? 7 |
| 1413 |
: c == 'I' ? 8 : c == 'J' ? 9 : c == 'K' ? 10 : c == 'L' ? 11 |
| 1414 |
: c == 'M' ? 12 : c == 'N' ? 13 : c == 'O' ? 14 : c == 'P' ? 15 |
| 1415 |
: c == 'Q' ? 16 : c == 'R' ? 17 : c == 'S' ? 18 : c == 'T' ? 19 |
| 1416 |
: c == 'U' ? 20 : c == 'V' ? 21 : c == 'W' ? 22 : c == 'X' ? 23 |
| 1417 |
: c == 'Y' ? 24 : c == 'Z' ? 25 |
| 1418 |
: c == 'a' ? 26 : c == 'b' ? 27 : c == 'c' ? 28 : c == 'd' ? 29 |
| 1419 |
: c == 'e' ? 30 : c == 'f' ? 31 : c == 'g' ? 32 : c == 'h' ? 33 |
| 1420 |
: c == 'i' ? 34 : c == 'j' ? 35 : c == 'k' ? 36 : c == 'l' ? 37 |
| 1421 |
: c == 'm' ? 38 : c == 'n' ? 39 : c == 'o' ? 40 : c == 'p' ? 41 |
| 1422 |
: c == 'q' ? 42 : c == 'r' ? 43 : c == 's' ? 44 : c == 't' ? 45 |
| 1423 |
: c == 'u' ? 46 : c == 'v' ? 47 : c == 'w' ? 48 : c == 'x' ? 49 |
| 1424 |
: c == 'y' ? 50 : c == 'z' ? 51 |
| 1425 |
: c == '0' ? 52 : c == '1' ? 53 : c == '2' ? 54 : c == '3' ? 55 |
| 1426 |
: c == '4' ? 56 : c == '5' ? 57 : c == '6' ? 58 : c == '7' ? 59 |
| 1427 |
: c == '8' ? 60 : c == '9' ? 61 : c == '+' ? 62 : c == '/' ? 63 |
| 1428 |
: -1); |
| 1429 |
} |
| 1430 |
|
| 1431 |
/* base64 decoding table. */ |
| 1432 |
|
| 1433 |
static constexpr signed char base64_dec[] = { |
| 1434 |
#define B64D0(x) base64_dec_fn (x) |
| 1435 |
#define B64D1(x) B64D0 (x), B64D0 (x + 1), B64D0 (x + 2), B64D0 (x + 3) |
| 1436 |
#define B64D2(x) B64D1 (x), B64D1 (x + 4), B64D1 (x + 8), B64D1 (x + 12) |
| 1437 |
#define B64D3(x) B64D2 (x), B64D2 (x + 16), B64D2 (x + 32), B64D2 (x + 48) |
| 1438 |
B64D3 (0), B64D3 (64), B64D3 (128), B64D3 (192) |
| 1439 |
}; |
| 1440 |
|
| 1441 |
/* Helper function for _cpp_stack_embed. Handle #embed/__has_embed with |
| 1442 |
gnu::base64 parameter. */ |
| 1443 |
|
| 1444 |
static int |
| 1445 |
finish_base64_embed (cpp_reader *pfile, const char *fname, bool angle, |
| 1446 |
struct cpp_embed_params *params) |
| 1447 |
{ |
| 1448 |
size_t len, end, i, j, base64_len = 0, cnt; |
| 1449 |
uchar *buf = NULL, *q, pbuf[4], qbuf[3]; |
| 1450 |
const uchar *base64_str; |
| 1451 |
if (angle || strcmp (fname, ".")) |
| 1452 |
{ |
| 1453 |
if (!params->has_embed) |
| 1454 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1455 |
"%<gnu::base64%> parameter can be only used with " |
| 1456 |
"%<\".\"%>"); |
| 1457 |
return 0; |
| 1458 |
} |
| 1459 |
tokenrun *cur_run = ¶ms->base64.base_run; |
| 1460 |
cpp_token *tend, *tok; |
| 1461 |
while (cur_run) |
| 1462 |
{ |
| 1463 |
tend = cur_run->next ? cur_run->limit : params->base64.cur_token; |
| 1464 |
for (tok = cur_run->base; tok < tend; ++tok) |
| 1465 |
{ |
| 1466 |
if (tok->val.str.len < 2 |
| 1467 |
|| tok->val.str.text[0] != '"' |
| 1468 |
|| tok->val.str.text[tok->val.str.len - 1] != '"') |
| 1469 |
{ |
| 1470 |
fail: |
| 1471 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1472 |
"%<gnu::base64%> argument not valid base64 " |
| 1473 |
"encoded string"); |
| 1474 |
free (buf); |
| 1475 |
return 0; |
| 1476 |
} |
| 1477 |
if (tok->val.str.len - 2 > (~(size_t) 0) - base64_len) |
| 1478 |
goto fail; |
| 1479 |
base64_len += tok->val.str.len - 2; |
| 1480 |
} |
| 1481 |
cur_run = cur_run->next; |
| 1482 |
} |
| 1483 |
if ((base64_len & 3) != 0) |
| 1484 |
goto fail; |
| 1485 |
len = base64_len / 4 * 3; |
| 1486 |
end = len; |
| 1487 |
|
| 1488 |
if (params->has_embed) |
| 1489 |
q = qbuf; |
| 1490 |
else |
| 1491 |
{ |
| 1492 |
buf = XNEWVEC (uchar, len ? len : 1); |
| 1493 |
q = buf; |
| 1494 |
} |
| 1495 |
cur_run = ¶ms->base64.base_run; |
| 1496 |
tend = cur_run->next ? cur_run->limit : params->base64.cur_token; |
| 1497 |
tok = cur_run->base; |
| 1498 |
base64_str = tok->val.str.text + 1; |
| 1499 |
cnt = tok->val.str.len - 2; |
| 1500 |
++tok; |
| 1501 |
for (i = 0; i < end; i += 3) |
| 1502 |
{ |
| 1503 |
for (j = 0; j < 4; ++j) |
| 1504 |
{ |
| 1505 |
while (cnt == 0) |
| 1506 |
{ |
| 1507 |
if (tok == tend) |
| 1508 |
{ |
| 1509 |
cur_run = cur_run->next; |
| 1510 |
tend = (cur_run->next ? cur_run->limit |
| 1511 |
: params->base64.cur_token); |
| 1512 |
tok = cur_run->base; |
| 1513 |
} |
| 1514 |
base64_str = tok->val.str.text + 1; |
| 1515 |
cnt = tok->val.str.len - 2; |
| 1516 |
++tok; |
| 1517 |
} |
| 1518 |
pbuf[j] = *base64_str; |
| 1519 |
base64_str++; |
| 1520 |
--cnt; |
| 1521 |
} |
| 1522 |
if (pbuf[3] == '=' && i + 3 >= end) |
| 1523 |
{ |
| 1524 |
end = len - 3; |
| 1525 |
--len; |
| 1526 |
if (pbuf[2] == '=') |
| 1527 |
--len; |
| 1528 |
break; |
| 1529 |
} |
| 1530 |
int a = base64_dec[pbuf[0]]; |
| 1531 |
int b = base64_dec[pbuf[1]]; |
| 1532 |
int c = base64_dec[pbuf[2]]; |
| 1533 |
int d = base64_dec[pbuf[3]]; |
| 1534 |
if (a == -1 || b == -1 || c == -1 || d == -1) |
| 1535 |
goto fail; |
| 1536 |
q[0] = (a << 2) | (b >> 4); |
| 1537 |
q[1] = (b << 4) | (c >> 2); |
| 1538 |
q[2] = (c << 6) | d; |
| 1539 |
if (!params->has_embed) |
| 1540 |
q += 3; |
| 1541 |
} |
| 1542 |
if (len != end) |
| 1543 |
{ |
| 1544 |
int a = base64_dec[pbuf[0]]; |
| 1545 |
int b = base64_dec[pbuf[1]]; |
| 1546 |
if (a == -1 || b == -1) |
| 1547 |
goto fail; |
| 1548 |
q[0] = (a << 2) | (b >> 4); |
| 1549 |
if (len - end == 2) |
| 1550 |
{ |
| 1551 |
int c = base64_dec[pbuf[2]]; |
| 1552 |
if (c == -1) |
| 1553 |
goto fail; |
| 1554 |
q[1] = (b << 4) | (c >> 2); |
| 1555 |
if ((c & 3) != 0) |
| 1556 |
goto fail; |
| 1557 |
} |
| 1558 |
else if ((b & 15) != 0) |
| 1559 |
goto fail; |
| 1560 |
} |
| 1561 |
if (params->has_embed) |
| 1562 |
return len ? 1 : 2; |
| 1563 |
_cpp_file *file = make_cpp_file (NULL, ""); |
| 1564 |
file->embed = 1; |
| 1565 |
file->next_file = pfile->all_files; |
| 1566 |
pfile->all_files = file; |
| 1567 |
params->limit = -1; |
| 1568 |
params->offset = 0; |
| 1569 |
file->limit = len; |
| 1570 |
file->buffer = buf; |
| 1571 |
file->path = xstrdup ("<base64>"); |
| 1572 |
return finish_embed (pfile, file, params); |
| 1573 |
} |
| 1574 |
|
| 1575 |
/* Try to load FNAME with #embed/__has_embed parameters PARAMS. |
| 1576 |
If !PARAMS->has_embed, return new token in pfile->directive_result |
| 1577 |
(first token) and rest in a pushed non-macro context. |
| 1578 |
Returns 0 for not found/errors, 1 for non-empty resource and 2 |
| 1579 |
for empty resource. */ |
| 1580 |
|
| 1581 |
int |
| 1582 |
_cpp_stack_embed (cpp_reader *pfile, const char *fname, bool angle, |
| 1583 |
struct cpp_embed_params *params) |
| 1584 |
{ |
| 1585 |
if (params->base64.count) |
| 1586 |
return finish_base64_embed (pfile, fname, angle, params); |
| 1587 |
cpp_dir *dir = search_path_head (pfile, fname, angle, IT_EMBED, |
| 1588 |
params->has_embed); |
| 1589 |
if (!dir) |
| 1590 |
return 0; |
| 1591 |
_cpp_file *file = _cpp_find_file (pfile, fname, dir, angle, |
| 1592 |
params->has_embed |
| 1593 |
? _cpp_FFK_HAS_EMBED : _cpp_FFK_EMBED, |
| 1594 |
params->loc); |
| 1595 |
if (!file) |
| 1596 |
return 0; |
| 1597 |
if (file->dont_read || file->err_no) |
| 1598 |
return 0; |
| 1599 |
_cpp_file *orig_file = file; |
| 1600 |
if (file->buffer_valid |
| 1601 |
&& (!S_ISREG (file->st.st_mode) |
| 1602 |
|| file->offset + (cpp_num_part) 0 > params->offset |
| 1603 |
|| (file->limit < file->st.st_size - file->offset + (size_t) 0 |
| 1604 |
&& (params->offset - file->offset > (cpp_num_part) file->limit |
| 1605 |
|| file->limit - (params->offset |
| 1606 |
- file->offset) < params->limit)))) |
| 1607 |
{ |
| 1608 |
bool found = false; |
| 1609 |
if (S_ISREG (file->st.st_mode)) |
| 1610 |
{ |
| 1611 |
while (file->next_file |
| 1612 |
&& file->next_file->embed |
| 1613 |
&& file->next_file->buffer_valid |
| 1614 |
&& file->next_file->dir == file->dir |
| 1615 |
&& strcmp (file->name, file->next_file->name) == 0 |
| 1616 |
&& strcmp (file->path, file->next_file->path) == 0) |
| 1617 |
{ |
| 1618 |
file = file->next_file; |
| 1619 |
if (file->offset + (cpp_num_part) 0 <= params->offset |
| 1620 |
&& (file->limit >= (file->st.st_size - file->offset |
| 1621 |
+ (size_t) 0) |
| 1622 |
|| (params->offset |
| 1623 |
- file->offset <= (cpp_num_part) file->limit |
| 1624 |
&& file->limit - (params->offset |
| 1625 |
- file->offset) >= params->limit))) |
| 1626 |
{ |
| 1627 |
found = true; |
| 1628 |
break; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
} |
| 1632 |
if (!found) |
| 1633 |
{ |
| 1634 |
_cpp_file *file2 = make_cpp_file (file->dir, file->name); |
| 1635 |
file2->path = xstrdup (file->path); |
| 1636 |
file2->next_file = file->next_file; |
| 1637 |
file2->embed = true; |
| 1638 |
file->next_file = file2; |
| 1639 |
file = file2; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
if (!file->buffer_valid) |
| 1643 |
{ |
| 1644 |
if (file->fd == -1 && !open_file (file)) |
| 1645 |
{ |
| 1646 |
if (params->has_embed) |
| 1647 |
file->deferred_error = true; |
| 1648 |
else |
| 1649 |
open_file_failed (pfile, file, 0, params->loc); |
| 1650 |
return 0; |
| 1651 |
} |
| 1652 |
if (S_ISBLK (file->st.st_mode)) |
| 1653 |
{ |
| 1654 |
if (params->has_embed) |
| 1655 |
{ |
| 1656 |
close (file->fd); |
| 1657 |
file->fd = -1; |
| 1658 |
return 0; |
| 1659 |
} |
| 1660 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1661 |
"%s is a block device", file->path); |
| 1662 |
fail: |
| 1663 |
close (file->fd); |
| 1664 |
file->fd = -1; |
| 1665 |
file->dont_read = true; |
| 1666 |
return 0; |
| 1667 |
} |
| 1668 |
|
| 1669 |
if (CPP_OPTION (pfile, deps.style) |
| 1670 |
&& !params->has_embed |
| 1671 |
&& file == orig_file |
| 1672 |
&& file->path[0]) |
| 1673 |
deps_add_dep (pfile->deps, file->path); |
| 1674 |
|
| 1675 |
bool regular = S_ISREG (file->st.st_mode) != 0; |
| 1676 |
ssize_t size, total, count; |
| 1677 |
uchar *buf; |
| 1678 |
if (regular) |
| 1679 |
{ |
| 1680 |
cpp_num_part limit; |
| 1681 |
if (file->st.st_size + (cpp_num_part) 0 < params->offset) |
| 1682 |
limit = 0; |
| 1683 |
else if (file->st.st_size - params->offset < params->limit) |
| 1684 |
limit = file->st.st_size - params->offset; |
| 1685 |
else |
| 1686 |
limit = params->limit; |
| 1687 |
if (params->has_embed) |
| 1688 |
return limit != 0 ? 1 : 2; |
| 1689 |
if (limit > INTTYPE_MAXIMUM (ssize_t)) |
| 1690 |
{ |
| 1691 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1692 |
"%s is too large", file->path); |
| 1693 |
goto fail; |
| 1694 |
} |
| 1695 |
if (lseek (file->fd, params->offset, SEEK_CUR) |
| 1696 |
!= (off_t) params->offset) |
| 1697 |
{ |
| 1698 |
cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, |
| 1699 |
params->loc); |
| 1700 |
goto fail; |
| 1701 |
} |
| 1702 |
file->offset = params->offset; |
| 1703 |
file->limit = limit; |
| 1704 |
size = limit; |
| 1705 |
} |
| 1706 |
else if (params->has_embed) |
| 1707 |
return 2; |
| 1708 |
else if (params->limit > 8 * 1024) |
| 1709 |
size = 8 * 1024; |
| 1710 |
else |
| 1711 |
size = params->limit; |
| 1712 |
buf = XNEWVEC (uchar, size ? size : 1); |
| 1713 |
total = 0; |
| 1714 |
|
| 1715 |
if (!regular && params->offset) |
| 1716 |
{ |
| 1717 |
uchar *buf2 = buf; |
| 1718 |
ssize_t size2 = size; |
| 1719 |
cpp_num_part total2 = params->offset; |
| 1720 |
|
| 1721 |
if (params->offset > 8 * 1024 && size < 8 * 1024) |
| 1722 |
{ |
| 1723 |
size2 = 32 * 1024; |
| 1724 |
buf2 = XNEWVEC (uchar, size2); |
| 1725 |
} |
| 1726 |
do |
| 1727 |
{ |
| 1728 |
if ((cpp_num_part) size2 > total2) |
| 1729 |
size2 = total2; |
| 1730 |
count = read (file->fd, buf2, size2); |
| 1731 |
if (count < 0) |
| 1732 |
{ |
| 1733 |
cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, |
| 1734 |
params->loc); |
| 1735 |
if (buf2 != buf) |
| 1736 |
free (buf2); |
| 1737 |
free (buf); |
| 1738 |
goto fail; |
| 1739 |
} |
| 1740 |
total2 -= count; |
| 1741 |
} |
| 1742 |
while (total2); |
| 1743 |
if (buf2 != buf) |
| 1744 |
free (buf2); |
| 1745 |
} |
| 1746 |
|
| 1747 |
while ((count = read (file->fd, buf + total, size - total)) > 0) |
| 1748 |
{ |
| 1749 |
total += count; |
| 1750 |
if (total == size) |
| 1751 |
{ |
| 1752 |
if (regular || size + (cpp_num_part) 0 == params->limit) |
| 1753 |
break; |
| 1754 |
size = (size_t) size * 2; |
| 1755 |
if (size < 0) |
| 1756 |
{ |
| 1757 |
if (params->limit <= INTTYPE_MAXIMUM (ssize_t)) |
| 1758 |
size = params->limit; |
| 1759 |
else |
| 1760 |
{ |
| 1761 |
cpp_error_at (pfile, CPP_DL_ERROR, params->loc, |
| 1762 |
"%s is too large", file->path); |
| 1763 |
free (buf); |
| 1764 |
goto fail; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
else if (size + (cpp_num_part) 0 > params->limit) |
| 1768 |
size = params->limit; |
| 1769 |
buf = XRESIZEVEC (uchar, buf, size); |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
if (count < 0) |
| 1774 |
{ |
| 1775 |
cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, params->loc); |
| 1776 |
free (buf); |
| 1777 |
goto fail; |
| 1778 |
} |
| 1779 |
|
| 1780 |
if (regular && total != size && STAT_SIZE_RELIABLE (file->st)) |
| 1781 |
{ |
| 1782 |
cpp_error_at (pfile, CPP_DL_WARNING, params->loc, |
| 1783 |
"%s is shorter than expected", file->path); |
| 1784 |
file->limit = total; |
| 1785 |
} |
| 1786 |
else if (!regular) |
| 1787 |
{ |
| 1788 |
file->offset = params->offset; |
| 1789 |
file->limit = total; |
| 1790 |
} |
| 1791 |
|
| 1792 |
file->buffer_start = buf; |
| 1793 |
file->buffer = buf; |
| 1794 |
file->buffer_valid = 1; |
| 1795 |
close (file->fd); |
| 1796 |
file->fd = -1; |
| 1797 |
} |
| 1798 |
else if (params->has_embed) |
| 1799 |
{ |
| 1800 |
if (params->offset - file->offset > file->limit) |
| 1801 |
return 2; |
| 1802 |
size_t limit = file->limit - (params->offset - file->offset); |
| 1803 |
return limit && params->limit ? 1 : 2; |
| 1804 |
} |
| 1805 |
|
| 1806 |
return finish_embed (pfile, file, params); |
| 1807 |
} |
| 1808 |
|
| 1809 |
/* Retrofit the just-entered main file asif it was an include. This |
| 1810 |
will permit correct include_next use, and mark it as a system |
| 1811 |
header if that's where it resides. We use filesystem-appropriate |
| 1812 |
prefix matching of the include path to locate the main file. */ |
| 1813 |
void |
| 1814 |
cpp_retrofit_as_include (cpp_reader *pfile) |
| 1815 |
{ |
| 1816 |
/* We should be the outermost. */ |
| 1817 |
gcc_assert (!pfile->buffer->prev); |
| 1818 |
|
| 1819 |
if (const char *name = pfile->main_file->name) |
| 1820 |
{ |
| 1821 |
/* Locate name on the include dir path, using a prefix match. */ |
| 1822 |
size_t name_len = strlen (name); |
| 1823 |
for (cpp_dir *dir = pfile->quote_include; dir; dir = dir->next) |
| 1824 |
if (dir->len < name_len |
| 1825 |
&& IS_DIR_SEPARATOR (name[dir->len]) |
| 1826 |
&& !filename_ncmp (name, dir->name, dir->len)) |
| 1827 |
{ |
| 1828 |
pfile->main_file->dir = dir; |
| 1829 |
if (dir->sysp) |
| 1830 |
cpp_make_system_header (pfile, 1, 0); |
| 1831 |
break; |
| 1832 |
} |
| 1833 |
} |
| 1834 |
|
| 1835 |
/* Initialize controlling macro state. */ |
| 1836 |
pfile->mi_valid = true; |
| 1837 |
pfile->mi_cmacro = 0; |
| 1838 |
} |
| 1839 |
|
| 1840 |
/* Could not open FILE. The complication is dependency output. */ |
| 1841 |
static void |
| 1842 |
open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets, |
| 1843 |
location_t loc) |
| 1844 |
{ |
| 1845 |
int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0; |
| 1846 |
bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp); |
| 1847 |
|
| 1848 |
errno = file->err_no; |
| 1849 |
if (print_dep && CPP_OPTION (pfile, deps.missing_files) && errno == ENOENT) |
| 1850 |
{ |
| 1851 |
deps_add_dep (pfile->deps, file->name); |
| 1852 |
/* If the preprocessor output (other than dependency information) is |
| 1853 |
being used, we must also flag an error. */ |
| 1854 |
if (CPP_OPTION (pfile, deps.need_preprocessor_output)) |
| 1855 |
cpp_errno_filename (pfile, CPP_DL_FATAL, |
| 1856 |
file->path ? file->path : file->name, |
| 1857 |
loc); |
| 1858 |
} |
| 1859 |
else |
| 1860 |
{ |
| 1861 |
/* If we are not outputting dependencies, or if we are and dependencies |
| 1862 |
were requested for this file, or if preprocessor output is needed |
| 1863 |
in addition to dependency information, this is an error. |
| 1864 |
|
| 1865 |
Otherwise (outputting dependencies but not for this file, and not |
| 1866 |
using the preprocessor output), we can still produce correct output |
| 1867 |
so it's only a warning. */ |
| 1868 |
if (CPP_OPTION (pfile, deps.style) == DEPS_NONE |
| 1869 |
|| print_dep |
| 1870 |
|| CPP_OPTION (pfile, deps.need_preprocessor_output)) |
| 1871 |
cpp_errno_filename (pfile, CPP_DL_FATAL, |
| 1872 |
file->path ? file->path : file->name, |
| 1873 |
loc); |
| 1874 |
else |
| 1875 |
cpp_errno_filename (pfile, CPP_DL_WARNING, |
| 1876 |
file->path ? file->path : file->name, |
| 1877 |
loc); |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
/* Search in the chain beginning at HEAD for a file whose search path |
| 1882 |
started at START_DIR != NULL. */ |
| 1883 |
static struct cpp_file_hash_entry * |
| 1884 |
search_cache (struct cpp_file_hash_entry *head, const cpp_dir *start_dir, |
| 1885 |
bool is_embed) |
| 1886 |
{ |
| 1887 |
while (head && (head->start_dir != start_dir |
| 1888 |
|| head->u.file->embed != is_embed)) |
| 1889 |
head = head->next; |
| 1890 |
|
| 1891 |
return head; |
| 1892 |
} |
| 1893 |
|
| 1894 |
/* Allocate a new _cpp_file structure. */ |
| 1895 |
static _cpp_file * |
| 1896 |
make_cpp_file (cpp_dir *dir, const char *fname) |
| 1897 |
{ |
| 1898 |
_cpp_file *file = XCNEW (_cpp_file); |
| 1899 |
file->fd = -1; |
| 1900 |
file->dir = dir; |
| 1901 |
file->name = xstrdup (fname); |
| 1902 |
|
| 1903 |
return file; |
| 1904 |
} |
| 1905 |
|
| 1906 |
/* Release a _cpp_file structure. */ |
| 1907 |
static void |
| 1908 |
destroy_cpp_file (_cpp_file *file) |
| 1909 |
{ |
| 1910 |
free ((void *) file->buffer_start); |
| 1911 |
free ((void *) file->name); |
| 1912 |
free ((void *) file->path); |
| 1913 |
free (file); |
| 1914 |
} |
| 1915 |
|
| 1916 |
/* Release all the files allocated by this reader. */ |
| 1917 |
static void |
| 1918 |
destroy_all_cpp_files (cpp_reader *pfile) |
| 1919 |
{ |
| 1920 |
_cpp_file *iter = pfile->all_files; |
| 1921 |
while (iter) |
| 1922 |
{ |
| 1923 |
_cpp_file *next = iter->next_file; |
| 1924 |
destroy_cpp_file (iter); |
| 1925 |
iter = next; |
| 1926 |
} |
| 1927 |
} |
| 1928 |
|
| 1929 |
/* A hash of directory names. The directory names are the path names |
| 1930 |
of files which contain a #include "", the included file name is |
| 1931 |
appended to this directories. |
| 1932 |
|
| 1933 |
To avoid duplicate entries we follow the convention that all |
| 1934 |
non-empty directory names should end in a '/'. DIR_NAME must be |
| 1935 |
stored in permanently allocated memory. */ |
| 1936 |
static cpp_dir * |
| 1937 |
make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp) |
| 1938 |
{ |
| 1939 |
struct cpp_file_hash_entry *entry, **hash_slot; |
| 1940 |
cpp_dir *dir; |
| 1941 |
|
| 1942 |
hash_slot = (struct cpp_file_hash_entry **) |
| 1943 |
htab_find_slot_with_hash (pfile->dir_hash, dir_name, |
| 1944 |
htab_hash_string (dir_name), |
| 1945 |
INSERT); |
| 1946 |
|
| 1947 |
/* Have we already hashed this directory? */ |
| 1948 |
for (entry = *hash_slot; entry; entry = entry->next) |
| 1949 |
if (entry->start_dir == NULL) |
| 1950 |
return entry->u.dir; |
| 1951 |
|
| 1952 |
dir = XCNEW (cpp_dir); |
| 1953 |
dir->next = pfile->quote_include; |
| 1954 |
dir->name = (char *) dir_name; |
| 1955 |
dir->len = strlen (dir_name); |
| 1956 |
dir->sysp = sysp; |
| 1957 |
dir->construct = 0; |
| 1958 |
|
| 1959 |
/* Store this new result in the hash table. */ |
| 1960 |
entry = new_file_hash_entry (pfile); |
| 1961 |
entry->next = *hash_slot; |
| 1962 |
entry->start_dir = NULL; |
| 1963 |
entry->location = pfile->line_table->highest_location; |
| 1964 |
entry->u.dir = dir; |
| 1965 |
*hash_slot = entry; |
| 1966 |
|
| 1967 |
return dir; |
| 1968 |
} |
| 1969 |
|
| 1970 |
/* Create a new block of memory for file hash entries. */ |
| 1971 |
static void |
| 1972 |
allocate_file_hash_entries (cpp_reader *pfile) |
| 1973 |
{ |
| 1974 |
struct file_hash_entry_pool *pool = XNEW (struct file_hash_entry_pool); |
| 1975 |
pool->file_hash_entries_used = 0; |
| 1976 |
pool->next = pfile->file_hash_entries; |
| 1977 |
pfile->file_hash_entries = pool; |
| 1978 |
} |
| 1979 |
|
| 1980 |
/* Return a new file hash entry. */ |
| 1981 |
static struct cpp_file_hash_entry * |
| 1982 |
new_file_hash_entry (cpp_reader *pfile) |
| 1983 |
{ |
| 1984 |
unsigned int idx; |
| 1985 |
if (pfile->file_hash_entries->file_hash_entries_used == FILE_HASH_POOL_SIZE) |
| 1986 |
allocate_file_hash_entries (pfile); |
| 1987 |
|
| 1988 |
idx = pfile->file_hash_entries->file_hash_entries_used++; |
| 1989 |
return &pfile->file_hash_entries->pool[idx]; |
| 1990 |
} |
| 1991 |
|
| 1992 |
/* Free the file hash entry pools. */ |
| 1993 |
static void |
| 1994 |
free_file_hash_entries (cpp_reader *pfile) |
| 1995 |
{ |
| 1996 |
struct file_hash_entry_pool *iter = pfile->file_hash_entries; |
| 1997 |
while (iter) |
| 1998 |
{ |
| 1999 |
struct file_hash_entry_pool *next = iter->next; |
| 2000 |
free (iter); |
| 2001 |
iter = next; |
| 2002 |
} |
| 2003 |
} |
| 2004 |
|
| 2005 |
/* Returns TRUE if a file FNAME has ever been successfully opened. |
| 2006 |
This routine is not intended to correctly handle filenames aliased |
| 2007 |
by links or redundant . or .. traversals etc. */ |
| 2008 |
bool |
| 2009 |
cpp_included (cpp_reader *pfile, const char *fname) |
| 2010 |
{ |
| 2011 |
struct cpp_file_hash_entry *entry; |
| 2012 |
|
| 2013 |
entry = (struct cpp_file_hash_entry *) |
| 2014 |
htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname)); |
| 2015 |
|
| 2016 |
while (entry && (entry->start_dir == NULL || entry->u.file->err_no)) |
| 2017 |
entry = entry->next; |
| 2018 |
|
| 2019 |
return entry != NULL; |
| 2020 |
} |
| 2021 |
|
| 2022 |
/* Returns TRUE if a file FNAME has ever been successfully opened |
| 2023 |
before LOCATION. This routine is not intended to correctly handle |
| 2024 |
filenames aliased by links or redundant . or .. traversals etc. */ |
| 2025 |
bool |
| 2026 |
cpp_included_before (cpp_reader *pfile, const char *fname, |
| 2027 |
location_t location) |
| 2028 |
{ |
| 2029 |
struct cpp_file_hash_entry *entry |
| 2030 |
= (struct cpp_file_hash_entry *) |
| 2031 |
htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname)); |
| 2032 |
|
| 2033 |
if (IS_ADHOC_LOC (location)) |
| 2034 |
location = get_location_from_adhoc_loc (pfile->line_table, location); |
| 2035 |
|
| 2036 |
while (entry && (entry->start_dir == NULL || entry->u.file->err_no |
| 2037 |
|| entry->location > location)) |
| 2038 |
entry = entry->next; |
| 2039 |
|
| 2040 |
return entry != NULL; |
| 2041 |
} |
| 2042 |
|
| 2043 |
/* Calculate the hash value of a file hash entry P. */ |
| 2044 |
|
| 2045 |
static hashval_t |
| 2046 |
file_hash_hash (const void *p) |
| 2047 |
{ |
| 2048 |
struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) p; |
| 2049 |
const char *hname; |
| 2050 |
if (entry->start_dir) |
| 2051 |
hname = entry->u.file->name; |
| 2052 |
else |
| 2053 |
hname = entry->u.dir->name; |
| 2054 |
|
| 2055 |
return htab_hash_string (hname); |
| 2056 |
} |
| 2057 |
|
| 2058 |
/* Compare a string Q against a file hash entry P. */ |
| 2059 |
static int |
| 2060 |
file_hash_eq (const void *p, const void *q) |
| 2061 |
{ |
| 2062 |
struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) p; |
| 2063 |
const char *fname = (const char *) q; |
| 2064 |
const char *hname; |
| 2065 |
|
| 2066 |
if (entry->start_dir) |
| 2067 |
hname = entry->u.file->name; |
| 2068 |
else |
| 2069 |
hname = entry->u.dir->name; |
| 2070 |
|
| 2071 |
return filename_cmp (hname, fname) == 0; |
| 2072 |
} |
| 2073 |
|
| 2074 |
/* Compare entries in the nonexistent file hash table. These are just |
| 2075 |
strings. */ |
| 2076 |
static int |
| 2077 |
nonexistent_file_hash_eq (const void *p, const void *q) |
| 2078 |
{ |
| 2079 |
return filename_cmp ((const char *) p, (const char *) q) == 0; |
| 2080 |
} |
| 2081 |
|
| 2082 |
/* Initialize everything in this source file. */ |
| 2083 |
void |
| 2084 |
_cpp_init_files (cpp_reader *pfile) |
| 2085 |
{ |
| 2086 |
pfile->file_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq, |
| 2087 |
NULL, xcalloc, free); |
| 2088 |
pfile->dir_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq, |
| 2089 |
NULL, xcalloc, free); |
| 2090 |
allocate_file_hash_entries (pfile); |
| 2091 |
pfile->nonexistent_file_hash = htab_create_alloc (127, htab_hash_string, |
| 2092 |
nonexistent_file_hash_eq, |
| 2093 |
NULL, xcalloc, free); |
| 2094 |
obstack_specify_allocation (&pfile->nonexistent_file_ob, 0, 0, |
| 2095 |
xmalloc, free); |
| 2096 |
} |
| 2097 |
|
| 2098 |
/* Finalize everything in this source file. */ |
| 2099 |
void |
| 2100 |
_cpp_cleanup_files (cpp_reader *pfile) |
| 2101 |
{ |
| 2102 |
htab_delete (pfile->file_hash); |
| 2103 |
htab_delete (pfile->dir_hash); |
| 2104 |
htab_delete (pfile->nonexistent_file_hash); |
| 2105 |
obstack_free (&pfile->nonexistent_file_ob, 0); |
| 2106 |
free_file_hash_entries (pfile); |
| 2107 |
destroy_all_cpp_files (pfile); |
| 2108 |
} |
| 2109 |
|
| 2110 |
/* Make the parser forget about files it has seen. This can be useful |
| 2111 |
for resetting the parser to start another run. */ |
| 2112 |
void |
| 2113 |
cpp_clear_file_cache (cpp_reader *pfile) |
| 2114 |
{ |
| 2115 |
_cpp_cleanup_files (pfile); |
| 2116 |
pfile->file_hash_entries = NULL; |
| 2117 |
pfile->all_files = NULL; |
| 2118 |
_cpp_init_files (pfile); |
| 2119 |
} |
| 2120 |
|
| 2121 |
/* Enter a file name in the hash for the sake of cpp_included. */ |
| 2122 |
void |
| 2123 |
_cpp_fake_include (cpp_reader *pfile, const char *fname) |
| 2124 |
{ |
| 2125 |
/* It does not matter what are the contents of fake_source_dir, it will never |
| 2126 |
be inspected; we just use its address to uniquely signify that this file |
| 2127 |
was added as a fake include, so a later call to _cpp_find_file (to include |
| 2128 |
the file for real) won't find the fake one in the hash table. */ |
| 2129 |
static cpp_dir fake_source_dir; |
| 2130 |
_cpp_find_file (pfile, fname, &fake_source_dir, 0, _cpp_FFK_FAKE, 0); |
| 2131 |
} |
| 2132 |
|
| 2133 |
/* Not everyone who wants to set system-header-ness on a buffer can |
| 2134 |
see the details of a buffer. This is an exported interface because |
| 2135 |
fix-header needs it. */ |
| 2136 |
void |
| 2137 |
cpp_make_system_header (cpp_reader *pfile, int syshdr, int externc) |
| 2138 |
{ |
| 2139 |
int flags = 0; |
| 2140 |
const class line_maps *line_table = pfile->line_table; |
| 2141 |
const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
| 2142 |
/* 1 = system header, 2 = system header to be treated as C. */ |
| 2143 |
if (syshdr) |
| 2144 |
flags = 1 + (externc != 0); |
| 2145 |
pfile->buffer->sysp = flags; |
| 2146 |
_cpp_do_file_change (pfile, LC_RENAME, ORDINARY_MAP_FILE_NAME (map), |
| 2147 |
SOURCE_LINE (map, pfile->line_table->highest_line), |
| 2148 |
flags); |
| 2149 |
} |
| 2150 |
|
| 2151 |
/* Allow the client to change the current file. Used by the front end |
| 2152 |
to achieve pseudo-file names like <built-in>. |
| 2153 |
If REASON is LC_LEAVE, then NEW_NAME must be NULL. */ |
| 2154 |
void |
| 2155 |
cpp_change_file (cpp_reader *pfile, enum lc_reason reason, |
| 2156 |
const char *new_name) |
| 2157 |
{ |
| 2158 |
_cpp_do_file_change (pfile, reason, new_name, 1, 0); |
| 2159 |
} |
| 2160 |
|
| 2161 |
struct report_missing_guard_data |
| 2162 |
{ |
| 2163 |
cpp_reader *pfile; |
| 2164 |
const char **paths; |
| 2165 |
size_t count; |
| 2166 |
}; |
| 2167 |
|
| 2168 |
/* Callback function for htab_traverse. */ |
| 2169 |
static int |
| 2170 |
report_missing_guard (void **slot, void *d) |
| 2171 |
{ |
| 2172 |
struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) *slot; |
| 2173 |
struct report_missing_guard_data *data |
| 2174 |
= (struct report_missing_guard_data *) d; |
| 2175 |
|
| 2176 |
/* Skip directories. */ |
| 2177 |
if (entry->start_dir != NULL) |
| 2178 |
{ |
| 2179 |
_cpp_file *file = entry->u.file; |
| 2180 |
|
| 2181 |
/* We don't want MI guard advice for the main file. */ |
| 2182 |
if (!file->once_only |
| 2183 |
&& file->cmacro == NULL |
| 2184 |
&& file->stack_count == 1 |
| 2185 |
&& data->pfile->main_file != file) |
| 2186 |
{ |
| 2187 |
if (data->paths == NULL) |
| 2188 |
{ |
| 2189 |
data->paths = XCNEWVEC (const char *, data->count); |
| 2190 |
data->count = 0; |
| 2191 |
} |
| 2192 |
|
| 2193 |
data->paths[data->count++] = file->path; |
| 2194 |
} |
| 2195 |
} |
| 2196 |
|
| 2197 |
/* Keep traversing the hash table. */ |
| 2198 |
return 1; |
| 2199 |
} |
| 2200 |
|
| 2201 |
/* Comparison function for qsort. */ |
| 2202 |
static int |
| 2203 |
report_missing_guard_cmp (const void *p1, const void *p2) |
| 2204 |
{ |
| 2205 |
return strcmp (*(const char *const *) p1, *(const char *const *) p2); |
| 2206 |
} |
| 2207 |
|
| 2208 |
/* Report on all files that might benefit from a multiple include guard. |
| 2209 |
Triggered by -H. */ |
| 2210 |
void |
| 2211 |
_cpp_report_missing_guards (cpp_reader *pfile) |
| 2212 |
{ |
| 2213 |
struct report_missing_guard_data data; |
| 2214 |
|
| 2215 |
data.pfile = pfile; |
| 2216 |
data.paths = NULL; |
| 2217 |
data.count = htab_elements (pfile->file_hash); |
| 2218 |
htab_traverse (pfile->file_hash, report_missing_guard, &data); |
| 2219 |
|
| 2220 |
if (data.paths != NULL) |
| 2221 |
{ |
| 2222 |
size_t i; |
| 2223 |
|
| 2224 |
/* Sort the paths to avoid outputting them in hash table |
| 2225 |
order. */ |
| 2226 |
qsort (data.paths, data.count, sizeof (const char *), |
| 2227 |
report_missing_guard_cmp); |
| 2228 |
fputs (_("Multiple include guards may be useful for:\n"), |
| 2229 |
stderr); |
| 2230 |
for (i = 0; i < data.count; i++) |
| 2231 |
{ |
| 2232 |
fputs (data.paths[i], stderr); |
| 2233 |
putc ('\n', stderr); |
| 2234 |
} |
| 2235 |
free (data.paths); |
| 2236 |
} |
| 2237 |
} |
| 2238 |
|
| 2239 |
/* Locate HEADER, and determine whether it is newer than the current |
| 2240 |
file. If it cannot be located or dated, return -1, if it is |
| 2241 |
newer, return 1, otherwise 0. */ |
| 2242 |
int |
| 2243 |
_cpp_compare_file_date (cpp_reader *pfile, const char *fname, |
| 2244 |
int angle_brackets) |
| 2245 |
{ |
| 2246 |
_cpp_file *file; |
| 2247 |
struct cpp_dir *dir; |
| 2248 |
|
| 2249 |
dir = search_path_head (pfile, fname, angle_brackets, IT_INCLUDE); |
| 2250 |
if (!dir) |
| 2251 |
return -1; |
| 2252 |
|
| 2253 |
file = _cpp_find_file (pfile, fname, dir, angle_brackets, _cpp_FFK_NORMAL, 0); |
| 2254 |
if (file->err_no) |
| 2255 |
return -1; |
| 2256 |
|
| 2257 |
if (file->fd != -1) |
| 2258 |
{ |
| 2259 |
close (file->fd); |
| 2260 |
file->fd = -1; |
| 2261 |
} |
| 2262 |
|
| 2263 |
return file->st.st_mtime > pfile->buffer->file->st.st_mtime; |
| 2264 |
} |
| 2265 |
|
| 2266 |
/* Pushes the given file onto the buffer stack. Returns nonzero if |
| 2267 |
successful. */ |
| 2268 |
bool |
| 2269 |
cpp_push_include (cpp_reader *pfile, const char *fname) |
| 2270 |
{ |
| 2271 |
return _cpp_stack_include (pfile, fname, false, IT_CMDLINE, |
| 2272 |
pfile->line_table->highest_line); |
| 2273 |
} |
| 2274 |
|
| 2275 |
/* Pushes the given file, implicitly included at the start of a |
| 2276 |
compilation, onto the buffer stack but without any errors if the |
| 2277 |
file is not found. Returns nonzero if successful. */ |
| 2278 |
bool |
| 2279 |
cpp_push_default_include (cpp_reader *pfile, const char *fname) |
| 2280 |
{ |
| 2281 |
return _cpp_stack_include (pfile, fname, true, IT_DEFAULT, |
| 2282 |
pfile->line_table->highest_line); |
| 2283 |
} |
| 2284 |
|
| 2285 |
/* Do appropriate cleanup when a file INC's buffer is popped off the |
| 2286 |
input stack. */ |
| 2287 |
void |
| 2288 |
_cpp_pop_file_buffer (cpp_reader *pfile, _cpp_file *file, |
| 2289 |
const unsigned char *to_free) |
| 2290 |
{ |
| 2291 |
/* Record the inclusion-preventing macro, which could be NULL |
| 2292 |
meaning no controlling macro. */ |
| 2293 |
if (pfile->mi_valid && file->cmacro == NULL) |
| 2294 |
{ |
| 2295 |
file->cmacro = pfile->mi_cmacro; |
| 2296 |
if (pfile->mi_cmacro |
| 2297 |
&& pfile->mi_def_cmacro |
| 2298 |
&& pfile->cb.get_suggestion) |
| 2299 |
{ |
| 2300 |
auto mi_cmacro = (const char *) NODE_NAME (pfile->mi_cmacro); |
| 2301 |
auto mi_def_cmacro = (const char *) NODE_NAME (pfile->mi_def_cmacro); |
| 2302 |
const char *names[] = { mi_def_cmacro, NULL }; |
| 2303 |
if (pfile->cb.get_suggestion (pfile, mi_cmacro, names) |
| 2304 |
&& cpp_warning_with_line (pfile, CPP_W_HEADER_GUARD, |
| 2305 |
pfile->mi_loc, 0, |
| 2306 |
"header guard %qs followed by " |
| 2307 |
"%<#define%> of a different macro", |
| 2308 |
mi_cmacro)) |
| 2309 |
cpp_error_at (pfile, CPP_DL_NOTE, pfile->mi_def_loc, |
| 2310 |
"%qs is defined here; did you mean %qs?", |
| 2311 |
mi_def_cmacro, mi_cmacro); |
| 2312 |
} |
| 2313 |
} |
| 2314 |
|
| 2315 |
/* Invalidate control macros in the #including file. */ |
| 2316 |
pfile->mi_valid = false; |
| 2317 |
|
| 2318 |
if (to_free) |
| 2319 |
{ |
| 2320 |
if (to_free == file->buffer_start) |
| 2321 |
{ |
| 2322 |
file->buffer_start = NULL; |
| 2323 |
file->buffer = NULL; |
| 2324 |
file->buffer_valid = false; |
| 2325 |
} |
| 2326 |
free ((void *) to_free); |
| 2327 |
} |
| 2328 |
} |
| 2329 |
|
| 2330 |
/* Return the file name associated with FILE. */ |
| 2331 |
const char * |
| 2332 |
_cpp_get_file_name (_cpp_file *file) |
| 2333 |
{ |
| 2334 |
return file->name; |
| 2335 |
} |
| 2336 |
|
| 2337 |
/* Inteface to file statistics record in _cpp_file structure. */ |
| 2338 |
struct stat * |
| 2339 |
_cpp_get_file_stat (_cpp_file *file) |
| 2340 |
{ |
| 2341 |
return &file->st; |
| 2342 |
} |
| 2343 |
|
| 2344 |
/* Return the directory where FILE was found. */ |
| 2345 |
struct cpp_dir * |
| 2346 |
_cpp_get_file_dir (_cpp_file *file) |
| 2347 |
{ |
| 2348 |
return file->dir; |
| 2349 |
} |
| 2350 |
|
| 2351 |
/* Set the include chain for "" to QUOTE, for <> to BRACKET. If |
| 2352 |
QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the |
| 2353 |
directory of the including file. |
| 2354 |
|
| 2355 |
If BRACKET does not lie in the QUOTE chain, it is set to QUOTE. |
| 2356 |
|
| 2357 |
EMBED is include chain for #embed <>. */ |
| 2358 |
void |
| 2359 |
cpp_set_include_chains (cpp_reader *pfile, cpp_dir *quote, cpp_dir *bracket, |
| 2360 |
cpp_dir *embed, int quote_ignores_source_dir) |
| 2361 |
{ |
| 2362 |
pfile->quote_include = quote; |
| 2363 |
pfile->bracket_include = quote; |
| 2364 |
pfile->quote_ignores_source_dir = quote_ignores_source_dir; |
| 2365 |
pfile->embed_include = embed; |
| 2366 |
|
| 2367 |
for (; quote; quote = quote->next) |
| 2368 |
{ |
| 2369 |
quote->name_map = NULL; |
| 2370 |
quote->len = strlen (quote->name); |
| 2371 |
if (quote == bracket) |
| 2372 |
pfile->bracket_include = bracket; |
| 2373 |
} |
| 2374 |
for (; embed; embed = embed->next) |
| 2375 |
{ |
| 2376 |
embed->name_map = NULL; |
| 2377 |
embed->len = strlen (embed->name); |
| 2378 |
} |
| 2379 |
} |
| 2380 |
|
| 2381 |
/* Append the file name to the directory to create the path, but don't |
| 2382 |
turn / into // or // into ///; // may be a namespace escape. */ |
| 2383 |
static char * |
| 2384 |
append_file_to_dir (const char *fname, cpp_dir *dir) |
| 2385 |
{ |
| 2386 |
size_t dlen, flen; |
| 2387 |
char *path; |
| 2388 |
|
| 2389 |
dlen = dir->len; |
| 2390 |
flen = strlen (fname); |
| 2391 |
path = XNEWVEC (char, dlen + 1 + flen + 1); |
| 2392 |
memcpy (path, dir->name, dlen); |
| 2393 |
if (dlen && !IS_DIR_SEPARATOR (path[dlen - 1])) |
| 2394 |
path[dlen++] = '/'; |
| 2395 |
memcpy (&path[dlen], fname, flen + 1); |
| 2396 |
|
| 2397 |
return path; |
| 2398 |
} |
| 2399 |
|
| 2400 |
/* Read a space delimited string of unlimited length from a stdio |
| 2401 |
file F. */ |
| 2402 |
static char * |
| 2403 |
read_filename_string (int ch, FILE *f) |
| 2404 |
{ |
| 2405 |
char *alloc, *set; |
| 2406 |
int len; |
| 2407 |
|
| 2408 |
len = 20; |
| 2409 |
set = alloc = XNEWVEC (char, len + 1); |
| 2410 |
if (! is_space (ch)) |
| 2411 |
{ |
| 2412 |
*set++ = ch; |
| 2413 |
while ((ch = getc (f)) != EOF && ! is_space (ch)) |
| 2414 |
{ |
| 2415 |
if (set - alloc == len) |
| 2416 |
{ |
| 2417 |
len *= 2; |
| 2418 |
alloc = XRESIZEVEC (char, alloc, len + 1); |
| 2419 |
set = alloc + len / 2; |
| 2420 |
} |
| 2421 |
*set++ = ch; |
| 2422 |
} |
| 2423 |
} |
| 2424 |
*set = '\0'; |
| 2425 |
ungetc (ch, f); |
| 2426 |
return alloc; |
| 2427 |
} |
| 2428 |
|
| 2429 |
/* Read the file name map file for DIR. */ |
| 2430 |
static void |
| 2431 |
read_name_map (cpp_dir *dir) |
| 2432 |
{ |
| 2433 |
static const char FILE_NAME_MAP_FILE[] = "header.gcc"; |
| 2434 |
char *name; |
| 2435 |
FILE *f; |
| 2436 |
size_t len, count = 0, room = 9; |
| 2437 |
|
| 2438 |
len = dir->len; |
| 2439 |
name = (char *) alloca (len + sizeof (FILE_NAME_MAP_FILE) + 1); |
| 2440 |
memcpy (name, dir->name, len); |
| 2441 |
if (len && !IS_DIR_SEPARATOR (name[len - 1])) |
| 2442 |
name[len++] = '/'; |
| 2443 |
strcpy (name + len, FILE_NAME_MAP_FILE); |
| 2444 |
f = fopen (name, "r"); |
| 2445 |
|
| 2446 |
dir->name_map = XNEWVEC (const char *, room); |
| 2447 |
|
| 2448 |
/* Silently return NULL if we cannot open. */ |
| 2449 |
if (f) |
| 2450 |
{ |
| 2451 |
int ch; |
| 2452 |
|
| 2453 |
while ((ch = getc (f)) != EOF) |
| 2454 |
{ |
| 2455 |
char *to; |
| 2456 |
|
| 2457 |
if (is_space (ch)) |
| 2458 |
continue; |
| 2459 |
|
| 2460 |
if (count + 2 > room) |
| 2461 |
{ |
| 2462 |
room += 8; |
| 2463 |
dir->name_map = XRESIZEVEC (const char *, dir->name_map, room); |
| 2464 |
} |
| 2465 |
|
| 2466 |
dir->name_map[count] = read_filename_string (ch, f); |
| 2467 |
while ((ch = getc (f)) != EOF && is_hspace (ch)) |
| 2468 |
; |
| 2469 |
|
| 2470 |
to = read_filename_string (ch, f); |
| 2471 |
if (IS_ABSOLUTE_PATH (to)) |
| 2472 |
dir->name_map[count + 1] = to; |
| 2473 |
else |
| 2474 |
{ |
| 2475 |
dir->name_map[count + 1] = append_file_to_dir (to, dir); |
| 2476 |
free (to); |
| 2477 |
} |
| 2478 |
|
| 2479 |
count += 2; |
| 2480 |
while ((ch = getc (f)) != '\n') |
| 2481 |
if (ch == EOF) |
| 2482 |
break; |
| 2483 |
} |
| 2484 |
|
| 2485 |
fclose (f); |
| 2486 |
} |
| 2487 |
|
| 2488 |
/* Terminate the list of maps. */ |
| 2489 |
dir->name_map[count] = NULL; |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* Remap a FILE's name based on the file_name_map, if any, for |
| 2493 |
FILE->dir. If the file name has any directory separators, |
| 2494 |
recursively check those directories too. */ |
| 2495 |
static char * |
| 2496 |
remap_filename (cpp_reader *pfile, _cpp_file *file) |
| 2497 |
{ |
| 2498 |
const char *fname, *p; |
| 2499 |
char *new_dir, *p3; |
| 2500 |
cpp_dir *dir; |
| 2501 |
size_t index, len; |
| 2502 |
|
| 2503 |
dir = file->dir; |
| 2504 |
fname = file->name; |
| 2505 |
|
| 2506 |
for (;;) |
| 2507 |
{ |
| 2508 |
if (!dir->name_map) |
| 2509 |
read_name_map (dir); |
| 2510 |
|
| 2511 |
for (index = 0; dir->name_map[index]; index += 2) |
| 2512 |
if (!filename_cmp (dir->name_map[index], fname)) |
| 2513 |
return xstrdup (dir->name_map[index + 1]); |
| 2514 |
if (IS_ABSOLUTE_PATH (fname)) |
| 2515 |
return NULL; |
| 2516 |
p = strchr (fname, '/'); |
| 2517 |
#ifdef HAVE_DOS_BASED_FILE_SYSTEM |
| 2518 |
{ |
| 2519 |
const char *p2 = strchr (fname, '\\'); |
| 2520 |
if (!p || (p2 && p > p2)) |
| 2521 |
p = p2; |
| 2522 |
} |
| 2523 |
#endif |
| 2524 |
if (!p || p == fname) |
| 2525 |
return NULL; |
| 2526 |
|
| 2527 |
len = dir->len + (p - fname + 1); |
| 2528 |
new_dir = XNEWVEC (char, len + 2); |
| 2529 |
p3 = new_dir + dir->len; |
| 2530 |
memcpy (new_dir, dir->name, dir->len); |
| 2531 |
if (dir->len && !IS_DIR_SEPARATOR (dir->name[dir->len - 1])) |
| 2532 |
{ |
| 2533 |
*p3++ = '/'; |
| 2534 |
len++; |
| 2535 |
} |
| 2536 |
memcpy (p3, fname, p - fname + 1); |
| 2537 |
new_dir[len] = '\0'; |
| 2538 |
|
| 2539 |
dir = make_cpp_dir (pfile, new_dir, dir->sysp); |
| 2540 |
fname = p + 1; |
| 2541 |
} |
| 2542 |
} |
| 2543 |
|
| 2544 |
/* Returns true if PCHNAME is a valid PCH file for FILE. */ |
| 2545 |
static bool |
| 2546 |
validate_pch (cpp_reader *pfile, _cpp_file *file, const char *pchname) |
| 2547 |
{ |
| 2548 |
const char *saved_path = file->path; |
| 2549 |
bool valid = false; |
| 2550 |
|
| 2551 |
file->path = pchname; |
| 2552 |
if (open_file (file)) |
| 2553 |
{ |
| 2554 |
valid = 1 & pfile->cb.valid_pch (pfile, pchname, file->fd); |
| 2555 |
|
| 2556 |
if (!valid) |
| 2557 |
{ |
| 2558 |
close (file->fd); |
| 2559 |
file->fd = -1; |
| 2560 |
} |
| 2561 |
|
| 2562 |
if (CPP_OPTION (pfile, print_include_names)) |
| 2563 |
{ |
| 2564 |
unsigned int i; |
| 2565 |
for (i = 1; i < pfile->line_table->depth; i++) |
| 2566 |
putc ('.', stderr); |
| 2567 |
fprintf (stderr, "%c %s\n", |
| 2568 |
valid ? '!' : 'x', pchname); |
| 2569 |
} |
| 2570 |
} |
| 2571 |
|
| 2572 |
file->path = saved_path; |
| 2573 |
return valid; |
| 2574 |
} |
| 2575 |
|
| 2576 |
/* Get the path associated with the _cpp_file F. The path includes |
| 2577 |
the base name from the include directive and the directory it was |
| 2578 |
found in via the search path. */ |
| 2579 |
|
| 2580 |
const char * |
| 2581 |
cpp_get_path (struct _cpp_file *f) |
| 2582 |
{ |
| 2583 |
return f->path; |
| 2584 |
} |
| 2585 |
|
| 2586 |
/* Get the directory associated with the _cpp_file F. */ |
| 2587 |
|
| 2588 |
cpp_dir * |
| 2589 |
cpp_get_dir (struct _cpp_file *f) |
| 2590 |
{ |
| 2591 |
return f->dir; |
| 2592 |
} |
| 2593 |
|
| 2594 |
/* Get the cpp_buffer currently associated with the cpp_reader |
| 2595 |
PFILE. */ |
| 2596 |
|
| 2597 |
cpp_buffer * |
| 2598 |
cpp_get_buffer (cpp_reader *pfile) |
| 2599 |
{ |
| 2600 |
return pfile->buffer; |
| 2601 |
} |
| 2602 |
|
| 2603 |
/* Get the _cpp_file associated with the cpp_buffer B. */ |
| 2604 |
|
| 2605 |
_cpp_file * |
| 2606 |
cpp_get_file (cpp_buffer *b) |
| 2607 |
{ |
| 2608 |
return b->file; |
| 2609 |
} |
| 2610 |
|
| 2611 |
/* Get the previous cpp_buffer given a cpp_buffer B. The previous |
| 2612 |
buffer is the buffer that included the given buffer. */ |
| 2613 |
|
| 2614 |
cpp_buffer * |
| 2615 |
cpp_get_prev (cpp_buffer *b) |
| 2616 |
{ |
| 2617 |
return b->prev; |
| 2618 |
} |
| 2619 |
|
| 2620 |
/* This data structure holds the list of header files that were seen |
| 2621 |
while the PCH was being built. The 'entries' field is kept sorted |
| 2622 |
in memcmp() order; yes, this means that on little-endian systems, |
| 2623 |
it's sorted initially by the least-significant byte of 'size', but |
| 2624 |
that's OK. The code does rely on having entries with the same size |
| 2625 |
next to each other. */ |
| 2626 |
|
| 2627 |
struct pchf_entry { |
| 2628 |
/* The size of this file. This is used to save running a MD5 checksum |
| 2629 |
if the sizes don't match. */ |
| 2630 |
off_t size; |
| 2631 |
/* The MD5 checksum of this file. */ |
| 2632 |
unsigned char sum[16]; |
| 2633 |
/* Is this file to be included only once? */ |
| 2634 |
bool once_only; |
| 2635 |
}; |
| 2636 |
|
| 2637 |
struct pchf_data { |
| 2638 |
/* Number of pchf_entry structures. */ |
| 2639 |
size_t count; |
| 2640 |
|
| 2641 |
/* Are there any values with once_only set? |
| 2642 |
This is used as an optimisation, it means we don't have to search |
| 2643 |
the structure if we're processing a regular #include. */ |
| 2644 |
bool have_once_only; |
| 2645 |
|
| 2646 |
struct pchf_entry entries[1]; |
| 2647 |
}; |
| 2648 |
|
| 2649 |
static struct pchf_data *pchf; |
| 2650 |
|
| 2651 |
/* A qsort ordering function for pchf_entry structures. */ |
| 2652 |
|
| 2653 |
static int |
| 2654 |
pchf_save_compare (const void *e1, const void *e2) |
| 2655 |
{ |
| 2656 |
return memcmp (e1, e2, sizeof (struct pchf_entry)); |
| 2657 |
} |
| 2658 |
|
| 2659 |
/* Create and write to F a pchf_data structure. */ |
| 2660 |
|
| 2661 |
bool |
| 2662 |
_cpp_save_file_entries (cpp_reader *pfile, FILE *fp) |
| 2663 |
{ |
| 2664 |
size_t count = 0; |
| 2665 |
struct pchf_data *result; |
| 2666 |
size_t result_size; |
| 2667 |
_cpp_file *f; |
| 2668 |
bool ret; |
| 2669 |
|
| 2670 |
for (f = pfile->all_files; f; f = f->next_file) |
| 2671 |
++count; |
| 2672 |
|
| 2673 |
result_size = (sizeof (struct pchf_data) |
| 2674 |
+ sizeof (struct pchf_entry) * (count - 1)); |
| 2675 |
result = XCNEWVAR (struct pchf_data, result_size); |
| 2676 |
|
| 2677 |
result->count = 0; |
| 2678 |
result->have_once_only = false; |
| 2679 |
|
| 2680 |
for (f = pfile->all_files; f; f = f->next_file) |
| 2681 |
{ |
| 2682 |
size_t count; |
| 2683 |
|
| 2684 |
/* This should probably never happen, since if a read error occurred |
| 2685 |
the PCH file shouldn't be written... */ |
| 2686 |
if (f->dont_read || f->err_no) |
| 2687 |
continue; |
| 2688 |
|
| 2689 |
if (f->stack_count == 0) |
| 2690 |
continue; |
| 2691 |
|
| 2692 |
count = result->count++; |
| 2693 |
|
| 2694 |
result->entries[count].once_only = f->once_only; |
| 2695 |
/* |= is avoided in the next line because of an HP C compiler bug */ |
| 2696 |
result->have_once_only = result->have_once_only | f->once_only; |
| 2697 |
if (f->buffer_valid) |
| 2698 |
md5_buffer ((const char *)f->buffer, |
| 2699 |
f->st.st_size, result->entries[count].sum); |
| 2700 |
else |
| 2701 |
{ |
| 2702 |
FILE *ff; |
| 2703 |
int oldfd = f->fd; |
| 2704 |
|
| 2705 |
if (!open_file (f)) |
| 2706 |
{ |
| 2707 |
open_file_failed (pfile, f, 0, 0); |
| 2708 |
free (result); |
| 2709 |
return false; |
| 2710 |
} |
| 2711 |
ff = fdopen (f->fd, "rb"); |
| 2712 |
md5_stream (ff, result->entries[count].sum); |
| 2713 |
fclose (ff); |
| 2714 |
f->fd = oldfd; |
| 2715 |
} |
| 2716 |
result->entries[count].size = f->st.st_size; |
| 2717 |
} |
| 2718 |
|
| 2719 |
result_size = (sizeof (struct pchf_data) |
| 2720 |
+ sizeof (struct pchf_entry) * (result->count - 1)); |
| 2721 |
|
| 2722 |
qsort (result->entries, result->count, sizeof (struct pchf_entry), |
| 2723 |
pchf_save_compare); |
| 2724 |
|
| 2725 |
ret = fwrite (result, result_size, 1, fp) == 1; |
| 2726 |
free (result); |
| 2727 |
return ret; |
| 2728 |
} |
| 2729 |
|
| 2730 |
/* Read the pchf_data structure from F. */ |
| 2731 |
|
| 2732 |
bool |
| 2733 |
_cpp_read_file_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f) |
| 2734 |
{ |
| 2735 |
struct pchf_data d; |
| 2736 |
|
| 2737 |
if (fread (&d, sizeof (struct pchf_data) - sizeof (struct pchf_entry), 1, f) |
| 2738 |
!= 1) |
| 2739 |
return false; |
| 2740 |
|
| 2741 |
pchf = XNEWVAR (struct pchf_data, sizeof (struct pchf_data) |
| 2742 |
+ sizeof (struct pchf_entry) * (d.count - 1)); |
| 2743 |
memcpy (pchf, &d, sizeof (struct pchf_data) - sizeof (struct pchf_entry)); |
| 2744 |
if (fread (pchf->entries, sizeof (struct pchf_entry), d.count, f) |
| 2745 |
!= d.count) |
| 2746 |
return false; |
| 2747 |
return true; |
| 2748 |
} |
| 2749 |
|
| 2750 |
/* The parameters for pchf_compare. */ |
| 2751 |
|
| 2752 |
struct pchf_compare_data |
| 2753 |
{ |
| 2754 |
/* The size of the file we're looking for. */ |
| 2755 |
off_t size; |
| 2756 |
|
| 2757 |
/* The MD5 checksum of the file, if it's been computed. */ |
| 2758 |
unsigned char sum[16]; |
| 2759 |
|
| 2760 |
/* Is SUM valid? */ |
| 2761 |
bool sum_computed; |
| 2762 |
|
| 2763 |
/* Do we need to worry about entries that don't have ONCE_ONLY set? */ |
| 2764 |
bool check_included; |
| 2765 |
|
| 2766 |
/* The file that we're searching for. */ |
| 2767 |
_cpp_file *f; |
| 2768 |
}; |
| 2769 |
|
| 2770 |
/* bsearch comparison function; look for D_P in E_P. */ |
| 2771 |
|
| 2772 |
static int |
| 2773 |
pchf_compare (const void *d_p, const void *e_p) |
| 2774 |
{ |
| 2775 |
const struct pchf_entry *e = (const struct pchf_entry *)e_p; |
| 2776 |
struct pchf_compare_data *d = (struct pchf_compare_data *)d_p; |
| 2777 |
int result; |
| 2778 |
|
| 2779 |
result = memcmp (&d->size, &e->size, sizeof (off_t)); |
| 2780 |
if (result != 0) |
| 2781 |
return result; |
| 2782 |
|
| 2783 |
if (! d->sum_computed) |
| 2784 |
{ |
| 2785 |
_cpp_file *const f = d->f; |
| 2786 |
|
| 2787 |
md5_buffer ((const char *)f->buffer, f->st.st_size, d->sum); |
| 2788 |
d->sum_computed = true; |
| 2789 |
} |
| 2790 |
|
| 2791 |
result = memcmp (d->sum, e->sum, 16); |
| 2792 |
if (result != 0) |
| 2793 |
return result; |
| 2794 |
|
| 2795 |
if (d->check_included || e->once_only) |
| 2796 |
return 0; |
| 2797 |
else |
| 2798 |
return 1; |
| 2799 |
} |
| 2800 |
|
| 2801 |
/* Check that F is not in a list read from a PCH file (if any). |
| 2802 |
Assumes that f->buffer_valid is true. Return TRUE if the file |
| 2803 |
should not be read. */ |
| 2804 |
|
| 2805 |
static bool |
| 2806 |
check_file_against_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, |
| 2807 |
_cpp_file *f, |
| 2808 |
bool check_included) |
| 2809 |
{ |
| 2810 |
struct pchf_compare_data d; |
| 2811 |
|
| 2812 |
if (pchf == NULL |
| 2813 |
|| (! check_included && ! pchf->have_once_only)) |
| 2814 |
return false; |
| 2815 |
|
| 2816 |
d.size = f->st.st_size; |
| 2817 |
d.sum_computed = false; |
| 2818 |
d.f = f; |
| 2819 |
d.check_included = check_included; |
| 2820 |
return bsearch (&d, pchf->entries, pchf->count, sizeof (struct pchf_entry), |
| 2821 |
pchf_compare) != NULL; |
| 2822 |
} |
| 2823 |
|
| 2824 |
/* Return true if the file FNAME is found in the appropriate include file path |
| 2825 |
as indicated by ANGLE_BRACKETS. */ |
| 2826 |
|
| 2827 |
bool |
| 2828 |
_cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets, |
| 2829 |
enum include_type type) |
| 2830 |
{ |
| 2831 |
cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type, |
| 2832 |
/* suppress_diagnostic = */ true); |
| 2833 |
if (!start_dir) |
| 2834 |
return false; |
| 2835 |
_cpp_file *file = _cpp_find_file (pfile, fname, start_dir, angle_brackets, |
| 2836 |
_cpp_FFK_HAS_INCLUDE, 0); |
| 2837 |
return file->err_no != ENOENT; |
| 2838 |
} |
| 2839 |
|
| 2840 |
/* Read a file and convert to input charset, the same as if it were being read |
| 2841 |
by a cpp_reader. */ |
| 2842 |
|
| 2843 |
cpp_converted_source |
| 2844 |
cpp_get_converted_source (const char *fname, const char *input_charset) |
| 2845 |
{ |
| 2846 |
cpp_converted_source res = {}; |
| 2847 |
_cpp_file file = {}; |
| 2848 |
file.fd = -1; |
| 2849 |
file.name = lbasename (fname); |
| 2850 |
file.path = fname; |
| 2851 |
if (!open_file (&file)) |
| 2852 |
return res; |
| 2853 |
const bool ok = read_file_guts (NULL, &file, 0, input_charset); |
| 2854 |
close (file.fd); |
| 2855 |
if (!ok) |
| 2856 |
return res; |
| 2857 |
res.to_free = (char *) file.buffer_start; |
| 2858 |
res.data = (char *) file.buffer; |
| 2859 |
res.len = file.st.st_size; |
| 2860 |
return res; |
| 2861 |
} |