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