Merge from vendor branch FILE:
[dragonfly.git] / contrib / gcc-3.4 / gcc / c-incpath.c
1 /* Set up combined include path chain for the preprocessor.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5    Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "prefix.h"
27 #include "intl.h"
28 #include "c-incpath.h"
29 #include "cppdefault.h"
30
31 /* Windows does not natively support inodes, and neither does MSDOS.
32    Cygwin's emulation can generate non-unique inodes, so don't use it.
33    VMS has non-numeric inodes.  */
34 #ifdef VMS
35 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
36 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
37 #else
38 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
39 #  define INO_T_EQ(A, B) 0
40 # else
41 #  define INO_T_EQ(A, B) ((A) == (B))
42 # endif
43 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
44 #endif
45
46 static void add_env_var_paths (const char *, int);
47 static void add_standard_paths (const char *, const char *, int);
48 static void free_path (struct cpp_dir *, int);
49 static void merge_include_chains (cpp_reader *, int);
50 static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
51                                            struct cpp_dir *,
52                                            struct cpp_dir *, int);
53
54 /* Include chains heads and tails.  */
55 static struct cpp_dir *heads[4];
56 static struct cpp_dir *tails[4];
57 static bool quote_ignores_source_dir;
58 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
59
60 /* Free an element of the include chain, possibly giving a reason.  */
61 static void
62 free_path (struct cpp_dir *path, int reason)
63 {
64   switch (reason)
65     {
66     case REASON_DUP:
67     case REASON_DUP_SYS:
68       fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
69       if (reason == REASON_DUP_SYS)
70         fprintf (stderr,
71  _("  as it is a non-system directory that duplicates a system directory\n"));
72       break;
73
74     case REASON_NOENT:
75       fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
76                path->name);
77       break;
78
79     case REASON_QUIET:
80     default:
81       break;
82     }
83
84   free (path->name);
85   free (path);
86 }
87
88 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
89    append all the names to the search path CHAIN.  */
90 static void
91 add_env_var_paths (const char *env_var, int chain)
92 {
93   char *p, *q, *path;
94
95   GET_ENVIRONMENT (q, env_var);
96
97   if (!q)
98     return;
99
100   for (p = q; *q; p = q + 1)
101     {
102       q = p;
103       while (*q != 0 && *q != PATH_SEPARATOR)
104         q++;
105
106       if (p == q)
107         path = xstrdup (".");
108       else
109         {
110           path = xmalloc (q - p + 1);
111           memcpy (path, p, q - p);
112           path[q - p] = '\0';
113         }
114
115       add_path (path, chain, chain == SYSTEM);
116     }
117 }
118
119 /* Append the standard include chain defined in cppdefault.c.  */
120 static void
121 add_standard_paths (const char *sysroot, const char *iprefix, int cxx_stdinc)
122 {
123   const struct default_include *p;
124   size_t len;
125
126   if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
127     {
128       /* Look for directories that start with the standard prefix.
129          "Translate" them, ie. replace /usr/local/lib/gcc... with
130          IPREFIX and search them first.  */
131       for (p = cpp_include_defaults; p->fname; p++)
132         {
133           if (!p->cplusplus || cxx_stdinc)
134             {
135               /* Should we be translating sysrooted dirs too?  Assume
136                  that iprefix and sysroot are mutually exclusive, for
137                  now.  */
138               if (sysroot && p->add_sysroot)
139                 continue;
140               if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
141                 {
142                   char *str = concat (iprefix, p->fname + len, NULL);
143                   add_path (str, SYSTEM, p->cxx_aware);
144                 }
145             }
146         }
147     }
148
149   for (p = cpp_include_defaults; p->fname; p++)
150     {
151       if (!p->cplusplus || cxx_stdinc)
152         {
153           char *str;
154
155           /* Should this directory start with the sysroot?  */
156           if (sysroot && p->add_sysroot)
157             str = concat (sysroot, p->fname, NULL);
158           else
159             str = update_path (p->fname, p->component);
160
161           add_path (str, SYSTEM, p->cxx_aware);
162         }
163     }
164 }
165
166 /* For each duplicate path in chain HEAD, keep just the first one.
167    Remove each path in chain HEAD that also exists in chain SYSTEM.
168    Set the NEXT pointer of the last path in the resulting chain to
169    JOIN, unless it duplicates JOIN in which case the last path is
170    removed.  Return the head of the resulting chain.  Any of HEAD,
171    JOIN and SYSTEM can be NULL.  */
172 static struct cpp_dir *
173 remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
174                    struct cpp_dir *system, struct cpp_dir *join,
175                    int verbose)
176 {
177   struct cpp_dir **pcur, *tmp, *cur;
178   struct stat st;
179
180   for (pcur = &head; *pcur; )
181     {
182       int reason = REASON_QUIET;
183
184       cur = *pcur;
185
186       if (stat (cur->name, &st))
187         {
188           /* Dirs that don't exist are silently ignored, unless verbose.  */
189           if (errno != ENOENT)
190             cpp_errno (pfile, CPP_DL_ERROR, cur->name);
191           else
192             reason = REASON_NOENT;
193         }
194       else if (!S_ISDIR (st.st_mode))
195         cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
196                              "%s: not a directory", cur->name);
197       else
198         {
199           INO_T_COPY (cur->ino, st.st_ino);
200           cur->dev  = st.st_dev;
201
202           /* Remove this one if it is in the system chain.  */
203           reason = REASON_DUP_SYS;
204           for (tmp = system; tmp; tmp = tmp->next)
205             if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev)
206               break;
207
208           if (!tmp)
209             {
210               /* Duplicate of something earlier in the same chain?  */
211               reason = REASON_DUP;
212               for (tmp = head; tmp != cur; tmp = tmp->next)
213                 if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev)
214                   break;
215
216               if (tmp == cur
217                   /* Last in the chain and duplicate of JOIN?  */
218                   && !(cur->next == NULL && join
219                        && INO_T_EQ (cur->ino, join->ino)
220                        && cur->dev == join->dev))
221                 {
222                   /* Unique, so keep this directory.  */
223                   pcur = &cur->next;
224                   continue;
225                 }
226             }
227         }
228
229       /* Remove this entry from the chain.  */
230       *pcur = cur->next;
231       free_path (cur, verbose ? reason: REASON_QUIET);
232     }
233
234   *pcur = join;
235   return head;
236 }
237
238 /* Merge the four include chains together in the order quote, bracket,
239    system, after.  Remove duplicate dirs (as determined by
240    INO_T_EQ()).
241
242    We can't just merge the lists and then uniquify them because then
243    we may lose directories from the <> search path that should be
244    there; consider -Ifoo -Ibar -I- -Ifoo -Iquux.  It is however safe
245    to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
246    -Iquux.  */
247 static void
248 merge_include_chains (cpp_reader *pfile, int verbose)
249 {
250   /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
251      resulting SYSTEM chain.  */
252   if (heads[SYSTEM])
253     tails[SYSTEM]->next = heads[AFTER];
254   else
255     heads[SYSTEM] = heads[AFTER];
256   heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
257
258   /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
259      join it to SYSTEM.  */
260   heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
261                                       heads[SYSTEM], verbose);
262
263   /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
264      join it to BRACKET.  */
265   heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
266                                     heads[BRACKET], verbose);
267
268   /* If verbose, print the list of dirs to search.  */
269   if (verbose)
270     {
271       struct cpp_dir *p;
272
273       fprintf (stderr, _("#include \"...\" search starts here:\n"));
274       for (p = heads[QUOTE];; p = p->next)
275         {
276           if (p == heads[BRACKET])
277             fprintf (stderr, _("#include <...> search starts here:\n"));
278           if (!p)
279             break;
280           fprintf (stderr, " %s\n", p->name);
281         }
282       fprintf (stderr, _("End of search list.\n"));
283     }
284 }
285
286 /* Use given -I paths for #include "..." but not #include <...>, and
287    don't search the directory of the present file for #include "...".
288    (Note that -I. -I- is not the same as the default setup; -I. uses
289    the compiler's working dir.)  */
290 void
291 split_quote_chain (void)
292 {
293   heads[QUOTE] = heads[BRACKET];
294   tails[QUOTE] = tails[BRACKET];
295   heads[BRACKET] = NULL;
296   tails[BRACKET] = NULL;
297   /* This is NOT redundant.  */
298   quote_ignores_source_dir = true;
299 }
300
301 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
302    NUL-terminated.  */
303 void
304 add_path (char *path, int chain, int cxx_aware)
305 {
306   struct cpp_dir *p;
307
308 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
309   /* Convert all backslashes to slashes.  The native CRT stat()
310      function does not recognise a directory that ends in a backslash
311      (unless it is a drive root dir, such "c:\").  Forward slashes,
312      trailing or otherwise, cause no problems for stat().  */
313   char* c;
314   for (c = path; *c; c++)
315     if (*c == '\\') *c = '/';
316 #endif
317
318   p = xmalloc (sizeof (struct cpp_dir));
319   p->next = NULL;
320   p->name = path;
321   if (chain == SYSTEM || chain == AFTER)
322     p->sysp = 1 + !cxx_aware;
323   else
324     p->sysp = 0;
325
326   if (tails[chain])
327     tails[chain]->next = p;
328   else
329     heads[chain] = p;
330   tails[chain] = p;
331 }
332
333 /* Exported function to handle include chain merging, duplicate
334    removal, and registration with cpplib.  */
335 void
336 register_include_chains (cpp_reader *pfile, const char *sysroot,
337                          const char *iprefix, int stdinc, int cxx_stdinc,
338                          int verbose)
339 {
340   static const char *const lang_env_vars[] =
341     { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
342       "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
343   cpp_options *cpp_opts = cpp_get_options (pfile);
344   size_t idx = (cpp_opts->objc ? 2: 0);
345
346   if (cpp_opts->cplusplus)
347     idx++;
348   else
349     cxx_stdinc = false;
350
351   /* CPATH and language-dependent environment variables may add to the
352      include chain.  */
353   add_env_var_paths ("CPATH", BRACKET);
354   add_env_var_paths (lang_env_vars[idx], SYSTEM);
355
356   /* Finally chain on the standard directories.  */
357   if (stdinc)
358     add_standard_paths (sysroot, iprefix, cxx_stdinc);
359
360   merge_include_chains (pfile, verbose);
361
362   cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
363                           quote_ignores_source_dir);
364 }