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