OSDN Git Service

PR c++/10784
[pf3gnuchains/gcc-fork.git] / 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_path *, int);
49 static void merge_include_chains (cpp_reader *, int);
50 static struct cpp_path *remove_duplicates (cpp_reader *, struct cpp_path *,
51                                            struct cpp_path *,
52                                            struct cpp_path *, int);
53
54 /* Include chains heads and tails.  */
55 static struct cpp_path *heads[4];
56 static struct cpp_path *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_path *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_path *
173 remove_duplicates (cpp_reader *pfile, struct cpp_path *head,
174                    struct cpp_path *system, struct cpp_path *join,
175                    int verbose)
176 {
177   struct cpp_path **pcur, *tmp, *cur;
178   struct stat st;
179
180   for (pcur = &head; *pcur; )
181     {
182       int reason = REASON_QUIET;
183
184       cur = *pcur;
185       cpp_simplify_path (cur->name);
186
187       if (stat (cur->name, &st))
188         {
189           /* Dirs that don't exist are silently ignored, unless verbose.  */
190           if (errno != ENOENT)
191             cpp_errno (pfile, DL_ERROR, cur->name);
192           else
193             reason = REASON_NOENT;
194         }
195       else if (!S_ISDIR (st.st_mode))
196         cpp_error_with_line (pfile, DL_ERROR, 0, 0,
197                              "%s: not a directory", cur->name);
198       else
199         {
200           INO_T_COPY (cur->ino, st.st_ino);
201           cur->dev  = st.st_dev;
202
203           /* Remove this one if it is in the system chain.  */
204           reason = REASON_DUP_SYS;
205           for (tmp = system; tmp; tmp = tmp->next)
206             if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev)
207               break;
208
209           if (!tmp)
210             {
211               /* Dupicate of something earlier in the same chain?  */
212               reason = REASON_DUP;
213               for (tmp = head; tmp != cur; tmp = tmp->next)
214                 if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev)
215                   break;
216
217               if (tmp == cur
218                   /* Last in the chain and duplicate of JOIN?  */
219                   && !(cur->next == NULL && join
220                        && INO_T_EQ (cur->ino, join->ino)
221                        && cur->dev == join->dev))
222                 {
223                   /* Unique, so keep this directory.  */
224                   pcur = &cur->next;
225                   continue;
226                 }
227             }
228         }
229
230       /* Remove this entry from the chain.  */
231       *pcur = cur->next;
232       free_path (cur, verbose ? reason: REASON_QUIET);
233     }
234
235   *pcur = join;
236   return head;
237 }
238
239 /* Merge the four include chains together in the order quote, bracket,
240    system, after.  Remove duplicate dirs (as determined by
241    INO_T_EQ()).
242
243    We can't just merge the lists and then uniquify them because then
244    we may lose directories from the <> search path that should be
245    there; consider -Ifoo -Ibar -I- -Ifoo -Iquux.  It is however safe
246    to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written -Ibar -I- -Ifoo
247    -Iquux.  */
248 static void
249 merge_include_chains (cpp_reader *pfile, int verbose)
250 {
251   /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
252      resulting SYSTEM chain.  */
253   if (heads[SYSTEM])
254     tails[SYSTEM]->next = heads[AFTER];
255   else
256     heads[SYSTEM] = heads[AFTER];
257   heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
258
259   /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
260      join it to SYSTEM.  */
261   heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
262                                       heads[SYSTEM], verbose);
263
264   /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
265      join it to BRACKET.  */
266   heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
267                                     heads[BRACKET], verbose);
268
269   /* If verbose, print the list of dirs to search.  */
270   if (verbose)
271     {
272       struct cpp_path *p;
273
274       fprintf (stderr, _("#include \"...\" search starts here:\n"));
275       for (p = heads[QUOTE];; p = p->next)
276         {
277           if (p == heads[BRACKET])
278             fprintf (stderr, _("#include <...> search starts here:\n"));
279           if (!p)
280             break;
281           fprintf (stderr, " %s\n", p->name);
282         }
283       fprintf (stderr, _("End of search list.\n"));
284     }
285 }
286
287 /* Use given -I paths for #include "..." but not #include <...>, and
288    don't search the directory of the present file for #include "...".
289    (Note that -I. -I- is not the same as the default setup; -I. uses
290    the compiler's working dir.)  */
291 void
292 split_quote_chain (void)
293 {
294   heads[QUOTE] = heads[BRACKET];
295   tails[QUOTE] = tails[BRACKET];
296   heads[BRACKET] = NULL;
297   tails[BRACKET] = NULL;
298   /* This is NOT redundant.  */
299   quote_ignores_source_dir = true;
300 }
301
302 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
303    NUL-terminated.  */
304 void
305 add_path (char *path, int chain, int cxx_aware)
306 {
307   struct cpp_path *p;
308
309   p = (struct cpp_path *) xmalloc (sizeof (struct cpp_path));
310   p->next = NULL;
311   p->name = path;
312   if (chain == SYSTEM || chain == AFTER)
313     p->sysp = 1 + !cxx_aware;
314   else
315     p->sysp = 0;
316
317   if (tails[chain])
318     tails[chain]->next = p;
319   else
320     heads[chain] = p;
321   tails[chain] = p;
322 }
323
324 /* Exported function to handle include chain merging, duplicate
325    removal, and registration with cpplib.  */
326 void
327 register_include_chains (cpp_reader *pfile, const char *sysroot,
328                          const char *iprefix, int stdinc, int cxx_stdinc,
329                          int verbose)
330 {
331   static const char *const lang_env_vars[] =
332     { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
333       "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
334   cpp_options *cpp_opts = cpp_get_options (pfile);
335   size_t idx = (cpp_opts->objc ? 2: 0);
336
337   if (cpp_opts->cplusplus)
338     idx++;
339   else
340     cxx_stdinc = false;
341
342   /* CPATH and language-dependent environment variables may add to the
343      include chain.  */
344   add_env_var_paths ("CPATH", BRACKET);
345   add_env_var_paths (lang_env_vars[idx], SYSTEM);
346
347   /* Finally chain on the standard directories.  */
348   if (stdinc)
349     add_standard_paths (sysroot, iprefix, cxx_stdinc);
350
351   merge_include_chains (pfile, verbose);
352
353   cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
354                           quote_ignores_source_dir);
355 }