xref: /netbsd-src/external/gpl3/gcc.old/dist/libcpp/files.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg /* Part of CPP library.  File handling.
2*8feb0f0bSmrg    Copyright (C) 1986-2020 Free Software Foundation, Inc.
336ac495dSmrg    Written by Per Bothner, 1994.
436ac495dSmrg    Based on CCCP program by Paul Rubin, June 1986
536ac495dSmrg    Adapted to ANSI C, Richard Stallman, Jan 1987
636ac495dSmrg    Split out of cpplib.c, Zack Weinberg, Oct 1998
736ac495dSmrg    Reimplemented, Neil Booth, Jul 2003
836ac495dSmrg 
936ac495dSmrg This program is free software; you can redistribute it and/or modify it
1036ac495dSmrg under the terms of the GNU General Public License as published by the
1136ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
1236ac495dSmrg later version.
1336ac495dSmrg 
1436ac495dSmrg This program is distributed in the hope that it will be useful,
1536ac495dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1636ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1736ac495dSmrg GNU General Public License for more details.
1836ac495dSmrg 
1936ac495dSmrg You should have received a copy of the GNU General Public License
2036ac495dSmrg along with this program; see the file COPYING3.  If not see
2136ac495dSmrg <http://www.gnu.org/licenses/>.  */
2236ac495dSmrg 
2336ac495dSmrg #include "config.h"
2436ac495dSmrg #include "system.h"
2536ac495dSmrg #include "cpplib.h"
2636ac495dSmrg #include "internal.h"
2736ac495dSmrg #include "mkdeps.h"
2836ac495dSmrg #include "obstack.h"
2936ac495dSmrg #include "hashtab.h"
3036ac495dSmrg #include "md5.h"
3136ac495dSmrg #include <dirent.h>
3236ac495dSmrg 
3336ac495dSmrg /* Variable length record files on VMS will have a stat size that includes
3436ac495dSmrg    record control characters that won't be included in the read size.  */
3536ac495dSmrg #ifdef VMS
3636ac495dSmrg # define FAB_C_VAR 2 /* variable length records (see Starlet fabdef.h) */
3736ac495dSmrg # define STAT_SIZE_RELIABLE(ST) ((ST).st_fab_rfm != FAB_C_VAR)
3836ac495dSmrg #else
3936ac495dSmrg # define STAT_SIZE_RELIABLE(ST) true
4036ac495dSmrg #endif
4136ac495dSmrg 
4236ac495dSmrg #ifdef __DJGPP__
4336ac495dSmrg #include <io.h>
4436ac495dSmrg   /* For DJGPP redirected input is opened in text mode.  */
4536ac495dSmrg #  define set_stdin_to_binary_mode() \
4636ac495dSmrg      if (! isatty (0)) setmode (0, O_BINARY)
4736ac495dSmrg #else
4836ac495dSmrg #  define set_stdin_to_binary_mode() /* Nothing */
4936ac495dSmrg #endif
5036ac495dSmrg 
5136ac495dSmrg /* This structure represents a file searched for by CPP, whether it
5236ac495dSmrg    exists or not.  An instance may be pointed to by more than one
5336ac495dSmrg    cpp_file_hash_entry; at present no reference count is kept.  */
5436ac495dSmrg struct _cpp_file
5536ac495dSmrg {
5636ac495dSmrg   /* Filename as given to #include or command line switch.  */
5736ac495dSmrg   const char *name;
5836ac495dSmrg 
5936ac495dSmrg   /* The full path used to find the file.  */
6036ac495dSmrg   const char *path;
6136ac495dSmrg 
6236ac495dSmrg   /* The full path of the pch file.  */
6336ac495dSmrg   const char *pchname;
6436ac495dSmrg 
6536ac495dSmrg   /* The file's path with the basename stripped.  NULL if it hasn't
6636ac495dSmrg      been calculated yet.  */
6736ac495dSmrg   const char *dir_name;
6836ac495dSmrg 
6936ac495dSmrg   /* Chain through all files.  */
7036ac495dSmrg   struct _cpp_file *next_file;
7136ac495dSmrg 
7236ac495dSmrg   /* The contents of NAME after calling read_file().  */
7336ac495dSmrg   const uchar *buffer;
7436ac495dSmrg 
7536ac495dSmrg   /* Pointer to the real start of BUFFER.  read_file() might increment
7636ac495dSmrg      BUFFER; when freeing, this this pointer must be used instead.  */
7736ac495dSmrg   const uchar *buffer_start;
7836ac495dSmrg 
7936ac495dSmrg   /* The macro, if any, preventing re-inclusion.  */
8036ac495dSmrg   const cpp_hashnode *cmacro;
8136ac495dSmrg 
8236ac495dSmrg   /* The directory in the search path where FILE was found.  Used for
8336ac495dSmrg      #include_next and determining whether a header is a system
8436ac495dSmrg      header.  */
8536ac495dSmrg   cpp_dir *dir;
8636ac495dSmrg 
8736ac495dSmrg   /* As filled in by stat(2) for the file.  */
8836ac495dSmrg   struct stat st;
8936ac495dSmrg 
9036ac495dSmrg   /* File descriptor.  Invalid if -1, otherwise open.  */
9136ac495dSmrg   int fd;
9236ac495dSmrg 
9336ac495dSmrg   /* Zero if this file was successfully opened and stat()-ed,
9436ac495dSmrg      otherwise errno obtained from failure.  */
9536ac495dSmrg   int err_no;
9636ac495dSmrg 
9736ac495dSmrg   /* Number of times the file has been stacked for preprocessing.  */
9836ac495dSmrg   unsigned short stack_count;
9936ac495dSmrg 
10036ac495dSmrg   /* If opened with #import or contains #pragma once.  */
101*8feb0f0bSmrg   bool once_only : 1;
10236ac495dSmrg 
10336ac495dSmrg   /* If read() failed before.  */
104*8feb0f0bSmrg   bool dont_read : 1;
10536ac495dSmrg 
10636ac495dSmrg   /* If this file is the main file.  */
107*8feb0f0bSmrg   bool main_file : 1;
10836ac495dSmrg 
10936ac495dSmrg   /* If BUFFER above contains the true contents of the file.  */
110*8feb0f0bSmrg   bool buffer_valid : 1;
11136ac495dSmrg 
11236ac495dSmrg   /* If this file is implicitly preincluded.  */
113*8feb0f0bSmrg   bool implicit_preinclude : 1;
11436ac495dSmrg };
11536ac495dSmrg 
11636ac495dSmrg /* A singly-linked list for all searches for a given file name, with
11736ac495dSmrg    its head pointed to by a slot in FILE_HASH.  The file name is what
11836ac495dSmrg    appeared between the quotes in a #include directive; it can be
11936ac495dSmrg    determined implicitly from the hash table location or explicitly
12036ac495dSmrg    from FILE->name.
12136ac495dSmrg 
12236ac495dSmrg    FILE is a structure containing details about the file that was
12336ac495dSmrg    found with that search, or details of how the search failed.
12436ac495dSmrg 
12536ac495dSmrg    START_DIR is the starting location of the search in the include
12636ac495dSmrg    chain.  The current directories for "" includes are also hashed in
12736ac495dSmrg    the hash table and therefore unique.  Files that are looked up
12836ac495dSmrg    without using a search path, such as absolute filenames and file
12936ac495dSmrg    names from the command line share a special starting directory so
13036ac495dSmrg    they don't cause cache hits with normal include-chain lookups.
13136ac495dSmrg 
13236ac495dSmrg    If START_DIR is NULL then the entry is for a directory, not a file,
13336ac495dSmrg    and the directory is in DIR.  Since the starting point in a file
13436ac495dSmrg    lookup chain is never NULL, this means that simple pointer
13536ac495dSmrg    comparisons against START_DIR can be made to determine cache hits
13636ac495dSmrg    in file lookups.
13736ac495dSmrg 
13836ac495dSmrg    If a cache lookup fails because of e.g. an extra "./" in the path,
13936ac495dSmrg    then nothing will break.  It is just less efficient as CPP will
14036ac495dSmrg    have to do more work re-preprocessing the file, and/or comparing
14136ac495dSmrg    its contents against earlier once-only files.
14236ac495dSmrg */
14336ac495dSmrg struct cpp_file_hash_entry
14436ac495dSmrg {
14536ac495dSmrg   struct cpp_file_hash_entry *next;
14636ac495dSmrg   cpp_dir *start_dir;
147c0a68be4Smrg   location_t location;
14836ac495dSmrg   union
14936ac495dSmrg   {
15036ac495dSmrg     _cpp_file *file;
15136ac495dSmrg     cpp_dir *dir;
15236ac495dSmrg   } u;
15336ac495dSmrg };
15436ac495dSmrg 
15536ac495dSmrg /* Number of entries to put in a cpp_file_hash_entry pool.  */
15636ac495dSmrg #define FILE_HASH_POOL_SIZE 127
15736ac495dSmrg 
15836ac495dSmrg /* A file hash entry pool.  We allocate cpp_file_hash_entry object from
15936ac495dSmrg    one of these.  */
16036ac495dSmrg struct file_hash_entry_pool
16136ac495dSmrg {
16236ac495dSmrg   /* Number of entries used from this pool.  */
16336ac495dSmrg   unsigned int file_hash_entries_used;
16436ac495dSmrg   /* Next pool in the chain; used when freeing.  */
16536ac495dSmrg   struct file_hash_entry_pool *next;
16636ac495dSmrg   /* The memory pool.  */
16736ac495dSmrg   struct cpp_file_hash_entry pool[FILE_HASH_POOL_SIZE];
16836ac495dSmrg };
16936ac495dSmrg 
17036ac495dSmrg static bool open_file (_cpp_file *file);
17136ac495dSmrg static bool pch_open_file (cpp_reader *pfile, _cpp_file *file,
17236ac495dSmrg 			   bool *invalid_pch);
17336ac495dSmrg static bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file,
174c0a68be4Smrg 			      bool *invalid_pch, location_t loc);
17536ac495dSmrg static bool read_file_guts (cpp_reader *pfile, _cpp_file *file,
176c0a68be4Smrg 			    location_t loc);
17736ac495dSmrg static bool read_file (cpp_reader *pfile, _cpp_file *file,
178c0a68be4Smrg 		       location_t loc);
17936ac495dSmrg static struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
18036ac495dSmrg 				 int angle_brackets, enum include_type);
18136ac495dSmrg static const char *dir_name_of_file (_cpp_file *file);
18236ac495dSmrg static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int,
183c0a68be4Smrg 			      location_t);
18436ac495dSmrg static struct cpp_file_hash_entry *search_cache (struct cpp_file_hash_entry *head,
18536ac495dSmrg 					     const cpp_dir *start_dir);
18636ac495dSmrg static _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname);
18736ac495dSmrg static void destroy_cpp_file (_cpp_file *);
18836ac495dSmrg static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp);
18936ac495dSmrg static void allocate_file_hash_entries (cpp_reader *pfile);
19036ac495dSmrg static struct cpp_file_hash_entry *new_file_hash_entry (cpp_reader *pfile);
19136ac495dSmrg static int report_missing_guard (void **slot, void *b);
19236ac495dSmrg static hashval_t file_hash_hash (const void *p);
19336ac495dSmrg static int file_hash_eq (const void *p, const void *q);
19436ac495dSmrg static char *read_filename_string (int ch, FILE *f);
19536ac495dSmrg static void read_name_map (cpp_dir *dir);
19636ac495dSmrg static char *remap_filename (cpp_reader *pfile, _cpp_file *file);
19736ac495dSmrg static char *append_file_to_dir (const char *fname, cpp_dir *dir);
19836ac495dSmrg static bool validate_pch (cpp_reader *, _cpp_file *file, const char *pchname);
19936ac495dSmrg static int pchf_save_compare (const void *e1, const void *e2);
20036ac495dSmrg static int pchf_compare (const void *d_p, const void *e_p);
20136ac495dSmrg static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool);
20236ac495dSmrg 
20336ac495dSmrg /* Given a filename in FILE->PATH, with the empty string interpreted
20436ac495dSmrg    as <stdin>, open it.
20536ac495dSmrg 
20636ac495dSmrg    On success FILE contains an open file descriptor and stat
20736ac495dSmrg    information for the file.  On failure the file descriptor is -1 and
20836ac495dSmrg    the appropriate errno is also stored in FILE.  Returns TRUE iff
20936ac495dSmrg    successful.
21036ac495dSmrg 
21136ac495dSmrg    We used to open files in nonblocking mode, but that caused more
21236ac495dSmrg    problems than it solved.  Do take care not to acquire a controlling
21336ac495dSmrg    terminal by mistake (this can't happen on sane systems, but
21436ac495dSmrg    paranoia is a virtue).
21536ac495dSmrg 
21636ac495dSmrg    Use the three-argument form of open even though we aren't
21736ac495dSmrg    specifying O_CREAT, to defend against broken system headers.
21836ac495dSmrg 
21936ac495dSmrg    O_BINARY tells some runtime libraries (notably DJGPP) not to do
22036ac495dSmrg    newline translation; we can handle DOS line breaks just fine
22136ac495dSmrg    ourselves.  */
22236ac495dSmrg static bool
open_file(_cpp_file * file)22336ac495dSmrg open_file (_cpp_file *file)
22436ac495dSmrg {
22536ac495dSmrg   const char *cpp_restricted;
22636ac495dSmrg 
22736ac495dSmrg   cpp_restricted = getenv ("CPP_RESTRICTED");
22836ac495dSmrg 
22936ac495dSmrg   if (file->path[0] == '\0')
23036ac495dSmrg     {
23136ac495dSmrg       file->fd = 0;
23236ac495dSmrg       set_stdin_to_binary_mode ();
23336ac495dSmrg     }
23436ac495dSmrg   else
23536ac495dSmrg     file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY
236*8feb0f0bSmrg 		     | ((cpp_restricted != NULL) ? O_NONBLOCK : 0), 0666);
23736ac495dSmrg 
23836ac495dSmrg 
23936ac495dSmrg   if (file->fd != -1)
24036ac495dSmrg     {
24136ac495dSmrg       if (fstat (file->fd, &file->st) == 0)
24236ac495dSmrg 	{
24336ac495dSmrg 	  if (!S_ISDIR (file->st.st_mode))
24436ac495dSmrg 	  if (cpp_restricted != NULL
24536ac495dSmrg 	      ? S_ISREG (file->st.st_mode) : !S_ISDIR (file->st.st_mode))
24636ac495dSmrg 
24736ac495dSmrg 	    {
24836ac495dSmrg 	      if (cpp_restricted)
24936ac495dSmrg 		fcntl(file->fd, F_SETFL,
25036ac495dSmrg 		      fcntl(file->fd, F_GETFL, 0) & ~O_NONBLOCK);
25136ac495dSmrg 	      file->err_no = 0;
25236ac495dSmrg 	      return true;
25336ac495dSmrg 	    }
25436ac495dSmrg 
25536ac495dSmrg 	  /* Ignore a directory and continue the search.  The file we're
25636ac495dSmrg 	     looking for may be elsewhere in the search path.  */
25736ac495dSmrg 	  errno = ENOENT;
25836ac495dSmrg 	}
25936ac495dSmrg 
26036ac495dSmrg       close (file->fd);
26136ac495dSmrg       file->fd = -1;
26236ac495dSmrg     }
26336ac495dSmrg #if defined(_WIN32) && !defined(__CYGWIN__)
26436ac495dSmrg   else if (errno == EACCES)
26536ac495dSmrg     {
26636ac495dSmrg       /* On most UNIX systems, open succeeds on a directory.  Above,
26736ac495dSmrg          we check if we have opened a directory and if so, set errno
26836ac495dSmrg          to ENOENT.  However, on Windows, opening a directory
26936ac495dSmrg          fails with EACCES.  We want to return ENOENT in that
27036ac495dSmrg          case too.  */
27136ac495dSmrg       if (stat (file->path, &file->st) == 0
27236ac495dSmrg           && S_ISDIR (file->st.st_mode))
27336ac495dSmrg         errno = ENOENT;
27436ac495dSmrg       else
27536ac495dSmrg 	/* The call to stat may have reset errno.  */
27636ac495dSmrg 	errno = EACCES;
27736ac495dSmrg     }
27836ac495dSmrg #endif
27936ac495dSmrg   else if (errno == ENOTDIR)
28036ac495dSmrg     errno = ENOENT;
28136ac495dSmrg 
28236ac495dSmrg   file->err_no = errno;
28336ac495dSmrg 
28436ac495dSmrg   return false;
28536ac495dSmrg }
28636ac495dSmrg 
28736ac495dSmrg /* Temporary PCH intercept of opening a file.  Try to find a PCH file
28836ac495dSmrg    based on FILE->name and FILE->dir, and test those found for
28936ac495dSmrg    validity using PFILE->cb.valid_pch.  Return true iff a valid file is
29036ac495dSmrg    found.  Set *INVALID_PCH if a PCH file is found but wasn't valid.  */
29136ac495dSmrg 
29236ac495dSmrg static bool
pch_open_file(cpp_reader * pfile,_cpp_file * file,bool * invalid_pch)29336ac495dSmrg pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
29436ac495dSmrg {
29536ac495dSmrg   static const char extension[] = ".gch";
29636ac495dSmrg   const char *path = file->path;
29736ac495dSmrg   size_t len, flen;
29836ac495dSmrg   char *pchname;
29936ac495dSmrg   struct stat st;
30036ac495dSmrg   bool valid = false;
30136ac495dSmrg 
30236ac495dSmrg   /* No PCH on <stdin> or if not requested.  */
30336ac495dSmrg   if (file->name[0] == '\0' || !pfile->cb.valid_pch)
30436ac495dSmrg     return false;
30536ac495dSmrg 
30636ac495dSmrg   /* If the file is not included as first include from either the toplevel
30736ac495dSmrg      file or the command-line it is not a valid use of PCH.  */
30836ac495dSmrg   for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
30936ac495dSmrg     if (f->implicit_preinclude)
31036ac495dSmrg       continue;
31136ac495dSmrg     else if (f->main_file)
31236ac495dSmrg       break;
31336ac495dSmrg     else
31436ac495dSmrg       return false;
31536ac495dSmrg 
31636ac495dSmrg   flen = strlen (path);
31736ac495dSmrg   len = flen + sizeof (extension);
31836ac495dSmrg   pchname = XNEWVEC (char, len);
31936ac495dSmrg   memcpy (pchname, path, flen);
32036ac495dSmrg   memcpy (pchname + flen, extension, sizeof (extension));
32136ac495dSmrg 
32236ac495dSmrg   if (stat (pchname, &st) == 0)
32336ac495dSmrg     {
32436ac495dSmrg       DIR *pchdir;
32536ac495dSmrg       struct dirent *d;
32636ac495dSmrg       size_t dlen, plen = len;
32736ac495dSmrg 
32836ac495dSmrg       if (!S_ISDIR (st.st_mode))
32936ac495dSmrg 	valid = validate_pch (pfile, file, pchname);
33036ac495dSmrg       else if ((pchdir = opendir (pchname)) != NULL)
33136ac495dSmrg 	{
33236ac495dSmrg 	  pchname[plen - 1] = '/';
33336ac495dSmrg 	  while ((d = readdir (pchdir)) != NULL)
33436ac495dSmrg 	    {
33536ac495dSmrg 	      dlen = strlen (d->d_name) + 1;
33636ac495dSmrg 	      if ((strcmp (d->d_name, ".") == 0)
33736ac495dSmrg 		  || (strcmp (d->d_name, "..") == 0))
33836ac495dSmrg 		continue;
33936ac495dSmrg 	      if (dlen + plen > len)
34036ac495dSmrg 		{
34136ac495dSmrg 		  len += dlen + 64;
34236ac495dSmrg 		  pchname = XRESIZEVEC (char, pchname, len);
34336ac495dSmrg 		}
34436ac495dSmrg 	      memcpy (pchname + plen, d->d_name, dlen);
34536ac495dSmrg 	      valid = validate_pch (pfile, file, pchname);
34636ac495dSmrg 	      if (valid)
34736ac495dSmrg 		break;
34836ac495dSmrg 	    }
34936ac495dSmrg 	  closedir (pchdir);
35036ac495dSmrg 	}
35136ac495dSmrg       if (!valid)
35236ac495dSmrg 	*invalid_pch = true;
35336ac495dSmrg     }
35436ac495dSmrg 
35536ac495dSmrg   if (valid)
35636ac495dSmrg     file->pchname = pchname;
35736ac495dSmrg   else
35836ac495dSmrg     free (pchname);
35936ac495dSmrg 
36036ac495dSmrg   return valid;
36136ac495dSmrg }
36236ac495dSmrg 
36336ac495dSmrg /* Canonicalize the path to FILE.  Return the canonical form if it is
36436ac495dSmrg    shorter, otherwise return NULL.  This function does NOT free the
36536ac495dSmrg    memory pointed by FILE.  */
36636ac495dSmrg 
36736ac495dSmrg static char *
maybe_shorter_path(const char * file)36836ac495dSmrg maybe_shorter_path (const char * file)
36936ac495dSmrg {
37036ac495dSmrg   char * file2 = lrealpath (file);
37136ac495dSmrg   if (file2 && strlen (file2) < strlen (file))
37236ac495dSmrg     {
37336ac495dSmrg       return file2;
37436ac495dSmrg     }
37536ac495dSmrg   else
37636ac495dSmrg     {
37736ac495dSmrg       free (file2);
37836ac495dSmrg       return NULL;
37936ac495dSmrg     }
38036ac495dSmrg }
38136ac495dSmrg 
38236ac495dSmrg /* Try to open the path FILE->name appended to FILE->dir.  This is
38336ac495dSmrg    where remap and PCH intercept the file lookup process.  Return true
38436ac495dSmrg    if the file was found, whether or not the open was successful.
38536ac495dSmrg    Set *INVALID_PCH to true if a PCH file is found but wasn't valid.
38636ac495dSmrg    Use LOC when emitting any diagnostics.  */
38736ac495dSmrg 
38836ac495dSmrg static bool
find_file_in_dir(cpp_reader * pfile,_cpp_file * file,bool * invalid_pch,location_t loc)38936ac495dSmrg find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch,
390c0a68be4Smrg 		  location_t loc)
39136ac495dSmrg {
39236ac495dSmrg   char *path;
39336ac495dSmrg 
39436ac495dSmrg   if (CPP_OPTION (pfile, remap) && (path = remap_filename (pfile, file)))
39536ac495dSmrg     ;
39636ac495dSmrg   else
39736ac495dSmrg     if (file->dir->construct)
39836ac495dSmrg       path = file->dir->construct (file->name, file->dir);
39936ac495dSmrg     else
40036ac495dSmrg       path = append_file_to_dir (file->name, file->dir);
40136ac495dSmrg 
40236ac495dSmrg   if (path)
40336ac495dSmrg     {
40436ac495dSmrg       hashval_t hv;
40536ac495dSmrg       char *copy;
40636ac495dSmrg       void **pp;
40736ac495dSmrg 
40836ac495dSmrg       /* We try to canonicalize system headers.  For DOS based file
40936ac495dSmrg        * system, we always try to shorten non-system headers, as DOS
41036ac495dSmrg        * has a tighter constraint on max path length.  */
41136ac495dSmrg       if ((CPP_OPTION (pfile, canonical_system_headers) && file->dir->sysp)
41236ac495dSmrg #ifdef HAVE_DOS_BASED_FILE_SYSTEM
41336ac495dSmrg 	  || !file->dir->sysp
41436ac495dSmrg #endif
41536ac495dSmrg 	 )
41636ac495dSmrg 	{
41736ac495dSmrg 	  char * canonical_path = maybe_shorter_path (path);
41836ac495dSmrg 	  if (canonical_path)
41936ac495dSmrg 	    {
42036ac495dSmrg 	      /* The canonical path was newly allocated.  Let's free the
42136ac495dSmrg 		 non-canonical one.  */
42236ac495dSmrg 	      free (path);
42336ac495dSmrg 	      path = canonical_path;
42436ac495dSmrg 	    }
42536ac495dSmrg 	}
42636ac495dSmrg 
42736ac495dSmrg       hv = htab_hash_string (path);
42836ac495dSmrg       if (htab_find_with_hash (pfile->nonexistent_file_hash, path, hv) != NULL)
42936ac495dSmrg 	{
43036ac495dSmrg 	  file->err_no = ENOENT;
43136ac495dSmrg 	  return false;
43236ac495dSmrg 	}
43336ac495dSmrg 
43436ac495dSmrg       file->path = path;
43536ac495dSmrg       if (pch_open_file (pfile, file, invalid_pch))
43636ac495dSmrg 	return true;
43736ac495dSmrg 
43836ac495dSmrg       if (open_file (file))
43936ac495dSmrg 	return true;
44036ac495dSmrg 
44136ac495dSmrg       if (file->err_no != ENOENT)
44236ac495dSmrg 	{
44336ac495dSmrg 	  open_file_failed (pfile, file, 0, loc);
44436ac495dSmrg 	  return true;
44536ac495dSmrg 	}
44636ac495dSmrg 
44736ac495dSmrg       /* We copy the path name onto an obstack partly so that we don't
44836ac495dSmrg 	 leak the memory, but mostly so that we don't fragment the
44936ac495dSmrg 	 heap.  */
45036ac495dSmrg       copy = (char *) obstack_copy0 (&pfile->nonexistent_file_ob, path,
45136ac495dSmrg 				     strlen (path));
45236ac495dSmrg       free (path);
45336ac495dSmrg       pp = htab_find_slot_with_hash (pfile->nonexistent_file_hash,
45436ac495dSmrg 				     copy, hv, INSERT);
45536ac495dSmrg       *pp = copy;
45636ac495dSmrg 
45736ac495dSmrg       file->path = file->name;
45836ac495dSmrg     }
45936ac495dSmrg   else
46036ac495dSmrg     {
46136ac495dSmrg       file->err_no = ENOENT;
46236ac495dSmrg       file->path = NULL;
46336ac495dSmrg     }
46436ac495dSmrg 
46536ac495dSmrg   return false;
46636ac495dSmrg }
46736ac495dSmrg 
468c0a68be4Smrg /* Return true iff the missing_header callback found the given HEADER.  */
46936ac495dSmrg static bool
search_path_exhausted(cpp_reader * pfile,const char * header,_cpp_file * file)47036ac495dSmrg search_path_exhausted (cpp_reader *pfile, const char *header, _cpp_file *file)
47136ac495dSmrg {
47236ac495dSmrg   missing_header_cb func = pfile->cb.missing_header;
47336ac495dSmrg 
47436ac495dSmrg   /* When the regular search path doesn't work, try context dependent
47536ac495dSmrg      headers search paths.  */
47636ac495dSmrg   if (func
47736ac495dSmrg       && file->dir == NULL)
47836ac495dSmrg     {
47936ac495dSmrg       if ((file->path = func (pfile, header, &file->dir)) != NULL)
48036ac495dSmrg 	{
48136ac495dSmrg 	  if (open_file (file))
48236ac495dSmrg 	    return true;
48336ac495dSmrg 	  free ((void *)file->path);
48436ac495dSmrg 	}
48536ac495dSmrg       file->path = file->name;
48636ac495dSmrg     }
48736ac495dSmrg 
48836ac495dSmrg   return false;
48936ac495dSmrg }
49036ac495dSmrg 
49136ac495dSmrg bool
_cpp_find_failed(_cpp_file * file)49236ac495dSmrg _cpp_find_failed (_cpp_file *file)
49336ac495dSmrg {
49436ac495dSmrg   return file->err_no != 0;
49536ac495dSmrg }
49636ac495dSmrg 
49736ac495dSmrg /* Given a filename FNAME search for such a file in the include path
49836ac495dSmrg    starting from START_DIR.  If FNAME is the empty string it is
49936ac495dSmrg    interpreted as STDIN if START_DIR is PFILE->no_search_path.
50036ac495dSmrg 
50136ac495dSmrg    If the file is not found in the file cache fall back to the O/S and
50236ac495dSmrg    add the result to our cache.
50336ac495dSmrg 
50436ac495dSmrg    If the file was not found in the filesystem, or there was an error
50536ac495dSmrg    opening it, then ERR_NO is nonzero and FD is -1.  If the file was
50636ac495dSmrg    found, then ERR_NO is zero and FD could be -1 or an open file
50736ac495dSmrg    descriptor.  FD can be -1 if the file was found in the cache and
50836ac495dSmrg    had previously been closed.  To open it again pass the return value
50936ac495dSmrg    to open_file().
51036ac495dSmrg 
51136ac495dSmrg    If IMPLICIT_PREINCLUDE then it is OK for the file to be missing.
51236ac495dSmrg    If present, it is OK for a precompiled header to be included after
51336ac495dSmrg    it.
51436ac495dSmrg 
51536ac495dSmrg    Use LOC as the location for any errors.  */
51636ac495dSmrg 
51736ac495dSmrg _cpp_file *
_cpp_find_file(cpp_reader * pfile,const char * fname,cpp_dir * start_dir,int angle_brackets,bool fake,bool implicit_preinclude,bool has_include,location_t loc)51836ac495dSmrg _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
519*8feb0f0bSmrg 		int angle_brackets,
520*8feb0f0bSmrg 		bool fake, bool implicit_preinclude, bool has_include,
521c0a68be4Smrg 		location_t loc)
52236ac495dSmrg {
52336ac495dSmrg   struct cpp_file_hash_entry *entry;
52436ac495dSmrg   void **hash_slot;
52536ac495dSmrg   _cpp_file *file;
52636ac495dSmrg   bool invalid_pch = false;
52736ac495dSmrg   bool saw_bracket_include = false;
52836ac495dSmrg   bool saw_quote_include = false;
52936ac495dSmrg   struct cpp_dir *found_in_cache = NULL;
53036ac495dSmrg 
53136ac495dSmrg   /* Ensure we get no confusion between cached files and directories.  */
53236ac495dSmrg   if (start_dir == NULL)
53336ac495dSmrg     cpp_error_at (pfile, CPP_DL_ICE, loc, "NULL directory in find_file");
53436ac495dSmrg 
53536ac495dSmrg   hash_slot
53636ac495dSmrg     = htab_find_slot_with_hash (pfile->file_hash, fname,
53736ac495dSmrg 				htab_hash_string (fname), INSERT);
53836ac495dSmrg 
53936ac495dSmrg   /* First check the cache before we resort to memory allocation.  */
54036ac495dSmrg   entry = search_cache ((struct cpp_file_hash_entry *) *hash_slot, start_dir);
54136ac495dSmrg   if (entry)
54236ac495dSmrg     return entry->u.file;
54336ac495dSmrg 
54436ac495dSmrg   file = make_cpp_file (pfile, start_dir, fname);
54536ac495dSmrg   file->implicit_preinclude
54636ac495dSmrg     = (implicit_preinclude
54736ac495dSmrg        || (pfile->buffer
54836ac495dSmrg 	   && pfile->buffer->file->implicit_preinclude));
54936ac495dSmrg 
550*8feb0f0bSmrg   if (!fake)
55136ac495dSmrg     /* Try each path in the include chain.  */
552*8feb0f0bSmrg     for (;;)
55336ac495dSmrg       {
55436ac495dSmrg 	if (find_file_in_dir (pfile, file, &invalid_pch, loc))
55536ac495dSmrg 	  break;
55636ac495dSmrg 
55736ac495dSmrg 	file->dir = file->dir->next;
55836ac495dSmrg 	if (file->dir == NULL)
55936ac495dSmrg 	  {
56036ac495dSmrg 	    if (search_path_exhausted (pfile, fname, file))
56136ac495dSmrg 	      {
562*8feb0f0bSmrg 		/* Although this file must not go in the cache,
563*8feb0f0bSmrg 		   because the file found might depend on things (like
564*8feb0f0bSmrg 		   the current file) that aren't represented in the
565*8feb0f0bSmrg 		   cache, it still has to go in the list of all files
566*8feb0f0bSmrg 		   so that #import works.  */
56736ac495dSmrg 		file->next_file = pfile->all_files;
56836ac495dSmrg 		pfile->all_files = file;
56936ac495dSmrg 		if (*hash_slot == NULL)
57036ac495dSmrg 		  {
571*8feb0f0bSmrg 		    /* If *hash_slot is NULL, the above
572*8feb0f0bSmrg 		       htab_find_slot_with_hash call just created the
573*8feb0f0bSmrg 		       slot, but we aren't going to store there
574*8feb0f0bSmrg 		       anything, so need to remove the newly created
575*8feb0f0bSmrg 		       entry.  htab_clear_slot requires that it is
576*8feb0f0bSmrg 		       non-NULL, so store there some non-NULL pointer,
577*8feb0f0bSmrg 		       htab_clear_slot will overwrite it
578*8feb0f0bSmrg 		       immediately.  */
57936ac495dSmrg 		    *hash_slot = file;
58036ac495dSmrg 		    htab_clear_slot (pfile->file_hash, hash_slot);
58136ac495dSmrg 		  }
58236ac495dSmrg 		return file;
58336ac495dSmrg 	      }
58436ac495dSmrg 
58536ac495dSmrg 	    if (invalid_pch)
58636ac495dSmrg 	      {
58736ac495dSmrg 		cpp_error (pfile, CPP_DL_ERROR,
588*8feb0f0bSmrg 			   "one or more PCH files were found,"
589*8feb0f0bSmrg 			   " but they were invalid");
59036ac495dSmrg 		if (!cpp_get_options (pfile)->warn_invalid_pch)
59136ac495dSmrg 		  cpp_error (pfile, CPP_DL_ERROR,
59236ac495dSmrg 			     "use -Winvalid-pch for more information");
59336ac495dSmrg 	      }
594*8feb0f0bSmrg 
59536ac495dSmrg 	    if (implicit_preinclude)
59636ac495dSmrg 	      {
59736ac495dSmrg 		free ((char *) file->name);
59836ac495dSmrg 		free (file);
59936ac495dSmrg 		if (*hash_slot == NULL)
60036ac495dSmrg 		  {
60136ac495dSmrg 		    /* See comment on the above htab_clear_slot call.  */
60236ac495dSmrg 		    *hash_slot = file;
60336ac495dSmrg 		    htab_clear_slot (pfile->file_hash, hash_slot);
60436ac495dSmrg 		  }
60536ac495dSmrg 		return NULL;
60636ac495dSmrg 	      }
607*8feb0f0bSmrg 
608*8feb0f0bSmrg 	    if (!has_include)
60936ac495dSmrg 	      open_file_failed (pfile, file, angle_brackets, loc);
61036ac495dSmrg 	    break;
61136ac495dSmrg 	  }
61236ac495dSmrg 
61336ac495dSmrg 	/* Only check the cache for the starting location (done above)
61436ac495dSmrg 	   and the quote and bracket chain heads because there are no
61536ac495dSmrg 	   other possible starting points for searches.  */
61636ac495dSmrg 	if (file->dir == pfile->bracket_include)
61736ac495dSmrg 	  saw_bracket_include = true;
61836ac495dSmrg 	else if (file->dir == pfile->quote_include)
61936ac495dSmrg 	  saw_quote_include = true;
62036ac495dSmrg 	else
62136ac495dSmrg 	  continue;
62236ac495dSmrg 
623*8feb0f0bSmrg 	entry
624*8feb0f0bSmrg 	  = search_cache ((struct cpp_file_hash_entry *) *hash_slot, file->dir);
62536ac495dSmrg 	if (entry)
62636ac495dSmrg 	  {
62736ac495dSmrg 	    found_in_cache = file->dir;
62836ac495dSmrg 	    break;
62936ac495dSmrg 	  }
63036ac495dSmrg       }
63136ac495dSmrg 
63236ac495dSmrg   if (entry)
63336ac495dSmrg     {
63436ac495dSmrg       /* Cache for START_DIR too, sharing the _cpp_file structure.  */
63536ac495dSmrg       free ((char *) file->name);
63636ac495dSmrg       free (file);
63736ac495dSmrg       file = entry->u.file;
63836ac495dSmrg     }
63936ac495dSmrg   else
64036ac495dSmrg     {
64136ac495dSmrg       /* This is a new file; put it in the list.  */
64236ac495dSmrg       file->next_file = pfile->all_files;
64336ac495dSmrg       pfile->all_files = file;
64436ac495dSmrg     }
64536ac495dSmrg 
64636ac495dSmrg   /* Store this new result in the hash table.  */
64736ac495dSmrg   entry = new_file_hash_entry (pfile);
64836ac495dSmrg   entry->next = (struct cpp_file_hash_entry *) *hash_slot;
64936ac495dSmrg   entry->start_dir = start_dir;
65036ac495dSmrg   entry->location = pfile->line_table->highest_location;
65136ac495dSmrg   entry->u.file = file;
65236ac495dSmrg   *hash_slot = (void *) entry;
65336ac495dSmrg 
65436ac495dSmrg   /* If we passed the quote or bracket chain heads, cache them also.
65536ac495dSmrg      This speeds up processing if there are lots of -I options.  */
65636ac495dSmrg   if (saw_bracket_include
65736ac495dSmrg       && pfile->bracket_include != start_dir
65836ac495dSmrg       && found_in_cache != pfile->bracket_include)
65936ac495dSmrg     {
66036ac495dSmrg       entry = new_file_hash_entry (pfile);
66136ac495dSmrg       entry->next = (struct cpp_file_hash_entry *) *hash_slot;
66236ac495dSmrg       entry->start_dir = pfile->bracket_include;
66336ac495dSmrg       entry->location = pfile->line_table->highest_location;
66436ac495dSmrg       entry->u.file = file;
66536ac495dSmrg       *hash_slot = (void *) entry;
66636ac495dSmrg     }
66736ac495dSmrg   if (saw_quote_include
66836ac495dSmrg       && pfile->quote_include != start_dir
66936ac495dSmrg       && found_in_cache != pfile->quote_include)
67036ac495dSmrg     {
67136ac495dSmrg       entry = new_file_hash_entry (pfile);
67236ac495dSmrg       entry->next = (struct cpp_file_hash_entry *) *hash_slot;
67336ac495dSmrg       entry->start_dir = pfile->quote_include;
67436ac495dSmrg       entry->location = pfile->line_table->highest_location;
67536ac495dSmrg       entry->u.file = file;
67636ac495dSmrg       *hash_slot = (void *) entry;
67736ac495dSmrg     }
67836ac495dSmrg 
67936ac495dSmrg   return file;
68036ac495dSmrg }
68136ac495dSmrg 
68236ac495dSmrg /* Read a file into FILE->buffer, returning true on success.
68336ac495dSmrg 
68436ac495dSmrg    If FILE->fd is something weird, like a block device, we don't want
68536ac495dSmrg    to read it at all.  Don't even try to figure out what something is,
68636ac495dSmrg    except for plain files and block devices, since there is no
68736ac495dSmrg    reliable portable way of doing this.
68836ac495dSmrg 
68936ac495dSmrg    Use LOC for any diagnostics.
69036ac495dSmrg 
69136ac495dSmrg    FIXME: Flush file cache and try again if we run out of memory.  */
69236ac495dSmrg static bool
read_file_guts(cpp_reader * pfile,_cpp_file * file,location_t loc)693c0a68be4Smrg read_file_guts (cpp_reader *pfile, _cpp_file *file, location_t loc)
69436ac495dSmrg {
69536ac495dSmrg   ssize_t size, total, count;
69636ac495dSmrg   uchar *buf;
69736ac495dSmrg   bool regular;
69836ac495dSmrg 
69936ac495dSmrg   if (S_ISBLK (file->st.st_mode))
70036ac495dSmrg     {
70136ac495dSmrg       cpp_error_at (pfile, CPP_DL_ERROR, loc,
70236ac495dSmrg 		    "%s is a block device", file->path);
70336ac495dSmrg       return false;
70436ac495dSmrg     }
70536ac495dSmrg 
70636ac495dSmrg   regular = S_ISREG (file->st.st_mode) != 0;
70736ac495dSmrg   if (regular)
70836ac495dSmrg     {
70936ac495dSmrg       /* off_t might have a wider range than ssize_t - in other words,
71036ac495dSmrg 	 the max size of a file might be bigger than the address
71136ac495dSmrg 	 space.  We can't handle a file that large.  (Anyone with
71236ac495dSmrg 	 a single source file bigger than 2GB needs to rethink
71336ac495dSmrg 	 their coding style.)  Some systems (e.g. AIX 4.1) define
71436ac495dSmrg 	 SSIZE_MAX to be much smaller than the actual range of the
71536ac495dSmrg 	 type.  Use INTTYPE_MAXIMUM unconditionally to ensure this
71636ac495dSmrg 	 does not bite us.  */
71736ac495dSmrg       if (file->st.st_size > INTTYPE_MAXIMUM (ssize_t))
71836ac495dSmrg 	{
71936ac495dSmrg 	  cpp_error_at (pfile, CPP_DL_ERROR, loc,
72036ac495dSmrg 			"%s is too large", file->path);
72136ac495dSmrg 	  return false;
72236ac495dSmrg 	}
72336ac495dSmrg 
72436ac495dSmrg       size = file->st.st_size;
72536ac495dSmrg     }
72636ac495dSmrg   else
72736ac495dSmrg     /* 8 kilobytes is a sensible starting size.  It ought to be bigger
72836ac495dSmrg        than the kernel pipe buffer, and it's definitely bigger than
72936ac495dSmrg        the majority of C source files.  */
73036ac495dSmrg     size = 8 * 1024;
73136ac495dSmrg 
73236ac495dSmrg   /* The + 16 here is space for the final '\n' and 15 bytes of padding,
73336ac495dSmrg      used to quiet warnings from valgrind or Address Sanitizer, when the
73436ac495dSmrg      optimized lexer accesses aligned 16-byte memory chunks, including
73536ac495dSmrg      the bytes after the malloced, area, and stops lexing on '\n'.  */
73636ac495dSmrg   buf = XNEWVEC (uchar, size + 16);
73736ac495dSmrg   total = 0;
73836ac495dSmrg   while ((count = read (file->fd, buf + total, size - total)) > 0)
73936ac495dSmrg     {
74036ac495dSmrg       total += count;
74136ac495dSmrg 
74236ac495dSmrg       if (total == size)
74336ac495dSmrg 	{
74436ac495dSmrg 	  if (regular)
74536ac495dSmrg 	    break;
74636ac495dSmrg 	  size *= 2;
74736ac495dSmrg 	  buf = XRESIZEVEC (uchar, buf, size + 16);
74836ac495dSmrg 	}
74936ac495dSmrg     }
75036ac495dSmrg 
75136ac495dSmrg   if (count < 0)
75236ac495dSmrg     {
75336ac495dSmrg       cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, loc);
75436ac495dSmrg       free (buf);
75536ac495dSmrg       return false;
75636ac495dSmrg     }
75736ac495dSmrg 
75836ac495dSmrg   if (regular && total != size && STAT_SIZE_RELIABLE (file->st))
75936ac495dSmrg     cpp_error_at (pfile, CPP_DL_WARNING, loc,
76036ac495dSmrg 	       "%s is shorter than expected", file->path);
76136ac495dSmrg 
76236ac495dSmrg   file->buffer = _cpp_convert_input (pfile,
76336ac495dSmrg 				     CPP_OPTION (pfile, input_charset),
76436ac495dSmrg 				     buf, size + 16, total,
76536ac495dSmrg 				     &file->buffer_start,
76636ac495dSmrg 				     &file->st.st_size);
76736ac495dSmrg   file->buffer_valid = true;
76836ac495dSmrg 
76936ac495dSmrg   return true;
77036ac495dSmrg }
77136ac495dSmrg 
77236ac495dSmrg /* Convenience wrapper around read_file_guts that opens the file if
77336ac495dSmrg    necessary and closes the file descriptor after reading.  FILE must
77436ac495dSmrg    have been passed through find_file() at some stage.  Use LOC for
77536ac495dSmrg    any diagnostics.  */
77636ac495dSmrg static bool
read_file(cpp_reader * pfile,_cpp_file * file,location_t loc)777c0a68be4Smrg read_file (cpp_reader *pfile, _cpp_file *file, location_t loc)
77836ac495dSmrg {
77936ac495dSmrg   /* If we already have its contents in memory, succeed immediately.  */
78036ac495dSmrg   if (file->buffer_valid)
78136ac495dSmrg     return true;
78236ac495dSmrg 
78336ac495dSmrg   /* If an earlier read failed for some reason don't try again.  */
78436ac495dSmrg   if (file->dont_read || file->err_no)
78536ac495dSmrg     return false;
78636ac495dSmrg 
78736ac495dSmrg   if (file->fd == -1 && !open_file (file))
78836ac495dSmrg     {
78936ac495dSmrg       open_file_failed (pfile, file, 0, loc);
79036ac495dSmrg       return false;
79136ac495dSmrg     }
79236ac495dSmrg 
79336ac495dSmrg   file->dont_read = !read_file_guts (pfile, file, loc);
79436ac495dSmrg   close (file->fd);
79536ac495dSmrg   file->fd = -1;
79636ac495dSmrg 
79736ac495dSmrg   return !file->dont_read;
79836ac495dSmrg }
79936ac495dSmrg 
800*8feb0f0bSmrg /* Returns TRUE if FILE is already known to be idempotent, and should
801*8feb0f0bSmrg    therefore not be read again.  */
80236ac495dSmrg static bool
is_known_idempotent_file(cpp_reader * pfile,_cpp_file * file,bool import)803*8feb0f0bSmrg is_known_idempotent_file (cpp_reader *pfile, _cpp_file *file, bool import)
80436ac495dSmrg {
80536ac495dSmrg   /* Skip once-only files.  */
80636ac495dSmrg   if (file->once_only)
807*8feb0f0bSmrg     return true;
80836ac495dSmrg 
80936ac495dSmrg   /* We must mark the file once-only if #import now, before header
81036ac495dSmrg      guard checks.  Otherwise, undefining the header guard might
81136ac495dSmrg      cause the file to be re-stacked.  */
81236ac495dSmrg   if (import)
81336ac495dSmrg     {
81436ac495dSmrg       _cpp_mark_file_once_only (pfile, file);
81536ac495dSmrg 
81636ac495dSmrg       /* Don't stack files that have been stacked before.  */
81736ac495dSmrg       if (file->stack_count)
818*8feb0f0bSmrg 	return true;
81936ac495dSmrg     }
82036ac495dSmrg 
82136ac495dSmrg   /* Skip if the file had a header guard and the macro is defined.
82236ac495dSmrg      PCH relies on this appearing before the PCH handler below.  */
823c0a68be4Smrg   if (file->cmacro && cpp_macro_p (file->cmacro))
824*8feb0f0bSmrg     return true;
82536ac495dSmrg 
82636ac495dSmrg   /* Handle PCH files immediately; don't stack them.  */
82736ac495dSmrg   if (file->pchname)
82836ac495dSmrg     {
82936ac495dSmrg       pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path);
83036ac495dSmrg       file->fd = -1;
83136ac495dSmrg       free ((void *) file->pchname);
83236ac495dSmrg       file->pchname = NULL;
833*8feb0f0bSmrg       return true;
834*8feb0f0bSmrg     }
835*8feb0f0bSmrg 
83636ac495dSmrg   return false;
83736ac495dSmrg }
83836ac495dSmrg 
839*8feb0f0bSmrg /* Return TRUE if file has unique contents, so we should read process
840*8feb0f0bSmrg    it.  The file's contents must already have been read.  */
84136ac495dSmrg 
842*8feb0f0bSmrg static bool
has_unique_contents(cpp_reader * pfile,_cpp_file * file,bool import,location_t loc)843*8feb0f0bSmrg has_unique_contents (cpp_reader *pfile, _cpp_file *file, bool import,
844*8feb0f0bSmrg 		     location_t loc)
845*8feb0f0bSmrg {
84636ac495dSmrg   /* Check the file against the PCH file.  This is done before
84736ac495dSmrg      checking against files we've already seen, since it may save on
84836ac495dSmrg      I/O.  */
84936ac495dSmrg   if (check_file_against_entries (pfile, file, import))
85036ac495dSmrg     {
85136ac495dSmrg       /* If this isn't a #import, but yet we can't include the file,
85236ac495dSmrg 	 that means that it was #import-ed in the PCH file,
85336ac495dSmrg 	 so we can never include it again.  */
85436ac495dSmrg       if (! import)
85536ac495dSmrg 	_cpp_mark_file_once_only (pfile, file);
85636ac495dSmrg       return false;
85736ac495dSmrg     }
85836ac495dSmrg 
85936ac495dSmrg   /* Now we've read the file's contents, we can stack it if there
86036ac495dSmrg      are no once-only files.  */
86136ac495dSmrg   if (!pfile->seen_once_only)
86236ac495dSmrg     return true;
86336ac495dSmrg 
86436ac495dSmrg   /* We may have read the file under a different name.  Look
86536ac495dSmrg      for likely candidates and compare file contents to be sure.  */
866*8feb0f0bSmrg   for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
86736ac495dSmrg     {
86836ac495dSmrg       if (f == file)
869*8feb0f0bSmrg 	continue; /* It'sa me!  */
87036ac495dSmrg 
87136ac495dSmrg       if ((import || f->once_only)
87236ac495dSmrg 	  && f->err_no == 0
87336ac495dSmrg 	  && f->st.st_mtime == file->st.st_mtime
87436ac495dSmrg 	  && f->st.st_size == file->st.st_size)
87536ac495dSmrg 	{
87636ac495dSmrg 	  _cpp_file *ref_file;
87736ac495dSmrg 
87836ac495dSmrg 	  if (f->buffer && !f->buffer_valid)
87936ac495dSmrg 	    {
88036ac495dSmrg 	      /* We already have a buffer but it is not valid, because
88136ac495dSmrg 		 the file is still stacked.  Make a new one.  */
88236ac495dSmrg 	      ref_file = make_cpp_file (pfile, f->dir, f->name);
88336ac495dSmrg 	      ref_file->path = f->path;
88436ac495dSmrg 	    }
88536ac495dSmrg 	  else
88636ac495dSmrg 	    /* The file is not stacked anymore.  We can reuse it.  */
88736ac495dSmrg 	    ref_file = f;
88836ac495dSmrg 
889*8feb0f0bSmrg 	  bool same_file_p = (read_file (pfile, ref_file, loc)
89036ac495dSmrg 			      /* Size might have changed in read_file().  */
89136ac495dSmrg 			      && ref_file->st.st_size == file->st.st_size
892*8feb0f0bSmrg 			      && !memcmp (ref_file->buffer, file->buffer,
893*8feb0f0bSmrg 					  file->st.st_size));
89436ac495dSmrg 
89536ac495dSmrg 	  if (f->buffer && !f->buffer_valid)
89636ac495dSmrg 	    {
89736ac495dSmrg 	      ref_file->path = 0;
89836ac495dSmrg 	      destroy_cpp_file (ref_file);
89936ac495dSmrg 	    }
90036ac495dSmrg 
90136ac495dSmrg 	  if (same_file_p)
902*8feb0f0bSmrg 	    /* Already seen under a different name.  */
903*8feb0f0bSmrg 	    return false;
90436ac495dSmrg 	}
90536ac495dSmrg     }
90636ac495dSmrg 
907*8feb0f0bSmrg   return true;
90836ac495dSmrg }
90936ac495dSmrg 
91036ac495dSmrg /* Place the file referenced by FILE into a new buffer on the buffer
91136ac495dSmrg    stack if possible.  IMPORT is true if this stacking attempt is
91236ac495dSmrg    because of a #import directive.  Returns true if a buffer is
91336ac495dSmrg    stacked.  Use LOC for any diagnostics.  */
91436ac495dSmrg bool
_cpp_stack_file(cpp_reader * pfile,_cpp_file * file,include_type type,location_t loc)915*8feb0f0bSmrg _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, include_type type,
916c0a68be4Smrg 		 location_t loc)
91736ac495dSmrg {
918*8feb0f0bSmrg   if (is_known_idempotent_file (pfile, file, type == IT_IMPORT))
91936ac495dSmrg     return false;
92036ac495dSmrg 
921*8feb0f0bSmrg   if (!read_file (pfile, file, loc))
922*8feb0f0bSmrg     return false;
923*8feb0f0bSmrg 
924*8feb0f0bSmrg   if (!has_unique_contents (pfile, file, type == IT_IMPORT, loc))
925*8feb0f0bSmrg     return false;
926*8feb0f0bSmrg 
927*8feb0f0bSmrg   int sysp = 0;
928*8feb0f0bSmrg   if (pfile->buffer && file->dir)
92936ac495dSmrg     sysp = MAX (pfile->buffer->sysp, file->dir->sysp);
93036ac495dSmrg 
93136ac495dSmrg   /* Add the file to the dependencies on its first inclusion.  */
932*8feb0f0bSmrg   if (CPP_OPTION (pfile, deps.style) > (sysp != 0)
933*8feb0f0bSmrg       && !file->stack_count
934*8feb0f0bSmrg       && file->path[0]
935*8feb0f0bSmrg       && !(file->main_file && CPP_OPTION (pfile, deps.ignore_main_file)))
93636ac495dSmrg     deps_add_dep (pfile->deps, file->path);
93736ac495dSmrg 
93836ac495dSmrg   /* Clear buffer_valid since _cpp_clean_line messes it up.  */
93936ac495dSmrg   file->buffer_valid = false;
94036ac495dSmrg   file->stack_count++;
94136ac495dSmrg 
94236ac495dSmrg   /* Stack the buffer.  */
943*8feb0f0bSmrg   cpp_buffer *buffer
944*8feb0f0bSmrg     = cpp_push_buffer (pfile, file->buffer, file->st.st_size,
94536ac495dSmrg 		       CPP_OPTION (pfile, preprocessed)
94636ac495dSmrg 		       && !CPP_OPTION (pfile, directives_only));
94736ac495dSmrg   buffer->file = file;
94836ac495dSmrg   buffer->sysp = sysp;
94936ac495dSmrg   buffer->to_free = file->buffer_start;
95036ac495dSmrg 
95136ac495dSmrg   /* Initialize controlling macro state.  */
95236ac495dSmrg   pfile->mi_valid = true;
95336ac495dSmrg   pfile->mi_cmacro = 0;
95436ac495dSmrg 
955*8feb0f0bSmrg   /* In the case of a normal #include, we're now at the start of the
956*8feb0f0bSmrg      line *following* the #include.  A separate location_t for this
957*8feb0f0bSmrg      location makes no sense, until we do the LC_LEAVE.
958*8feb0f0bSmrg 
959*8feb0f0bSmrg      This does not apply if we found a PCH file, we're not a regular
960*8feb0f0bSmrg      include, or we ran out of locations.  */
961*8feb0f0bSmrg   if (file->pchname == NULL
962*8feb0f0bSmrg       && type < IT_DIRECTIVE_HWM
963*8feb0f0bSmrg       && pfile->line_table->highest_location != LINE_MAP_MAX_LOCATION - 1)
964*8feb0f0bSmrg     pfile->line_table->highest_location--;
965*8feb0f0bSmrg 
966*8feb0f0bSmrg   /* Add line map and do callbacks.  */
96736ac495dSmrg   _cpp_do_file_change (pfile, LC_ENTER, file->path, 1, sysp);
96836ac495dSmrg 
96936ac495dSmrg   return true;
97036ac495dSmrg }
97136ac495dSmrg 
97236ac495dSmrg /* Mark FILE to be included once only.  */
97336ac495dSmrg void
_cpp_mark_file_once_only(cpp_reader * pfile,_cpp_file * file)97436ac495dSmrg _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file)
97536ac495dSmrg {
97636ac495dSmrg   pfile->seen_once_only = true;
97736ac495dSmrg   file->once_only = true;
97836ac495dSmrg }
97936ac495dSmrg 
98036ac495dSmrg /* Return the directory from which searching for FNAME should start,
98136ac495dSmrg    considering the directive TYPE and ANGLE_BRACKETS.  If there is
98236ac495dSmrg    nothing left in the path, returns NULL.  */
98336ac495dSmrg static struct cpp_dir *
search_path_head(cpp_reader * pfile,const char * fname,int angle_brackets,enum include_type type)98436ac495dSmrg search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
98536ac495dSmrg 		  enum include_type type)
98636ac495dSmrg {
98736ac495dSmrg   cpp_dir *dir;
98836ac495dSmrg   _cpp_file *file;
98936ac495dSmrg 
99036ac495dSmrg   if (IS_ABSOLUTE_PATH (fname))
99136ac495dSmrg     return &pfile->no_search_path;
99236ac495dSmrg 
99336ac495dSmrg   /* pfile->buffer is NULL when processing an -include command-line flag.  */
99436ac495dSmrg   file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file;
99536ac495dSmrg 
99636ac495dSmrg   /* For #include_next, skip in the search path past the dir in which
99736ac495dSmrg      the current file was found, but if it was found via an absolute
99836ac495dSmrg      path use the normal search logic.  */
99936ac495dSmrg   if (type == IT_INCLUDE_NEXT && file->dir
100036ac495dSmrg       && file->dir != &pfile->no_search_path)
100136ac495dSmrg     dir = file->dir->next;
100236ac495dSmrg   else if (angle_brackets)
100336ac495dSmrg     dir = pfile->bracket_include;
100436ac495dSmrg   else if (type == IT_CMDLINE)
100536ac495dSmrg     /* -include and -imacros use the #include "" chain with the
100636ac495dSmrg        preprocessor's cwd prepended.  */
100736ac495dSmrg     return make_cpp_dir (pfile, "./", false);
100836ac495dSmrg   else if (pfile->quote_ignores_source_dir)
100936ac495dSmrg     dir = pfile->quote_include;
101036ac495dSmrg   else
101136ac495dSmrg     return make_cpp_dir (pfile, dir_name_of_file (file),
101236ac495dSmrg 			 pfile->buffer ? pfile->buffer->sysp : 0);
101336ac495dSmrg 
101436ac495dSmrg   if (dir == NULL)
101536ac495dSmrg     cpp_error (pfile, CPP_DL_ERROR,
101636ac495dSmrg 	       "no include path in which to search for %s", fname);
101736ac495dSmrg 
101836ac495dSmrg   return dir;
101936ac495dSmrg }
102036ac495dSmrg 
102136ac495dSmrg /* Strip the basename from the file's path.  It ends with a slash if
102236ac495dSmrg    of nonzero length.  Note that this procedure also works for
102336ac495dSmrg    <stdin>, which is represented by the empty string.  */
102436ac495dSmrg static const char *
dir_name_of_file(_cpp_file * file)102536ac495dSmrg dir_name_of_file (_cpp_file *file)
102636ac495dSmrg {
102736ac495dSmrg   if (!file->dir_name)
102836ac495dSmrg     {
102936ac495dSmrg       size_t len = lbasename (file->path) - file->path;
103036ac495dSmrg       char *dir_name = XNEWVEC (char, len + 1);
103136ac495dSmrg 
103236ac495dSmrg       memcpy (dir_name, file->path, len);
103336ac495dSmrg       dir_name[len] = '\0';
103436ac495dSmrg       file->dir_name = dir_name;
103536ac495dSmrg     }
103636ac495dSmrg 
103736ac495dSmrg   return file->dir_name;
103836ac495dSmrg }
103936ac495dSmrg 
104036ac495dSmrg /* Handles #include-family directives (distinguished by TYPE),
104136ac495dSmrg    including HEADER, and the command line -imacros and -include.
104236ac495dSmrg    Returns true if a buffer was stacked.  */
104336ac495dSmrg bool
_cpp_stack_include(cpp_reader * pfile,const char * fname,int angle_brackets,enum include_type type,location_t loc)104436ac495dSmrg _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
1045c0a68be4Smrg 		    enum include_type type, location_t loc)
104636ac495dSmrg {
104736ac495dSmrg   /* For -include command-line flags we have type == IT_CMDLINE.
104836ac495dSmrg      When the first -include file is processed we have the case, where
104936ac495dSmrg      pfile->cur_token == pfile->cur_run->base, we are directly called up
105036ac495dSmrg      by the front end.  However in the case of the second -include file,
105136ac495dSmrg      we are called from _cpp_lex_token -> _cpp_get_fresh_line ->
105236ac495dSmrg      cpp_push_include, with pfile->cur_token != pfile->cur_run->base,
105336ac495dSmrg      and pfile->cur_token[-1].src_loc not (yet) initialized.
105436ac495dSmrg      However, when the include file cannot be found, we need src_loc to
105536ac495dSmrg      be initialized to some safe value: 0 means UNKNOWN_LOCATION.  */
105636ac495dSmrg   if (type == IT_CMDLINE && pfile->cur_token != pfile->cur_run->base)
105736ac495dSmrg     pfile->cur_token[-1].src_loc = 0;
105836ac495dSmrg 
1059*8feb0f0bSmrg   cpp_dir *dir = search_path_head (pfile, fname, angle_brackets, type);
106036ac495dSmrg   if (!dir)
106136ac495dSmrg     return false;
106236ac495dSmrg 
1063*8feb0f0bSmrg   _cpp_file *file = _cpp_find_file (pfile, fname, dir, angle_brackets,
1064*8feb0f0bSmrg 				    false, type == IT_DEFAULT, false, loc);
106536ac495dSmrg   if (type == IT_DEFAULT && file == NULL)
106636ac495dSmrg     return false;
106736ac495dSmrg 
1068*8feb0f0bSmrg   return _cpp_stack_file (pfile, file, type, loc);
106936ac495dSmrg }
107036ac495dSmrg 
107136ac495dSmrg /* Could not open FILE.  The complication is dependency output.  */
107236ac495dSmrg static void
open_file_failed(cpp_reader * pfile,_cpp_file * file,int angle_brackets,location_t loc)107336ac495dSmrg open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets,
1074c0a68be4Smrg 		  location_t loc)
107536ac495dSmrg {
107636ac495dSmrg   int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0;
107736ac495dSmrg   bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp);
107836ac495dSmrg 
107936ac495dSmrg   errno = file->err_no;
108036ac495dSmrg   if (print_dep && CPP_OPTION (pfile, deps.missing_files) && errno == ENOENT)
108136ac495dSmrg     {
108236ac495dSmrg       deps_add_dep (pfile->deps, file->name);
108336ac495dSmrg       /* If the preprocessor output (other than dependency information) is
108436ac495dSmrg          being used, we must also flag an error.  */
108536ac495dSmrg       if (CPP_OPTION (pfile, deps.need_preprocessor_output))
108636ac495dSmrg 	cpp_errno_filename (pfile, CPP_DL_FATAL,
108736ac495dSmrg 			    file->path ? file->path : file->name,
108836ac495dSmrg 			    loc);
108936ac495dSmrg     }
109036ac495dSmrg   else
109136ac495dSmrg     {
109236ac495dSmrg       /* If we are not outputting dependencies, or if we are and dependencies
109336ac495dSmrg          were requested for this file, or if preprocessor output is needed
109436ac495dSmrg          in addition to dependency information, this is an error.
109536ac495dSmrg 
109636ac495dSmrg          Otherwise (outputting dependencies but not for this file, and not
109736ac495dSmrg          using the preprocessor output), we can still produce correct output
109836ac495dSmrg          so it's only a warning.  */
109936ac495dSmrg       if (CPP_OPTION (pfile, deps.style) == DEPS_NONE
110036ac495dSmrg           || print_dep
110136ac495dSmrg           || CPP_OPTION (pfile, deps.need_preprocessor_output))
110236ac495dSmrg 	cpp_errno_filename (pfile, CPP_DL_FATAL,
110336ac495dSmrg 			    file->path ? file->path : file->name,
110436ac495dSmrg 			    loc);
110536ac495dSmrg       else
110636ac495dSmrg 	cpp_errno_filename (pfile, CPP_DL_WARNING,
110736ac495dSmrg 			    file->path ? file->path : file->name,
110836ac495dSmrg 			    loc);
110936ac495dSmrg     }
111036ac495dSmrg }
111136ac495dSmrg 
111236ac495dSmrg /* Search in the chain beginning at HEAD for a file whose search path
111336ac495dSmrg    started at START_DIR != NULL.  */
111436ac495dSmrg static struct cpp_file_hash_entry *
search_cache(struct cpp_file_hash_entry * head,const cpp_dir * start_dir)111536ac495dSmrg search_cache (struct cpp_file_hash_entry *head, const cpp_dir *start_dir)
111636ac495dSmrg {
111736ac495dSmrg   while (head && head->start_dir != start_dir)
111836ac495dSmrg     head = head->next;
111936ac495dSmrg 
112036ac495dSmrg   return head;
112136ac495dSmrg }
112236ac495dSmrg 
112336ac495dSmrg /* Allocate a new _cpp_file structure.  */
112436ac495dSmrg static _cpp_file *
make_cpp_file(cpp_reader * pfile,cpp_dir * dir,const char * fname)112536ac495dSmrg make_cpp_file (cpp_reader *pfile, cpp_dir *dir, const char *fname)
112636ac495dSmrg {
112736ac495dSmrg   _cpp_file *file;
112836ac495dSmrg 
112936ac495dSmrg   file = XCNEW (_cpp_file);
113036ac495dSmrg   file->main_file = !pfile->buffer;
113136ac495dSmrg   file->fd = -1;
113236ac495dSmrg   file->dir = dir;
113336ac495dSmrg   file->name = xstrdup (fname);
113436ac495dSmrg 
113536ac495dSmrg   return file;
113636ac495dSmrg }
113736ac495dSmrg 
113836ac495dSmrg /* Release a _cpp_file structure.  */
113936ac495dSmrg static void
destroy_cpp_file(_cpp_file * file)114036ac495dSmrg destroy_cpp_file (_cpp_file *file)
114136ac495dSmrg {
114236ac495dSmrg   free ((void *) file->buffer_start);
114336ac495dSmrg   free ((void *) file->name);
114436ac495dSmrg   free ((void *) file->path);
114536ac495dSmrg   free (file);
114636ac495dSmrg }
114736ac495dSmrg 
114836ac495dSmrg /* Release all the files allocated by this reader.  */
114936ac495dSmrg static void
destroy_all_cpp_files(cpp_reader * pfile)115036ac495dSmrg destroy_all_cpp_files (cpp_reader *pfile)
115136ac495dSmrg {
115236ac495dSmrg   _cpp_file *iter = pfile->all_files;
115336ac495dSmrg   while (iter)
115436ac495dSmrg     {
115536ac495dSmrg       _cpp_file *next = iter->next_file;
115636ac495dSmrg       destroy_cpp_file (iter);
115736ac495dSmrg       iter = next;
115836ac495dSmrg     }
115936ac495dSmrg }
116036ac495dSmrg 
116136ac495dSmrg /* A hash of directory names.  The directory names are the path names
116236ac495dSmrg    of files which contain a #include "", the included file name is
116336ac495dSmrg    appended to this directories.
116436ac495dSmrg 
116536ac495dSmrg    To avoid duplicate entries we follow the convention that all
116636ac495dSmrg    non-empty directory names should end in a '/'.  DIR_NAME must be
116736ac495dSmrg    stored in permanently allocated memory.  */
116836ac495dSmrg static cpp_dir *
make_cpp_dir(cpp_reader * pfile,const char * dir_name,int sysp)116936ac495dSmrg make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp)
117036ac495dSmrg {
117136ac495dSmrg   struct cpp_file_hash_entry *entry, **hash_slot;
117236ac495dSmrg   cpp_dir *dir;
117336ac495dSmrg 
117436ac495dSmrg   hash_slot = (struct cpp_file_hash_entry **)
117536ac495dSmrg     htab_find_slot_with_hash (pfile->dir_hash, dir_name,
117636ac495dSmrg 			      htab_hash_string (dir_name),
117736ac495dSmrg 			      INSERT);
117836ac495dSmrg 
117936ac495dSmrg   /* Have we already hashed this directory?  */
118036ac495dSmrg   for (entry = *hash_slot; entry; entry = entry->next)
118136ac495dSmrg     if (entry->start_dir == NULL)
118236ac495dSmrg       return entry->u.dir;
118336ac495dSmrg 
118436ac495dSmrg   dir = XCNEW (cpp_dir);
118536ac495dSmrg   dir->next = pfile->quote_include;
118636ac495dSmrg   dir->name = (char *) dir_name;
118736ac495dSmrg   dir->len = strlen (dir_name);
118836ac495dSmrg   dir->sysp = sysp;
118936ac495dSmrg   dir->construct = 0;
119036ac495dSmrg 
119136ac495dSmrg   /* Store this new result in the hash table.  */
119236ac495dSmrg   entry = new_file_hash_entry (pfile);
119336ac495dSmrg   entry->next = *hash_slot;
119436ac495dSmrg   entry->start_dir = NULL;
119536ac495dSmrg   entry->location = pfile->line_table->highest_location;
119636ac495dSmrg   entry->u.dir = dir;
119736ac495dSmrg   *hash_slot = entry;
119836ac495dSmrg 
119936ac495dSmrg   return dir;
120036ac495dSmrg }
120136ac495dSmrg 
120236ac495dSmrg /* Create a new block of memory for file hash entries.  */
120336ac495dSmrg static void
allocate_file_hash_entries(cpp_reader * pfile)120436ac495dSmrg allocate_file_hash_entries (cpp_reader *pfile)
120536ac495dSmrg {
120636ac495dSmrg   struct file_hash_entry_pool *pool = XNEW (struct file_hash_entry_pool);
120736ac495dSmrg   pool->file_hash_entries_used = 0;
120836ac495dSmrg   pool->next = pfile->file_hash_entries;
120936ac495dSmrg   pfile->file_hash_entries = pool;
121036ac495dSmrg }
121136ac495dSmrg 
121236ac495dSmrg /* Return a new file hash entry.  */
121336ac495dSmrg static struct cpp_file_hash_entry *
new_file_hash_entry(cpp_reader * pfile)121436ac495dSmrg new_file_hash_entry (cpp_reader *pfile)
121536ac495dSmrg {
121636ac495dSmrg   unsigned int idx;
121736ac495dSmrg   if (pfile->file_hash_entries->file_hash_entries_used == FILE_HASH_POOL_SIZE)
121836ac495dSmrg     allocate_file_hash_entries (pfile);
121936ac495dSmrg 
122036ac495dSmrg   idx = pfile->file_hash_entries->file_hash_entries_used++;
122136ac495dSmrg   return &pfile->file_hash_entries->pool[idx];
122236ac495dSmrg }
122336ac495dSmrg 
122436ac495dSmrg /* Free the file hash entry pools.  */
122536ac495dSmrg static void
free_file_hash_entries(cpp_reader * pfile)122636ac495dSmrg free_file_hash_entries (cpp_reader *pfile)
122736ac495dSmrg {
122836ac495dSmrg   struct file_hash_entry_pool *iter = pfile->file_hash_entries;
122936ac495dSmrg   while (iter)
123036ac495dSmrg     {
123136ac495dSmrg       struct file_hash_entry_pool *next = iter->next;
123236ac495dSmrg       free (iter);
123336ac495dSmrg       iter = next;
123436ac495dSmrg     }
123536ac495dSmrg }
123636ac495dSmrg 
123736ac495dSmrg /* Returns TRUE if a file FNAME has ever been successfully opened.
123836ac495dSmrg    This routine is not intended to correctly handle filenames aliased
123936ac495dSmrg    by links or redundant . or .. traversals etc.  */
124036ac495dSmrg bool
cpp_included(cpp_reader * pfile,const char * fname)124136ac495dSmrg cpp_included (cpp_reader *pfile, const char *fname)
124236ac495dSmrg {
124336ac495dSmrg   struct cpp_file_hash_entry *entry;
124436ac495dSmrg 
124536ac495dSmrg   entry = (struct cpp_file_hash_entry *)
124636ac495dSmrg      htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname));
124736ac495dSmrg 
124836ac495dSmrg   while (entry && (entry->start_dir == NULL || entry->u.file->err_no))
124936ac495dSmrg     entry = entry->next;
125036ac495dSmrg 
125136ac495dSmrg   return entry != NULL;
125236ac495dSmrg }
125336ac495dSmrg 
125436ac495dSmrg /* Returns TRUE if a file FNAME has ever been successfully opened
125536ac495dSmrg    before LOCATION.  This routine is not intended to correctly handle
125636ac495dSmrg    filenames aliased by links or redundant . or .. traversals etc.  */
125736ac495dSmrg bool
cpp_included_before(cpp_reader * pfile,const char * fname,location_t location)125836ac495dSmrg cpp_included_before (cpp_reader *pfile, const char *fname,
1259c0a68be4Smrg 		     location_t location)
126036ac495dSmrg {
126136ac495dSmrg   struct cpp_file_hash_entry *entry
126236ac495dSmrg     = (struct cpp_file_hash_entry *)
126336ac495dSmrg       htab_find_with_hash (pfile->file_hash, fname, htab_hash_string (fname));
126436ac495dSmrg 
126536ac495dSmrg   if (IS_ADHOC_LOC (location))
126636ac495dSmrg     location = get_location_from_adhoc_loc (pfile->line_table, location);
126736ac495dSmrg 
126836ac495dSmrg   while (entry && (entry->start_dir == NULL || entry->u.file->err_no
126936ac495dSmrg 		   || entry->location > location))
127036ac495dSmrg     entry = entry->next;
127136ac495dSmrg 
127236ac495dSmrg   return entry != NULL;
127336ac495dSmrg }
127436ac495dSmrg 
127536ac495dSmrg /* Calculate the hash value of a file hash entry P.  */
127636ac495dSmrg 
127736ac495dSmrg static hashval_t
file_hash_hash(const void * p)127836ac495dSmrg file_hash_hash (const void *p)
127936ac495dSmrg {
128036ac495dSmrg   struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) p;
128136ac495dSmrg   const char *hname;
128236ac495dSmrg   if (entry->start_dir)
128336ac495dSmrg     hname = entry->u.file->name;
128436ac495dSmrg   else
128536ac495dSmrg     hname = entry->u.dir->name;
128636ac495dSmrg 
128736ac495dSmrg   return htab_hash_string (hname);
128836ac495dSmrg }
128936ac495dSmrg 
129036ac495dSmrg /* Compare a string Q against a file hash entry P.  */
129136ac495dSmrg static int
file_hash_eq(const void * p,const void * q)129236ac495dSmrg file_hash_eq (const void *p, const void *q)
129336ac495dSmrg {
129436ac495dSmrg   struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) p;
129536ac495dSmrg   const char *fname = (const char *) q;
129636ac495dSmrg   const char *hname;
129736ac495dSmrg 
129836ac495dSmrg   if (entry->start_dir)
129936ac495dSmrg     hname = entry->u.file->name;
130036ac495dSmrg   else
130136ac495dSmrg     hname = entry->u.dir->name;
130236ac495dSmrg 
130336ac495dSmrg   return filename_cmp (hname, fname) == 0;
130436ac495dSmrg }
130536ac495dSmrg 
130636ac495dSmrg /* Compare entries in the nonexistent file hash table.  These are just
130736ac495dSmrg    strings.  */
130836ac495dSmrg static int
nonexistent_file_hash_eq(const void * p,const void * q)130936ac495dSmrg nonexistent_file_hash_eq (const void *p, const void *q)
131036ac495dSmrg {
131136ac495dSmrg   return filename_cmp ((const char *) p, (const char *) q) == 0;
131236ac495dSmrg }
131336ac495dSmrg 
131436ac495dSmrg /* Initialize everything in this source file.  */
131536ac495dSmrg void
_cpp_init_files(cpp_reader * pfile)131636ac495dSmrg _cpp_init_files (cpp_reader *pfile)
131736ac495dSmrg {
131836ac495dSmrg   pfile->file_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq,
131936ac495dSmrg 					NULL, xcalloc, free);
132036ac495dSmrg   pfile->dir_hash = htab_create_alloc (127, file_hash_hash, file_hash_eq,
132136ac495dSmrg 					NULL, xcalloc, free);
132236ac495dSmrg   allocate_file_hash_entries (pfile);
132336ac495dSmrg   pfile->nonexistent_file_hash = htab_create_alloc (127, htab_hash_string,
132436ac495dSmrg 						    nonexistent_file_hash_eq,
132536ac495dSmrg 						    NULL, xcalloc, free);
132636ac495dSmrg   obstack_specify_allocation (&pfile->nonexistent_file_ob, 0, 0,
132736ac495dSmrg 			      xmalloc, free);
132836ac495dSmrg }
132936ac495dSmrg 
133036ac495dSmrg /* Finalize everything in this source file.  */
133136ac495dSmrg void
_cpp_cleanup_files(cpp_reader * pfile)133236ac495dSmrg _cpp_cleanup_files (cpp_reader *pfile)
133336ac495dSmrg {
133436ac495dSmrg   htab_delete (pfile->file_hash);
133536ac495dSmrg   htab_delete (pfile->dir_hash);
133636ac495dSmrg   htab_delete (pfile->nonexistent_file_hash);
133736ac495dSmrg   obstack_free (&pfile->nonexistent_file_ob, 0);
133836ac495dSmrg   free_file_hash_entries (pfile);
133936ac495dSmrg   destroy_all_cpp_files (pfile);
134036ac495dSmrg }
134136ac495dSmrg 
134236ac495dSmrg /* Make the parser forget about files it has seen.  This can be useful
134336ac495dSmrg    for resetting the parser to start another run.  */
134436ac495dSmrg void
cpp_clear_file_cache(cpp_reader * pfile)134536ac495dSmrg cpp_clear_file_cache (cpp_reader *pfile)
134636ac495dSmrg {
134736ac495dSmrg   _cpp_cleanup_files (pfile);
134836ac495dSmrg   pfile->file_hash_entries = NULL;
134936ac495dSmrg   pfile->all_files = NULL;
135036ac495dSmrg   _cpp_init_files (pfile);
135136ac495dSmrg }
135236ac495dSmrg 
135336ac495dSmrg /* Enter a file name in the hash for the sake of cpp_included.  */
135436ac495dSmrg void
_cpp_fake_include(cpp_reader * pfile,const char * fname)135536ac495dSmrg _cpp_fake_include (cpp_reader *pfile, const char *fname)
135636ac495dSmrg {
1357*8feb0f0bSmrg   _cpp_find_file (pfile, fname, pfile->buffer->file->dir,
1358*8feb0f0bSmrg 		  0, true, false, false, 0);
135936ac495dSmrg }
136036ac495dSmrg 
136136ac495dSmrg /* Not everyone who wants to set system-header-ness on a buffer can
136236ac495dSmrg    see the details of a buffer.  This is an exported interface because
136336ac495dSmrg    fix-header needs it.  */
136436ac495dSmrg void
cpp_make_system_header(cpp_reader * pfile,int syshdr,int externc)136536ac495dSmrg cpp_make_system_header (cpp_reader *pfile, int syshdr, int externc)
136636ac495dSmrg {
136736ac495dSmrg   int flags = 0;
1368*8feb0f0bSmrg   const class line_maps *line_table = pfile->line_table;
136936ac495dSmrg   const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
137036ac495dSmrg   /* 1 = system header, 2 = system header to be treated as C.  */
137136ac495dSmrg   if (syshdr)
137236ac495dSmrg     flags = 1 + (externc != 0);
137336ac495dSmrg   pfile->buffer->sysp = flags;
137436ac495dSmrg   _cpp_do_file_change (pfile, LC_RENAME, ORDINARY_MAP_FILE_NAME (map),
137536ac495dSmrg 		       SOURCE_LINE (map, pfile->line_table->highest_line), flags);
137636ac495dSmrg }
137736ac495dSmrg 
137836ac495dSmrg /* Allow the client to change the current file.  Used by the front end
137936ac495dSmrg    to achieve pseudo-file names like <built-in>.
138036ac495dSmrg    If REASON is LC_LEAVE, then NEW_NAME must be NULL.  */
138136ac495dSmrg void
cpp_change_file(cpp_reader * pfile,enum lc_reason reason,const char * new_name)138236ac495dSmrg cpp_change_file (cpp_reader *pfile, enum lc_reason reason,
138336ac495dSmrg 		 const char *new_name)
138436ac495dSmrg {
138536ac495dSmrg   _cpp_do_file_change (pfile, reason, new_name, 1, 0);
138636ac495dSmrg }
138736ac495dSmrg 
138836ac495dSmrg struct report_missing_guard_data
138936ac495dSmrg {
139036ac495dSmrg   const char **paths;
139136ac495dSmrg   size_t count;
139236ac495dSmrg };
139336ac495dSmrg 
139436ac495dSmrg /* Callback function for htab_traverse.  */
139536ac495dSmrg static int
report_missing_guard(void ** slot,void * d)139636ac495dSmrg report_missing_guard (void **slot, void *d)
139736ac495dSmrg {
139836ac495dSmrg   struct cpp_file_hash_entry *entry = (struct cpp_file_hash_entry *) *slot;
139936ac495dSmrg   struct report_missing_guard_data *data
140036ac495dSmrg     = (struct report_missing_guard_data *) d;
140136ac495dSmrg 
140236ac495dSmrg   /* Skip directories.  */
140336ac495dSmrg   if (entry->start_dir != NULL)
140436ac495dSmrg     {
140536ac495dSmrg       _cpp_file *file = entry->u.file;
140636ac495dSmrg 
140736ac495dSmrg       /* We don't want MI guard advice for the main file.  */
140836ac495dSmrg       if (!file->once_only && file->cmacro == NULL
140936ac495dSmrg 	  && file->stack_count == 1 && !file->main_file)
141036ac495dSmrg 	{
141136ac495dSmrg 	  if (data->paths == NULL)
141236ac495dSmrg 	    {
141336ac495dSmrg 	      data->paths = XCNEWVEC (const char *, data->count);
141436ac495dSmrg 	      data->count = 0;
141536ac495dSmrg 	    }
141636ac495dSmrg 
141736ac495dSmrg 	  data->paths[data->count++] = file->path;
141836ac495dSmrg 	}
141936ac495dSmrg     }
142036ac495dSmrg 
142136ac495dSmrg   /* Keep traversing the hash table.  */
142236ac495dSmrg   return 1;
142336ac495dSmrg }
142436ac495dSmrg 
142536ac495dSmrg /* Comparison function for qsort.  */
142636ac495dSmrg static int
report_missing_guard_cmp(const void * p1,const void * p2)142736ac495dSmrg report_missing_guard_cmp (const void *p1, const void *p2)
142836ac495dSmrg {
142936ac495dSmrg   return strcmp (*(const char *const *) p1, *(const char *const *) p2);
143036ac495dSmrg }
143136ac495dSmrg 
143236ac495dSmrg /* Report on all files that might benefit from a multiple include guard.
143336ac495dSmrg    Triggered by -H.  */
143436ac495dSmrg void
_cpp_report_missing_guards(cpp_reader * pfile)143536ac495dSmrg _cpp_report_missing_guards (cpp_reader *pfile)
143636ac495dSmrg {
143736ac495dSmrg   struct report_missing_guard_data data;
143836ac495dSmrg 
143936ac495dSmrg   data.paths = NULL;
144036ac495dSmrg   data.count = htab_elements (pfile->file_hash);
144136ac495dSmrg   htab_traverse (pfile->file_hash, report_missing_guard, &data);
144236ac495dSmrg 
144336ac495dSmrg   if (data.paths != NULL)
144436ac495dSmrg     {
144536ac495dSmrg       size_t i;
144636ac495dSmrg 
144736ac495dSmrg       /* Sort the paths to avoid outputting them in hash table
144836ac495dSmrg 	 order.  */
144936ac495dSmrg       qsort (data.paths, data.count, sizeof (const char *),
145036ac495dSmrg 	     report_missing_guard_cmp);
145136ac495dSmrg       fputs (_("Multiple include guards may be useful for:\n"),
145236ac495dSmrg 	     stderr);
145336ac495dSmrg       for (i = 0; i < data.count; i++)
145436ac495dSmrg 	{
145536ac495dSmrg 	  fputs (data.paths[i], stderr);
145636ac495dSmrg 	  putc ('\n', stderr);
145736ac495dSmrg 	}
145836ac495dSmrg       free (data.paths);
145936ac495dSmrg     }
146036ac495dSmrg }
146136ac495dSmrg 
146236ac495dSmrg /* Locate HEADER, and determine whether it is newer than the current
146336ac495dSmrg    file.  If it cannot be located or dated, return -1, if it is
146436ac495dSmrg    newer, return 1, otherwise 0.  */
146536ac495dSmrg int
_cpp_compare_file_date(cpp_reader * pfile,const char * fname,int angle_brackets)146636ac495dSmrg _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
146736ac495dSmrg 			int angle_brackets)
146836ac495dSmrg {
146936ac495dSmrg   _cpp_file *file;
147036ac495dSmrg   struct cpp_dir *dir;
147136ac495dSmrg 
147236ac495dSmrg   dir = search_path_head (pfile, fname, angle_brackets, IT_INCLUDE);
147336ac495dSmrg   if (!dir)
147436ac495dSmrg     return -1;
147536ac495dSmrg 
1476*8feb0f0bSmrg   file = _cpp_find_file (pfile, fname, dir, angle_brackets,
1477*8feb0f0bSmrg 			 false, false, false, 0);
147836ac495dSmrg   if (file->err_no)
147936ac495dSmrg     return -1;
148036ac495dSmrg 
148136ac495dSmrg   if (file->fd != -1)
148236ac495dSmrg     {
148336ac495dSmrg       close (file->fd);
148436ac495dSmrg       file->fd = -1;
148536ac495dSmrg     }
148636ac495dSmrg 
148736ac495dSmrg   return file->st.st_mtime > pfile->buffer->file->st.st_mtime;
148836ac495dSmrg }
148936ac495dSmrg 
149036ac495dSmrg /* Pushes the given file onto the buffer stack.  Returns nonzero if
149136ac495dSmrg    successful.  */
149236ac495dSmrg bool
cpp_push_include(cpp_reader * pfile,const char * fname)149336ac495dSmrg cpp_push_include (cpp_reader *pfile, const char *fname)
149436ac495dSmrg {
149536ac495dSmrg   return _cpp_stack_include (pfile, fname, false, IT_CMDLINE, 0);
149636ac495dSmrg }
149736ac495dSmrg 
149836ac495dSmrg /* Pushes the given file, implicitly included at the start of a
149936ac495dSmrg    compilation, onto the buffer stack but without any errors if the
150036ac495dSmrg    file is not found.  Returns nonzero if successful.  */
150136ac495dSmrg bool
cpp_push_default_include(cpp_reader * pfile,const char * fname)150236ac495dSmrg cpp_push_default_include (cpp_reader *pfile, const char *fname)
150336ac495dSmrg {
150436ac495dSmrg   return _cpp_stack_include (pfile, fname, true, IT_DEFAULT, 0);
150536ac495dSmrg }
150636ac495dSmrg 
150736ac495dSmrg /* Do appropriate cleanup when a file INC's buffer is popped off the
150836ac495dSmrg    input stack.  */
150936ac495dSmrg void
_cpp_pop_file_buffer(cpp_reader * pfile,_cpp_file * file,const unsigned char * to_free)151036ac495dSmrg _cpp_pop_file_buffer (cpp_reader *pfile, _cpp_file *file,
151136ac495dSmrg 		      const unsigned char *to_free)
151236ac495dSmrg {
151336ac495dSmrg   /* Record the inclusion-preventing macro, which could be NULL
151436ac495dSmrg      meaning no controlling macro.  */
151536ac495dSmrg   if (pfile->mi_valid && file->cmacro == NULL)
151636ac495dSmrg     file->cmacro = pfile->mi_cmacro;
151736ac495dSmrg 
151836ac495dSmrg   /* Invalidate control macros in the #including file.  */
151936ac495dSmrg   pfile->mi_valid = false;
152036ac495dSmrg 
152136ac495dSmrg   if (to_free)
152236ac495dSmrg     {
152336ac495dSmrg       if (to_free == file->buffer_start)
152436ac495dSmrg 	{
152536ac495dSmrg 	  file->buffer_start = NULL;
152636ac495dSmrg 	  file->buffer = NULL;
152736ac495dSmrg 	  file->buffer_valid = false;
152836ac495dSmrg 	}
152936ac495dSmrg       free ((void *) to_free);
153036ac495dSmrg     }
153136ac495dSmrg }
153236ac495dSmrg 
153336ac495dSmrg /* Return the file name associated with FILE.  */
153436ac495dSmrg const char *
_cpp_get_file_name(_cpp_file * file)153536ac495dSmrg _cpp_get_file_name (_cpp_file *file)
153636ac495dSmrg {
153736ac495dSmrg   return file->name;
153836ac495dSmrg }
153936ac495dSmrg 
154036ac495dSmrg /* Inteface to file statistics record in _cpp_file structure. */
154136ac495dSmrg struct stat *
_cpp_get_file_stat(_cpp_file * file)154236ac495dSmrg _cpp_get_file_stat (_cpp_file *file)
154336ac495dSmrg {
154436ac495dSmrg     return &file->st;
154536ac495dSmrg }
154636ac495dSmrg 
154736ac495dSmrg /* Set the include chain for "" to QUOTE, for <> to BRACKET.  If
154836ac495dSmrg    QUOTE_IGNORES_SOURCE_DIR, then "" includes do not look in the
154936ac495dSmrg    directory of the including file.
155036ac495dSmrg 
155136ac495dSmrg    If BRACKET does not lie in the QUOTE chain, it is set to QUOTE.  */
155236ac495dSmrg void
cpp_set_include_chains(cpp_reader * pfile,cpp_dir * quote,cpp_dir * bracket,int quote_ignores_source_dir)155336ac495dSmrg cpp_set_include_chains (cpp_reader *pfile, cpp_dir *quote, cpp_dir *bracket,
155436ac495dSmrg 			int quote_ignores_source_dir)
155536ac495dSmrg {
155636ac495dSmrg   pfile->quote_include = quote;
155736ac495dSmrg   pfile->bracket_include = quote;
155836ac495dSmrg   pfile->quote_ignores_source_dir = quote_ignores_source_dir;
155936ac495dSmrg 
156036ac495dSmrg   for (; quote; quote = quote->next)
156136ac495dSmrg     {
156236ac495dSmrg       quote->name_map = NULL;
156336ac495dSmrg       quote->len = strlen (quote->name);
156436ac495dSmrg       if (quote == bracket)
156536ac495dSmrg 	pfile->bracket_include = bracket;
156636ac495dSmrg     }
156736ac495dSmrg }
156836ac495dSmrg 
156936ac495dSmrg /* Append the file name to the directory to create the path, but don't
157036ac495dSmrg    turn / into // or // into ///; // may be a namespace escape.  */
157136ac495dSmrg static char *
append_file_to_dir(const char * fname,cpp_dir * dir)157236ac495dSmrg append_file_to_dir (const char *fname, cpp_dir *dir)
157336ac495dSmrg {
157436ac495dSmrg   size_t dlen, flen;
157536ac495dSmrg   char *path;
157636ac495dSmrg 
157736ac495dSmrg   dlen = dir->len;
157836ac495dSmrg   flen = strlen (fname);
157936ac495dSmrg   path = XNEWVEC (char, dlen + 1 + flen + 1);
158036ac495dSmrg   memcpy (path, dir->name, dlen);
158136ac495dSmrg   if (dlen && !IS_DIR_SEPARATOR (path[dlen - 1]))
158236ac495dSmrg     path[dlen++] = '/';
158336ac495dSmrg   memcpy (&path[dlen], fname, flen + 1);
158436ac495dSmrg 
158536ac495dSmrg   return path;
158636ac495dSmrg }
158736ac495dSmrg 
158836ac495dSmrg /* Read a space delimited string of unlimited length from a stdio
158936ac495dSmrg    file F.  */
159036ac495dSmrg static char *
read_filename_string(int ch,FILE * f)159136ac495dSmrg read_filename_string (int ch, FILE *f)
159236ac495dSmrg {
159336ac495dSmrg   char *alloc, *set;
159436ac495dSmrg   int len;
159536ac495dSmrg 
159636ac495dSmrg   len = 20;
159736ac495dSmrg   set = alloc = XNEWVEC (char, len + 1);
159836ac495dSmrg   if (! is_space (ch))
159936ac495dSmrg     {
160036ac495dSmrg       *set++ = ch;
160136ac495dSmrg       while ((ch = getc (f)) != EOF && ! is_space (ch))
160236ac495dSmrg 	{
160336ac495dSmrg 	  if (set - alloc == len)
160436ac495dSmrg 	    {
160536ac495dSmrg 	      len *= 2;
160636ac495dSmrg 	      alloc = XRESIZEVEC (char, alloc, len + 1);
160736ac495dSmrg 	      set = alloc + len / 2;
160836ac495dSmrg 	    }
160936ac495dSmrg 	  *set++ = ch;
161036ac495dSmrg 	}
161136ac495dSmrg     }
161236ac495dSmrg   *set = '\0';
161336ac495dSmrg   ungetc (ch, f);
161436ac495dSmrg   return alloc;
161536ac495dSmrg }
161636ac495dSmrg 
161736ac495dSmrg /* Read the file name map file for DIR.  */
161836ac495dSmrg static void
read_name_map(cpp_dir * dir)161936ac495dSmrg read_name_map (cpp_dir *dir)
162036ac495dSmrg {
162136ac495dSmrg   static const char FILE_NAME_MAP_FILE[] = "header.gcc";
162236ac495dSmrg   char *name;
162336ac495dSmrg   FILE *f;
162436ac495dSmrg   size_t len, count = 0, room = 9;
162536ac495dSmrg 
162636ac495dSmrg   len = dir->len;
162736ac495dSmrg   name = (char *) alloca (len + sizeof (FILE_NAME_MAP_FILE) + 1);
162836ac495dSmrg   memcpy (name, dir->name, len);
162936ac495dSmrg   if (len && !IS_DIR_SEPARATOR (name[len - 1]))
163036ac495dSmrg     name[len++] = '/';
163136ac495dSmrg   strcpy (name + len, FILE_NAME_MAP_FILE);
163236ac495dSmrg   f = fopen (name, "r");
163336ac495dSmrg 
163436ac495dSmrg   dir->name_map = XNEWVEC (const char *, room);
163536ac495dSmrg 
163636ac495dSmrg   /* Silently return NULL if we cannot open.  */
163736ac495dSmrg   if (f)
163836ac495dSmrg     {
163936ac495dSmrg       int ch;
164036ac495dSmrg 
164136ac495dSmrg       while ((ch = getc (f)) != EOF)
164236ac495dSmrg 	{
164336ac495dSmrg 	  char *to;
164436ac495dSmrg 
164536ac495dSmrg 	  if (is_space (ch))
164636ac495dSmrg 	    continue;
164736ac495dSmrg 
164836ac495dSmrg 	  if (count + 2 > room)
164936ac495dSmrg 	    {
165036ac495dSmrg 	      room += 8;
165136ac495dSmrg 	      dir->name_map = XRESIZEVEC (const char *, dir->name_map, room);
165236ac495dSmrg 	    }
165336ac495dSmrg 
165436ac495dSmrg 	  dir->name_map[count] = read_filename_string (ch, f);
165536ac495dSmrg 	  while ((ch = getc (f)) != EOF && is_hspace (ch))
165636ac495dSmrg 	    ;
165736ac495dSmrg 
165836ac495dSmrg 	  to = read_filename_string (ch, f);
165936ac495dSmrg 	  if (IS_ABSOLUTE_PATH (to))
166036ac495dSmrg 	    dir->name_map[count + 1] = to;
166136ac495dSmrg 	  else
166236ac495dSmrg 	    {
166336ac495dSmrg 	      dir->name_map[count + 1] = append_file_to_dir (to, dir);
166436ac495dSmrg 	      free (to);
166536ac495dSmrg 	    }
166636ac495dSmrg 
166736ac495dSmrg 	  count += 2;
166836ac495dSmrg 	  while ((ch = getc (f)) != '\n')
166936ac495dSmrg 	    if (ch == EOF)
167036ac495dSmrg 	      break;
167136ac495dSmrg 	}
167236ac495dSmrg 
167336ac495dSmrg       fclose (f);
167436ac495dSmrg     }
167536ac495dSmrg 
167636ac495dSmrg   /* Terminate the list of maps.  */
167736ac495dSmrg   dir->name_map[count] = NULL;
167836ac495dSmrg }
167936ac495dSmrg 
168036ac495dSmrg /* Remap a FILE's name based on the file_name_map, if any, for
168136ac495dSmrg    FILE->dir.  If the file name has any directory separators,
168236ac495dSmrg    recursively check those directories too.  */
168336ac495dSmrg static char *
remap_filename(cpp_reader * pfile,_cpp_file * file)168436ac495dSmrg remap_filename (cpp_reader *pfile, _cpp_file *file)
168536ac495dSmrg {
168636ac495dSmrg   const char *fname, *p;
168736ac495dSmrg   char *new_dir, *p3;
168836ac495dSmrg   cpp_dir *dir;
168936ac495dSmrg   size_t index, len;
169036ac495dSmrg 
169136ac495dSmrg   dir = file->dir;
169236ac495dSmrg   fname = file->name;
169336ac495dSmrg 
169436ac495dSmrg   for (;;)
169536ac495dSmrg     {
169636ac495dSmrg       if (!dir->name_map)
169736ac495dSmrg 	read_name_map (dir);
169836ac495dSmrg 
169936ac495dSmrg       for (index = 0; dir->name_map[index]; index += 2)
170036ac495dSmrg 	if (!filename_cmp (dir->name_map[index], fname))
170136ac495dSmrg 	    return xstrdup (dir->name_map[index + 1]);
170236ac495dSmrg       if (IS_ABSOLUTE_PATH (fname))
170336ac495dSmrg 	return NULL;
170436ac495dSmrg       p = strchr (fname, '/');
170536ac495dSmrg #ifdef HAVE_DOS_BASED_FILE_SYSTEM
170636ac495dSmrg       {
170736ac495dSmrg 	char *p2 = strchr (fname, '\\');
170836ac495dSmrg 	if (!p || (p > p2))
170936ac495dSmrg 	  p = p2;
171036ac495dSmrg       }
171136ac495dSmrg #endif
171236ac495dSmrg       if (!p || p == fname)
171336ac495dSmrg 	return NULL;
171436ac495dSmrg 
171536ac495dSmrg       len = dir->len + (p - fname + 1);
171636ac495dSmrg       new_dir = XNEWVEC (char, len + 2);
171736ac495dSmrg       p3 = new_dir + dir->len;
171836ac495dSmrg       memcpy (new_dir, dir->name, dir->len);
171936ac495dSmrg       if (dir->len && !IS_DIR_SEPARATOR (dir->name[dir->len - 1]))
172036ac495dSmrg 	{
172136ac495dSmrg 	  *p3++ = '/';
172236ac495dSmrg 	  len++;
172336ac495dSmrg 	}
172436ac495dSmrg       memcpy (p3, fname, p - fname + 1);
172536ac495dSmrg       new_dir[len] = '\0';
172636ac495dSmrg 
172736ac495dSmrg       dir = make_cpp_dir (pfile, new_dir, dir->sysp);
172836ac495dSmrg       fname = p + 1;
172936ac495dSmrg     }
173036ac495dSmrg }
173136ac495dSmrg 
173236ac495dSmrg /* Returns true if PCHNAME is a valid PCH file for FILE.  */
173336ac495dSmrg static bool
validate_pch(cpp_reader * pfile,_cpp_file * file,const char * pchname)173436ac495dSmrg validate_pch (cpp_reader *pfile, _cpp_file *file, const char *pchname)
173536ac495dSmrg {
173636ac495dSmrg   const char *saved_path = file->path;
173736ac495dSmrg   bool valid = false;
173836ac495dSmrg 
173936ac495dSmrg   file->path = pchname;
174036ac495dSmrg   if (open_file (file))
174136ac495dSmrg     {
174236ac495dSmrg       valid = 1 & pfile->cb.valid_pch (pfile, pchname, file->fd);
174336ac495dSmrg 
174436ac495dSmrg       if (!valid)
174536ac495dSmrg 	{
174636ac495dSmrg 	  close (file->fd);
174736ac495dSmrg 	  file->fd = -1;
174836ac495dSmrg 	}
174936ac495dSmrg 
175036ac495dSmrg       if (CPP_OPTION (pfile, print_include_names))
175136ac495dSmrg 	{
175236ac495dSmrg 	  unsigned int i;
175336ac495dSmrg 	  for (i = 1; i < pfile->line_table->depth; i++)
175436ac495dSmrg 	    putc ('.', stderr);
175536ac495dSmrg 	  fprintf (stderr, "%c %s\n",
175636ac495dSmrg 		   valid ? '!' : 'x', pchname);
175736ac495dSmrg 	}
175836ac495dSmrg     }
175936ac495dSmrg 
176036ac495dSmrg   file->path = saved_path;
176136ac495dSmrg   return valid;
176236ac495dSmrg }
176336ac495dSmrg 
176436ac495dSmrg /* Get the path associated with the _cpp_file F.  The path includes
176536ac495dSmrg    the base name from the include directive and the directory it was
176636ac495dSmrg    found in via the search path.  */
176736ac495dSmrg 
176836ac495dSmrg const char *
cpp_get_path(struct _cpp_file * f)176936ac495dSmrg cpp_get_path (struct _cpp_file *f)
177036ac495dSmrg {
177136ac495dSmrg   return f->path;
177236ac495dSmrg }
177336ac495dSmrg 
177436ac495dSmrg /* Get the directory associated with the _cpp_file F.  */
177536ac495dSmrg 
177636ac495dSmrg cpp_dir *
cpp_get_dir(struct _cpp_file * f)177736ac495dSmrg cpp_get_dir (struct _cpp_file *f)
177836ac495dSmrg {
177936ac495dSmrg   return f->dir;
178036ac495dSmrg }
178136ac495dSmrg 
178236ac495dSmrg /* Get the cpp_buffer currently associated with the cpp_reader
178336ac495dSmrg    PFILE.  */
178436ac495dSmrg 
178536ac495dSmrg cpp_buffer *
cpp_get_buffer(cpp_reader * pfile)178636ac495dSmrg cpp_get_buffer (cpp_reader *pfile)
178736ac495dSmrg {
178836ac495dSmrg   return pfile->buffer;
178936ac495dSmrg }
179036ac495dSmrg 
179136ac495dSmrg /* Get the _cpp_file associated with the cpp_buffer B.  */
179236ac495dSmrg 
179336ac495dSmrg _cpp_file *
cpp_get_file(cpp_buffer * b)179436ac495dSmrg cpp_get_file (cpp_buffer *b)
179536ac495dSmrg {
179636ac495dSmrg   return b->file;
179736ac495dSmrg }
179836ac495dSmrg 
179936ac495dSmrg /* Get the previous cpp_buffer given a cpp_buffer B.  The previous
180036ac495dSmrg    buffer is the buffer that included the given buffer.  */
180136ac495dSmrg 
180236ac495dSmrg cpp_buffer *
cpp_get_prev(cpp_buffer * b)180336ac495dSmrg cpp_get_prev (cpp_buffer *b)
180436ac495dSmrg {
180536ac495dSmrg   return b->prev;
180636ac495dSmrg }
180736ac495dSmrg 
180836ac495dSmrg /* This data structure holds the list of header files that were seen
180936ac495dSmrg    while the PCH was being built.  The 'entries' field is kept sorted
181036ac495dSmrg    in memcmp() order; yes, this means that on little-endian systems,
181136ac495dSmrg    it's sorted initially by the least-significant byte of 'size', but
181236ac495dSmrg    that's OK.  The code does rely on having entries with the same size
181336ac495dSmrg    next to each other.  */
181436ac495dSmrg 
181536ac495dSmrg struct pchf_entry {
181636ac495dSmrg   /* The size of this file.  This is used to save running a MD5 checksum
181736ac495dSmrg      if the sizes don't match.  */
181836ac495dSmrg   off_t size;
181936ac495dSmrg   /* The MD5 checksum of this file.  */
182036ac495dSmrg   unsigned char sum[16];
182136ac495dSmrg   /* Is this file to be included only once?  */
182236ac495dSmrg   bool once_only;
182336ac495dSmrg };
182436ac495dSmrg 
182536ac495dSmrg struct pchf_data {
182636ac495dSmrg   /* Number of pchf_entry structures.  */
182736ac495dSmrg   size_t count;
182836ac495dSmrg 
182936ac495dSmrg   /* Are there any values with once_only set?
183036ac495dSmrg      This is used as an optimisation, it means we don't have to search
183136ac495dSmrg      the structure if we're processing a regular #include.  */
183236ac495dSmrg   bool have_once_only;
183336ac495dSmrg 
183436ac495dSmrg   struct pchf_entry entries[1];
183536ac495dSmrg };
183636ac495dSmrg 
183736ac495dSmrg static struct pchf_data *pchf;
183836ac495dSmrg 
183936ac495dSmrg /* A qsort ordering function for pchf_entry structures.  */
184036ac495dSmrg 
184136ac495dSmrg static int
pchf_save_compare(const void * e1,const void * e2)184236ac495dSmrg pchf_save_compare (const void *e1, const void *e2)
184336ac495dSmrg {
184436ac495dSmrg   return memcmp (e1, e2, sizeof (struct pchf_entry));
184536ac495dSmrg }
184636ac495dSmrg 
184736ac495dSmrg /* Create and write to F a pchf_data structure.  */
184836ac495dSmrg 
184936ac495dSmrg bool
_cpp_save_file_entries(cpp_reader * pfile,FILE * fp)185036ac495dSmrg _cpp_save_file_entries (cpp_reader *pfile, FILE *fp)
185136ac495dSmrg {
185236ac495dSmrg   size_t count = 0;
185336ac495dSmrg   struct pchf_data *result;
185436ac495dSmrg   size_t result_size;
185536ac495dSmrg   _cpp_file *f;
185636ac495dSmrg   bool ret;
185736ac495dSmrg 
185836ac495dSmrg   for (f = pfile->all_files; f; f = f->next_file)
185936ac495dSmrg     ++count;
186036ac495dSmrg 
186136ac495dSmrg   result_size = (sizeof (struct pchf_data)
186236ac495dSmrg 		 + sizeof (struct pchf_entry) * (count - 1));
186336ac495dSmrg   result = XCNEWVAR (struct pchf_data, result_size);
186436ac495dSmrg 
186536ac495dSmrg   result->count = 0;
186636ac495dSmrg   result->have_once_only = false;
186736ac495dSmrg 
186836ac495dSmrg   for (f = pfile->all_files; f; f = f->next_file)
186936ac495dSmrg     {
187036ac495dSmrg       size_t count;
187136ac495dSmrg 
187236ac495dSmrg       /* This should probably never happen, since if a read error occurred
187336ac495dSmrg 	 the PCH file shouldn't be written...  */
187436ac495dSmrg       if (f->dont_read || f->err_no)
187536ac495dSmrg 	continue;
187636ac495dSmrg 
187736ac495dSmrg       if (f->stack_count == 0)
187836ac495dSmrg 	continue;
187936ac495dSmrg 
188036ac495dSmrg       count = result->count++;
188136ac495dSmrg 
188236ac495dSmrg       result->entries[count].once_only = f->once_only;
188336ac495dSmrg       /* |= is avoided in the next line because of an HP C compiler bug */
188436ac495dSmrg       result->have_once_only = result->have_once_only | f->once_only;
188536ac495dSmrg       if (f->buffer_valid)
188636ac495dSmrg 	md5_buffer ((const char *)f->buffer,
188736ac495dSmrg 		    f->st.st_size, result->entries[count].sum);
188836ac495dSmrg       else
188936ac495dSmrg 	{
189036ac495dSmrg 	  FILE *ff;
189136ac495dSmrg 	  int oldfd = f->fd;
189236ac495dSmrg 
189336ac495dSmrg 	  if (!open_file (f))
189436ac495dSmrg 	    {
189536ac495dSmrg 	      open_file_failed (pfile, f, 0, 0);
189636ac495dSmrg 	      free (result);
189736ac495dSmrg 	      return false;
189836ac495dSmrg 	    }
189936ac495dSmrg 	  ff = fdopen (f->fd, "rb");
190036ac495dSmrg 	  md5_stream (ff, result->entries[count].sum);
190136ac495dSmrg 	  fclose (ff);
190236ac495dSmrg 	  f->fd = oldfd;
190336ac495dSmrg 	}
190436ac495dSmrg       result->entries[count].size = f->st.st_size;
190536ac495dSmrg     }
190636ac495dSmrg 
190736ac495dSmrg   result_size = (sizeof (struct pchf_data)
190836ac495dSmrg                  + sizeof (struct pchf_entry) * (result->count - 1));
190936ac495dSmrg 
191036ac495dSmrg   qsort (result->entries, result->count, sizeof (struct pchf_entry),
191136ac495dSmrg 	 pchf_save_compare);
191236ac495dSmrg 
191336ac495dSmrg   ret = fwrite (result, result_size, 1, fp) == 1;
191436ac495dSmrg   free (result);
191536ac495dSmrg   return ret;
191636ac495dSmrg }
191736ac495dSmrg 
191836ac495dSmrg /* Read the pchf_data structure from F.  */
191936ac495dSmrg 
192036ac495dSmrg bool
_cpp_read_file_entries(cpp_reader * pfile ATTRIBUTE_UNUSED,FILE * f)192136ac495dSmrg _cpp_read_file_entries (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f)
192236ac495dSmrg {
192336ac495dSmrg   struct pchf_data d;
192436ac495dSmrg 
192536ac495dSmrg   if (fread (&d, sizeof (struct pchf_data) - sizeof (struct pchf_entry), 1, f)
192636ac495dSmrg        != 1)
192736ac495dSmrg     return false;
192836ac495dSmrg 
192936ac495dSmrg   pchf = XNEWVAR (struct pchf_data, sizeof (struct pchf_data)
193036ac495dSmrg 		  + sizeof (struct pchf_entry) * (d.count - 1));
193136ac495dSmrg   memcpy (pchf, &d, sizeof (struct pchf_data) - sizeof (struct pchf_entry));
193236ac495dSmrg   if (fread (pchf->entries, sizeof (struct pchf_entry), d.count, f)
193336ac495dSmrg       != d.count)
193436ac495dSmrg     return false;
193536ac495dSmrg   return true;
193636ac495dSmrg }
193736ac495dSmrg 
193836ac495dSmrg /* The parameters for pchf_compare.  */
193936ac495dSmrg 
194036ac495dSmrg struct pchf_compare_data
194136ac495dSmrg {
194236ac495dSmrg   /* The size of the file we're looking for.  */
194336ac495dSmrg   off_t size;
194436ac495dSmrg 
194536ac495dSmrg   /* The MD5 checksum of the file, if it's been computed.  */
194636ac495dSmrg   unsigned char sum[16];
194736ac495dSmrg 
194836ac495dSmrg   /* Is SUM valid?  */
194936ac495dSmrg   bool sum_computed;
195036ac495dSmrg 
195136ac495dSmrg   /* Do we need to worry about entries that don't have ONCE_ONLY set?  */
195236ac495dSmrg   bool check_included;
195336ac495dSmrg 
195436ac495dSmrg   /* The file that we're searching for.  */
195536ac495dSmrg   _cpp_file *f;
195636ac495dSmrg };
195736ac495dSmrg 
195836ac495dSmrg /* bsearch comparison function; look for D_P in E_P.  */
195936ac495dSmrg 
196036ac495dSmrg static int
pchf_compare(const void * d_p,const void * e_p)196136ac495dSmrg pchf_compare (const void *d_p, const void *e_p)
196236ac495dSmrg {
196336ac495dSmrg   const struct pchf_entry *e = (const struct pchf_entry *)e_p;
196436ac495dSmrg   struct pchf_compare_data *d = (struct pchf_compare_data *)d_p;
196536ac495dSmrg   int result;
196636ac495dSmrg 
196736ac495dSmrg   result = memcmp (&d->size, &e->size, sizeof (off_t));
196836ac495dSmrg   if (result != 0)
196936ac495dSmrg     return result;
197036ac495dSmrg 
197136ac495dSmrg   if (! d->sum_computed)
197236ac495dSmrg     {
197336ac495dSmrg       _cpp_file *const f = d->f;
197436ac495dSmrg 
197536ac495dSmrg       md5_buffer ((const char *)f->buffer, f->st.st_size, d->sum);
197636ac495dSmrg       d->sum_computed = true;
197736ac495dSmrg     }
197836ac495dSmrg 
197936ac495dSmrg   result = memcmp (d->sum, e->sum, 16);
198036ac495dSmrg   if (result != 0)
198136ac495dSmrg     return result;
198236ac495dSmrg 
198336ac495dSmrg   if (d->check_included || e->once_only)
198436ac495dSmrg     return 0;
198536ac495dSmrg   else
198636ac495dSmrg     return 1;
198736ac495dSmrg }
198836ac495dSmrg 
198936ac495dSmrg /* Check that F is not in a list read from a PCH file (if any).
199036ac495dSmrg    Assumes that f->buffer_valid is true.  Return TRUE if the file
199136ac495dSmrg    should not be read.  */
199236ac495dSmrg 
199336ac495dSmrg static bool
check_file_against_entries(cpp_reader * pfile ATTRIBUTE_UNUSED,_cpp_file * f,bool check_included)199436ac495dSmrg check_file_against_entries (cpp_reader *pfile ATTRIBUTE_UNUSED,
199536ac495dSmrg 			    _cpp_file *f,
199636ac495dSmrg 			    bool check_included)
199736ac495dSmrg {
199836ac495dSmrg   struct pchf_compare_data d;
199936ac495dSmrg 
200036ac495dSmrg   if (pchf == NULL
200136ac495dSmrg       || (! check_included && ! pchf->have_once_only))
200236ac495dSmrg     return false;
200336ac495dSmrg 
200436ac495dSmrg   d.size = f->st.st_size;
200536ac495dSmrg   d.sum_computed = false;
200636ac495dSmrg   d.f = f;
200736ac495dSmrg   d.check_included = check_included;
200836ac495dSmrg   return bsearch (&d, pchf->entries, pchf->count, sizeof (struct pchf_entry),
200936ac495dSmrg 		  pchf_compare) != NULL;
201036ac495dSmrg }
201136ac495dSmrg 
201236ac495dSmrg /* Return true if the file FNAME is found in the appropriate include file path
201336ac495dSmrg    as indicated by ANGLE_BRACKETS.  */
201436ac495dSmrg 
201536ac495dSmrg bool
_cpp_has_header(cpp_reader * pfile,const char * fname,int angle_brackets,enum include_type type)201636ac495dSmrg _cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets,
201736ac495dSmrg 		 enum include_type type)
201836ac495dSmrg {
201936ac495dSmrg   cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type);
2020*8feb0f0bSmrg   _cpp_file *file = _cpp_find_file (pfile, fname, start_dir, angle_brackets,
2021*8feb0f0bSmrg 				    /*fake=*/false,
2022*8feb0f0bSmrg 				    /*implicit_preinclude=*/false,
2023*8feb0f0bSmrg 				    /*has_include=*/true,
2024*8feb0f0bSmrg 				    0);
202536ac495dSmrg   return file->err_no != ENOENT;
202636ac495dSmrg }
202736ac495dSmrg 
2028