* SDCPP synchronized with GCC CPP release version 4.1.1,
[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, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, false);\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, i.e. 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, false);\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, false);\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 \r
171 static struct cpp_dir *\r
172 remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,\r
173                    struct cpp_dir *system, struct cpp_dir *join,\r
174                    int verbose)\r
175 {\r
176   struct cpp_dir **pcur, *tmp, *cur;\r
177   struct stat st;\r
178 \r
179   for (pcur = &head; *pcur; )\r
180     {\r
181       int reason = REASON_QUIET;\r
182 \r
183       cur = *pcur;\r
184 \r
185       if (stat (cur->name, &st))\r
186         {\r
187           /* Dirs that don't exist are silently ignored, unless verbose.  */\r
188           if (errno != ENOENT)\r
189             cpp_errno (pfile, CPP_DL_ERROR, cur->name);\r
190           else\r
191             {\r
192               /* If -Wmissing-include-dirs is given, warn.  */\r
193               cpp_options *opts = cpp_get_options (pfile);\r
194               if (opts->warn_missing_include_dirs && cur->user_supplied_p)\r
195                 cpp_errno (pfile, CPP_DL_WARNING, cur->name);\r
196               reason = REASON_NOENT;\r
197             }\r
198         }\r
199       else if (!S_ISDIR (st.st_mode))\r
200         cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,\r
201                              "%s: not a directory", cur->name);\r
202       else\r
203         {\r
204           INO_T_COPY (cur->ino, st.st_ino);\r
205           cur->dev  = st.st_dev;\r
206 \r
207           /* Remove this one if it is in the system chain.  */\r
208           reason = REASON_DUP_SYS;\r
209           for (tmp = system; tmp; tmp = tmp->next)\r
210            if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev\r
211                && cur->construct == tmp->construct)\r
212               break;\r
213 \r
214           if (!tmp)\r
215             {\r
216               /* Duplicate of something earlier in the same chain?  */\r
217               reason = REASON_DUP;\r
218               for (tmp = head; tmp != cur; tmp = tmp->next)\r
219                if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev\r
220                    && cur->construct == tmp->construct)\r
221                   break;\r
222 \r
223               if (tmp == cur\r
224                   /* Last in the chain and duplicate of JOIN?  */\r
225                   && !(cur->next == NULL && join\r
226                        && INO_T_EQ (cur->ino, join->ino)\r
227                       && cur->dev == join->dev\r
228                       && cur->construct == join->construct))\r
229                 {\r
230                   /* Unique, so keep this directory.  */\r
231                   pcur = &cur->next;\r
232                   continue;\r
233                 }\r
234             }\r
235         }\r
236 \r
237       /* Remove this entry from the chain.  */\r
238       *pcur = cur->next;\r
239       free_path (cur, verbose ? reason: REASON_QUIET);\r
240     }\r
241 \r
242   *pcur = join;\r
243   return head;\r
244 }\r
245 \r
246 /* Merge the four include chains together in the order quote, bracket,\r
247    system, after.  Remove duplicate dirs (as determined by\r
248    INO_T_EQ()).\r
249 \r
250    We can't just merge the lists and then uniquify them because then\r
251    we may lose directories from the <> search path that should be\r
252    there; consider -iquote foo -iquote bar -Ifoo -Iquux.  It is\r
253    however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if\r
254    written -iquote bar -Ifoo -Iquux.  */\r
255 \r
256 static void\r
257 merge_include_chains (cpp_reader *pfile, int verbose)\r
258 {\r
259   /* Join the SYSTEM and AFTER chains.  Remove duplicates in the\r
260      resulting SYSTEM chain.  */\r
261   if (heads[SYSTEM])\r
262     tails[SYSTEM]->next = heads[AFTER];\r
263   else\r
264     heads[SYSTEM] = heads[AFTER];\r
265   heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);\r
266 \r
267   /* Remove duplicates from BRACKET that are in itself or SYSTEM, and\r
268      join it to SYSTEM.  */\r
269   heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],\r
270                                       heads[SYSTEM], verbose);\r
271 \r
272   /* Remove duplicates from QUOTE that are in itself or SYSTEM, and\r
273      join it to BRACKET.  */\r
274   heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],\r
275                                     heads[BRACKET], verbose);\r
276 \r
277   /* If verbose, print the list of dirs to search.  */\r
278   if (verbose)\r
279     {\r
280       struct cpp_dir *p;\r
281 \r
282       fprintf (stderr, _("#include \"...\" search starts here:\n"));\r
283       for (p = heads[QUOTE];; p = p->next)\r
284         {\r
285           if (p == heads[BRACKET])\r
286             fprintf (stderr, _("#include <...> search starts here:\n"));\r
287           if (!p)\r
288             break;\r
289           fprintf (stderr, " %s\n", p->name);\r
290         }\r
291       fprintf (stderr, _("End of search list.\n"));\r
292     }\r
293 }\r
294 \r
295 /* Use given -I paths for #include "..." but not #include <...>, and\r
296    don't search the directory of the present file for #include "...".\r
297    (Note that -I. -I- is not the same as the default setup; -I. uses\r
298    the compiler's working dir.)  */\r
299 void\r
300 split_quote_chain (void)\r
301 {\r
302   heads[QUOTE] = heads[BRACKET];\r
303   tails[QUOTE] = tails[BRACKET];\r
304   heads[BRACKET] = NULL;\r
305   tails[BRACKET] = NULL;\r
306   /* This is NOT redundant.  */\r
307   quote_ignores_source_dir = true;\r
308 }\r
309 \r
310 /* Add P to the chain specified by CHAIN.  */\r
311 \r
312 void\r
313 add_cpp_dir_path (cpp_dir *p, int chain)\r
314 {\r
315   if (tails[chain])\r
316     tails[chain]->next = p;\r
317   else\r
318     heads[chain] = p;\r
319   tails[chain] = p;\r
320 }\r
321 \r
322 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and\r
323    NUL-terminated.  */\r
324 void\r
325 add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)\r
326 {\r
327   cpp_dir *p;\r
328 \r
329 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)\r
330   /* Convert all backslashes to slashes.  The native CRT stat()\r
331      function does not recognize a directory that ends in a backslash\r
332      (unless it is a drive root dir, such "c:\").  Forward slashes,\r
333      trailing or otherwise, cause no problems for stat().  */\r
334   char* c;\r
335   for (c = path; *c; c++)\r
336     if (*c == '\\') *c = '/';\r
337 #endif\r
338 \r
339   p = xmalloc (sizeof (cpp_dir));\r
340   p->next = NULL;\r
341   p->name = path;\r
342   if (chain == SYSTEM || chain == AFTER)\r
343     p->sysp = 1 + !cxx_aware;\r
344   else\r
345     p->sysp = 0;\r
346   p->construct = 0;\r
347   p->user_supplied_p = user_supplied_p;\r
348 \r
349   add_cpp_dir_path (p, chain);\r
350 }\r
351 \r
352 /* Exported function to handle include chain merging, duplicate\r
353    removal, and registration with cpplib.  */\r
354 void\r
355 register_include_chains (cpp_reader *pfile, const char *sysroot,\r
356                          const char *iprefix, int stdinc, int cxx_stdinc,\r
357                          int verbose)\r
358 {\r
359   static const char *const lang_env_vars[] =\r
360     { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",\r
361       "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };\r
362   cpp_options *cpp_opts = cpp_get_options (pfile);\r
363   size_t idx = (cpp_opts->objc ? 2: 0);\r
364 \r
365   if (cpp_opts->cplusplus)\r
366     idx++;\r
367   else\r
368     cxx_stdinc = false;\r
369 \r
370   /* CPATH and language-dependent environment variables may add to the\r
371      include chain.  */\r
372   add_env_var_paths ("CPATH", BRACKET);\r
373   add_env_var_paths (lang_env_vars[idx], SYSTEM);\r
374 \r
375   target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc);\r
376 \r
377   /* Finally chain on the standard directories.  */\r
378   if (stdinc)\r
379     add_standard_paths (sysroot, iprefix, cxx_stdinc);\r
380 \r
381   target_c_incpath.extra_includes (sysroot, iprefix, stdinc);\r
382 \r
383   merge_include_chains (pfile, verbose);\r
384 \r
385   cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],\r
386                           quote_ignores_source_dir);\r
387 }\r
388 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)\r
389 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED,\r
390                                            const char *iprefix ATTRIBUTE_UNUSED,\r
391                                            int stdinc ATTRIBUTE_UNUSED)\r
392 {\r
393 }\r
394 #endif\r
395 \r
396 #ifndef TARGET_EXTRA_INCLUDES\r
397 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int\r
398 #endif\r
399 #ifndef TARGET_EXTRA_PRE_INCLUDES\r
400 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int\r
401 #endif\r
402 \r
403 struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES };\r
404 \r