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.
5 This file is part of GCC.
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)
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.
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. */
24 #include "coretypes.h"
30 #include "c-incpath.h"
36 #define BAD(msgid) do { warning (msgid); return; } while (0)
38 static bool using_frameworks
= false;
40 /* Maintain a small stack of alignments. This is similar to pragma
41 pack's stack, but simpler. */
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
,
50 typedef struct align_stack
53 struct align_stack
* prev
;
56 static struct align_stack
* field_align_stack
= NULL
;
59 push_field_alignment (int bit_alignment
)
61 align_stack
*entry
= (align_stack
*) xmalloc (sizeof (align_stack
));
63 entry
->alignment
= maximum_field_alignment
;
64 entry
->prev
= field_align_stack
;
65 field_align_stack
= entry
;
67 maximum_field_alignment
= bit_alignment
;
71 pop_field_alignment (void)
73 if (field_align_stack
)
75 align_stack
*entry
= field_align_stack
;
77 maximum_field_alignment
= entry
->alignment
;
78 field_align_stack
= entry
->prev
;
82 error ("too many #pragma options align=reset");
85 /* Handlers for Darwin-specific pragmas. */
88 darwin_pragma_ignore (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
93 /* #pragma options align={mac68k|power|reset} */
96 darwin_pragma_options (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
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");
111 if (c_lex (&x
) != CPP_EOF
)
112 warning ("junk at end of '#pragma options'");
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 ();
122 warning ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
125 /* #pragma unused ([var {, var}*]) */
128 darwin_pragma_unused (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
133 if (c_lex (&x
) != CPP_OPEN_PAREN
)
134 BAD ("missing '(' after '#pragma unused', ignoring");
139 if (tok
== CPP_NAME
&& decl
)
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;
146 if (tok
!= CPP_COMMA
)
151 if (tok
!= CPP_CLOSE_PAREN
)
152 BAD ("missing ')' after '#pragma unused', ignoring");
154 if (c_lex (&x
) != CPP_EOF
)
155 warning ("junk at end of '#pragma unused'");
162 } *frameworks_in_use
;
163 static int num_frameworks
= 0;
164 static int max_frameworks
= 0;
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
174 add_framework (const char *name
, size_t len
, cpp_dir
*dir
)
178 for (i
= 0; i
< num_frameworks
; ++i
)
180 if (len
== frameworks_in_use
[i
].len
181 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
186 if (i
>= max_frameworks
)
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
));
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
;
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. */
206 static struct cpp_dir
*
207 find_framework (const char *name
, size_t len
)
210 for (i
= 0; i
< num_frameworks
; ++i
)
212 if (len
== frameworks_in_use
[i
].len
213 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
215 return frameworks_in_use
[i
].dir
;
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,
227 struct framework_header
{const char * dirName
; int dirNameLen
; };
228 static struct framework_header framework_header_dirs
[] = {
230 { "PrivateHeaders", 14 },
234 /* Returns a pointer to a malloced string that contains the real pathname
235 to the file, given the base name and the name. */
238 framework_construct_pathname (const char *fname
, cpp_dir
*dir
)
241 size_t fname_len
, frname_len
;
247 /* Framework names must have a / in them. */
248 buf
= strchr (fname
, '/');
250 fname_len
= buf
- fname
;
254 fast_dir
= find_framework (fname
, fname_len
);
256 /* Framework includes must all come from one framework. */
257 if (fast_dir
&& dir
!= fast_dir
)
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/");
271 /* Append framework_header_dirs and header file name */
272 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
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
],
280 if (stat (frname
, &st
) == 0)
283 add_framework (fname
, fname_len
, dir
);
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. */
297 find_subframework_file (const char *fname
, const char *pname
)
300 const char *dot_framework
= ".framework/";
302 int sfrname_len
, i
, fname_len
;
303 struct cpp_dir
*fast_dir
;
304 static struct cpp_dir subframe_dir
;
307 bufptr
= strchr (fname
, '/');
309 /* Subframework files must have / in the name. */
313 fname_len
= bufptr
- fname
;
314 fast_dir
= find_framework (fname
, fname_len
);
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
);
321 /* If the parent header is not of any framework, then this header
322 can not be part of any subframework. */
326 /* Now translate. For example, +- bufptr
327 fname = CarbonCore/OSUtils.h |
328 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
330 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
332 sfrname
= (char *) xmalloc (strlen (pname
) + strlen (fname
) + 2 +
333 strlen ("Frameworks/") + strlen (".framework/")
334 + strlen ("PrivateHeaders"));
336 bufptr
+= strlen (dot_framework
);
338 sfrname_len
= bufptr
- pname
;
340 strncpy (&sfrname
[0], pname
, sfrname_len
);
342 strncpy (&sfrname
[sfrname_len
], "Frameworks/", strlen ("Frameworks/"));
343 sfrname_len
+= strlen("Frameworks/");
345 strncpy (&sfrname
[sfrname_len
], fname
, fname_len
);
346 sfrname_len
+= fname_len
;
348 strncpy (&sfrname
[sfrname_len
], ".framework/", strlen (".framework/"));
349 sfrname_len
+= strlen (".framework/");
351 /* Append framework_header_dirs and header file name */
352 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
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
],
360 if (stat (sfrname
, &st
) == 0)
362 if (fast_dir
!= &subframe_dir
)
365 warning ("subframework include %s conflicts with framework include",
368 add_framework (fname
, fname_len
, &subframe_dir
);
379 /* Add PATH to the system includes. PATH must be malloc-ed and
380 NUL-terminated. System framework paths are C++ aware. */
383 add_system_framework_path (char *path
)
388 p
= xmalloc (sizeof (cpp_dir
));
391 p
->sysp
= 1 + !cxx_aware
;
392 p
->construct
= framework_construct_pathname
;
393 using_frameworks
= 1;
395 add_cpp_dir_path (p
, SYSTEM
);
398 /* Add PATH to the bracket includes. PATH must be malloc-ed and
402 add_framework_path (char *path
)
406 p
= xmalloc (sizeof (cpp_dir
));
410 p
->construct
= framework_construct_pathname
;
411 using_frameworks
= 1;
413 add_cpp_dir_path (p
, BRACKET
);
416 static const char *framework_defaults
[] =
418 "/System/Library/Frameworks",
419 "/Library/Frameworks",
420 "/Local/Library/Frameworks",
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. */
429 darwin_register_frameworks (int stdinc
)
435 /* Setup default search path for frameworks. */
436 for (i
=0; i
<sizeof (framework_defaults
)/sizeof(const char *); ++i
)
438 /* System Framework headers are cxx aware. */
439 add_system_framework_path (xstrdup (framework_defaults
[i
]));
443 if (using_frameworks
)
444 cpp_get_callbacks (parse_in
)->missing_header
= find_subframework_header
;
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
456 find_subframework_header (cpp_reader
*pfile
, const char *header
, cpp_dir
**dirp
)
458 const char *fname
= header
;
459 struct cpp_buffer
*b
;
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
))
466 n
= find_subframework_file (fname
, cpp_get_path (cpp_get_file (b
)));
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
));
481 struct target_c_incpath_s target_c_incpath
= C_INCPATH_INIT
;