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