* config/arm/arm.h (REG_CLASS_NAMES): Add missing comma.
[official-gcc.git] / gcc / c-incpath.c
blob7b08c1a12a3ccb7231eadf278a629377e704fc38
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.
5 Broken out of cppinit.c and cppfiles.c and rewritten Mar 2003.
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.
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.
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. */
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"
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
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);
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 };
60 /* Free an element of the include chain, possibly giving a reason. */
61 static void
62 free_path (struct cpp_dir *path, int reason)
64 switch (reason)
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;
74 case REASON_NOENT:
75 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
76 path->name);
77 break;
79 case REASON_QUIET:
80 default:
81 break;
84 free (path->name);
85 free (path);
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)
93 char *p, *q, *path;
95 GET_ENVIRONMENT (q, env_var);
97 if (!q)
98 return;
100 for (p = q; *q; p = q + 1)
102 q = p;
103 while (*q != 0 && *q != PATH_SEPARATOR)
104 q++;
106 if (p == q)
107 path = xstrdup (".");
108 else
110 path = xmalloc (q - p + 1);
111 memcpy (path, p, q - p);
112 path[q - p] = '\0';
115 add_path (path, chain, chain == SYSTEM);
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)
123 const struct default_include *p;
124 size_t len;
126 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
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++)
133 if (!p->cplusplus || cxx_stdinc)
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))
142 char *str = concat (iprefix, p->fname + len, NULL);
143 add_path (str, SYSTEM, p->cxx_aware);
149 for (p = cpp_include_defaults; p->fname; p++)
151 if (!p->cplusplus || cxx_stdinc)
153 char *str;
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);
161 add_path (str, SYSTEM, p->cxx_aware);
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)
177 struct cpp_dir **pcur, *tmp, *cur;
178 struct stat st;
180 for (pcur = &head; *pcur; )
182 int reason = REASON_QUIET;
184 cur = *pcur;
186 if (stat (cur->name, &st))
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;
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
199 INO_T_COPY (cur->ino, st.st_ino);
200 cur->dev = st.st_dev;
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;
208 if (!tmp)
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;
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))
222 /* Unique, so keep this directory. */
223 pcur = &cur->next;
224 continue;
229 /* Remove this entry from the chain. */
230 *pcur = cur->next;
231 free_path (cur, verbose ? reason: REASON_QUIET);
234 *pcur = join;
235 return head;
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()).
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)
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);
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);
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);
268 /* If verbose, print the list of dirs to search. */
269 if (verbose)
271 struct cpp_dir *p;
273 fprintf (stderr, _("#include \"...\" search starts here:\n"));
274 for (p = heads[QUOTE];; p = p->next)
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);
282 fprintf (stderr, _("End of search list.\n"));
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)
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;
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)
306 struct cpp_dir *p;
308 p = xmalloc (sizeof (struct cpp_dir));
309 p->next = NULL;
310 p->name = path;
311 if (chain == SYSTEM || chain == AFTER)
312 p->sysp = 1 + !cxx_aware;
313 else
314 p->sysp = 0;
316 if (tails[chain])
317 tails[chain]->next = p;
318 else
319 heads[chain] = p;
320 tails[chain] = p;
323 /* Exported function to handle include chain merging, duplicate
324 removal, and registration with cpplib. */
325 void
326 register_include_chains (cpp_reader *pfile, const char *sysroot,
327 const char *iprefix, int stdinc, int cxx_stdinc,
328 int verbose)
330 static const char *const lang_env_vars[] =
331 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
332 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
333 cpp_options *cpp_opts = cpp_get_options (pfile);
334 size_t idx = (cpp_opts->objc ? 2: 0);
336 if (cpp_opts->cplusplus)
337 idx++;
338 else
339 cxx_stdinc = false;
341 /* CPATH and language-dependent environment variables may add to the
342 include chain. */
343 add_env_var_paths ("CPATH", BRACKET);
344 add_env_var_paths (lang_env_vars[idx], SYSTEM);
346 /* Finally chain on the standard directories. */
347 if (stdinc)
348 add_standard_paths (sysroot, iprefix, cxx_stdinc);
350 merge_include_chains (pfile, verbose);
352 cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
353 quote_ignores_source_dir);