libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / libcpp / files.cc
blobfbbd59e62a3db3b5fd122952f9a22685fe956e10
1 /* Part of CPP library. File handling.
2 Copyright (C) 1986-2024 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
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.
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.
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/>. */
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>
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
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
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
56 /* Filename as given to #include or command line switch. */
57 const char *name;
59 /* The full path used to find the file. */
60 const char *path;
62 /* The full path of the pch file. */
63 const char *pchname;
65 /* The file's path with the basename stripped. NULL if it hasn't
66 been calculated yet. */
67 const char *dir_name;
69 /* Chain through all files. */
70 struct _cpp_file *next_file;
72 /* The contents of NAME after calling read_file(). */
73 const uchar *buffer;
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;
79 /* The macro, if any, preventing re-inclusion. */
80 const cpp_hashnode *cmacro;
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;
87 /* As filled in by stat(2) for the file. */
88 struct stat st;
90 /* Size for #embed, perhaps smaller than st.st_size. */
91 size_t limit;
93 /* Offset for #embed. */
94 off_t offset;
96 /* File descriptor. Invalid if -1, otherwise open. */
97 int fd;
99 /* Zero if this file was successfully opened and stat()-ed,
100 otherwise errno obtained from failure. */
101 int err_no;
103 /* Number of times the file has been stacked for preprocessing. */
104 unsigned short stack_count;
106 /* If opened with #import or contains #pragma once. */
107 bool once_only : 1;
109 /* If read() failed before. */
110 bool dont_read : 1;
112 /* If BUFFER above contains the true contents of the file. */
113 bool buffer_valid : 1;
115 /* If this file is implicitly preincluded. */
116 bool implicit_preinclude : 1;
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;
122 /* File loaded from #embed. */
123 bool embed : 1;
125 /* > 0: Known C++ Module header unit, <0: known not. ==0, unknown */
126 int header_unit : 2;
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.
135 FILE is a structure containing details about the file that was
136 found with that search, or details of how the search failed.
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.
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.
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.
156 struct cpp_file_hash_entry
158 struct cpp_file_hash_entry *next;
159 cpp_dir *start_dir;
160 location_t location;
161 union
163 _cpp_file *file;
164 cpp_dir *dir;
165 } u;
168 /* Number of entries to put in a cpp_file_hash_entry pool. */
169 #define FILE_HASH_POOL_SIZE 127
171 /* A file hash entry pool. We allocate cpp_file_hash_entry object from
172 one of these. */
173 struct file_hash_entry_pool
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];
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 struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
193 int angle_brackets, enum include_type,
194 bool suppress_diagnostic = false);
195 static const char *dir_name_of_file (_cpp_file *file);
196 static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int,
197 location_t);
198 static struct cpp_file_hash_entry *search_cache (struct cpp_file_hash_entry *head,
199 const cpp_dir *start_dir,
200 bool is_embed);
201 static _cpp_file *make_cpp_file (cpp_dir *, const char *fname);
202 static void destroy_cpp_file (_cpp_file *);
203 static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp);
204 static void allocate_file_hash_entries (cpp_reader *pfile);
205 static struct cpp_file_hash_entry *new_file_hash_entry (cpp_reader *pfile);
206 static int report_missing_guard (void **slot, void *b);
207 static hashval_t file_hash_hash (const void *p);
208 static int file_hash_eq (const void *p, const void *q);
209 static char *read_filename_string (int ch, FILE *f);
210 static void read_name_map (cpp_dir *dir);
211 static char *remap_filename (cpp_reader *pfile, _cpp_file *file);
212 static char *append_file_to_dir (const char *fname, cpp_dir *dir);
213 static bool validate_pch (cpp_reader *, _cpp_file *file, const char *pchname);
214 static int pchf_save_compare (const void *e1, const void *e2);
215 static int pchf_compare (const void *d_p, const void *e_p);
216 static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool);
218 /* Given a filename in FILE->PATH, with the empty string interpreted
219 as <stdin>, open it.
221 On success FILE contains an open file descriptor and stat
222 information for the file. On failure the file descriptor is -1 and
223 the appropriate errno is also stored in FILE. Returns TRUE iff
224 successful.
226 We used to open files in nonblocking mode, but that caused more
227 problems than it solved. Do take care not to acquire a controlling
228 terminal by mistake (this can't happen on sane systems, but
229 paranoia is a virtue).
231 Use the three-argument form of open even though we aren't
232 specifying O_CREAT, to defend against broken system headers.
234 O_BINARY tells some runtime libraries (notably DJGPP) not to do
235 newline translation; we can handle DOS line breaks just fine
236 ourselves. */
237 static bool
238 open_file (_cpp_file *file)
240 if (file->path[0] == '\0')
242 file->fd = 0;
243 set_stdin_to_binary_mode ();
245 else
246 file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY, 0666);
248 if (file->fd != -1)
250 if (fstat (file->fd, &file->st) == 0)
252 if (!S_ISDIR (file->st.st_mode))
254 file->err_no = 0;
255 return true;
258 /* Ignore a directory and continue the search. The file we're
259 looking for may be elsewhere in the search path. */
260 errno = ENOENT;
263 close (file->fd);
264 file->fd = -1;
266 #if defined(_WIN32) && !defined(__CYGWIN__)
267 else if (errno == EACCES)
269 /* On most UNIX systems, open succeeds on a directory. Above,
270 we check if we have opened a directory and if so, set errno
271 to ENOENT. However, on Windows, opening a directory
272 fails with EACCES. We want to return ENOENT in that
273 case too. */
274 if (stat (file->path, &file->st) == 0
275 && S_ISDIR (file->st.st_mode))
276 errno = ENOENT;
277 else
278 /* The call to stat may have reset errno. */
279 errno = EACCES;
281 #endif
282 else if (errno == ENOTDIR)
283 errno = ENOENT;
285 file->err_no = errno;
287 return false;
290 /* Temporary PCH intercept of opening a file. Try to find a PCH file
291 based on FILE->name and FILE->dir, and test those found for
292 validity using PFILE->cb.valid_pch. Return true iff a valid file is
293 found. Set *INVALID_PCH if a PCH file is found but wasn't valid. */
295 static bool
296 pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
298 static const char extension[] = ".gch";
299 const char *path = file->path;
300 size_t len, flen;
301 char *pchname;
302 struct stat st;
303 bool valid = false;
305 /* No PCH on <stdin> or if not requested. */
306 if (file->name[0] == '\0' || !pfile->cb.valid_pch)
307 return false;
309 /* If the file is not included as first include from either the toplevel
310 file or the command-line it is not a valid use of PCH. */
311 for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
312 if (f->implicit_preinclude)
313 continue;
314 else if (pfile->main_file == f)
315 break;
316 else
317 return false;
319 flen = strlen (path);
320 len = flen + sizeof (extension);
321 pchname = XNEWVEC (char, len);
322 memcpy (pchname, path, flen);
323 memcpy (pchname + flen, extension, sizeof (extension));
325 if (stat (pchname, &st) == 0)
327 DIR *pchdir;
328 struct dirent *d;
329 size_t dlen, plen = len;
331 if (!S_ISDIR (st.st_mode))
332 valid = validate_pch (pfile, file, pchname);
333 else if ((pchdir = opendir (pchname)) != NULL)
335 pchname[plen - 1] = '/';
336 while ((d = readdir (pchdir)) != NULL)
338 dlen = strlen (d->d_name) + 1;
339 if ((strcmp (d->d_name, ".") == 0)
340 || (strcmp (d->d_name, "..") == 0))
341 continue;
342 if (dlen + plen > len)
344 len += dlen + 64;
345 pchname = XRESIZEVEC (char, pchname, len);
347 memcpy (pchname + plen, d->d_name, dlen);
348 valid = validate_pch (pfile, file, pchname);
349 if (valid)
350 break;
352 closedir (pchdir);
354 if (!valid)
355 *invalid_pch = true;
358 if (valid)
359 file->pchname = pchname;
360 else
361 free (pchname);
363 return valid;
366 /* Canonicalize the path to FILE. Return the canonical form if it is
367 shorter, otherwise return NULL. This function does NOT free the
368 memory pointed by FILE. */
370 static char *
371 maybe_shorter_path (const char * file)
373 char * file2 = lrealpath (file);
374 if (file2 && strlen (file2) < strlen (file))
376 return file2;
378 else
380 free (file2);
381 return NULL;
385 /* Try to open the path FILE->name appended to FILE->dir. This is
386 where remap and PCH intercept the file lookup process. Return true
387 if the file was found, whether or not the open was successful.
388 Set *INVALID_PCH to true if a PCH file is found but wasn't valid.
389 Use LOC when emitting any diagnostics. */
391 static bool
392 find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch,
393 location_t loc)
395 char *path;
397 if (CPP_OPTION (pfile, remap) && (path = remap_filename (pfile, file)))
399 else
400 if (file->dir->construct)
401 path = file->dir->construct (file->name, file->dir);
402 else
403 path = append_file_to_dir (file->name, file->dir);
405 if (path)
407 hashval_t hv;
408 char *copy;
409 void **pp;
411 /* We try to canonicalize system headers. For DOS based file
412 * system, we always try to shorten non-system headers, as DOS
413 * has a tighter constraint on max path length. */
414 if ((CPP_OPTION (pfile, canonical_system_headers) && file->dir->sysp)
415 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
416 || !file->dir->sysp
417 #endif
420 char * canonical_path = maybe_shorter_path (path);
421 if (canonical_path)
423 /* The canonical path was newly allocated. Let's free the
424 non-canonical one. */
425 free (path);
426 path = canonical_path;
430 hv = htab_hash_string (path);
431 if (htab_find_with_hash (pfile->nonexistent_file_hash, path, hv) != NULL)
433 file->err_no = ENOENT;
434 return false;
437 file->path = path;
438 if (!file->embed && pch_open_file (pfile, file, invalid_pch))
439 return true;
441 if (open_file (file))
442 return true;
444 if (file->err_no != ENOENT)
446 open_file_failed (pfile, file, 0, loc);
447 return true;
450 /* We copy the path name onto an obstack partly so that we don't
451 leak the memory, but mostly so that we don't fragment the
452 heap. */
453 copy = (char *) obstack_copy0 (&pfile->nonexistent_file_ob, path,
454 strlen (path));
455 free (path);
456 pp = htab_find_slot_with_hash (pfile->nonexistent_file_hash,
457 copy, hv, INSERT);
458 *pp = copy;
460 file->path = file->name;
462 else
464 file->err_no = ENOENT;
465 file->path = NULL;
468 return false;
471 /* Return true iff the missing_header callback found the given HEADER. */
472 static bool
473 search_path_exhausted (cpp_reader *pfile, const char *header, _cpp_file *file)
475 missing_header_cb func = pfile->cb.missing_header;
477 /* When the regular search path doesn't work, try context dependent
478 headers search paths. */
479 if (func
480 && file->dir == NULL)
482 if ((file->path = func (pfile, header, &file->dir)) != NULL)
484 if (open_file (file))
485 return true;
486 free ((void *)file->path);
488 file->path = file->name;
491 return false;
494 bool
495 _cpp_find_failed (_cpp_file *file)
497 return file->err_no != 0;
500 /* Given a filename FNAME search for such a file in the include path
501 starting from START_DIR. If FNAME is the empty string it is
502 interpreted as STDIN if START_DIR is PFILE->no_search_path.
504 If the file is not found in the file cache fall back to the O/S and
505 add the result to our cache.
507 If the file was not found in the filesystem, or there was an error
508 opening it, then ERR_NO is nonzero and FD is -1. If the file was
509 found, then ERR_NO is zero and FD could be -1 or an open file
510 descriptor. FD can be -1 if the file was found in the cache and
511 had previously been closed. To open it again pass the return value
512 to open_file().
514 If KIND is _cpp_FFK_PRE_INCLUDE then it is OK for the file to be
515 missing. If present, it is OK for a precompiled header to be
516 included after it.
518 Use LOC as the location for any errors. */
520 _cpp_file *
521 _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
522 int angle_brackets, _cpp_find_file_kind kind, location_t loc)
524 bool invalid_pch = false;
525 bool saw_bracket_include = false;
526 bool saw_quote_include = false;
527 bool saw_embed_include = false;
528 struct cpp_dir *found_in_cache = NULL;
529 bool is_embed = kind == _cpp_FFK_EMBED || kind == _cpp_FFK_HAS_EMBED;
531 /* Ensure we get no confusion between cached files and directories. */
532 if (start_dir == NULL)
533 cpp_error_at (pfile, CPP_DL_ICE, loc, "NULL directory in %<find_file%>");
535 void **hash_slot
536 = htab_find_slot_with_hash (pfile->file_hash, fname,
537 htab_hash_string (fname), INSERT);
539 /* First check the cache before we resort to memory allocation. */
540 cpp_file_hash_entry *entry
541 = search_cache ((struct cpp_file_hash_entry *) *hash_slot, start_dir,
542 is_embed);
543 if (entry)
545 if (entry->u.file->deferred_error
546 && (kind == _cpp_FFK_NORMAL || kind == _cpp_FFK_EMBED))
548 open_file_failed (pfile, entry->u.file, angle_brackets, loc);
549 entry->u.file->deferred_error = false;
551 return entry->u.file;
554 _cpp_file *file = make_cpp_file (start_dir, fname);
555 file->implicit_preinclude
556 = (kind == _cpp_FFK_PRE_INCLUDE
557 || (pfile->buffer && pfile->buffer->file->implicit_preinclude));
558 file->embed = is_embed;
560 if (kind == _cpp_FFK_FAKE)
561 file->dont_read = true;
562 else
563 /* Try each path in the include chain. */
564 for (;;)
566 if (find_file_in_dir (pfile, file, &invalid_pch, loc))
567 break;
569 if (is_embed
570 && file->dir == start_dir
571 && start_dir != pfile->embed_include
572 && start_dir != &pfile->no_search_path)
573 file->dir = pfile->embed_include;
574 else
575 file->dir = file->dir->next;
576 if (file->dir == NULL)
578 if (!is_embed
579 && search_path_exhausted (pfile, fname, file))
581 /* Although this file must not go in the cache,
582 because the file found might depend on things (like
583 the current file) that aren't represented in the
584 cache, it still has to go in the list of all files
585 so that #import works. */
586 file->next_file = pfile->all_files;
587 pfile->all_files = file;
588 if (*hash_slot == NULL)
590 /* If *hash_slot is NULL, the above
591 htab_find_slot_with_hash call just created the
592 slot, but we aren't going to store there anything
593 of use, so need to remove the newly created entry.
594 htab_clear_slot requires that it is non-NULL, so
595 store some non-NULL but valid pointer there,
596 htab_clear_slot will immediately overwrite it. */
597 *hash_slot = file;
598 htab_clear_slot (pfile->file_hash, hash_slot);
600 return file;
603 if (invalid_pch)
605 cpp_error (pfile, CPP_DL_ERROR,
606 "one or more PCH files were found,"
607 " but they were invalid");
608 if (!cpp_get_options (pfile)->warn_invalid_pch)
609 cpp_error (pfile, CPP_DL_NOTE,
610 "use %<-Winvalid-pch%> for more information");
613 if (kind == _cpp_FFK_PRE_INCLUDE)
615 free ((char *) file->name);
616 free (file);
617 if (*hash_slot == NULL)
619 /* See comment on the above htab_clear_slot call. */
620 *hash_slot = &hash_slot;
621 htab_clear_slot (pfile->file_hash, hash_slot);
623 return NULL;
626 if (kind != _cpp_FFK_HAS_INCLUDE && kind != _cpp_FFK_HAS_EMBED)
627 open_file_failed (pfile, file, angle_brackets, loc);
628 else
629 file->deferred_error = true;
630 break;
633 /* Only check the cache for the starting location (done above)
634 and the quote and bracket chain heads because there are no
635 other possible starting points for searches. */
636 if (file->dir == pfile->bracket_include)
637 saw_bracket_include = true;
638 else if (file->dir == pfile->quote_include)
639 saw_quote_include = true;
640 else if (file->dir == pfile->embed_include)
641 saw_embed_include = true;
642 else
643 continue;
645 entry
646 = search_cache ((struct cpp_file_hash_entry *) *hash_slot,
647 file->dir, is_embed);
648 if (entry)
650 found_in_cache = file->dir;
651 break;
655 if (entry)
657 /* Cache for START_DIR too, sharing the _cpp_file structure. */
658 free ((char *) file->name);
659 free (file);
660 file = entry->u.file;
662 else
664 /* This is a new file; put it in the list. */
665 file->next_file = pfile->all_files;
666 pfile->all_files = file;
669 /* Store this new result in the hash table. */
670 entry = new_file_hash_entry (pfile);
671 entry->next = (struct cpp_file_hash_entry *) *hash_slot;
672 entry->start_dir = start_dir;
673 entry->location = loc;
674 entry->u.file = file;
675 *hash_slot = (void *) entry;
677 /* If we passed the quote or bracket chain heads, cache them also.
678 This speeds up processing if there are lots of -I options. */
679 if (saw_bracket_include
680 && pfile->bracket_include != start_dir
681 && found_in_cache != pfile->bracket_include)
683 entry = new_file_hash_entry (pfile);
684 entry->next = (struct cpp_file_hash_entry *) *hash_slot;
685 entry->start_dir = pfile->bracket_include;
686 entry->location = loc;
687 entry->u.file = file;
688 *hash_slot = (void *) entry;
690 if (saw_quote_include
691 && pfile->quote_include != start_dir
692 && found_in_cache != pfile->quote_include)
694 entry = new_file_hash_entry (pfile);
695 entry->next = (struct cpp_file_hash_entry *) *hash_slot;
696 entry->start_dir = pfile->quote_include;
697 entry->location = loc;
698 entry->u.file = file;
699 *hash_slot = (void *) entry;
701 if (saw_embed_include
702 && pfile->embed_include != start_dir
703 && found_in_cache != pfile->embed_include)
705 entry = new_file_hash_entry (pfile);
706 entry->next = (struct cpp_file_hash_entry *) *hash_slot;
707 entry->start_dir = pfile->embed_include;
708 entry->location = loc;
709 entry->u.file = file;
710 *hash_slot = (void *) entry;
713 return file;
716 /* Read a file into FILE->buffer, returning true on success.
718 If FILE->fd is something weird, like a block device, we don't want
719 to read it at all. Don't even try to figure out what something is,
720 except for plain files and block devices, since there is no
721 reliable portable way of doing this.
723 Use LOC for any diagnostics.
725 PFILE may be NULL. In this case, no diagnostics are issued.
727 FIXME: Flush file cache and try again if we run out of memory. */
728 static bool
729 read_file_guts (cpp_reader *pfile, _cpp_file *file, location_t loc,
730 const char *input_charset)
732 ssize_t size, pad, total, count;
733 uchar *buf;
734 bool regular;
736 if (S_ISBLK (file->st.st_mode))
738 if (pfile)
739 cpp_error_at (pfile, CPP_DL_ERROR, loc,
740 "%s is a block device", file->path);
741 return false;
744 regular = S_ISREG (file->st.st_mode) != 0;
745 if (regular)
747 /* off_t might have a wider range than ssize_t - in other words,
748 the max size of a file might be bigger than the address
749 space. We can't handle a file that large. (Anyone with
750 a single source file bigger than 2GB needs to rethink
751 their coding style.) Some systems (e.g. AIX 4.1) define
752 SSIZE_MAX to be much smaller than the actual range of the
753 type. Use INTTYPE_MAXIMUM unconditionally to ensure this
754 does not bite us. */
755 if (file->st.st_size > INTTYPE_MAXIMUM (ssize_t))
757 if (pfile)
758 cpp_error_at (pfile, CPP_DL_ERROR, loc,
759 "%s is too large", file->path);
760 return false;
763 size = file->st.st_size;
765 else
766 /* 8 kilobytes is a sensible starting size. It ought to be bigger
767 than the kernel pipe buffer, and it's definitely bigger than
768 the majority of C source files. */
769 size = 8 * 1024;
771 pad = CPP_BUFFER_PADDING;
772 /* The '+ PAD' here is space for the final '\n' and PAD-1 bytes of padding,
773 allowing search_line_fast to use (possibly misaligned) vector loads. */
774 buf = XNEWVEC (uchar, size + pad);
775 total = 0;
776 while ((count = read (file->fd, buf + total, size - total)) > 0)
778 total += count;
780 if (total == size)
782 if (regular)
783 break;
784 size *= 2;
785 buf = XRESIZEVEC (uchar, buf, size + pad);
789 if (count < 0)
791 if (pfile)
792 cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, loc);
793 free (buf);
794 return false;
797 if (pfile && regular && total != size && STAT_SIZE_RELIABLE (file->st))
798 cpp_error_at (pfile, CPP_DL_WARNING, loc,
799 "%s is shorter than expected", file->path);
801 file->buffer = _cpp_convert_input (pfile,
802 input_charset,
803 buf, size + pad, total,
804 &file->buffer_start,
805 &file->st.st_size);
806 file->buffer_valid = file->buffer;
807 return file->buffer_valid;
810 /* Convenience wrapper around read_file_guts that opens the file if
811 necessary and closes the file descriptor after reading. FILE must
812 have been passed through find_file() at some stage. Use LOC for
813 any diagnostics. Unlike read_file_guts(), PFILE may not be NULL. */
814 static bool
815 read_file (cpp_reader *pfile, _cpp_file *file, location_t loc)
817 /* If we already have its contents in memory, succeed immediately. */
818 if (file->buffer_valid)
819 return true;
821 /* If an earlier read failed for some reason don't try again. */
822 if (file->dont_read || file->err_no)
823 return false;
825 if (file->fd == -1 && !open_file (file))
827 open_file_failed (pfile, file, 0, loc);
828 return false;
831 file->dont_read = !read_file_guts (pfile, file, loc,
832 CPP_OPTION (pfile, input_charset));
833 close (file->fd);
834 file->fd = -1;
836 return !file->dont_read;
839 /* Returns TRUE if FILE is already known to be idempotent, and should
840 therefore not be read again. */
841 static bool
842 is_known_idempotent_file (cpp_reader *pfile, _cpp_file *file, bool import)
844 /* Skip once-only files. */
845 if (file->once_only)
846 return true;
848 /* We must mark the file once-only if #import now, before header
849 guard checks. Otherwise, undefining the header guard might
850 cause the file to be re-stacked. */
851 if (import)
853 _cpp_mark_file_once_only (pfile, file);
855 /* Don't stack files that have been stacked before. */
856 if (file->stack_count)
857 return true;
860 /* Skip if the file had a header guard and the macro is defined.
861 PCH relies on this appearing before the PCH handler below. */
862 if (file->cmacro && cpp_macro_p (file->cmacro))
863 return true;
865 /* Handle PCH files immediately; don't stack them. */
866 if (file->pchname)
868 pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path);
869 file->fd = -1;
870 free ((void *) file->pchname);
871 file->pchname = NULL;
872 return true;
875 return false;
878 /* Return TRUE if file has unique contents, so we should read process
879 it. The file's contents must already have been read. */
881 static bool
882 has_unique_contents (cpp_reader *pfile, _cpp_file *file, bool import,
883 location_t loc)
885 /* Check the file against the PCH file. This is done before
886 checking against files we've already seen, since it may save on
887 I/O. */
888 if (check_file_against_entries (pfile, file, import))
890 /* If this isn't a #import, but yet we can't include the file,
891 that means that it was #import-ed in the PCH file,
892 so we can never include it again. */
893 if (! import)
894 _cpp_mark_file_once_only (pfile, file);
895 return false;
898 /* Now we've read the file's contents, we can stack it if there
899 are no once-only files. */
900 if (!pfile->seen_once_only)
901 return true;
903 /* We may have read the file under a different name. Look
904 for likely candidates and compare file contents to be sure. */
905 for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
907 if (f == file)
908 continue; /* It'sa me! */
910 if (f->embed)
911 continue;
913 if ((import || f->once_only)
914 && f->err_no == 0
915 && f->st.st_mtime == file->st.st_mtime
916 && f->st.st_size == file->st.st_size)
918 _cpp_file *ref_file;
920 if (f->buffer && !f->buffer_valid)
922 /* We already have a buffer but it is not valid, because
923 the file is still stacked. Make a new one. */
924 ref_file = make_cpp_file (f->dir, f->name);
925 ref_file->path = f->path;
927 else
928 /* The file is not stacked anymore. We can reuse it. */
929 ref_file = f;
931 bool same_file_p = (read_file (pfile, ref_file, loc)
932 /* Size might have changed in read_file(). */
933 && ref_file->st.st_size == file->st.st_size
934 && !memcmp (ref_file->buffer, file->buffer,
935 file->st.st_size));
937 if (f->buffer && !f->buffer_valid)
939 ref_file->path = 0;
940 destroy_cpp_file (ref_file);
943 if (same_file_p)
944 /* Already seen under a different name. */
945 return false;
949 return true;
952 /* Place the file referenced by FILE into a new buffer on the buffer
953 stack if possible. Returns true if a buffer is stacked. Use LOC
954 for any diagnostics. */
956 bool
957 _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, include_type type,
958 location_t loc)
960 if (is_known_idempotent_file (pfile, file, type == IT_IMPORT))
961 return false;
963 int sysp = 0;
964 char *buf = nullptr;
966 /* Check C++ module include translation. */
967 if (!file->header_unit && type < IT_HEADER_HWM
968 /* Do not include translate include-next. */
969 && type != IT_INCLUDE_NEXT
970 && pfile->cb.translate_include)
971 buf = (pfile->cb.translate_include
972 (pfile, pfile->line_table, loc, file->path));
974 if (buf)
976 /* We don't increment the line number at the end of a buffer,
977 because we don't usually need that location (we're popping an
978 include file). However in this case we do want to do the
979 increment. So push a writable buffer of two newlines to acheive
980 that. (We also need an extra newline, so this looks like a regular
981 file, which we do that to to make sure we don't fall off the end in the
982 middle of a line. */
983 static uchar newlines[] = "\n\n\n";
984 cpp_push_buffer (pfile, newlines, 2, true);
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;
993 file->header_unit = +1;
994 _cpp_mark_file_once_only (pfile, file);
996 else
998 /* Not a header unit, and we know it. */
999 file->header_unit = -1;
1001 if (!read_file (pfile, file, loc))
1002 return false;
1004 if (!has_unique_contents (pfile, file, type == IT_IMPORT, loc))
1005 return false;
1007 if (pfile->buffer && file->dir)
1008 sysp = MAX (pfile->buffer->sysp, file->dir->sysp);
1010 /* Add the file to the dependencies on its first inclusion. */
1011 if (CPP_OPTION (pfile, deps.style) > (sysp != 0)
1012 && !file->stack_count
1013 && file->path[0]
1014 && !(pfile->main_file == file
1015 && CPP_OPTION (pfile, deps.ignore_main_file)))
1016 deps_add_dep (pfile->deps, file->path);
1018 /* Clear buffer_valid since _cpp_clean_line messes it up. */
1019 file->buffer_valid = false;
1020 file->stack_count++;
1022 /* Stack the buffer. */
1023 cpp_buffer *buffer
1024 = cpp_push_buffer (pfile, file->buffer, file->st.st_size,
1025 CPP_OPTION (pfile, preprocessed)
1026 && !CPP_OPTION (pfile, directives_only));
1027 buffer->file = file;
1028 buffer->sysp = sysp;
1029 buffer->to_free = file->buffer_start;
1031 /* Initialize controlling macro state. */
1032 pfile->mi_valid = true;
1033 pfile->mi_cmacro = 0;
1036 /* In the case of a normal #include, we're now at the start of the
1037 line *following* the #include. A separate location_t for this
1038 location makes no sense, until we do the LC_LEAVE.
1040 This does not apply if we found a PCH file, we're not a regular
1041 include, or we ran out of locations. */
1042 bool decrement = (file->pchname == NULL
1043 && type < IT_DIRECTIVE_HWM
1044 && (pfile->line_table->highest_location
1045 != LINE_MAP_MAX_LOCATION - 1));
1046 if (decrement)
1047 pfile->line_table->highest_location--;
1049 /* Normally a header unit becomes an __import directive in the current file,
1050 but with -include we need something to LC_LEAVE to trigger the file_change
1051 hook and continue to the next -include or the main source file. */
1052 if (file->header_unit <= 0 || type == IT_CMDLINE)
1053 /* Add line map and do callbacks. */
1054 _cpp_do_file_change (pfile, LC_ENTER, file->path,
1055 /* With preamble injection, start on line zero,
1056 so the preamble doesn't appear to have been
1057 included from line 1. Likewise when
1058 starting preprocessed, we expect an initial
1059 locating line. */
1060 type == IT_PRE_MAIN ? 0 : 1, sysp);
1061 else if (decrement)
1063 /* Adjust the line back one so we appear on the #include line itself. */
1064 const line_map_ordinary *map
1065 = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
1066 linenum_type line = SOURCE_LINE (map, pfile->line_table->highest_line);
1067 linemap_line_start (pfile->line_table, line - 1, 0);
1070 return true;
1073 /* Mark FILE to be included once only. */
1074 void
1075 _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file)
1077 pfile->seen_once_only = true;
1078 file->once_only = true;
1081 /* Return the directory from which searching for FNAME should start,
1082 considering the directive TYPE and ANGLE_BRACKETS. If there is
1083 nothing left in the path, returns NULL. */
1084 static struct cpp_dir *
1085 search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
1086 enum include_type type, bool suppress_diagnostic)
1088 cpp_dir *dir;
1089 _cpp_file *file;
1091 if (IS_ABSOLUTE_PATH (fname))
1092 return &pfile->no_search_path;
1094 /* pfile->buffer is NULL when processing an -include command-line flag. */
1095 file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file;
1097 /* For #include_next, skip in the search path past the dir in which
1098 the current file was found, but if it was found via an absolute
1099 path use the normal search logic. */
1100 if (type == IT_INCLUDE_NEXT && file->dir
1101 && file->dir != &pfile->no_search_path)
1102 dir = file->dir->next;
1103 else if (angle_brackets)
1104 dir = type == IT_EMBED ? pfile->embed_include : pfile->bracket_include;
1105 else if (type == IT_CMDLINE)
1106 /* -include and -imacros use the #include "" chain with the
1107 preprocessor's cwd prepended. */
1108 return make_cpp_dir (pfile, "./", false);
1109 else if (pfile->quote_ignores_source_dir && type != IT_EMBED)
1110 dir = pfile->quote_include;
1111 else
1112 return make_cpp_dir (pfile, dir_name_of_file (file),
1113 pfile->buffer ? pfile->buffer->sysp : 0);
1115 if (dir == NULL && !suppress_diagnostic)
1116 cpp_error (pfile, CPP_DL_ERROR,
1117 "no include path in which to search for %s", fname);
1119 return dir;
1122 /* Strip the basename from the file's path. It ends with a slash if
1123 of nonzero length. Note that this procedure also works for
1124 <stdin>, which is represented by the empty string. */
1125 static const char *
1126 dir_name_of_file (_cpp_file *file)
1128 if (!file->dir_name)
1130 size_t len = lbasename (file->path) - file->path;
1131 char *dir_name = XNEWVEC (char, len + 1);
1133 memcpy (dir_name, file->path, len);
1134 dir_name[len] = '\0';
1135 file->dir_name = dir_name;
1138 return file->dir_name;
1141 /* Handles #include-family directives (distinguished by TYPE),
1142 including HEADER, and the command line -imacros and -include.
1143 Returns true if a buffer was stacked. */
1144 bool
1145 _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
1146 enum include_type type, location_t loc)
1148 /* For -include command-line flags we have type == IT_CMDLINE.
1149 When the first -include file is processed we have the case, where
1150 pfile->cur_token == pfile->cur_run->base, we are directly called up
1151 by the front end. However in the case of the second -include file,
1152 we are called from _cpp_lex_token -> _cpp_get_fresh_line ->
1153 cpp_push_include, with pfile->cur_token != pfile->cur_run->base,
1154 and pfile->cur_token[-1].src_loc not (yet) initialized.
1155 However, when the include file cannot be found, we need src_loc to
1156 be initialized to some safe value: 0 means UNKNOWN_LOCATION. */
1157 if (type == IT_CMDLINE && pfile->cur_token != pfile->cur_run->base)
1158 pfile->cur_token[-1].src_loc = 0;
1160 cpp_dir *dir = search_path_head (pfile, fname, angle_brackets, type);
1161 if (!dir)
1162 return false;
1164 _cpp_file *file = _cpp_find_file (pfile, fname, dir, angle_brackets,
1165 type == IT_DEFAULT ? _cpp_FFK_PRE_INCLUDE
1166 : _cpp_FFK_NORMAL, loc);
1167 if (type == IT_DEFAULT && file == NULL)
1168 return false;
1170 return _cpp_stack_file (pfile, file, type, loc);
1173 /* NAME is a header file name, find the _cpp_file, if any. */
1175 static _cpp_file *
1176 test_header_unit (cpp_reader *pfile, const char *name, bool angle,
1177 location_t loc)
1179 if (cpp_dir *dir = search_path_head (pfile, name, angle, IT_INCLUDE))
1180 return _cpp_find_file (pfile, name, dir, angle, _cpp_FFK_NORMAL, loc);
1182 return nullptr;
1185 /* NAME is a header file name, find the path we'll use to open it and infer that
1186 it is a header-unit. */
1188 const char *
1189 _cpp_find_header_unit (cpp_reader *pfile, const char *name, bool angle,
1190 location_t loc)
1192 if (_cpp_file *file = test_header_unit (pfile, name, angle, loc))
1194 if (file->fd > 0)
1196 /* Don't leave it open. */
1197 close (file->fd);
1198 file->fd = 0;
1201 file->header_unit = +1;
1202 _cpp_mark_file_once_only (pfile, file);
1204 return file->path;
1207 return nullptr;
1210 /* NAME is a header file name, find the path we'll use to open it. But do not
1211 infer it is a header unit. */
1213 const char *
1214 cpp_probe_header_unit (cpp_reader *pfile, const char *name, bool angle,
1215 location_t loc)
1217 if (_cpp_file *file = test_header_unit (pfile, name, angle, loc))
1218 return file->path;
1220 return nullptr;
1223 /* Helper function for _cpp_stack_embed. Finish #embed/__has_embed processing
1224 after a file is found and data loaded into buffer. */
1226 static int
1227 finish_embed (cpp_reader *pfile, _cpp_file *file,
1228 struct cpp_embed_params *params)
1230 const uchar *buffer = file->buffer;
1231 size_t limit = file->limit;
1232 if (params->offset - file->offset > limit)
1233 limit = 0;
1234 else
1236 buffer += params->offset - file->offset;
1237 limit -= params->offset - file->offset;
1239 if (params->limit < limit)
1240 limit = params->limit;
1242 size_t embed_tokens = 0;
1243 if (!CPP_OPTION (pfile, cplusplus)
1244 && CPP_OPTION (pfile, lang) != CLK_ASM
1245 && limit >= 64)
1246 embed_tokens = ((limit - 2) / INT_MAX) + (((limit - 2) % INT_MAX) != 0);
1248 size_t max = INTTYPE_MAXIMUM (size_t) / sizeof (cpp_token);
1249 if ((embed_tokens ? (embed_tokens > (max - 3) / 2) : (limit > max / 2))
1250 || (limit
1251 ? (params->prefix.count > max
1252 || params->suffix.count > max
1253 || ((embed_tokens ? embed_tokens * 2 + 3 : limit * 2 - 1)
1254 + params->prefix.count
1255 + params->suffix.count > max))
1256 : params->if_empty.count > max))
1258 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1259 "%s is too large", file->path);
1260 return 0;
1263 size_t len = 0;
1264 for (size_t i = 0; i < limit; ++i)
1266 if (buffer[i] < 10)
1267 len += 2;
1268 else if (buffer[i] < 100)
1269 len += 3;
1270 #if UCHAR_MAX == 255
1271 else
1272 len += 4;
1273 #else
1274 else if (buffer[i] < 1000)
1275 len += 4;
1276 else
1278 char buf[64];
1279 len += sprintf (buf, "%d", buffer[i]) + 1;
1281 #endif
1282 if (len > INTTYPE_MAXIMUM (ssize_t))
1284 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1285 "%s is too large", file->path);
1286 return 0;
1288 if (embed_tokens && i == 0)
1289 i = limit - 2;
1291 uchar *s = len ? _cpp_unaligned_alloc (pfile, len) : NULL;
1292 _cpp_buff *tok_buff = NULL;
1293 cpp_token *tok = &pfile->directive_result, *toks = tok;
1294 size_t count = 0;
1295 if (limit)
1296 count = (params->prefix.count
1297 + (embed_tokens ? embed_tokens * 2 + 3 : limit * 2 - 1)
1298 + params->suffix.count) - 1;
1299 else if (params->if_empty.count)
1300 count = params->if_empty.count - 1;
1301 if (count)
1303 tok_buff = _cpp_get_buff (pfile, count * sizeof (cpp_token));
1304 toks = (cpp_token *) tok_buff->base;
1306 cpp_embed_params_tokens *prefix
1307 = limit ? &params->prefix : &params->if_empty;
1308 if (prefix->count)
1310 *tok = *prefix->base_run.base;
1311 tok = toks;
1312 tokenrun *cur_run = &prefix->base_run;
1313 while (cur_run)
1315 size_t cnt = (cur_run->next ? cur_run->limit
1316 : prefix->cur_token) - cur_run->base;
1317 cpp_token *t = cur_run->base;
1318 if (cur_run == &prefix->base_run)
1320 t++;
1321 cnt--;
1323 memcpy (tok, t, cnt * sizeof (cpp_token));
1324 tok += cnt;
1325 cur_run = cur_run->next;
1328 for (size_t i = 0; i < limit; ++i)
1330 tok->src_loc = params->loc;
1331 tok->type = CPP_NUMBER;
1332 tok->flags = NO_EXPAND;
1333 if (i == 0)
1334 tok->flags |= PREV_WHITE;
1335 tok->val.str.text = s;
1336 tok->val.str.len = sprintf ((char *) s, "%d", buffer[i]);
1337 s += tok->val.str.len + 1;
1338 if (tok == &pfile->directive_result)
1339 tok = toks;
1340 else
1341 tok++;
1342 if (i < limit - 1)
1344 tok->src_loc = params->loc;
1345 tok->type = CPP_COMMA;
1346 tok->flags = NO_EXPAND;
1347 tok++;
1349 if (i == 0 && embed_tokens)
1351 ++i;
1352 for (size_t j = 0; j < embed_tokens; ++j)
1354 tok->src_loc = params->loc;
1355 tok->type = CPP_EMBED;
1356 tok->flags = NO_EXPAND;
1357 tok->val.str.text = &buffer[i];
1358 tok->val.str.len
1359 = limit - 1 - i > INT_MAX ? INT_MAX : limit - 1 - i;
1360 i += tok->val.str.len;
1361 if (tok->val.str.len < 32 && j)
1363 /* Avoid CPP_EMBED with a fewer than 32 bytes, shrink the
1364 previous CPP_EMBED by 64 and grow this one by 64. */
1365 tok[-2].val.str.len -= 64;
1366 tok->val.str.text -= 64;
1367 tok->val.str.len += 64;
1369 tok++;
1370 tok->src_loc = params->loc;
1371 tok->type = CPP_COMMA;
1372 tok->flags = NO_EXPAND;
1373 tok++;
1375 --i;
1378 if (limit && params->suffix.count)
1380 tokenrun *cur_run = &params->suffix.base_run;
1381 cpp_token *orig_tok = tok;
1382 while (cur_run)
1384 size_t cnt = (cur_run->next ? cur_run->limit
1385 : params->suffix.cur_token) - cur_run->base;
1386 cpp_token *t = cur_run->base;
1387 memcpy (tok, t, cnt * sizeof (cpp_token));
1388 tok += cnt;
1389 cur_run = cur_run->next;
1391 orig_tok->flags |= PREV_WHITE;
1393 pfile->directive_result.flags |= PREV_WHITE;
1394 if (count)
1396 _cpp_push_token_context (pfile, NULL, toks, count);
1397 pfile->context->buff = tok_buff;
1399 return limit ? 1 : 2;
1402 /* Helper function for initialization of base64_dec table.
1403 Can't rely on ASCII compatibility, so check each letter
1404 separately. */
1406 constexpr signed char
1407 base64_dec_fn (unsigned char c)
1409 return (c == 'A' ? 0 : c == 'B' ? 1 : c == 'C' ? 2 : c == 'D' ? 3
1410 : c == 'E' ? 4 : c == 'F' ? 5 : c == 'G' ? 6 : c == 'H' ? 7
1411 : c == 'I' ? 8 : c == 'J' ? 9 : c == 'K' ? 10 : c == 'L' ? 11
1412 : c == 'M' ? 12 : c == 'N' ? 13 : c == 'O' ? 14 : c == 'P' ? 15
1413 : c == 'Q' ? 16 : c == 'R' ? 17 : c == 'S' ? 18 : c == 'T' ? 19
1414 : c == 'U' ? 20 : c == 'V' ? 21 : c == 'W' ? 22 : c == 'X' ? 23
1415 : c == 'Y' ? 24 : c == 'Z' ? 25
1416 : c == 'a' ? 26 : c == 'b' ? 27 : c == 'c' ? 28 : c == 'd' ? 29
1417 : c == 'e' ? 30 : c == 'f' ? 31 : c == 'g' ? 32 : c == 'h' ? 33
1418 : c == 'i' ? 34 : c == 'j' ? 35 : c == 'k' ? 36 : c == 'l' ? 37
1419 : c == 'm' ? 38 : c == 'n' ? 39 : c == 'o' ? 40 : c == 'p' ? 41
1420 : c == 'q' ? 42 : c == 'r' ? 43 : c == 's' ? 44 : c == 't' ? 45
1421 : c == 'u' ? 46 : c == 'v' ? 47 : c == 'w' ? 48 : c == 'x' ? 49
1422 : c == 'y' ? 50 : c == 'z' ? 51
1423 : c == '0' ? 52 : c == '1' ? 53 : c == '2' ? 54 : c == '3' ? 55
1424 : c == '4' ? 56 : c == '5' ? 57 : c == '6' ? 58 : c == '7' ? 59
1425 : c == '8' ? 60 : c == '9' ? 61 : c == '+' ? 62 : c == '/' ? 63
1426 : -1);
1429 /* base64 decoding table. */
1431 static constexpr signed char base64_dec[] = {
1432 #define B64D0(x) base64_dec_fn (x)
1433 #define B64D1(x) B64D0 (x), B64D0 (x + 1), B64D0 (x + 2), B64D0 (x + 3)
1434 #define B64D2(x) B64D1 (x), B64D1 (x + 4), B64D1 (x + 8), B64D1 (x + 12)
1435 #define B64D3(x) B64D2 (x), B64D2 (x + 16), B64D2 (x + 32), B64D2 (x + 48)
1436 B64D3 (0), B64D3 (64), B64D3 (128), B64D3 (192)
1439 /* Helper function for _cpp_stack_embed. Handle #embed/__has_embed with
1440 gnu::base64 parameter. */
1442 static int
1443 finish_base64_embed (cpp_reader *pfile, const char *fname, bool angle,
1444 struct cpp_embed_params *params)
1446 size_t len, end, i, j, base64_len = 0, cnt;
1447 uchar *buf = NULL, *q, pbuf[4], qbuf[3];
1448 const uchar *base64_str;
1449 if (angle || strcmp (fname, "."))
1451 if (!params->has_embed)
1452 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1453 "%<gnu::base64%> parameter can be only used with "
1454 "%<\".\"%>");
1455 return 0;
1457 tokenrun *cur_run = &params->base64.base_run;
1458 cpp_token *tend, *tok;
1459 while (cur_run)
1461 tend = cur_run->next ? cur_run->limit : params->base64.cur_token;
1462 for (tok = cur_run->base; tok < tend; ++tok)
1464 if (tok->val.str.len < 2
1465 || tok->val.str.text[0] != '"'
1466 || tok->val.str.text[tok->val.str.len - 1] != '"')
1468 fail:
1469 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1470 "%<gnu::base64%> argument not valid base64 "
1471 "encoded string");
1472 free (buf);
1473 return 0;
1475 if (tok->val.str.len - 2 > (~(size_t) 0) - base64_len)
1476 goto fail;
1477 base64_len += tok->val.str.len - 2;
1479 cur_run = cur_run->next;
1481 if ((base64_len & 3) != 0)
1482 goto fail;
1483 len = base64_len / 4 * 3;
1484 end = len;
1486 if (params->has_embed)
1487 q = qbuf;
1488 else
1490 buf = XNEWVEC (uchar, len ? len : 1);
1491 q = buf;
1493 cur_run = &params->base64.base_run;
1494 tend = cur_run->next ? cur_run->limit : params->base64.cur_token;
1495 tok = cur_run->base;
1496 base64_str = tok->val.str.text + 1;
1497 cnt = tok->val.str.len - 2;
1498 ++tok;
1499 for (i = 0; i < end; i += 3)
1501 for (j = 0; j < 4; ++j)
1503 while (cnt == 0)
1505 if (tok == tend)
1507 cur_run = cur_run->next;
1508 tend = (cur_run->next ? cur_run->limit
1509 : params->base64.cur_token);
1510 tok = cur_run->base;
1512 base64_str = tok->val.str.text + 1;
1513 cnt = tok->val.str.len - 2;
1514 ++tok;
1516 pbuf[j] = *base64_str;
1517 base64_str++;
1518 --cnt;
1520 if (pbuf[3] == '=' && i + 3 >= end)
1522 end = len - 3;
1523 --len;
1524 if (pbuf[2] == '=')
1525 --len;
1526 break;
1528 int a = base64_dec[pbuf[0]];
1529 int b = base64_dec[pbuf[1]];
1530 int c = base64_dec[pbuf[2]];
1531 int d = base64_dec[pbuf[3]];
1532 if (a == -1 || b == -1 || c == -1 || d == -1)
1533 goto fail;
1534 q[0] = (a << 2) | (b >> 4);
1535 q[1] = (b << 4) | (c >> 2);
1536 q[2] = (c << 6) | d;
1537 if (!params->has_embed)
1538 q += 3;
1540 if (len != end)
1542 int a = base64_dec[pbuf[0]];
1543 int b = base64_dec[pbuf[1]];
1544 if (a == -1 || b == -1)
1545 goto fail;
1546 q[0] = (a << 2) | (b >> 4);
1547 if (len - end == 2)
1549 int c = base64_dec[pbuf[2]];
1550 if (c == -1)
1551 goto fail;
1552 q[1] = (b << 4) | (c >> 2);
1553 if ((c & 3) != 0)
1554 goto fail;
1556 else if ((b & 15) != 0)
1557 goto fail;
1559 if (params->has_embed)
1560 return len ? 1 : 2;
1561 _cpp_file *file = make_cpp_file (NULL, "");
1562 file->embed = 1;
1563 file->next_file = pfile->all_files;
1564 pfile->all_files = file;
1565 params->limit = -1;
1566 params->offset = 0;
1567 file->limit = len;
1568 file->buffer = buf;
1569 file->path = xstrdup ("<base64>");
1570 return finish_embed (pfile, file, params);
1573 /* Try to load FNAME with #embed/__has_embed parameters PARAMS.
1574 If !PARAMS->has_embed, return new token in pfile->directive_result
1575 (first token) and rest in a pushed non-macro context.
1576 Returns 0 for not found/errors, 1 for non-empty resource and 2
1577 for empty resource. */
1580 _cpp_stack_embed (cpp_reader *pfile, const char *fname, bool angle,
1581 struct cpp_embed_params *params)
1583 if (params->base64.count)
1584 return finish_base64_embed (pfile, fname, angle, params);
1585 cpp_dir *dir = search_path_head (pfile, fname, angle, IT_EMBED,
1586 params->has_embed);
1587 if (!dir)
1588 return 0;
1589 _cpp_file *file = _cpp_find_file (pfile, fname, dir, angle,
1590 params->has_embed
1591 ? _cpp_FFK_HAS_EMBED : _cpp_FFK_EMBED,
1592 params->loc);
1593 if (!file)
1594 return 0;
1595 if (file->dont_read || file->err_no)
1596 return 0;
1597 _cpp_file *orig_file = file;
1598 if (file->buffer_valid
1599 && (!S_ISREG (file->st.st_mode)
1600 || file->offset + (cpp_num_part) 0 > params->offset
1601 || (file->limit < file->st.st_size - file->offset + (size_t) 0
1602 && (params->offset - file->offset > (cpp_num_part) file->limit
1603 || file->limit - (params->offset
1604 - file->offset) < params->limit))))
1606 bool found = false;
1607 if (S_ISREG (file->st.st_mode))
1609 while (file->next_file
1610 && file->next_file->embed
1611 && file->next_file->buffer_valid
1612 && file->next_file->dir == file->dir
1613 && strcmp (file->name, file->next_file->name) == 0
1614 && strcmp (file->path, file->next_file->path) == 0)
1616 file = file->next_file;
1617 if (file->offset + (cpp_num_part) 0 <= params->offset
1618 && (file->limit >= (file->st.st_size - file->offset
1619 + (size_t) 0)
1620 || (params->offset
1621 - file->offset <= (cpp_num_part) file->limit
1622 && file->limit - (params->offset
1623 - file->offset) >= params->limit)))
1625 found = true;
1626 break;
1630 if (!found)
1632 _cpp_file *file2 = make_cpp_file (file->dir, file->name);
1633 file2->path = xstrdup (file->path);
1634 file2->next_file = file->next_file;
1635 file2->embed = true;
1636 file->next_file = file2;
1637 file = file2;
1640 if (!file->buffer_valid)
1642 if (file->fd == -1 && !open_file (file))
1644 if (params->has_embed)
1645 file->deferred_error = true;
1646 else
1647 open_file_failed (pfile, file, 0, params->loc);
1648 return 0;
1650 if (S_ISBLK (file->st.st_mode))
1652 if (params->has_embed)
1654 close (file->fd);
1655 file->fd = -1;
1656 return 0;
1658 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1659 "%s is a block device", file->path);
1660 fail:
1661 close (file->fd);
1662 file->fd = -1;
1663 file->dont_read = true;
1664 return 0;
1667 if (CPP_OPTION (pfile, deps.style)
1668 && !params->has_embed
1669 && file == orig_file
1670 && file->path[0])
1671 deps_add_dep (pfile->deps, file->path);
1673 bool regular = S_ISREG (file->st.st_mode) != 0;
1674 ssize_t size, total, count;
1675 uchar *buf;
1676 if (regular)
1678 cpp_num_part limit;
1679 if (file->st.st_size + (cpp_num_part) 0 < params->offset)
1680 limit = 0;
1681 else if (file->st.st_size - params->offset < params->limit)
1682 limit = file->st.st_size - params->offset;
1683 else
1684 limit = params->limit;
1685 if (params->has_embed)
1686 return limit != 0 ? 1 : 2;
1687 if (limit > INTTYPE_MAXIMUM (ssize_t))
1689 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1690 "%s is too large", file->path);
1691 goto fail;
1693 if (lseek (file->fd, params->offset, SEEK_CUR)
1694 != (off_t) params->offset)
1696 cpp_errno_filename (pfile, CPP_DL_ERROR, file->path,
1697 params->loc);
1698 goto fail;
1700 file->offset = params->offset;
1701 file->limit = limit;
1702 size = limit;
1704 else if (params->has_embed)
1705 return 2;
1706 else if (params->limit > 8 * 1024)
1707 size = 8 * 1024;
1708 else
1709 size = params->limit;
1710 buf = XNEWVEC (uchar, size ? size : 1);
1711 total = 0;
1713 if (!regular && params->offset)
1715 uchar *buf2 = buf;
1716 ssize_t size2 = size;
1717 cpp_num_part total2 = params->offset;
1719 if (params->offset > 8 * 1024 && size < 8 * 1024)
1721 size2 = 32 * 1024;
1722 buf2 = XNEWVEC (uchar, size2);
1726 if ((cpp_num_part) size2 > total2)
1727 size2 = total2;
1728 count = read (file->fd, buf2, size2);
1729 if (count < 0)
1731 cpp_errno_filename (pfile, CPP_DL_ERROR, file->path,
1732 params->loc);
1733 if (buf2 != buf)
1734 free (buf2);
1735 free (buf);
1736 goto fail;
1738 total2 -= count;
1740 while (total2);
1741 if (buf2 != buf)
1742 free (buf2);
1745 while ((count = read (file->fd, buf + total, size - total)) > 0)
1747 total += count;
1748 if (total == size)
1750 if (regular || size + (cpp_num_part) 0 == params->limit)
1751 break;
1752 size = (size_t) size * 2;
1753 if (size < 0)
1755 if (params->limit <= INTTYPE_MAXIMUM (ssize_t))
1756 size = params->limit;
1757 else
1759 cpp_error_at (pfile, CPP_DL_ERROR, params->loc,
1760 "%s is too large", file->path);
1761 free (buf);
1762 goto fail;
1765 else if (size + (cpp_num_part) 0 > params->limit)
1766 size = params->limit;
1767 buf = XRESIZEVEC (uchar, buf, size);
1771 if (count < 0)
1773 cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, params->loc);
1774 free (buf);
1775 goto fail;
1778 if (regular && total != size && STAT_SIZE_RELIABLE (file->st))
1780 cpp_error_at (pfile, CPP_DL_WARNING, params->loc,
1781 "%s is shorter than expected", file->path);
1782 file->limit = total;
1784 else if (!regular)
1786 file->offset = params->offset;
1787 file->limit = total;
1790 file->buffer_start = buf;
1791 file->buffer = buf;
1792 file->buffer_valid = 1;
1793 close (file->fd);
1794 file->fd = -1;
1796 else if (params->has_embed)
1798 if (params->offset - file->offset > file->limit)
1799 return 2;
1800 size_t limit = file->limit - (params->offset - file->offset);
1801 return limit && params->limit ? 1 : 2;
1804 return finish_embed (pfile, file, params);
1807 /* Retrofit the just-entered main file asif it was an include. This
1808 will permit correct include_next use, and mark it as a system
1809 header if that's where it resides. We use filesystem-appropriate
1810 prefix matching of the include path to locate the main file. */
1811 void
1812 cpp_retrofit_as_include (cpp_reader *pfile)
1814 /* We should be the outermost. */
1815 gcc_assert (!pfile->buffer->prev);
1817 if (const char *name = pfile->main_file->name)
1819 /* Locate name on the include dir path, using a prefix match. */
1820 size_t name_len = strlen (name);
1821 for (cpp_dir *dir = pfile->quote_include; dir; dir = dir->next)
1822 if (dir->len < name_len
1823 && IS_DIR_SEPARATOR (name[dir->len])
1824 && !filename_ncmp (name, dir->name, dir->len))
1826 pfile->main_file->dir = dir;
1827 if (dir->sysp)
1828 cpp_make_system_header (pfile, 1, 0);
1829 break;
1833 /* Initialize controlling macro state. */
1834 pfile->mi_valid = true;
1835 pfile->mi_cmacro = 0;
1838 /* Could not open FILE. The complication is dependency output. */
1839 static void
1840 open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets,
1841 location_t loc)
1843 int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0;
1844 bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp);
1846 errno = file->err_no;
1847 if (print_dep && CPP_OPTION (pfile, deps.missing_files) && errno == ENOENT)
1849 deps_add_dep (pfile->deps, file->name);
1850 /* If the preprocessor output (other than dependency information) is
1851 being used, we must also flag an error. */
1852 if (CPP_OPTION (pfile, deps.need_preprocessor_output))
1853 cpp_errno_filename (pfile, CPP_DL_FATAL,
1854 file->path ? file->path : file->name,
1855 loc);
1857 else
1859 /* If we are not outputting dependencies, or if we are and dependencies
1860 were requested for this file, or if preprocessor output is needed
1861 in addition to dependency information, this is an error.
1863 Otherwise (outputting dependencies but not for this file, and not
1864 using the preprocessor output), we can still produce correct output
1865 so it's only a warning. */
1866 if (CPP_OPTION (pfile, deps.style) == DEPS_NONE
1867 || print_dep
1868 || CPP_OPTION (pfile, deps.need_preprocessor_output))
1869 cpp_errno_filename (pfile, CPP_DL_FATAL,
1870 file->path ? file->path : file->name,
1871 loc);
1872 else
1873 cpp_errno_filename (pfile, CPP_DL_WARNING,
1874 file->path ? file->path : file->name,
1875 loc);
1879 /* Search in the chain beginning at HEAD for a file whose search path
1880 started at START_DIR != NULL. */
1881 static struct cpp_file_hash_entry *
1882 search_cache (struct cpp_file_hash_entry *head, const cpp_dir *start_dir,
1883 bool is_embed)
1885 while (head && (head->start_dir != start_dir
1886 || head->u.file->embed != is_embed))
1887 head = head->next;
1889 return head;
1892 /* Allocate a new _cpp_file structure. */
1893 static _cpp_file *
1894 make_cpp_file (cpp_dir *dir, const char *fname)
1896 _cpp_file *file = XCNEW (_cpp_file);
1897 file->fd = -1;
1898 file->dir = dir;
1899 file->name = xstrdup (fname);
1901 return file;
1904 /* Release a _cpp_file structure. */
1905 static void
1906 destroy_cpp_file (_cpp_file *file)
1908 free ((void *) file->buffer_start);
1909 free ((void *) file->name);
1910 free ((void *) file->path);
1911 free (file);
1914 /* Release all the files allocated by this reader. */
1915 static void
1916 destroy_all_cpp_files (cpp_reader *pfile)
1918 _cpp_file *iter = pfile->all_files;
1919 while (iter)
1921 _cpp_file *next = iter->next_file;
1922 destroy_cpp_file (iter);
1923 iter = next;
1927 /* A hash of directory names. The directory names are the path names
1928 of files which contain a #include "", the included file name is
1929 appended to this directories.
1931 To avoid duplicate entries we follow the convention that all
1932 non-empty directory names should end in a '/'. DIR_NAME must be
1933 stored in permanently allocated memory. */
1934 static cpp_dir *
1935 make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp)
1937 struct cpp_file_hash_entry *entry, **hash_slot;
1938 cpp_dir *dir;
1940 hash_slot = (struct cpp_file_hash_entry **)
1941 htab_find_slot_with_hash (pfile->dir_hash, dir_name,
1942 htab_hash_string (dir_name),
1943 INSERT);
1945 /* Have we already hashed this directory? */
1946 for (entry = *hash_slot; entry; entry = entry->next)
1947 if (entry->start_dir == NULL)
1948 return entry->u.dir;
1950 dir = XCNEW (cpp_dir);
1951 dir->next = pfile->quote_include;
1952 dir->name = (char *) dir_name;
1953 dir->len = strlen (dir_name);
1954 dir->sysp = sysp;
1955 dir->construct = 0;
1957 /* Store this new result in the hash table. */
1958 entry = new_file_hash_entry (pfile);
1959 entry->next = *hash_slot;
1960 entry->start_dir = NULL;
1961 entry->location = pfile->line_table->highest_location;
1962 entry->u.dir = dir;
1963 *hash_slot = entry;
1965 return dir;
1968 /* Create a new block of memory for file hash entries. */
1969 static void
1970 allocate_file_hash_entries (cpp_reader *pfile)
1972 struct file_hash_entry_pool *pool = XNEW (struct file_hash_entry_pool);
1973 pool->file_hash_entries_used = 0;
1974 pool->next = pfile->file_hash_entries;
1975 pfile->file_hash_entries = pool;
1978 /* Return a new file hash entry. */
1979 static struct cpp_file_hash_entry *
1980 new_file_hash_entry (cpp_reader *pfile)
1982 unsigned int idx;
1983 if (pfile->file_hash_entries->file_hash_entries_used == FILE_HASH_POOL_SIZE)
1984 allocate_file_hash_entries (pfile);
1986 idx = pfile->file_hash_entries->file_hash_entries_used++;
1987 return &pfile->file_hash_entries->pool[idx];
1990 /* Free the file hash entry pools. */
1991 static void
1992 free_file_hash_entries (cpp_reader *pfile)
1994 struct file_hash_entry_pool *iter = pfile->file_hash_entries;
1995 while (iter)
1997 struct file_hash_entry_pool *next = iter->next;
1998 free (iter);
1999 iter = next;
2003 /* Returns TRUE if a file FNAME has ever been successfully opened.
2004 This routine is not intended to correctly handle filenames aliased
2005 by links or redundant . or .. traversals etc. */
2006 bool
2007 cpp_included (cpp_reader *pfile, const char *fname)
2009 struct cpp_file_hash_entry *entry;
2011 entry = (struct cpp_file_hash_entry *)
2012 htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname));
2014 while (entry && (entry->start_dir == NULL || entry->u.file->err_no))
2015 entry = entry->next;
2017 return entry != NULL;
2020 /* Returns TRUE if a file FNAME has ever been successfully opened
2021 before LOCATION. This routine is not intended to correctly handle
2022 filenames aliased by links or redundant . or .. traversals etc. */
2023 bool
2024 cpp_included_before (cpp_reader *pfile, const char *fname,
2025 location_t location)
2027 struct cpp_file_hash_entry *entry
2028 = (struct cpp_file_hash_entry *)
2029 htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname));
2031 if (IS_ADHOC_LOC (location))
2032 location = get_location_from_adhoc_loc (pfile->line_table, location);
2034 while (entry && (entry->start_dir == NULL || entry->u.file->err_no
2035 || entry->location > location))
2036 entry = entry->next;
2038 return entry != NULL;
2041 /* Calculate the hash value of a file hash entry P. */
2043 static hashval_t
2044 file_hash_hash (const void *p)
2046 struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) p;
2047 const char *hname;
2048 if (entry->start_dir)
2049 hname = entry->u.file->name;
2050 else
2051 hname = entry->u.dir->name;
2053 return htab_hash_string (hname);
2056 /* Compare a string Q against a file hash entry P. */
2057 static int
2058 file_hash_eq (const void *p, const void *q)
2060 struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) p;
2061 const char *fname = (const char *) q;
2062 const char *hname;
2064 if (entry->start_dir)
2065 hname = entry->u.file->name;
2066 else
2067 hname = entry->u.dir->name;
2069 return filename_cmp (hname, fname) == 0;
2072 /* Compare entries in the nonexistent file hash table. These are just
2073 strings. */
2074 static int
2075 nonexistent_file_hash_eq (const void *p, const void *q)
2077 return filename_cmp ((const char *) p, (const char *) q) == 0;
2080 /* Initialize everything in this source file. */
2081 void
2082 _cpp_init_files (cpp_reader *pfile)
2084 pfile->file_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq,
2085 NULL, xcalloc, free);
2086 pfile->dir_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq,
2087 NULL, xcalloc, free);
2088 allocate_file_hash_entries (pfile);
2089 pfile->nonexistent_file_hash = htab_create_alloc (127, htab_hash_string,
2090 nonexistent_file_hash_eq,
2091 NULL, xcalloc, free);
2092 obstack_specify_allocation (&pfile->nonexistent_file_ob, 0, 0,
2093 xmalloc, free);
2096 /* Finalize everything in this source file. */
2097 void
2098 _cpp_cleanup_files (cpp_reader *pfile)
2100 htab_delete (pfile->file_hash);
2101 htab_delete (pfile->dir_hash);
2102 htab_delete (pfile->nonexistent_file_hash);
2103 obstack_free (&pfile->nonexistent_file_ob, 0);
2104 free_file_hash_entries (pfile);
2105 destroy_all_cpp_files (pfile);
2108 /* Make the parser forget about files it has seen. This can be useful
2109 for resetting the parser to start another run. */
2110 void
2111 cpp_clear_file_cache (cpp_reader *pfile)
2113 _cpp_cleanup_files (pfile);
2114 pfile->file_hash_entries = NULL;
2115 pfile->all_files = NULL;
2116 _cpp_init_files (pfile);
2119 /* Enter a file name in the hash for the sake of cpp_included. */
2120 void
2121 _cpp_fake_include (cpp_reader *pfile, const char *fname)
2123 /* It does not matter what are the contents of fake_source_dir, it will never
2124 be inspected; we just use its address to uniquely signify that this file
2125 was added as a fake include, so a later call to _cpp_find_file (to include
2126 the file for real) won't find the fake one in the hash table. */
2127 static cpp_dir fake_source_dir;
2128 _cpp_find_file (pfile, fname, &fake_source_dir, 0, _cpp_FFK_FAKE, 0);
2131 /* Not everyone who wants to set system-header-ness on a buffer can
2132 see the details of a buffer. This is an exported interface because
2133 fix-header needs it. */
2134 void
2135 cpp_make_system_header (cpp_reader *pfile, int syshdr, int externc)
2137 int flags = 0;
2138 const class line_maps *line_table = pfile->line_table;
2139 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
2140 /* 1 = system header, 2 = system header to be treated as C. */
2141 if (syshdr)
2142 flags = 1 + (externc != 0);
2143 pfile->buffer->sysp = flags;
2144 _cpp_do_file_change (pfile, LC_RENAME, ORDINARY_MAP_FILE_NAME (map),
2145 SOURCE_LINE (map, pfile->line_table->highest_line),
2146 flags);
2149 /* Allow the client to change the current file. Used by the front end
2150 to achieve pseudo-file names like <built-in>.
2151 If REASON is LC_LEAVE, then NEW_NAME must be NULL. */
2152 void
2153 cpp_change_file (cpp_reader *pfile, enum lc_reason reason,
2154 const char *new_name)
2156 _cpp_do_file_change (pfile, reason, new_name, 1, 0);
2159 struct report_missing_guard_data
2161 cpp_reader *pfile;
2162 const char **paths;
2163 size_t count;
2166 /* Callback function for htab_traverse. */
2167 static int
2168 report_missing_guard (void **slot, void *d)
2170 struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) *slot;
2171 struct report_missing_guard_data *data
2172 = (struct report_missing_guard_data *) d;
2174 /* Skip directories. */
2175 if (entry->start_dir != NULL)
2177 _cpp_file *file = entry->u.file;
2179 /* We don't want MI guard advice for the main file. */
2180 if (!file->once_only
2181 && file->cmacro == NULL
2182 && file->stack_count == 1
2183 && data->pfile->main_file != file)
2185 if (data->paths == NULL)
2187 data->paths = XCNEWVEC (const char *, data->count);
2188 data->count = 0;
2191 data->paths[data->count++] = file->path;
2195 /* Keep traversing the hash table. */
2196 return 1;
2199 /* Comparison function for qsort. */
2200 static int
2201 report_missing_guard_cmp (const void *p1, const void *p2)
2203 return strcmp (*(const char *const *) p1, *(const char *const *) p2);
2206 /* Report on all files that might benefit from a multiple include guard.
2207 Triggered by -H. */
2208 void
2209 _cpp_report_missing_guards (cpp_reader *pfile)
2211 struct report_missing_guard_data data;
2213 data.pfile = pfile;
2214 data.paths = NULL;
2215 data.count = htab_elements (pfile->file_hash);
2216 htab_traverse (pfile->file_hash, report_missing_guard, &data);
2218 if (data.paths != NULL)
2220 size_t i;
2222 /* Sort the paths to avoid outputting them in hash table
2223 order. */
2224 qsort (data.paths, data.count, sizeof (const char *),
2225 report_missing_guard_cmp);
2226 fputs (_("Multiple include guards may be useful for:\n"),
2227 stderr);
2228 for (i = 0; i < data.count; i++)
2230 fputs (data.paths[i], stderr);
2231 putc ('\n', stderr);
2233 free (data.paths);
2237 /* Locate HEADER, and determine whether it is newer than the current
2238 file. If it cannot be located or dated, return -1, if it is
2239 newer, return 1, otherwise 0. */
2241 _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
2242 int angle_brackets)
2244 _cpp_file *file;
2245 struct cpp_dir *dir;
2247 dir = search_path_head (pfile, fname, angle_brackets, IT_INCLUDE);
2248 if (!dir)
2249 return -1;
2251 file = _cpp_find_file (pfile, fname, dir, angle_brackets, _cpp_FFK_NORMAL, 0);
2252 if (file->err_no)
2253 return -1;
2255 if (file->fd != -1)
2257 close (file->fd);
2258 file->fd = -1;
2261 return file->st.st_mtime > pfile->buffer->file->st.st_mtime;
2264 /* Pushes the given file onto the buffer stack. Returns nonzero if
2265 successful. */
2266 bool
2267 cpp_push_include (cpp_reader *pfile, const char *fname)
2269 return _cpp_stack_include (pfile, fname, false, IT_CMDLINE,
2270 pfile->line_table->highest_line);
2273 /* Pushes the given file, implicitly included at the start of a
2274 compilation, onto the buffer stack but without any errors if the
2275 file is not found. Returns nonzero if successful. */
2276 bool
2277 cpp_push_default_include (cpp_reader *pfile, const char *fname)
2279 return _cpp_stack_include (pfile, fname, true, IT_DEFAULT,
2280 pfile->line_table->highest_line);
2283 /* Do appropriate cleanup when a file INC's buffer is popped off the
2284 input stack. */
2285 void
2286 _cpp_pop_file_buffer (cpp_reader *pfile, _cpp_file *file,
2287 const unsigned char *to_free)
2289 /* Record the inclusion-preventing macro, which could be NULL
2290 meaning no controlling macro. */
2291 if (pfile->mi_valid && file->cmacro == NULL)
2293 file->cmacro = pfile->mi_cmacro;
2294 if (pfile->mi_cmacro
2295 && pfile->mi_def_cmacro
2296 && pfile->cb.get_suggestion)
2298 auto mi_cmacro = (const char *) NODE_NAME (pfile->mi_cmacro);
2299 auto mi_def_cmacro = (const char *) NODE_NAME (pfile->mi_def_cmacro);
2300 const char *names[] = { mi_def_cmacro, NULL };
2301 if (pfile->cb.get_suggestion (pfile, mi_cmacro, names)
2302 && cpp_warning_with_line (pfile, CPP_W_HEADER_GUARD,
2303 pfile->mi_loc, 0,
2304 "header guard %qs followed by "
2305 "%<#define%> of a different macro",
2306 mi_cmacro))
2307 cpp_error_at (pfile, CPP_DL_NOTE, pfile->mi_def_loc,
2308 "%qs is defined here; did you mean %qs?",
2309 mi_def_cmacro, mi_cmacro);
2313 /* Invalidate control macros in the #including file. */
2314 pfile->mi_valid = false;
2316 if (to_free)
2318 if (to_free == file->buffer_start)
2320 file->buffer_start = NULL;
2321 file->buffer = NULL;
2322 file->buffer_valid = false;
2324 free ((void *) to_free);
2328 /* Return the file name associated with FILE. */
2329 const char *
2330 _cpp_get_file_name (_cpp_file *file)
2332 return file->name;
2335 /* Inteface to file statistics record in _cpp_file structure. */
2336 struct stat *
2337 _cpp_get_file_stat (_cpp_file *file)
2339 return &file->st;
2342 /* Set the include chain for "" to QUOTE, for <> to BRACKET. If
2343 QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the
2344 directory of the including file.
2346 If BRACKET does not lie in the QUOTE chain, it is set to QUOTE.
2348 EMBED is include chain for #embed <>. */
2349 void
2350 cpp_set_include_chains (cpp_reader *pfile, cpp_dir *quote, cpp_dir *bracket,
2351 cpp_dir *embed, int quote_ignores_source_dir)
2353 pfile->quote_include = quote;
2354 pfile->bracket_include = quote;
2355 pfile->quote_ignores_source_dir = quote_ignores_source_dir;
2356 pfile->embed_include = embed;
2358 for (; quote; quote = quote->next)
2360 quote->name_map = NULL;
2361 quote->len = strlen (quote->name);
2362 if (quote == bracket)
2363 pfile->bracket_include = bracket;
2365 for (; embed; embed = embed->next)
2367 embed->name_map = NULL;
2368 embed->len = strlen (embed->name);
2372 /* Append the file name to the directory to create the path, but don't
2373 turn / into // or // into ///; // may be a namespace escape. */
2374 static char *
2375 append_file_to_dir (const char *fname, cpp_dir *dir)
2377 size_t dlen, flen;
2378 char *path;
2380 dlen = dir->len;
2381 flen = strlen (fname);
2382 path = XNEWVEC (char, dlen + 1 + flen + 1);
2383 memcpy (path, dir->name, dlen);
2384 if (dlen && !IS_DIR_SEPARATOR (path[dlen - 1]))
2385 path[dlen++] = '/';
2386 memcpy (&path[dlen], fname, flen + 1);
2388 return path;
2391 /* Read a space delimited string of unlimited length from a stdio
2392 file F. */
2393 static char *
2394 read_filename_string (int ch, FILE *f)
2396 char *alloc, *set;
2397 int len;
2399 len = 20;
2400 set = alloc = XNEWVEC (char, len + 1);
2401 if (! is_space (ch))
2403 *set++ = ch;
2404 while ((ch = getc (f)) != EOF && ! is_space (ch))
2406 if (set - alloc == len)
2408 len *= 2;
2409 alloc = XRESIZEVEC (char, alloc, len + 1);
2410 set = alloc + len / 2;
2412 *set++ = ch;
2415 *set = '\0';
2416 ungetc (ch, f);
2417 return alloc;
2420 /* Read the file name map file for DIR. */
2421 static void
2422 read_name_map (cpp_dir *dir)
2424 static const char FILE_NAME_MAP_FILE[] = "header.gcc";
2425 char *name;
2426 FILE *f;
2427 size_t len, count = 0, room = 9;
2429 len = dir->len;
2430 name = (char *) alloca (len + sizeof (FILE_NAME_MAP_FILE) + 1);
2431 memcpy (name, dir->name, len);
2432 if (len && !IS_DIR_SEPARATOR (name[len - 1]))
2433 name[len++] = '/';
2434 strcpy (name + len, FILE_NAME_MAP_FILE);
2435 f = fopen (name, "r");
2437 dir->name_map = XNEWVEC (const char *, room);
2439 /* Silently return NULL if we cannot open. */
2440 if (f)
2442 int ch;
2444 while ((ch = getc (f)) != EOF)
2446 char *to;
2448 if (is_space (ch))
2449 continue;
2451 if (count + 2 > room)
2453 room += 8;
2454 dir->name_map = XRESIZEVEC (const char *, dir->name_map, room);
2457 dir->name_map[count] = read_filename_string (ch, f);
2458 while ((ch = getc (f)) != EOF && is_hspace (ch))
2461 to = read_filename_string (ch, f);
2462 if (IS_ABSOLUTE_PATH (to))
2463 dir->name_map[count + 1] = to;
2464 else
2466 dir->name_map[count + 1] = append_file_to_dir (to, dir);
2467 free (to);
2470 count += 2;
2471 while ((ch = getc (f)) != '\n')
2472 if (ch == EOF)
2473 break;
2476 fclose (f);
2479 /* Terminate the list of maps. */
2480 dir->name_map[count] = NULL;
2483 /* Remap a FILE's name based on the file_name_map, if any, for
2484 FILE->dir. If the file name has any directory separators,
2485 recursively check those directories too. */
2486 static char *
2487 remap_filename (cpp_reader *pfile, _cpp_file *file)
2489 const char *fname, *p;
2490 char *new_dir, *p3;
2491 cpp_dir *dir;
2492 size_t index, len;
2494 dir = file->dir;
2495 fname = file->name;
2497 for (;;)
2499 if (!dir->name_map)
2500 read_name_map (dir);
2502 for (index = 0; dir->name_map[index]; index += 2)
2503 if (!filename_cmp (dir->name_map[index], fname))
2504 return xstrdup (dir->name_map[index + 1]);
2505 if (IS_ABSOLUTE_PATH (fname))
2506 return NULL;
2507 p = strchr (fname, '/');
2508 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2510 const char *p2 = strchr (fname, '\\');
2511 if (!p || (p2 && p > p2))
2512 p = p2;
2514 #endif
2515 if (!p || p == fname)
2516 return NULL;
2518 len = dir->len + (p - fname + 1);
2519 new_dir = XNEWVEC (char, len + 2);
2520 p3 = new_dir + dir->len;
2521 memcpy (new_dir, dir->name, dir->len);
2522 if (dir->len && !IS_DIR_SEPARATOR (dir->name[dir->len - 1]))
2524 *p3++ = '/';
2525 len++;
2527 memcpy (p3, fname, p - fname + 1);
2528 new_dir[len] = '\0';
2530 dir = make_cpp_dir (pfile, new_dir, dir->sysp);
2531 fname = p + 1;
2535 /* Returns true if PCHNAME is a valid PCH file for FILE. */
2536 static bool
2537 validate_pch (cpp_reader *pfile, _cpp_file *file, const char *pchname)
2539 const char *saved_path = file->path;
2540 bool valid = false;
2542 file->path = pchname;
2543 if (open_file (file))
2545 valid = 1 & pfile->cb.valid_pch (pfile, pchname, file->fd);
2547 if (!valid)
2549 close (file->fd);
2550 file->fd = -1;
2553 if (CPP_OPTION (pfile, print_include_names))
2555 unsigned int i;
2556 for (i = 1; i < pfile->line_table->depth; i++)
2557 putc ('.', stderr);
2558 fprintf (stderr, "%c %s\n",
2559 valid ? '!' : 'x', pchname);
2563 file->path = saved_path;
2564 return valid;
2567 /* Get the path associated with the _cpp_file F. The path includes
2568 the base name from the include directive and the directory it was
2569 found in via the search path. */
2571 const char *
2572 cpp_get_path (struct _cpp_file *f)
2574 return f->path;
2577 /* Get the directory associated with the _cpp_file F. */
2579 cpp_dir *
2580 cpp_get_dir (struct _cpp_file *f)
2582 return f->dir;
2585 /* Get the cpp_buffer currently associated with the cpp_reader
2586 PFILE. */
2588 cpp_buffer *
2589 cpp_get_buffer (cpp_reader *pfile)
2591 return pfile->buffer;
2594 /* Get the _cpp_file associated with the cpp_buffer B. */
2596 _cpp_file *
2597 cpp_get_file (cpp_buffer *b)
2599 return b->file;
2602 /* Get the previous cpp_buffer given a cpp_buffer B. The previous
2603 buffer is the buffer that included the given buffer. */
2605 cpp_buffer *
2606 cpp_get_prev (cpp_buffer *b)
2608 return b->prev;
2611 /* This data structure holds the list of header files that were seen
2612 while the PCH was being built. The 'entries' field is kept sorted
2613 in memcmp() order; yes, this means that on little-endian systems,
2614 it's sorted initially by the least-significant byte of 'size', but
2615 that's OK. The code does rely on having entries with the same size
2616 next to each other. */
2618 struct pchf_entry {
2619 /* The size of this file. This is used to save running a MD5 checksum
2620 if the sizes don't match. */
2621 off_t size;
2622 /* The MD5 checksum of this file. */
2623 unsigned char sum[16];
2624 /* Is this file to be included only once? */
2625 bool once_only;
2628 struct pchf_data {
2629 /* Number of pchf_entry structures. */
2630 size_t count;
2632 /* Are there any values with once_only set?
2633 This is used as an optimisation, it means we don't have to search
2634 the structure if we're processing a regular #include. */
2635 bool have_once_only;
2637 struct pchf_entry entries[1];
2640 static struct pchf_data *pchf;
2642 /* A qsort ordering function for pchf_entry structures. */
2644 static int
2645 pchf_save_compare (const void *e1, const void *e2)
2647 return memcmp (e1, e2, sizeof (struct pchf_entry));
2650 /* Create and write to F a pchf_data structure. */
2652 bool
2653 _cpp_save_file_entries (cpp_reader *pfile, FILE *fp)
2655 size_t count = 0;
2656 struct pchf_data *result;
2657 size_t result_size;
2658 _cpp_file *f;
2659 bool ret;
2661 for (f = pfile->all_files; f; f = f->next_file)
2662 ++count;
2664 result_size = (sizeof (struct pchf_data)
2665 + sizeof (struct pchf_entry) * (count - 1));
2666 result = XCNEWVAR (struct pchf_data, result_size);
2668 result->count = 0;
2669 result->have_once_only = false;
2671 for (f = pfile->all_files; f; f = f->next_file)
2673 size_t count;
2675 /* This should probably never happen, since if a read error occurred
2676 the PCH file shouldn't be written... */
2677 if (f->dont_read || f->err_no)
2678 continue;
2680 if (f->stack_count == 0)
2681 continue;
2683 count = result->count++;
2685 result->entries[count].once_only = f->once_only;
2686 /* |= is avoided in the next line because of an HP C compiler bug */
2687 result->have_once_only = result->have_once_only | f->once_only;
2688 if (f->buffer_valid)
2689 md5_buffer ((const char *)f->buffer,
2690 f->st.st_size, result->entries[count].sum);
2691 else
2693 FILE *ff;
2694 int oldfd = f->fd;
2696 if (!open_file (f))
2698 open_file_failed (pfile, f, 0, 0);
2699 free (result);
2700 return false;
2702 ff = fdopen (f->fd, "rb");
2703 md5_stream (ff, result->entries[count].sum);
2704 fclose (ff);
2705 f->fd = oldfd;
2707 result->entries[count].size = f->st.st_size;
2710 result_size = (sizeof (struct pchf_data)
2711 + sizeof (struct pchf_entry) * (result->count - 1));
2713 qsort (result->entries, result->count, sizeof (struct pchf_entry),
2714 pchf_save_compare);
2716 ret = fwrite (result, result_size, 1, fp) == 1;
2717 free (result);
2718 return ret;
2721 /* Read the pchf_data structure from F. */
2723 bool
2724 _cpp_read_file_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f)
2726 struct pchf_data d;
2728 if (fread (&d, sizeof (struct pchf_data) - sizeof (struct pchf_entry), 1, f)
2729 != 1)
2730 return false;
2732 pchf = XNEWVAR (struct pchf_data, sizeof (struct pchf_data)
2733 + sizeof (struct pchf_entry) * (d.count - 1));
2734 memcpy (pchf, &d, sizeof (struct pchf_data) - sizeof (struct pchf_entry));
2735 if (fread (pchf->entries, sizeof (struct pchf_entry), d.count, f)
2736 != d.count)
2737 return false;
2738 return true;
2741 /* The parameters for pchf_compare. */
2743 struct pchf_compare_data
2745 /* The size of the file we're looking for. */
2746 off_t size;
2748 /* The MD5 checksum of the file, if it's been computed. */
2749 unsigned char sum[16];
2751 /* Is SUM valid? */
2752 bool sum_computed;
2754 /* Do we need to worry about entries that don't have ONCE_ONLY set? */
2755 bool check_included;
2757 /* The file that we're searching for. */
2758 _cpp_file *f;
2761 /* bsearch comparison function; look for D_P in E_P. */
2763 static int
2764 pchf_compare (const void *d_p, const void *e_p)
2766 const struct pchf_entry *e = (const struct pchf_entry *)e_p;
2767 struct pchf_compare_data *d = (struct pchf_compare_data *)d_p;
2768 int result;
2770 result = memcmp (&d->size, &e->size, sizeof (off_t));
2771 if (result != 0)
2772 return result;
2774 if (! d->sum_computed)
2776 _cpp_file *const f = d->f;
2778 md5_buffer ((const char *)f->buffer, f->st.st_size, d->sum);
2779 d->sum_computed = true;
2782 result = memcmp (d->sum, e->sum, 16);
2783 if (result != 0)
2784 return result;
2786 if (d->check_included || e->once_only)
2787 return 0;
2788 else
2789 return 1;
2792 /* Check that F is not in a list read from a PCH file (if any).
2793 Assumes that f->buffer_valid is true. Return TRUE if the file
2794 should not be read. */
2796 static bool
2797 check_file_against_entries (cpp_reader *pfile ATTRIBUTE_UNUSED,
2798 _cpp_file *f,
2799 bool check_included)
2801 struct pchf_compare_data d;
2803 if (pchf == NULL
2804 || (! check_included && ! pchf->have_once_only))
2805 return false;
2807 d.size = f->st.st_size;
2808 d.sum_computed = false;
2809 d.f = f;
2810 d.check_included = check_included;
2811 return bsearch (&d, pchf->entries, pchf->count, sizeof (struct pchf_entry),
2812 pchf_compare) != NULL;
2815 /* Return true if the file FNAME is found in the appropriate include file path
2816 as indicated by ANGLE_BRACKETS. */
2818 bool
2819 _cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets,
2820 enum include_type type)
2822 cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type,
2823 /* suppress_diagnostic = */ true);
2824 if (!start_dir)
2825 return false;
2826 _cpp_file *file = _cpp_find_file (pfile, fname, start_dir, angle_brackets,
2827 _cpp_FFK_HAS_INCLUDE, 0);
2828 return file->err_no != ENOENT;
2831 /* Read a file and convert to input charset, the same as if it were being read
2832 by a cpp_reader. */
2834 cpp_converted_source
2835 cpp_get_converted_source (const char *fname, const char *input_charset)
2837 cpp_converted_source res = {};
2838 _cpp_file file = {};
2839 file.fd = -1;
2840 file.name = lbasename (fname);
2841 file.path = fname;
2842 if (!open_file (&file))
2843 return res;
2844 const bool ok = read_file_guts (NULL, &file, 0, input_charset);
2845 close (file.fd);
2846 if (!ok)
2847 return res;
2848 res.to_free = (char *) file.buffer_start;
2849 res.data = (char *) file.buffer;
2850 res.len = file.st.st_size;
2851 return res;