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