* support/scripts/gen_known_bugs.pl: cosmetic fix
[fw/sdcc] / support / cpp2 / 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, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "prefix.h"
25 #include "intl.h"
26 #include "c-incpath.h"
27 #include "cppdefault.h"
28
29 /* Windows does not natively support inodes, and neither does MSDOS.
30    Cygwin's emulation can generate non-unique inodes, so don't use it.
31    VMS has non-numeric inodes.  */
32 #ifdef VMS
33 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
34 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
35 #else
36 # if (defined _WIN32 && !defined (_UWIN)) || defined __MSDOS__
37 #  define INO_T_EQ(A, B) 0
38 # else
39 #  define INO_T_EQ(A, B) ((A) == (B))
40 # endif
41 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
42 #endif
43
44 static void add_env_var_paths (const char *, int);
45 static void add_standard_paths (const char *, const char *, int);
46 static void free_path (struct cpp_dir *, int);
47 static void merge_include_chains (cpp_reader *, int);
48 static struct cpp_dir *remove_duplicates (cpp_reader *, struct cpp_dir *,
49                                            struct cpp_dir *,
50                                            struct cpp_dir *, int);
51
52 /* Include chains heads and tails.  */
53 static struct cpp_dir *heads[4];
54 static struct cpp_dir *tails[4];
55 static bool quote_ignores_source_dir;
56 enum { REASON_QUIET = 0, REASON_NOENT, REASON_DUP, REASON_DUP_SYS };
57
58 /* Free an element of the include chain, possibly giving a reason.  */
59 static void
60 free_path (struct cpp_dir *path, int reason)
61 {
62   switch (reason)
63     {
64     case REASON_DUP:
65     case REASON_DUP_SYS:
66       fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), path->name);
67       if (reason == REASON_DUP_SYS)
68         fprintf (stderr,
69  _("  as it is a non-system directory that duplicates a system directory\n"));
70       break;
71
72     case REASON_NOENT:
73       fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"),
74                path->name);
75       break;
76
77     case REASON_QUIET:
78     default:
79       break;
80     }
81
82   free (path->name);
83   free (path);
84 }
85
86 /* Read ENV_VAR for a PATH_SEPARATOR-separated list of file names; and
87    append all the names to the search path CHAIN.  */
88 static void
89 add_env_var_paths (const char *env_var, int chain)
90 {
91   char *p, *q, *path;
92
93   GET_ENVIRONMENT (q, env_var);
94
95   if (!q)
96     return;
97
98   for (p = q; *q; p = q + 1)
99     {
100       q = p;
101       while (*q != 0 && *q != PATH_SEPARATOR)
102         q++;
103
104       if (p == q)
105         path = xstrdup (".");
106       else
107         {
108           path = xmalloc (q - p + 1);
109           memcpy (path, p, q - p);
110           path[q - p] = '\0';
111         }
112
113       add_path (path, chain, chain == SYSTEM, false);
114     }
115 }
116
117 /* Append the standard include chain defined in cppdefault.c.  */
118 static void
119 add_standard_paths (const char *sysroot, const char *iprefix, int cxx_stdinc)
120 {
121   const struct default_include *p;
122   size_t len;
123
124   if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0)
125     {
126       /* Look for directories that start with the standard prefix.
127          "Translate" them, i.e. replace /usr/local/lib/gcc... with
128          IPREFIX and search them first.  */
129       for (p = cpp_include_defaults; p->fname; p++)
130         {
131           if (!p->cplusplus || cxx_stdinc)
132             {
133               /* Should we be translating sysrooted dirs too?  Assume
134                  that iprefix and sysroot are mutually exclusive, for
135                  now.  */
136               if (sysroot && p->add_sysroot)
137                 continue;
138               if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
139                 {
140                   char *str = concat (iprefix, p->fname + len, NULL);
141                   add_path (str, SYSTEM, p->cxx_aware, false);
142                 }
143             }
144         }
145     }
146
147   for (p = cpp_include_defaults; p->fname; p++)
148     {
149       if (!p->cplusplus || cxx_stdinc)
150         {
151           char *str;
152
153           /* Should this directory start with the sysroot?  */
154           if (sysroot && p->add_sysroot)
155             str = concat (sysroot, p->fname, NULL);
156           else
157             str = update_path (p->fname, p->component);
158
159           add_path (str, SYSTEM, p->cxx_aware, false);
160         }
161     }
162 }
163
164 /* For each duplicate path in chain HEAD, keep just the first one.
165    Remove each path in chain HEAD that also exists in chain SYSTEM.
166    Set the NEXT pointer of the last path in the resulting chain to
167    JOIN, unless it duplicates JOIN in which case the last path is
168    removed.  Return the head of the resulting chain.  Any of HEAD,
169    JOIN and SYSTEM can be NULL.  */
170
171 static struct cpp_dir *
172 remove_duplicates (cpp_reader *pfile, struct cpp_dir *head,
173                    struct cpp_dir *system, struct cpp_dir *join,
174                    int verbose)
175 {
176   struct cpp_dir **pcur, *tmp, *cur;
177   struct stat st;
178
179   for (pcur = &head; *pcur; )
180     {
181       int reason = REASON_QUIET;
182
183       cur = *pcur;
184
185       if (stat (cur->name, &st))
186         {
187           /* Dirs that don't exist are silently ignored, unless verbose.  */
188           if (errno != ENOENT)
189             cpp_errno (pfile, CPP_DL_ERROR, cur->name);
190           else
191             {
192               /* If -Wmissing-include-dirs is given, warn.  */
193               cpp_options *opts = cpp_get_options (pfile);
194               if (opts->warn_missing_include_dirs && cur->user_supplied_p)
195                 cpp_errno (pfile, CPP_DL_WARNING, cur->name);
196               reason = REASON_NOENT;
197             }
198         }
199       else if (!S_ISDIR (st.st_mode))
200         cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
201                              "%s: not a directory", cur->name);
202       else
203         {
204           INO_T_COPY (cur->ino, st.st_ino);
205           cur->dev  = st.st_dev;
206
207           /* Remove this one if it is in the system chain.  */
208           reason = REASON_DUP_SYS;
209           for (tmp = system; tmp; tmp = tmp->next)
210            if (INO_T_EQ (tmp->ino, cur->ino) && tmp->dev == cur->dev
211                && cur->construct == tmp->construct)
212               break;
213
214           if (!tmp)
215             {
216               /* Duplicate of something earlier in the same chain?  */
217               reason = REASON_DUP;
218               for (tmp = head; tmp != cur; tmp = tmp->next)
219                if (INO_T_EQ (cur->ino, tmp->ino) && cur->dev == tmp->dev
220                    && cur->construct == tmp->construct)
221                   break;
222
223               if (tmp == cur
224                   /* Last in the chain and duplicate of JOIN?  */
225                   && !(cur->next == NULL && join
226                        && INO_T_EQ (cur->ino, join->ino)
227                       && cur->dev == join->dev
228                       && cur->construct == join->construct))
229                 {
230                   /* Unique, so keep this directory.  */
231                   pcur = &cur->next;
232                   continue;
233                 }
234             }
235         }
236
237       /* Remove this entry from the chain.  */
238       *pcur = cur->next;
239       free_path (cur, verbose ? reason: REASON_QUIET);
240     }
241
242   *pcur = join;
243   return head;
244 }
245
246 /* Merge the four include chains together in the order quote, bracket,
247    system, after.  Remove duplicate dirs (as determined by
248    INO_T_EQ()).
249
250    We can't just merge the lists and then uniquify them because then
251    we may lose directories from the <> search path that should be
252    there; consider -iquote foo -iquote bar -Ifoo -Iquux.  It is
253    however safe to treat -iquote bar -iquote foo -Ifoo -Iquux as if
254    written -iquote bar -Ifoo -Iquux.  */
255
256 static void
257 merge_include_chains (cpp_reader *pfile, int verbose)
258 {
259   /* Join the SYSTEM and AFTER chains.  Remove duplicates in the
260      resulting SYSTEM chain.  */
261   if (heads[SYSTEM])
262     tails[SYSTEM]->next = heads[AFTER];
263   else
264     heads[SYSTEM] = heads[AFTER];
265   heads[SYSTEM] = remove_duplicates (pfile, heads[SYSTEM], 0, 0, verbose);
266
267   /* Remove duplicates from BRACKET that are in itself or SYSTEM, and
268      join it to SYSTEM.  */
269   heads[BRACKET] = remove_duplicates (pfile, heads[BRACKET], heads[SYSTEM],
270                                       heads[SYSTEM], verbose);
271
272   /* Remove duplicates from QUOTE that are in itself or SYSTEM, and
273      join it to BRACKET.  */
274   heads[QUOTE] = remove_duplicates (pfile, heads[QUOTE], heads[SYSTEM],
275                                     heads[BRACKET], verbose);
276
277   /* If verbose, print the list of dirs to search.  */
278   if (verbose)
279     {
280       struct cpp_dir *p;
281
282       fprintf (stderr, _("#include \"...\" search starts here:\n"));
283       for (p = heads[QUOTE];; p = p->next)
284         {
285           if (p == heads[BRACKET])
286             fprintf (stderr, _("#include <...> search starts here:\n"));
287           if (!p)
288             break;
289           fprintf (stderr, " %s\n", p->name);
290         }
291       fprintf (stderr, _("End of search list.\n"));
292     }
293 }
294
295 /* Use given -I paths for #include "..." but not #include <...>, and
296    don't search the directory of the present file for #include "...".
297    (Note that -I. -I- is not the same as the default setup; -I. uses
298    the compiler's working dir.)  */
299 void
300 split_quote_chain (void)
301 {
302   heads[QUOTE] = heads[BRACKET];
303   tails[QUOTE] = tails[BRACKET];
304   heads[BRACKET] = NULL;
305   tails[BRACKET] = NULL;
306   /* This is NOT redundant.  */
307   quote_ignores_source_dir = true;
308 }
309
310 /* Add P to the chain specified by CHAIN.  */
311
312 void
313 add_cpp_dir_path (cpp_dir *p, int chain)
314 {
315   if (tails[chain])
316     tails[chain]->next = p;
317   else
318     heads[chain] = p;
319   tails[chain] = p;
320 }
321
322 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
323    NUL-terminated.  */
324 void
325 add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)
326 {
327   cpp_dir *p;
328
329 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
330   /* Convert all backslashes to slashes.  The native CRT stat()
331      function does not recognize a directory that ends in a backslash
332      (unless it is a drive root dir, such "c:\").  Forward slashes,
333      trailing or otherwise, cause no problems for stat().  */
334   char* c;
335   for (c = path; *c; c++)
336     if (*c == '\\') *c = '/';
337 #endif
338
339   p = xmalloc (sizeof (cpp_dir));
340   p->next = NULL;
341   p->name = path;
342   if (chain == SYSTEM || chain == AFTER)
343     p->sysp = 1 + !cxx_aware;
344   else
345     p->sysp = 0;
346   p->construct = 0;
347   p->user_supplied_p = user_supplied_p;
348
349   add_cpp_dir_path (p, chain);
350 }
351
352 /* Exported function to handle include chain merging, duplicate
353    removal, and registration with cpplib.  */
354 void
355 register_include_chains (cpp_reader *pfile, const char *sysroot,
356                          const char *iprefix, int stdinc, int cxx_stdinc,
357                          int verbose)
358 {
359   static const char *const lang_env_vars[] =
360     { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
361       "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
362   cpp_options *cpp_opts = cpp_get_options (pfile);
363   size_t idx = (cpp_opts->objc ? 2: 0);
364
365   if (cpp_opts->cplusplus)
366     idx++;
367   else
368     cxx_stdinc = false;
369
370   /* CPATH and language-dependent environment variables may add to the
371      include chain.  */
372   add_env_var_paths ("CPATH", BRACKET);
373   add_env_var_paths (lang_env_vars[idx], SYSTEM);
374
375   target_c_incpath.extra_pre_includes (sysroot, iprefix, stdinc);
376
377   /* Finally chain on the standard directories.  */
378   if (stdinc)
379     add_standard_paths (sysroot, iprefix, cxx_stdinc);
380
381   target_c_incpath.extra_includes (sysroot, iprefix, stdinc);
382
383   merge_include_chains (pfile, verbose);
384
385   cpp_set_include_chains (pfile, heads[QUOTE], heads[BRACKET],
386                           quote_ignores_source_dir);
387 }
388 #if !(defined TARGET_EXTRA_INCLUDES) || !(defined TARGET_EXTRA_PRE_INCLUDES)
389 static void hook_void_charptr_charptr_int (const char *sysroot ATTRIBUTE_UNUSED,
390                                            const char *iprefix ATTRIBUTE_UNUSED,
391                                            int stdinc ATTRIBUTE_UNUSED)
392 {
393 }
394 #endif
395
396 #ifndef TARGET_EXTRA_INCLUDES
397 #define TARGET_EXTRA_INCLUDES hook_void_charptr_charptr_int
398 #endif
399 #ifndef TARGET_EXTRA_PRE_INCLUDES
400 #define TARGET_EXTRA_PRE_INCLUDES hook_void_charptr_charptr_int
401 #endif
402
403 struct target_c_incpath_s target_c_incpath = { TARGET_EXTRA_PRE_INCLUDES, TARGET_EXTRA_INCLUDES };
404