2010-07-05 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libcpp / system.h
blob2472799c4b23e47eefdd56420f91153bc4d65710
1 /* Get common system includes and various definitions and declarations based
2 on autoconf macros.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #ifndef LIBCPP_SYSTEM_H
24 #define LIBCPP_SYSTEM_H
26 /* We must include stdarg.h before stdio.h. */
27 #include <stdarg.h>
29 #ifdef HAVE_STDDEF_H
30 # include <stddef.h>
31 #endif
33 #include <stdio.h>
35 /* Define a generic NULL if one hasn't already been defined. */
36 #ifndef NULL
37 #define NULL 0
38 #endif
40 /* Use the unlocked open routines from libiberty. */
41 #define fopen(PATH,MODE) fopen_unlocked(PATH,MODE)
42 #define fdopen(FILDES,MODE) fdopen_unlocked(FILDES,MODE)
43 #define freopen(PATH,MODE,STREAM) freopen_unlocked(PATH,MODE,STREAM)
45 /* The compiler is not a multi-threaded application and therefore we
46 do not have to use the locking functions. In fact, using the locking
47 functions can cause the compiler to be significantly slower under
48 I/O bound conditions (such as -g -O0 on very large source files).
50 HAVE_DECL_PUTC_UNLOCKED actually indicates whether or not the stdio
51 code is multi-thread safe by default. If it is set to 0, then do
52 not worry about using the _unlocked functions.
54 fputs_unlocked, fwrite_unlocked, and fprintf_unlocked are
55 extensions and need to be prototyped by hand (since we do not
56 define _GNU_SOURCE). */
58 #if defined HAVE_DECL_PUTC_UNLOCKED && HAVE_DECL_PUTC_UNLOCKED
60 # ifdef HAVE_PUTC_UNLOCKED
61 # undef putc
62 # define putc(C, Stream) putc_unlocked (C, Stream)
63 # endif
64 # ifdef HAVE_PUTCHAR_UNLOCKED
65 # undef putchar
66 # define putchar(C) putchar_unlocked (C)
67 # endif
68 # ifdef HAVE_GETC_UNLOCKED
69 # undef getc
70 # define getc(Stream) getc_unlocked (Stream)
71 # endif
72 # ifdef HAVE_GETCHAR_UNLOCKED
73 # undef getchar
74 # define getchar() getchar_unlocked ()
75 # endif
76 # ifdef HAVE_FPUTC_UNLOCKED
77 # undef fputc
78 # define fputc(C, Stream) fputc_unlocked (C, Stream)
79 # endif
81 # ifdef HAVE_CLEARERR_UNLOCKED
82 # undef clearerr
83 # define clearerr(Stream) clearerr_unlocked (Stream)
84 # if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
85 extern void clearerr_unlocked (FILE *);
86 # endif
87 # endif
88 # ifdef HAVE_FEOF_UNLOCKED
89 # undef feof
90 # define feof(Stream) feof_unlocked (Stream)
91 # if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
92 extern int feof_unlocked (FILE *);
93 # endif
94 # endif
95 # ifdef HAVE_FILENO_UNLOCKED
96 # undef fileno
97 # define fileno(Stream) fileno_unlocked (Stream)
98 # if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
99 extern int fileno_unlocked (FILE *);
100 # endif
101 # endif
102 # ifdef HAVE_FFLUSH_UNLOCKED
103 # undef fflush
104 # define fflush(Stream) fflush_unlocked (Stream)
105 # if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
106 extern int fflush_unlocked (FILE *);
107 # endif
108 # endif
109 # ifdef HAVE_FGETC_UNLOCKED
110 # undef fgetc
111 # define fgetc(Stream) fgetc_unlocked (Stream)
112 # if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
113 extern int fgetc_unlocked (FILE *);
114 # endif
115 # endif
116 # ifdef HAVE_FGETS_UNLOCKED
117 # undef fgets
118 # define fgets(S, n, Stream) fgets_unlocked (S, n, Stream)
119 # if defined (HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
120 extern char *fgets_unlocked (char *, int, FILE *);
121 # endif
122 # endif
123 # ifdef HAVE_FPUTS_UNLOCKED
124 # undef fputs
125 # define fputs(String, Stream) fputs_unlocked (String, Stream)
126 # if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
127 extern int fputs_unlocked (const char *, FILE *);
128 # endif
129 # endif
130 # ifdef HAVE_FERROR_UNLOCKED
131 # undef ferror
132 # define ferror(Stream) ferror_unlocked (Stream)
133 # if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
134 extern int ferror_unlocked (FILE *);
135 # endif
136 # endif
137 # ifdef HAVE_FREAD_UNLOCKED
138 # undef fread
139 # define fread(Ptr, Size, N, Stream) fread_unlocked (Ptr, Size, N, Stream)
140 # if defined (HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
141 extern size_t fread_unlocked (void *, size_t, size_t, FILE *);
142 # endif
143 # endif
144 # ifdef HAVE_FWRITE_UNLOCKED
145 # undef fwrite
146 # define fwrite(Ptr, Size, N, Stream) fwrite_unlocked (Ptr, Size, N, Stream)
147 # if defined (HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
148 extern size_t fwrite_unlocked (const void *, size_t, size_t, FILE *);
149 # endif
150 # endif
151 # ifdef HAVE_FPRINTF_UNLOCKED
152 # undef fprintf
153 /* We can't use a function-like macro here because we don't know if
154 we have varargs macros. */
155 # define fprintf fprintf_unlocked
156 # if defined (HAVE_DECL_FPRINTF_UNLOCKED) && !HAVE_DECL_FPRINTF_UNLOCKED
157 extern int fprintf_unlocked (FILE *, const char *, ...);
158 # endif
159 # endif
161 #endif
163 /* ??? Glibc's fwrite/fread_unlocked macros cause
164 "warning: signed and unsigned type in conditional expression". */
165 #undef fread_unlocked
166 #undef fwrite_unlocked
168 #include <sys/types.h>
169 #include <errno.h>
171 #if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
172 extern int errno;
173 #endif
175 /* Some of glibc's string inlines cause warnings. Plus we'd rather
176 rely on (and therefore test) GCC's string builtins. */
177 #define __NO_STRING_INLINES
179 #ifdef STRING_WITH_STRINGS
180 # include <string.h>
181 # include <strings.h>
182 #else
183 # ifdef HAVE_STRING_H
184 # include <string.h>
185 # else
186 # ifdef HAVE_STRINGS_H
187 # include <strings.h>
188 # endif
189 # endif
190 #endif
192 #ifdef HAVE_STDLIB_H
193 # include <stdlib.h>
194 #endif
196 #ifdef HAVE_UNISTD_H
197 # include <unistd.h>
198 #endif
200 #if HAVE_LIMITS_H
201 # include <limits.h>
202 #endif
204 /* Infrastructure for defining missing _MAX and _MIN macros. Note that
205 macros defined with these cannot be used in #if. */
207 /* The extra casts work around common compiler bugs. */
208 #define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
209 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
210 It is necessary at least when t == time_t. */
211 #define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
212 ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
213 #define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
215 /* Use that infrastructure to provide a few constants. */
216 #ifndef UCHAR_MAX
217 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
218 #endif
220 #ifdef TIME_WITH_SYS_TIME
221 # include <sys/time.h>
222 # include <time.h>
223 #else
224 # if HAVE_SYS_TIME_H
225 # include <sys/time.h>
226 # else
227 # ifdef HAVE_TIME_H
228 # include <time.h>
229 # endif
230 # endif
231 #endif
233 #ifdef HAVE_FCNTL_H
234 # include <fcntl.h>
235 #else
236 # ifdef HAVE_SYS_FILE_H
237 # include <sys/file.h>
238 # endif
239 #endif
241 #ifdef HAVE_LOCALE_H
242 # include <locale.h>
243 #endif
245 #ifdef HAVE_LANGINFO_CODESET
246 # include <langinfo.h>
247 #endif
249 #ifndef HAVE_SETLOCALE
250 # define setlocale(category, locale) (locale)
251 #endif
253 #ifdef ENABLE_NLS
254 #include <libintl.h>
255 #else
256 /* Stubs. */
257 # undef dgettext
258 # define dgettext(package, msgid) (msgid)
259 #endif
261 #ifndef _
262 # define _(msgid) dgettext (PACKAGE, msgid)
263 #endif
265 #ifndef N_
266 # define N_(msgid) msgid
267 #endif
269 /* Some systems define these in, e.g., param.h. We undefine these names
270 here to avoid the warnings. We prefer to use our definitions since we
271 know they are correct. */
273 #undef MIN
274 #undef MAX
275 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
276 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
278 /* The HAVE_DECL_* macros are three-state, undefined, 0 or 1. If they
279 are defined to 0 then we must provide the relevant declaration
280 here. These checks will be in the undefined state while configure
281 is running so be careful to test "defined (HAVE_DECL_*)". */
283 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
284 extern void abort (void);
285 #endif
287 #if HAVE_SYS_STAT_H
288 # include <sys/stat.h>
289 #endif
291 /* Test if something is a normal file. */
292 #ifndef S_ISREG
293 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
294 #endif
296 /* Test if something is a directory. */
297 #ifndef S_ISDIR
298 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
299 #endif
301 /* Test if something is a character special file. */
302 #ifndef S_ISCHR
303 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
304 #endif
306 /* Test if something is a block special file. */
307 #ifndef S_ISBLK
308 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
309 #endif
311 /* Test if something is a socket. */
312 #ifndef S_ISSOCK
313 # ifdef S_IFSOCK
314 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
315 # else
316 # define S_ISSOCK(m) 0
317 # endif
318 #endif
320 /* Test if something is a FIFO. */
321 #ifndef S_ISFIFO
322 # ifdef S_IFIFO
323 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
324 # else
325 # define S_ISFIFO(m) 0
326 # endif
327 #endif
329 /* Approximate O_NOCTTY and O_BINARY. */
330 #ifndef O_NOCTTY
331 #define O_NOCTTY 0
332 #endif
333 #ifndef O_BINARY
334 # define O_BINARY 0
335 #endif
337 /* Filename handling macros. */
338 #include "filenames.h"
340 /* Get libiberty declarations. */
341 #include "libiberty.h"
342 #include "safe-ctype.h"
344 /* 1 if we have C99 designated initializers.
346 ??? C99 designated initializers are not supported by most C++
347 compilers, including G++. -- gdr, 2005-05-18 */
348 #if !defined(HAVE_DESIGNATED_INITIALIZERS)
349 #define HAVE_DESIGNATED_INITIALIZERS \
350 ((!defined(__cplusplus) && (GCC_VERSION >= 2007)) \
351 || (__STDC_VERSION__ >= 199901L))
352 #endif
354 /* Be conservative and only use enum bitfields with GCC.
355 FIXME: provide a complete autoconf test for buggy enum bitfields. */
357 #if (GCC_VERSION > 2000)
358 #define ENUM_BITFIELD(TYPE) __extension__ enum TYPE
359 #else
360 #define ENUM_BITFIELD(TYPE) unsigned int
361 #endif
363 #ifndef offsetof
364 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
365 #endif
367 /* __builtin_expect(A, B) evaluates to A, but notifies the compiler that
368 the most likely value of A is B. This feature was added at some point
369 between 2.95 and 3.0. Let's use 3.0 as the lower bound for now. */
370 #if (GCC_VERSION < 3000)
371 #define __builtin_expect(a, b) (a)
372 #endif
374 /* Provide a fake boolean type. We make no attempt to use the
375 C99 _Bool, as it may not be available in the bootstrap compiler,
376 and even if it is, it is liable to be buggy.
377 This must be after all inclusion of system headers, as some of
378 them will mess us up. */
379 #undef bool
380 #undef true
381 #undef false
382 #undef TRUE
383 #undef FALSE
385 #ifndef __cplusplus
386 #define bool unsigned char
387 #endif
388 #define true 1
389 #define false 0
391 /* Some compilers do not allow the use of unsigned char in bitfields. */
392 #define BOOL_BITFIELD unsigned int
394 /* Poison identifiers we do not want to use. */
395 #if (GCC_VERSION >= 3000)
396 #undef calloc
397 #undef strdup
398 #undef malloc
399 #undef realloc
400 #pragma GCC poison calloc strdup
401 #pragma GCC poison malloc realloc
403 /* Libiberty macros that are no longer used in GCC. */
404 #undef ANSI_PROTOTYPES
405 #undef PTR_CONST
406 #undef LONG_DOUBLE
407 #undef VPARAMS
408 #undef VA_OPEN
409 #undef VA_FIXEDARG
410 #undef VA_CLOSE
411 #undef VA_START
412 #pragma GCC poison ANSI_PROTOTYPES PTR_CONST LONG_DOUBLE VPARAMS VA_OPEN \
413 VA_FIXEDARG VA_CLOSE VA_START
415 /* Note: not all uses of the `index' token (e.g. variable names and
416 structure members) have been eliminated. */
417 #undef bcopy
418 #undef bzero
419 #undef bcmp
420 #undef rindex
421 #pragma GCC poison bcopy bzero bcmp rindex
423 #endif /* GCC >= 3.0 */
424 #endif /* ! LIBCPP_SYSTEM_H */