* g++.dg/tree-ssa/copyprop-1.C: Remove xfail.
[official-gcc.git] / gcc / c-incpath.c
blobf8b524d6e1176a5a0e7ded5d83050bc377375f23
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, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "machmode.h"
26 #include "target.h"
27 #include "tm.h"
28 #include "cpplib.h"
29 #include "prefix.h"
30 #include "intl.h"
31 #include "c-incpath.h"
32 #include "cppdefault.h"
34 /* Windows does not natively support inodes, and neither does MSDOS.
35 Cygwin's emulation can generate non-unique inodes, so don't use it.
36 VMS has non-numeric inodes. */
37 #ifdef VMS
38 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
39 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
40 #else
41 # if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__
42 # define INO_T_EQ(A, B) 0
43 # else
44 # define INO_T_EQ(A, B) ((A) == (B))
45 # endif
46 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
47 #endif
49 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
51 static void add_env_var_paths (const char *, int);
52 static void add_standard_paths (const char *, const char *, const char *, int);
53 static void free_path (struct cpp_dir *, int);
54 static void merge_include_chains (const char *, cpp_reader *, int);
55 static void add_sysroot_to_chain (const char *, int);
56 static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
57 struct cpp_dir *,
58 struct cpp_dir *, int);
60 /* Include chains heads and tails. */
61 static struct cpp_dir *heads[4];
62 static struct cpp_dir *tails[4];
63 static bool quote_ignores_source_dir;
64 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
66 /* Free an element of the include chain, possibly giving a reason. */
67 static void
68 free_path (struct cpp_dir *path, int reason)
70 switch (reason)
72 case REASON_DUP:
73 case REASON_DUP_SYS:
74 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
75 if (reason == REASON_DUP_SYS)
76 fprintf (stderr,
77 _(" as it is a non-system directory that duplicates a system directory\n"));
78 break;
80 case REASON_NOENT:
81 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
82 path->name);
83 break;
85 case REASON_QUIET:
86 default:
87 break;
90 free (path->name);
91 free (path);
94 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
95 append all the names to the search path CHAIN. */
96 static void
97 add_env_var_paths (const char *env_var, int chain)
99 char *p, *q, *path;
101 GET_ENVIRONMENT (q, env_var);
103 if (!q)
104 return;
106 for (p = q; *q; p = q + 1)
108 q = p;
109 while (*q != 0 && *q != PATH_SEPARATOR)
110 q++;
112 if (p == q)
113 path = xstrdup (".");
114 else
116 path = XNEWVEC (char, q - p + 1);
117 memcpy (path, p, q - p);
118 path[q - p] = '\0';
121 add_path (path, chain, chain == SYSTEM, false);
125 /* Append the standard include chain defined in cppdefault.c. */
126 static void
127 add_standard_paths (const char *sysroot, const char *iprefix,
128 const char *imultilib, int cxx_stdinc)
130 const struct default_include *p;
131 int relocated = cpp_relocated();
132 size_t len;
134 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
136 /* Look for directories that start with the standard prefix.
137 "Translate" them, i.e. replace /usr/local/lib/gcc... with
138 IPREFIX and search them first. */
139 for (p = cpp_include_defaults; p->fname; p++)
141 if (!p->cplusplus || cxx_stdinc)
143 /* Should we be translating sysrooted dirs too? Assume
144 that iprefix and sysroot are mutually exclusive, for
145 now. */
146 if (sysroot && p->add_sysroot)
147 continue;
148 if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
150 char *str = concat (iprefix, p->fname + len, NULL);
151 if (p->multilib && imultilib)
152 str = concat (str, dir_separator_str, imultilib, NULL);
153 add_path (str, SYSTEM, p->cxx_aware, false);
159 for (p = cpp_include_defaults; p->fname; p++)
161 if (!p->cplusplus || cxx_stdinc)
163 char *str;
165 /* Should this directory start with the sysroot? */
166 if (sysroot && p->add_sysroot)
167 str = concat (sysroot, p->fname, NULL);
168 else if (!p->add_sysroot && relocated
169 && strncmp (p->fname, cpp_PREFIX, cpp_PREFIX_len) == 0)
171 static const char *relocated_prefix;
172 /* If this path starts with the configure-time prefix,
173 but the compiler has been relocated, replace it
174 with the run-time prefix. The run-time exec prefix
175 is GCC_EXEC_PREFIX. Compute the path from there back
176 to the toplevel prefix. */
177 if (!relocated_prefix)
179 char *dummy;
180 /* Make relative prefix expects the first argument
181 to be a program, not a directory. */
182 dummy = concat (gcc_exec_prefix, "dummy", NULL);
183 relocated_prefix
184 = make_relative_prefix (dummy,
185 cpp_EXEC_PREFIX,
186 cpp_PREFIX);
188 str = concat (relocated_prefix,
189 p->fname + cpp_PREFIX_len,
190 NULL);
191 str = update_path (str, p->component);
193 else
194 str = update_path (p->fname, p->component);
196 if (p->multilib && imultilib)
197 str = concat (str, dir_separator_str, imultilib, NULL);
199 add_path (str, SYSTEM, p->cxx_aware, false);
204 /* For each duplicate path in chain HEAD, keep just the first one.
205 Remove each path in chain HEAD that also exists in chain SYSTEM.
206 Set the NEXT pointer of the last path in the resulting chain to
207 JOIN, unless it duplicates JOIN in which case the last path is
208 removed. Return the head of the resulting chain. Any of HEAD,
209 JOIN and SYSTEM can be NULL. */
211 static struct cpp_dir *
212 remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
213 struct cpp_dir *system, struct cpp_dir *join,
214 int verbose)
216 struct cpp_dir **pcur, *tmp, *cur;
217 struct stat st;
219 for (pcur = &head; *pcur; )
221 int reason = REASON_QUIET;
223 cur = *pcur;
225 if (stat (cur->name, &st))
227 /* Dirs that don't exist are silently ignored, unless verbose. */
228 if (errno != ENOENT)
229 cpp_errno (pfile, CPP_DL_ERROR, cur->name);
230 else
232 /* If -Wmissing-include-dirs is given, warn. */
233 cpp_options *opts = cpp_get_options (pfile);
234 if (opts->warn_missing_include_dirs && cur->user_supplied_p)
235 cpp_errno (pfile, CPP_DL_WARNING, cur->name);
236 reason = REASON_NOENT;
239 else if (!S_ISDIR (st.st_mode))
240 cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
241 "%s: not a directory", cur->name);
242 else
244 INO_T_COPY (cur->ino, st.st_ino);
245 cur->dev = st.st_dev;
247 /* Remove this one if it is in the system chain. */
248 reason = REASON_DUP_SYS;
249 for (tmp = system; tmp; tmp = tmp->next)
250 if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev
251 && cur->construct == tmp->construct)
252 break;
254 if (!tmp)
256 /* Duplicate of something earlier in the same chain? */
257 reason = REASON_DUP;
258 for (tmp = head; tmp != cur; tmp = tmp->next)
259 if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev
260 && cur->construct == tmp->construct)
261 break;
263 if (tmp == cur
264 /* Last in the chain and duplicate of JOIN? */
265 && !(cur->next == NULL && join
266 && INO_T_EQ (cur->ino, join->ino)
267 && cur->dev == join->dev
268 && cur->construct == join->construct))
270 /* Unique, so keep this directory. */
271 pcur = &cur->next;
272 continue;
277 /* Remove this entry from the chain. */
278 *pcur = cur->next;
279 free_path (cur, verbose ? reason: REASON_QUIET);
282 *pcur = join;
283 return head;
286 /* Add SYSROOT to any user-supplied paths in CHAIN starting with
287 "=". */
289 static void
290 add_sysroot_to_chain (const char *sysroot, int chain)
292 struct cpp_dir *p;
294 for (p = heads[chain]; p != NULL; p = p->next)
295 if (p->name[0] == '=' && p->user_supplied_p)
296 p->name = concat (sysroot, p->name + 1, NULL);
299 /* Merge the four include chains together in the order quote, bracket,
300 system, after. Remove duplicate dirs (as determined by
301 INO_T_EQ()).
303 We can't just merge the lists and then uniquify them because then
304 we may lose directories from the <> search path that should be
305 there; consider -iquote foo -iquote bar -Ifoo -Iquux. It is
306 however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
307 written -iquote bar -Ifoo -Iquux. */
309 static void
310 merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
312 /* Add the sysroot to user-supplied paths starting with "=". */
313 if (sysroot)
315 add_sysroot_to_chain (sysroot, QUOTE);
316 add_sysroot_to_chain (sysroot, BRACKET);
317 add_sysroot_to_chain (sysroot, SYSTEM);
318 add_sysroot_to_chain (sysroot, AFTER);
321 /* Join the SYSTEM and AFTER chains. Remove duplicates in the
322 resulting SYSTEM chain. */
323 if (heads[SYSTEM])
324 tails[SYSTEM]->next = heads[AFTER];
325 else
326 heads[SYSTEM] = heads[AFTER];
327 heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
329 /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
330 join it to SYSTEM. */
331 heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
332 heads[SYSTEM], verbose);
334 /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
335 join it to BRACKET. */
336 heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
337 heads[BRACKET], verbose);
339 /* If verbose, print the list of dirs to search. */
340 if (verbose)
342 struct cpp_dir *p;
344 fprintf (stderr, _("#include \"...\" search starts here:\n"));
345 for (p = heads[QUOTE];; p = p->next)
347 if (p == heads[BRACKET])
348 fprintf (stderr, _("#include <...> search starts here:\n"));
349 if (!p)
350 break;
351 fprintf (stderr, " %s\n", p->name);
353 fprintf (stderr, _("End of search list.\n"));
357 /* Use given -I paths for #include "..." but not #include <...>, and
358 don't search the directory of the present file for #include "...".
359 (Note that -I. -I- is not the same as the default setup; -I. uses
360 the compiler's working dir.) */
361 void
362 split_quote_chain (void)
364 heads[QUOTE] = heads[BRACKET];
365 tails[QUOTE] = tails[BRACKET];
366 heads[BRACKET] = NULL;
367 tails[BRACKET] = NULL;
368 /* This is NOT redundant. */
369 quote_ignores_source_dir = true;
372 /* Add P to the chain specified by CHAIN. */
374 void
375 add_cpp_dir_path (cpp_dir *p, int chain)
377 if (tails[chain])
378 tails[chain]->next = p;
379 else
380 heads[chain] = p;
381 tails[chain] = p;
384 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
385 NUL-terminated. */
386 void
387 add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)
389 cpp_dir *p;
391 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
392 /* Remove unnecessary trailing slashes. On some versions of MS
393 Windows, trailing _forward_ slashes cause no problems for stat().
394 On newer versions, stat() does not recognize a directory that ends
395 in a '\\' or '/', unless it is a drive root dir, such as "c:/",
396 where it is obligatory. */
397 int pathlen = strlen (path);
398 char* end = path + pathlen - 1;
399 /* Preserve the lead '/' or lead "c:/". */
400 char* start = path + (pathlen > 2 && path[1] == ':' ? 3 : 1);
402 for (; end > start && IS_DIR_SEPARATOR (*end); end--)
403 *end = 0;
404 #endif
406 p = XNEW (cpp_dir);
407 p->next = NULL;
408 p->name = path;
409 if (chain == SYSTEM || chain == AFTER)
410 p->sysp = 1 + !cxx_aware;
411 else
412 p->sysp = 0;
413 p->construct = 0;
414 p->user_supplied_p = user_supplied_p;
416 add_cpp_dir_path (p, chain);
419 /* Exported function to handle include chain merging, duplicate
420 removal, and registration with cpplib. */
421 void
422 register_include_chains (cpp_reader *pfile, const char *sysroot,
423 const char *iprefix, const char *imultilib,
424 int stdinc, int cxx_stdinc, int verbose)
426 static const char *const lang_env_vars[] =
427 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
428 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
429 cpp_options *cpp_opts = cpp_get_options (pfile);
430 size_t idx = (cpp_opts->objc ? 2: 0);
432 if (cpp_opts->cplusplus)
433 idx++;
434 else
435 cxx_stdinc = false;
437 /* CPATH and language-dependent environment variables may add to the
438 include chain. */
439 add_env_var_paths ("CPATH", BRACKET);
440 add_env_var_paths (lang_env_vars[idx], SYSTEM);
442 target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc);
444 /* Finally chain on the standard directories. */
445 if (stdinc)
446 add_standard_paths (sysroot, iprefix, imultilib, cxx_stdinc);
448 target_c_incpath.extra_includes (sysroot, iprefix, stdinc);
450 merge_include_chains (sysroot, pfile, verbose);
452 cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
453 quote_ignores_source_dir);
455 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
456 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED,
457 const char *iprefix ATTRIBUTE_UNUSED,
458 int stdinc ATTRIBUTE_UNUSED)
461 #endif
463 #ifndef TARGET_EXTRA_INCLUDES
464 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
465 #endif
466 #ifndef TARGET_EXTRA_PRE_INCLUDES
467 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
468 #endif
470 struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES };