OSDN Git Service

* config/darwin-c.c (add_framework): Copy the directory name as it
[pf3gnuchains/gcc-fork.git] / gcc / config / darwin-c.c
1 /* Darwin support needed only by C/C++ frontends.
2    Copyright (C) 2001, 2003, 2004  Free Software Foundation, Inc.
3    Contributed by Apple Computer Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC 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 GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "c-pragma.h"
29 #include "c-tree.h"
30 #include "c-incpath.h"
31 #include "toplev.h"
32 #include "tm_p.h"
33
34 /* Pragmas.  */
35
36 #define BAD(msgid) do { warning (msgid); return; } while (0)
37
38 static bool using_frameworks = false;
39
40 /* Maintain a small stack of alignments.  This is similar to pragma
41    pack's stack, but simpler.  */
42
43 static void push_field_alignment (int);
44 static void pop_field_alignment (void);
45 static const char *find_subframework_file (const char *, const char *);
46 static void add_system_framework_path (char *);
47 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
48                                              cpp_dir **dirp);
49
50 typedef struct align_stack
51 {
52   int alignment;
53   struct align_stack * prev;
54 } align_stack;
55
56 static struct align_stack * field_align_stack = NULL;
57
58 static void
59 push_field_alignment (int bit_alignment)
60 {
61   align_stack *entry = (align_stack *) xmalloc (sizeof (align_stack));
62
63   entry->alignment = maximum_field_alignment;
64   entry->prev = field_align_stack;
65   field_align_stack = entry;
66
67   maximum_field_alignment = bit_alignment;
68 }
69
70 static void
71 pop_field_alignment (void)
72 {
73   if (field_align_stack)
74     {
75       align_stack *entry = field_align_stack;
76
77       maximum_field_alignment = entry->alignment;
78       field_align_stack = entry->prev;
79       free (entry);
80     }
81   else
82     error ("too many #pragma options align=reset");
83 }
84
85 /* Handlers for Darwin-specific pragmas.  */
86
87 void
88 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
89 {
90   /* Do nothing.  */
91 }
92
93 /* #pragma options align={mac68k|power|reset} */
94
95 void
96 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
97 {
98   const char *arg;
99   tree t, x;
100
101   if (c_lex (&t) != CPP_NAME)
102     BAD ("malformed '#pragma options', ignoring");
103   arg = IDENTIFIER_POINTER (t);
104   if (strcmp (arg, "align"))
105     BAD ("malformed '#pragma options', ignoring");
106   if (c_lex (&t) != CPP_EQ)
107     BAD ("malformed '#pragma options', ignoring");
108   if (c_lex (&t) != CPP_NAME)
109     BAD ("malformed '#pragma options', ignoring");
110
111   if (c_lex (&x) != CPP_EOF)
112     warning ("junk at end of '#pragma options'");
113
114   arg = IDENTIFIER_POINTER (t);
115   if (!strcmp (arg, "mac68k"))
116     push_field_alignment (16);
117   else if (!strcmp (arg, "power"))
118     push_field_alignment (0);
119   else if (!strcmp (arg, "reset"))
120     pop_field_alignment ();
121   else
122     warning ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
123 }
124
125 /* #pragma unused ([var {, var}*]) */
126
127 void
128 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
129 {
130   tree decl, x;
131   int tok;
132
133   if (c_lex (&x) != CPP_OPEN_PAREN)
134     BAD ("missing '(' after '#pragma unused', ignoring");
135
136   while (1)
137     {
138       tok = c_lex (&decl);
139       if (tok == CPP_NAME && decl)
140         {
141           tree local = lookup_name (decl);
142           if (local && (TREE_CODE (local) == PARM_DECL
143                         || TREE_CODE (local) == VAR_DECL))
144             TREE_USED (local) = 1;
145           tok = c_lex (&x);
146           if (tok != CPP_COMMA)
147             break;
148         }
149     }
150
151   if (tok != CPP_CLOSE_PAREN)
152     BAD ("missing ')' after '#pragma unused', ignoring");
153
154   if (c_lex (&x) != CPP_EOF)
155     warning ("junk at end of '#pragma unused'");
156 }
157
158 static struct {
159   size_t len;
160   const char *name;
161   cpp_dir* dir;
162 } *frameworks_in_use;
163 static int num_frameworks = 0;
164 static int max_frameworks = 0;
165
166
167 /* Remember which frameworks have been seen, so that we can ensure
168    that all uses of that framework come from the same framework.  DIR
169    is the place where the named framework NAME, which is of length
170    LEN, was found.  We copy the directory name from NAME, as it will be
171    freed by others.  */
172
173 static void
174 add_framework (const char *name, size_t len, cpp_dir *dir)
175 {
176   char *dir_name;
177   int i;
178   for (i = 0; i < num_frameworks; ++i)
179     {
180       if (len == frameworks_in_use[i].len
181           && strncmp (name, frameworks_in_use[i].name, len) == 0)
182         {
183           return;
184         }
185     }
186   if (i >= max_frameworks)
187     {
188       max_frameworks = i*2;
189       max_frameworks += i == 0;
190       frameworks_in_use = xrealloc (frameworks_in_use,
191                                     max_frameworks*sizeof(*frameworks_in_use));
192     }
193   dir_name = xmalloc (len + 1);
194   memcpy (dir_name, name, len);
195   dir_name[len] = '\0';
196   frameworks_in_use[num_frameworks].name = dir_name;
197   frameworks_in_use[num_frameworks].len = len;
198   frameworks_in_use[num_frameworks].dir = dir;
199   ++num_frameworks;
200 }
201
202 /* Recall if we have seen the named framework NAME, before, and where
203    we saw it.  NAME is LEN bytes long.  The return value is the place
204    where it was seen before.  */
205
206 static struct cpp_dir*
207 find_framework (const char *name, size_t len)
208 {
209   int i;
210   for (i = 0; i < num_frameworks; ++i)
211     {
212       if (len == frameworks_in_use[i].len
213           && strncmp (name, frameworks_in_use[i].name, len) == 0)
214         {
215           return frameworks_in_use[i].dir;
216         }
217     }
218   return 0;
219 }
220
221 /* There are two directories in a framework that contain header files,
222    Headers and PrivateHeaders.  We search Headers first as it is more
223    common to upgrade a header from PrivateHeaders to Headers and when
224    that is done, the old one might hang around and be out of data,
225    causing grief.  */
226
227 struct framework_header {const char * dirName; int dirNameLen; };
228 static struct framework_header framework_header_dirs[] = {
229   { "Headers", 7 },
230   { "PrivateHeaders", 14 },
231   { NULL, 0 }
232 };
233
234 /* Returns a pointer to a malloced string that contains the real pathname
235    to the file, given the base name and the name.  */
236
237 static char *
238 framework_construct_pathname (const char *fname, cpp_dir *dir)
239 {
240   char *buf;
241   size_t fname_len, frname_len;
242   cpp_dir *fast_dir;
243   char *frname;
244   struct stat st;
245   int i;
246
247   /* Framework names must have a / in them.  */
248   buf = strchr (fname, '/');
249   if (buf)
250     fname_len = buf - fname;
251   else
252     return 0;
253
254   fast_dir = find_framework (fname, fname_len);
255
256   /* Framework includes must all come from one framework.  */
257   if (fast_dir && dir != fast_dir)
258     return 0;
259
260   frname = xmalloc (strlen (fname) + dir->len + 2
261                     + strlen(".framework/") + strlen("PrivateHeaders"));
262   strncpy (&frname[0], dir->name, dir->len);
263   frname_len = dir->len;
264   if (frname_len && frname[frname_len-1] != '/')
265     frname[frname_len++] = '/';
266   strncpy (&frname[frname_len], fname, fname_len);
267   frname_len += fname_len;
268   strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
269   frname_len += strlen (".framework/");
270
271   /* Append framework_header_dirs and header file name */
272   for (i = 0; framework_header_dirs[i].dirName; i++)
273     {
274       strncpy (&frname[frname_len], 
275                framework_header_dirs[i].dirName,
276                framework_header_dirs[i].dirNameLen);
277       strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
278               &fname[fname_len]);
279
280       if (stat (frname, &st) == 0)
281         {
282           if (fast_dir == 0)
283             add_framework (fname, fname_len, dir);
284           return frname;
285         }
286     }
287
288   free (frname);
289   return 0;
290 }
291
292 /* Search for FNAME in sub-frameworks.  pname is the context that we
293    wish to search in.  Return the path the file was found at,
294    otherwise return 0.  */
295
296 static const char*
297 find_subframework_file (const char *fname, const char *pname)
298 {
299   char *sfrname;
300   const char *dot_framework = ".framework/";
301   char *bufptr; 
302   int sfrname_len, i, fname_len; 
303   struct cpp_dir *fast_dir;
304   static struct cpp_dir subframe_dir;
305   struct stat st;
306
307   bufptr = strchr (fname, '/');
308
309   /* Subframework files must have / in the name.  */
310   if (bufptr == 0)
311     return 0;
312     
313   fname_len = bufptr - fname;
314   fast_dir = find_framework (fname, fname_len);
315
316   /* Sub framework header filename includes parent framework name and
317      header name in the "CarbonCore/OSUtils.h" form. If it does not
318      include slash it is not a sub framework include.  */
319   bufptr = strstr (pname, dot_framework);
320
321   /* If the parent header is not of any framework, then this header
322      can not be part of any subframework.  */
323   if (!bufptr)
324     return 0;
325
326   /* Now translate. For example,                  +- bufptr
327      fname = CarbonCore/OSUtils.h                 | 
328      pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
329      into
330      sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
331
332   sfrname = (char *) xmalloc (strlen (pname) + strlen (fname) + 2 +
333                               strlen ("Frameworks/") + strlen (".framework/")
334                               + strlen ("PrivateHeaders"));
335  
336   bufptr += strlen (dot_framework);
337
338   sfrname_len = bufptr - pname; 
339
340   strncpy (&sfrname[0], pname, sfrname_len);
341
342   strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
343   sfrname_len += strlen("Frameworks/");
344
345   strncpy (&sfrname[sfrname_len], fname, fname_len);
346   sfrname_len += fname_len;
347
348   strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
349   sfrname_len += strlen (".framework/");
350
351   /* Append framework_header_dirs and header file name */
352   for (i = 0; framework_header_dirs[i].dirName; i++)
353     {
354       strncpy (&sfrname[sfrname_len], 
355                framework_header_dirs[i].dirName,
356                framework_header_dirs[i].dirNameLen);
357       strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
358               &fname[fname_len]);
359     
360       if (stat (sfrname, &st) == 0)
361         {
362           if (fast_dir != &subframe_dir)
363             {
364               if (fast_dir)
365                 warning ("subframework include %s conflicts with framework include",
366                          fname);
367               else
368                 add_framework (fname, fname_len, &subframe_dir);
369             }
370
371           return sfrname;
372         }
373     }
374   free (sfrname);
375
376   return 0;
377 }
378
379 /* Add PATH to the system includes. PATH must be malloc-ed and
380    NUL-terminated.  System framework paths are C++ aware.  */
381
382 static void
383 add_system_framework_path (char *path)
384 {
385   int cxx_aware = 1;
386   cpp_dir *p;
387
388   p = xmalloc (sizeof (cpp_dir));
389   p->next = NULL;
390   p->name = path;
391   p->sysp = 1 + !cxx_aware;
392   p->construct = framework_construct_pathname;
393   using_frameworks = 1;
394
395   add_cpp_dir_path (p, SYSTEM);
396 }
397
398 /* Add PATH to the bracket includes. PATH must be malloc-ed and
399    NUL-terminated.  */
400
401 void
402 add_framework_path (char *path)
403 {
404   cpp_dir *p;
405
406   p = xmalloc (sizeof (cpp_dir));
407   p->next = NULL;
408   p->name = path;
409   p->sysp = 0;
410   p->construct = framework_construct_pathname;
411   using_frameworks = 1;
412
413   add_cpp_dir_path (p, BRACKET);
414 }
415
416 static const char *framework_defaults [] = 
417   {
418     "/System/Library/Frameworks",
419     "/Library/Frameworks",
420     "/Local/Library/Frameworks",
421   };
422
423
424 /* Register all the system framework paths if STDINC is true and setup
425    the missing_header callback for subframework searching if any
426    frameworks had been registered.  */
427
428 void
429 darwin_register_frameworks (int stdinc)
430 {
431   if (stdinc)
432     {
433       size_t i;
434
435       /* Setup default search path for frameworks.  */
436       for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
437         {
438           /* System Framework headers are cxx aware.  */
439           add_system_framework_path (xstrdup (framework_defaults[i]));
440         }
441     }
442
443   if (using_frameworks)
444     cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
445 }
446
447 /* Search for HEADER in context dependent way.  The return value is
448    the malloced name of a header to try and open, if any, or NULL
449    otherwise.  This is called after normal header lookup processing
450    fails to find a header.  We search each file in the include stack,
451    using FUNC, starting from the most deeply nested include and
452    finishing with the main input file.  We stop searching when FUNC
453    returns non-zero.  */
454
455 static const char*
456 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
457 {
458   const char *fname = header;
459   struct cpp_buffer *b;
460   const char *n;
461
462   for (b = cpp_get_buffer (pfile);
463        b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
464        b = cpp_get_prev (b))
465     {
466       n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
467       if (n)
468         {
469           /* Logically, the place where we found the subframework is
470              the place where we found the Framework that contains the
471              subframework.  This is useful for tracking wether or not
472              we are in a system header.  */
473           *dirp = cpp_get_dir (cpp_get_file (b));
474           return n;
475         }
476     }
477
478   return 0;
479 }
480
481 struct target_c_incpath_s target_c_incpath = C_INCPATH_INIT;