]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/darwin-c.c
Don't mark statements modified when we are in ipa mode
[thirdparty/gcc.git] / gcc / config / darwin-c.c
CommitLineData
0168a849 1/* Darwin support needed only by C/C++ frontends.
2f83c7d6 2 Copyright (C) 2001, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
0168a849
SS
3 Contributed by Apple Computer Inc.
4
7ec022b2 5This file is part of GCC.
0168a849 6
7ec022b2 7GCC is free software; you can redistribute it and/or modify
0168a849 8it under the terms of the GNU General Public License as published by
2f83c7d6 9the Free Software Foundation; either version 3, or (at your option)
0168a849
SS
10any later version.
11
7ec022b2 12GCC is distributed in the hope that it will be useful,
0168a849
SS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
2f83c7d6
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
0168a849
SS
20
21#include "config.h"
22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
0168a849
SS
25#include "cpplib.h"
26#include "tree.h"
27#include "c-pragma.h"
0168a849 28#include "c-tree.h"
e8a25ca8 29#include "incpath.h"
10e6657a 30#include "c-common.h"
0168a849 31#include "toplev.h"
43b3a5b1 32#include "flags.h"
0168a849 33#include "tm_p.h"
3dd53121
AP
34#include "cppdefault.h"
35#include "prefix.h"
1f1d5130
MS
36#include "target.h"
37#include "target-def.h"
0168a849
SS
38
39/* Pragmas. */
40
76ad5c10
MS
41#define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
42#define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
0168a849 43
94d1613b
MS
44static bool using_frameworks = false;
45
e3c287c9
MS
46static const char *find_subframework_header (cpp_reader *pfile, const char *header,
47 cpp_dir **dirp);
0168a849
SS
48
49typedef struct align_stack
50{
51 int alignment;
52 struct align_stack * prev;
53} align_stack;
54
55static struct align_stack * field_align_stack = NULL;
56
1f1d5130
MS
57/* Maintain a small stack of alignments. This is similar to pragma
58 pack's stack, but simpler. */
59
0168a849 60static void
9c808aad 61push_field_alignment (int bit_alignment)
0168a849 62{
5ed6ace5 63 align_stack *entry = XNEW (align_stack);
0168a849
SS
64
65 entry->alignment = maximum_field_alignment;
66 entry->prev = field_align_stack;
67 field_align_stack = entry;
68
69 maximum_field_alignment = bit_alignment;
70}
71
72static void
9c808aad 73pop_field_alignment (void)
0168a849
SS
74{
75 if (field_align_stack)
76 {
77 align_stack *entry = field_align_stack;
78
79 maximum_field_alignment = entry->alignment;
80 field_align_stack = entry->prev;
81 free (entry);
82 }
83 else
84 error ("too many #pragma options align=reset");
85}
86
87/* Handlers for Darwin-specific pragmas. */
88
89void
9c808aad 90darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
0168a849
SS
91{
92 /* Do nothing. */
93}
94
95/* #pragma options align={mac68k|power|reset} */
96
97void
9c808aad 98darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
0168a849 99{
7ae8cf75 100 const char *arg;
0168a849
SS
101 tree t, x;
102
75ce3d48 103 if (pragma_lex (&t) != CPP_NAME)
0168a849
SS
104 BAD ("malformed '#pragma options', ignoring");
105 arg = IDENTIFIER_POINTER (t);
106 if (strcmp (arg, "align"))
107 BAD ("malformed '#pragma options', ignoring");
75ce3d48 108 if (pragma_lex (&t) != CPP_EQ)
0168a849 109 BAD ("malformed '#pragma options', ignoring");
75ce3d48 110 if (pragma_lex (&t) != CPP_NAME)
0168a849
SS
111 BAD ("malformed '#pragma options', ignoring");
112
75ce3d48 113 if (pragma_lex (&x) != CPP_EOF)
aea8e035 114 warning (OPT_Wpragmas, "junk at end of '#pragma options'");
0168a849
SS
115
116 arg = IDENTIFIER_POINTER (t);
117 if (!strcmp (arg, "mac68k"))
118 push_field_alignment (16);
119 else if (!strcmp (arg, "power"))
120 push_field_alignment (0);
121 else if (!strcmp (arg, "reset"))
122 pop_field_alignment ();
123 else
213af8c8 124 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
0168a849
SS
125}
126
127/* #pragma unused ([var {, var}*]) */
128
129void
9c808aad 130darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
0168a849
SS
131{
132 tree decl, x;
133 int tok;
134
75ce3d48 135 if (pragma_lex (&x) != CPP_OPEN_PAREN)
0168a849
SS
136 BAD ("missing '(' after '#pragma unused', ignoring");
137
138 while (1)
139 {
75ce3d48 140 tok = pragma_lex (&decl);
0168a849
SS
141 if (tok == CPP_NAME && decl)
142 {
10e6657a 143 tree local = lookup_name (decl);
0168a849
SS
144 if (local && (TREE_CODE (local) == PARM_DECL
145 || TREE_CODE (local) == VAR_DECL))
146 TREE_USED (local) = 1;
75ce3d48 147 tok = pragma_lex (&x);
0168a849
SS
148 if (tok != CPP_COMMA)
149 break;
150 }
151 }
152
153 if (tok != CPP_CLOSE_PAREN)
154 BAD ("missing ')' after '#pragma unused', ignoring");
155
75ce3d48 156 if (pragma_lex (&x) != CPP_EOF)
213af8c8 157 BAD ("junk at end of '#pragma unused'");
0168a849 158}
94d1613b 159
16d6f994
EC
160/* Parse the ms_struct pragma. */
161void
162darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
163{
164 const char *arg;
165 tree t;
166
167 if (pragma_lex (&t) != CPP_NAME)
168 BAD ("malformed '#pragma ms_struct', ignoring");
169 arg = IDENTIFIER_POINTER (t);
170
171 if (!strcmp (arg, "on"))
172 darwin_ms_struct = true;
173 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
174 darwin_ms_struct = false;
175 else
213af8c8 176 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
16d6f994
EC
177
178 if (pragma_lex (&t) != CPP_EOF)
213af8c8 179 BAD ("junk at end of '#pragma ms_struct'");
16d6f994
EC
180}
181
94d1613b
MS
182static struct {
183 size_t len;
184 const char *name;
185 cpp_dir* dir;
186} *frameworks_in_use;
187static int num_frameworks = 0;
188static int max_frameworks = 0;
189
190
191/* Remember which frameworks have been seen, so that we can ensure
192 that all uses of that framework come from the same framework. DIR
193 is the place where the named framework NAME, which is of length
e3c287c9
MS
194 LEN, was found. We copy the directory name from NAME, as it will be
195 freed by others. */
94d1613b
MS
196
197static void
198add_framework (const char *name, size_t len, cpp_dir *dir)
199{
e3c287c9 200 char *dir_name;
94d1613b
MS
201 int i;
202 for (i = 0; i < num_frameworks; ++i)
203 {
204 if (len == frameworks_in_use[i].len
205 && strncmp (name, frameworks_in_use[i].name, len) == 0)
206 {
207 return;
208 }
209 }
210 if (i >= max_frameworks)
211 {
212 max_frameworks = i*2;
e3c287c9 213 max_frameworks += i == 0;
94d1613b
MS
214 frameworks_in_use = xrealloc (frameworks_in_use,
215 max_frameworks*sizeof(*frameworks_in_use));
216 }
5ed6ace5 217 dir_name = XNEWVEC (char, len + 1);
e3c287c9
MS
218 memcpy (dir_name, name, len);
219 dir_name[len] = '\0';
220 frameworks_in_use[num_frameworks].name = dir_name;
94d1613b
MS
221 frameworks_in_use[num_frameworks].len = len;
222 frameworks_in_use[num_frameworks].dir = dir;
223 ++num_frameworks;
224}
225
226/* Recall if we have seen the named framework NAME, before, and where
227 we saw it. NAME is LEN bytes long. The return value is the place
228 where it was seen before. */
229
230static struct cpp_dir*
231find_framework (const char *name, size_t len)
232{
233 int i;
234 for (i = 0; i < num_frameworks; ++i)
235 {
236 if (len == frameworks_in_use[i].len
237 && strncmp (name, frameworks_in_use[i].name, len) == 0)
238 {
239 return frameworks_in_use[i].dir;
240 }
241 }
242 return 0;
243}
244
245/* There are two directories in a framework that contain header files,
246 Headers and PrivateHeaders. We search Headers first as it is more
247 common to upgrade a header from PrivateHeaders to Headers and when
248 that is done, the old one might hang around and be out of data,
249 causing grief. */
250
251struct framework_header {const char * dirName; int dirNameLen; };
252static struct framework_header framework_header_dirs[] = {
253 { "Headers", 7 },
254 { "PrivateHeaders", 14 },
255 { NULL, 0 }
256};
257
258/* Returns a pointer to a malloced string that contains the real pathname
259 to the file, given the base name and the name. */
260
261static char *
262framework_construct_pathname (const char *fname, cpp_dir *dir)
263{
264 char *buf;
265 size_t fname_len, frname_len;
266 cpp_dir *fast_dir;
267 char *frname;
268 struct stat st;
269 int i;
270
271 /* Framework names must have a / in them. */
272 buf = strchr (fname, '/');
273 if (buf)
274 fname_len = buf - fname;
275 else
276 return 0;
277
278 fast_dir = find_framework (fname, fname_len);
279
280 /* Framework includes must all come from one framework. */
281 if (fast_dir && dir != fast_dir)
282 return 0;
283
5ed6ace5 284 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
94d1613b
MS
285 + strlen(".framework/") + strlen("PrivateHeaders"));
286 strncpy (&frname[0], dir->name, dir->len);
287 frname_len = dir->len;
288 if (frname_len && frname[frname_len-1] != '/')
289 frname[frname_len++] = '/';
290 strncpy (&frname[frname_len], fname, fname_len);
291 frname_len += fname_len;
292 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
293 frname_len += strlen (".framework/");
294
a68bdb0b
MS
295 if (fast_dir == 0)
296 {
297 frname[frname_len-1] = 0;
298 if (stat (frname, &st) == 0)
299 {
300 /* As soon as we find the first instance of the framework,
301 we stop and never use any later instance of that
302 framework. */
303 add_framework (fname, fname_len, dir);
304 }
305 else
306 {
307 /* If we can't find the parent directory, no point looking
308 further. */
309 free (frname);
310 return 0;
311 }
312 frname[frname_len-1] = '/';
313 }
314
94d1613b
MS
315 /* Append framework_header_dirs and header file name */
316 for (i = 0; framework_header_dirs[i].dirName; i++)
317 {
16d6f994 318 strncpy (&frname[frname_len],
94d1613b
MS
319 framework_header_dirs[i].dirName,
320 framework_header_dirs[i].dirNameLen);
321 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
322 &fname[fname_len]);
323
324 if (stat (frname, &st) == 0)
a68bdb0b 325 return frname;
94d1613b
MS
326 }
327
328 free (frname);
329 return 0;
330}
331
332/* Search for FNAME in sub-frameworks. pname is the context that we
333 wish to search in. Return the path the file was found at,
334 otherwise return 0. */
335
336static const char*
337find_subframework_file (const char *fname, const char *pname)
338{
339 char *sfrname;
340 const char *dot_framework = ".framework/";
16d6f994
EC
341 char *bufptr;
342 int sfrname_len, i, fname_len;
94d1613b
MS
343 struct cpp_dir *fast_dir;
344 static struct cpp_dir subframe_dir;
345 struct stat st;
346
347 bufptr = strchr (fname, '/');
348
349 /* Subframework files must have / in the name. */
350 if (bufptr == 0)
351 return 0;
16d6f994 352
94d1613b
MS
353 fname_len = bufptr - fname;
354 fast_dir = find_framework (fname, fname_len);
355
356 /* Sub framework header filename includes parent framework name and
357 header name in the "CarbonCore/OSUtils.h" form. If it does not
358 include slash it is not a sub framework include. */
359 bufptr = strstr (pname, dot_framework);
360
361 /* If the parent header is not of any framework, then this header
1e5f1716 362 cannot be part of any subframework. */
94d1613b
MS
363 if (!bufptr)
364 return 0;
365
366 /* Now translate. For example, +- bufptr
16d6f994 367 fname = CarbonCore/OSUtils.h |
94d1613b
MS
368 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
369 into
370 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
371
5ed6ace5 372 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
94d1613b
MS
373 strlen ("Frameworks/") + strlen (".framework/")
374 + strlen ("PrivateHeaders"));
16d6f994 375
94d1613b
MS
376 bufptr += strlen (dot_framework);
377
16d6f994 378 sfrname_len = bufptr - pname;
94d1613b
MS
379
380 strncpy (&sfrname[0], pname, sfrname_len);
381
382 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
383 sfrname_len += strlen("Frameworks/");
384
385 strncpy (&sfrname[sfrname_len], fname, fname_len);
386 sfrname_len += fname_len;
387
388 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
389 sfrname_len += strlen (".framework/");
390
391 /* Append framework_header_dirs and header file name */
392 for (i = 0; framework_header_dirs[i].dirName; i++)
393 {
16d6f994 394 strncpy (&sfrname[sfrname_len],
94d1613b
MS
395 framework_header_dirs[i].dirName,
396 framework_header_dirs[i].dirNameLen);
397 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
398 &fname[fname_len]);
16d6f994 399
94d1613b
MS
400 if (stat (sfrname, &st) == 0)
401 {
402 if (fast_dir != &subframe_dir)
403 {
404 if (fast_dir)
d4ee4d25 405 warning (0, "subframework include %s conflicts with framework include",
94d1613b
MS
406 fname);
407 else
408 add_framework (fname, fname_len, &subframe_dir);
409 }
410
411 return sfrname;
412 }
413 }
414 free (sfrname);
415
416 return 0;
417}
418
419/* Add PATH to the system includes. PATH must be malloc-ed and
420 NUL-terminated. System framework paths are C++ aware. */
421
422static void
423add_system_framework_path (char *path)
424{
425 int cxx_aware = 1;
426 cpp_dir *p;
427
5ed6ace5 428 p = XNEW (cpp_dir);
94d1613b
MS
429 p->next = NULL;
430 p->name = path;
431 p->sysp = 1 + !cxx_aware;
432 p->construct = framework_construct_pathname;
433 using_frameworks = 1;
434
435 add_cpp_dir_path (p, SYSTEM);
436}
437
438/* Add PATH to the bracket includes. PATH must be malloc-ed and
439 NUL-terminated. */
440
441void
442add_framework_path (char *path)
443{
444 cpp_dir *p;
445
5ed6ace5 446 p = XNEW (cpp_dir);
94d1613b
MS
447 p->next = NULL;
448 p->name = path;
449 p->sysp = 0;
450 p->construct = framework_construct_pathname;
451 using_frameworks = 1;
452
453 add_cpp_dir_path (p, BRACKET);
454}
455
16d6f994 456static const char *framework_defaults [] =
94d1613b
MS
457 {
458 "/System/Library/Frameworks",
459 "/Library/Frameworks",
94d1613b
MS
460 };
461
3dd53121
AP
462/* Register the GNU objective-C runtime include path if STDINC. */
463
464void
465darwin_register_objc_includes (const char *sysroot, const char *iprefix,
466 int stdinc)
467{
468 const char *fname;
469 size_t len;
470 /* We do not do anything if we do not want the standard includes. */
471 if (!stdinc)
472 return;
16d6f994 473
3dd53121 474 fname = GCC_INCLUDE_DIR "-gnu-runtime";
16d6f994 475
3dd53121
AP
476 /* Register the GNU OBJC runtime include path if we are compiling OBJC
477 with GNU-runtime. */
478
479 if (c_dialect_objc () && !flag_next_runtime)
480 {
481 char *str;
482 /* See if our directory starts with the standard prefix.
112cdef5 483 "Translate" them, i.e. replace /usr/local/lib/gcc... with
3dd53121
AP
484 IPREFIX and search them first. */
485 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
486 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
487 {
488 str = concat (iprefix, fname + len, NULL);
489 /* FIXME: wrap the headers for C++awareness. */
490 add_path (str, SYSTEM, /*c++aware=*/false, false);
491 }
16d6f994 492
3dd53121
AP
493 /* Should this directory start with the sysroot? */
494 if (sysroot)
495 str = concat (sysroot, fname, NULL);
496 else
497 str = update_path (fname, "");
16d6f994 498
3dd53121
AP
499 add_path (str, SYSTEM, /*c++aware=*/false, false);
500 }
501}
502
94d1613b
MS
503
504/* Register all the system framework paths if STDINC is true and setup
505 the missing_header callback for subframework searching if any
506 frameworks had been registered. */
507
508void
479ec1d1 509darwin_register_frameworks (const char *sysroot,
3dd53121 510 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
94d1613b
MS
511{
512 if (stdinc)
513 {
514 size_t i;
515
516 /* Setup default search path for frameworks. */
517 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
518 {
479ec1d1
DP
519 char *str;
520 if (sysroot)
521 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
522 else
523 str = xstrdup (framework_defaults[i]);
94d1613b 524 /* System Framework headers are cxx aware. */
479ec1d1 525 add_system_framework_path (str);
94d1613b
MS
526 }
527 }
528
529 if (using_frameworks)
530 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
531}
532
533/* Search for HEADER in context dependent way. The return value is
534 the malloced name of a header to try and open, if any, or NULL
535 otherwise. This is called after normal header lookup processing
536 fails to find a header. We search each file in the include stack,
537 using FUNC, starting from the most deeply nested include and
538 finishing with the main input file. We stop searching when FUNC
0e40b5f2 539 returns nonzero. */
94d1613b
MS
540
541static const char*
e3c287c9 542find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
94d1613b
MS
543{
544 const char *fname = header;
545 struct cpp_buffer *b;
546 const char *n;
547
548 for (b = cpp_get_buffer (pfile);
549 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
550 b = cpp_get_prev (b))
551 {
552 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
553 if (n)
e3c287c9
MS
554 {
555 /* Logically, the place where we found the subframework is
556 the place where we found the Framework that contains the
557 subframework. This is useful for tracking wether or not
558 we are in a system header. */
559 *dirp = cpp_get_dir (cpp_get_file (b));
560 return n;
561 }
94d1613b
MS
562 }
563
564 return 0;
565}
ed5b9f96
GK
566
567/* Return the value of darwin_macosx_version_min suitable for the
568 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
e46b55d0
GK
569 so '10.4.2' becomes 1040. The lowest digit is always zero.
570 Print a warning if the version number can't be understood. */
ed5b9f96
GK
571static const char *
572version_as_macro (void)
573{
574 static char result[] = "1000";
16d6f994 575
ed5b9f96
GK
576 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
577 goto fail;
578 if (! ISDIGIT (darwin_macosx_version_min[3]))
579 goto fail;
580 result[2] = darwin_macosx_version_min[3];
e46b55d0
GK
581 if (darwin_macosx_version_min[4] != '\0'
582 && darwin_macosx_version_min[4] != '.')
583 goto fail;
16d6f994 584
ed5b9f96 585 return result;
16d6f994 586
ed5b9f96
GK
587 fail:
588 error ("Unknown value %qs of -mmacosx-version-min",
589 darwin_macosx_version_min);
590 return "1000";
591}
592
593/* Define additional CPP flags for Darwin. */
594
595#define builtin_define(TXT) cpp_define (pfile, TXT)
596
597void
598darwin_cpp_builtins (cpp_reader *pfile)
599{
600 builtin_define ("__MACH__");
601 builtin_define ("__APPLE__");
602
603 /* __APPLE_CC__ is defined as some old Apple include files expect it
604 to be defined and won't work if it isn't. */
605 builtin_define_with_value ("__APPLE_CC__", "1", false);
606
e4cad568
GK
607 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
608 version_as_macro(), false);
ed5b9f96 609}
1f1d5130
MS
610
611/* Handle C family front-end options. */
612
613static bool
614handle_c_option (size_t code,
615 const char *arg,
616 int value ATTRIBUTE_UNUSED)
617{
618 switch (code)
619 {
620 default:
621 /* Unrecognized options that we said we'd handle turn into
622 errors if not listed here. */
623 return false;
624
625 case OPT_iframework:
626 add_system_framework_path (xstrdup (arg));
627 break;
c40ce8f3
MS
628
629 case OPT_fapple_kext:
630 ;
1f1d5130
MS
631 }
632
633 /* We recognized the option. */
634 return true;
635}
636
637#undef TARGET_HANDLE_C_OPTION
638#define TARGET_HANDLE_C_OPTION handle_c_option
639
640struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;