1 /* Get common system includes and various definitions and declarations based
3 Copyright (C) 1998-2018 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #ifndef LIBCPP_SYSTEM_H
23 #define LIBCPP_SYSTEM_H
25 /* We must include stdarg.h before stdio.h. */
34 #ifdef HAVE_INTTYPES_H
35 # include <inttypes.h>
40 /* Define a generic NULL if one hasn't already been defined. */
45 /* Use the unlocked open routines from libiberty. */
47 /* Some of these are #define on some systems, e.g. on AIX to redirect
48 the names to 64bit capable functions for LARGE_FILES support. These
49 redefs are pointless here so we can override them. */
54 #define fopen(PATH,MODE) fopen_unlocked(PATH,MODE)
55 #define fdopen(FILDES,MODE) fdopen_unlocked(FILDES,MODE)
56 #define freopen(PATH,MODE,STREAM) freopen_unlocked(PATH,MODE,STREAM)
58 /* The compiler is not a multi-threaded application and therefore we
59 do not have to use the locking functions. In fact, using the locking
60 functions can cause the compiler to be significantly slower under
61 I/O bound conditions (such as -g -O0 on very large source files).
63 HAVE_DECL_PUTC_UNLOCKED actually indicates whether or not the stdio
64 code is multi-thread safe by default. If it is set to 0, then do
65 not worry about using the _unlocked functions.
67 fputs_unlocked, fwrite_unlocked, and fprintf_unlocked are
68 extensions and need to be prototyped by hand (since we do not
69 define _GNU_SOURCE). */
71 #if defined HAVE_DECL_PUTC_UNLOCKED && HAVE_DECL_PUTC_UNLOCKED
73 # ifdef HAVE_PUTC_UNLOCKED
75 # define putc(C, Stream) putc_unlocked (C, Stream)
77 # ifdef HAVE_PUTCHAR_UNLOCKED
79 # define putchar(C) putchar_unlocked (C)
81 # ifdef HAVE_GETC_UNLOCKED
83 # define getc(Stream) getc_unlocked (Stream)
85 # ifdef HAVE_GETCHAR_UNLOCKED
87 # define getchar() getchar_unlocked ()
89 # ifdef HAVE_FPUTC_UNLOCKED
91 # define fputc(C, Stream) fputc_unlocked (C, Stream)
98 # ifdef HAVE_CLEARERR_UNLOCKED
100 # define clearerr(Stream) clearerr_unlocked (Stream)
101 # if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
102 extern void clearerr_unlocked (FILE *);
105 # ifdef HAVE_FEOF_UNLOCKED
107 # define feof(Stream) feof_unlocked (Stream)
108 # if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
109 extern int feof_unlocked (FILE *);
112 # ifdef HAVE_FILENO_UNLOCKED
114 # define fileno(Stream) fileno_unlocked (Stream)
115 # if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
116 extern int fileno_unlocked (FILE *);
119 # ifdef HAVE_FFLUSH_UNLOCKED
121 # define fflush(Stream) fflush_unlocked (Stream)
122 # if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
123 extern int fflush_unlocked (FILE *);
126 # ifdef HAVE_FGETC_UNLOCKED
128 # define fgetc(Stream) fgetc_unlocked (Stream)
129 # if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
130 extern int fgetc_unlocked (FILE *);
133 # ifdef HAVE_FGETS_UNLOCKED
135 # define fgets(S, n, Stream) fgets_unlocked (S, n, Stream)
136 # if defined (HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
137 extern char *fgets_unlocked (char *, int, FILE *);
140 # ifdef HAVE_FPUTS_UNLOCKED
142 # define fputs(String, Stream) fputs_unlocked (String, Stream)
143 # if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
144 extern int fputs_unlocked (const char *, FILE *);
147 # ifdef HAVE_FERROR_UNLOCKED
149 # define ferror(Stream) ferror_unlocked (Stream)
150 # if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
151 extern int ferror_unlocked (FILE *);
154 # ifdef HAVE_FREAD_UNLOCKED
156 # define fread(Ptr, Size, N, Stream) fread_unlocked (Ptr, Size, N, Stream)
157 # if defined (HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
158 extern size_t fread_unlocked (void *, size_t, size_t, FILE *);
161 # ifdef HAVE_FWRITE_UNLOCKED
163 # define fwrite(Ptr, Size, N, Stream) fwrite_unlocked (Ptr, Size, N, Stream)
164 # if defined (HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
165 extern size_t fwrite_unlocked (const void *, size_t, size_t, FILE *);
168 # ifdef HAVE_FPRINTF_UNLOCKED
170 /* We can't use a function-like macro here because we don't know if
171 we have varargs macros. */
172 # define fprintf fprintf_unlocked
173 # if defined (HAVE_DECL_FPRINTF_UNLOCKED) && !HAVE_DECL_FPRINTF_UNLOCKED
174 extern int fprintf_unlocked (FILE *, const char *, ...);
184 /* ??? Glibc's fwrite/fread_unlocked macros cause
185 "warning: signed and unsigned type in conditional expression". */
186 #undef fread_unlocked
187 #undef fwrite_unlocked
189 #include <sys/types.h>
192 #if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
196 /* Some of glibc's string inlines cause warnings. Plus we'd rather
197 rely on (and therefore test) GCC's string builtins. */
198 #define __NO_STRING_INLINES
200 #ifdef STRING_WITH_STRINGS
202 # include <strings.h>
204 # ifdef HAVE_STRING_H
207 # ifdef HAVE_STRINGS_H
208 # include <strings.h>
225 /* Infrastructure for defining missing _MAX and _MIN macros. Note that
226 macros defined with these cannot be used in #if. */
228 /* The extra casts work around common compiler bugs. */
229 #define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
230 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
231 It is necessary at least when t == time_t. */
232 #define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
233 ? (t) 1 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
234 #define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
236 /* Use that infrastructure to provide a few constants. */
238 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
241 #ifdef TIME_WITH_SYS_TIME
242 # include <sys/time.h>
246 # include <sys/time.h>
257 # ifdef HAVE_SYS_FILE_H
258 # include <sys/file.h>
266 #ifdef HAVE_LANGINFO_CODESET
267 # include <langinfo.h>
270 #ifndef HAVE_SETLOCALE
271 # define setlocale(category, locale) (locale)
279 # define dgettext(package, msgid) (msgid)
283 # define _(msgid) dgettext (PACKAGE, msgid)
287 # define N_(msgid) msgid
290 /* Some systems define these in, e.g., param.h. We undefine these names
291 here to avoid the warnings. We prefer to use our definitions since we
292 know they are correct. */
296 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
297 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
299 /* The HAVE_DECL_* macros are three-state, undefined, 0 or 1. If they
300 are defined to 0 then we must provide the relevant declaration
301 here. These checks will be in the undefined state while configure
302 is running so be careful to test "defined (HAVE_DECL_*)". */
308 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
309 extern void abort (void);
317 # include <sys/stat.h>
320 /* Test if something is a normal file. */
322 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
325 /* Test if something is a directory. */
327 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
330 /* Test if something is a character special file. */
332 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
335 /* Test if something is a block special file. */
337 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
340 /* Test if something is a socket. */
343 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
345 # define S_ISSOCK(m) 0
349 /* Test if something is a FIFO. */
352 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
354 # define S_ISFIFO(m) 0
358 /* Approximate O_NOCTTY and O_BINARY. */
366 /* Filename handling macros. */
367 #include "filenames.h"
369 /* Get libiberty declarations. */
370 #include "libiberty.h"
371 #include "safe-ctype.h"
373 /* 1 if we have C99 designated initializers.
375 ??? C99 designated initializers are not supported by most C++
376 compilers, including G++. -- gdr, 2005-05-18 */
377 #if !defined(HAVE_DESIGNATED_INITIALIZERS)
379 #define HAVE_DESIGNATED_INITIALIZERS 0
381 #define HAVE_DESIGNATED_INITIALIZERS \
382 ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
387 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
390 /* __builtin_expect(A, B) evaluates to A, but notifies the compiler that
391 the most likely value of A is B. This feature was added at some point
392 between 2.95 and 3.0. Let's use 3.0 as the lower bound for now. */
393 #if (GCC_VERSION < 3000)
394 #define __builtin_expect(a, b) (a)
397 /* Redefine abort to report an internal error w/o coredump, and
398 reporting the location of the error in the source file. */
399 extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN
;
400 #define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
402 /* Use gcc_assert(EXPR) to test invariants. */
403 #if ENABLE_ASSERT_CHECKING
404 #define gcc_assert(EXPR) \
405 ((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
406 #elif (GCC_VERSION >= 4005)
407 #define gcc_assert(EXPR) \
408 ((void)(__builtin_expect (!(EXPR), 0) ? __builtin_unreachable (), 0 : 0))
410 /* Include EXPR, so that unused variable warnings do not occur. */
411 #define gcc_assert(EXPR) ((void)(0 && (EXPR)))
415 #define gcc_checking_assert(EXPR) gcc_assert (EXPR)
417 /* N.B.: in release build EXPR is not evaluated. */
418 #define gcc_checking_assert(EXPR) ((void)(0 && (EXPR)))
421 /* Provide a fake boolean type. We make no attempt to use the
422 C99 _Bool, as it may not be available in the bootstrap compiler,
423 and even if it is, it is liable to be buggy.
424 This must be after all inclusion of system headers, as some of
425 them will mess us up. */
433 #define bool unsigned char
438 /* Some compilers do not allow the use of unsigned char in bitfields. */
439 #define BOOL_BITFIELD unsigned int
441 /* Poison identifiers we do not want to use. */
442 #if (GCC_VERSION >= 3000)
447 #pragma GCC poison calloc strdup
448 #pragma GCC poison malloc realloc
450 /* Libiberty macros that are no longer used in GCC. */
451 #undef ANSI_PROTOTYPES
459 #pragma GCC poison ANSI_PROTOTYPES PTR_CONST LONG_DOUBLE VPARAMS VA_OPEN \
460 VA_FIXEDARG VA_CLOSE VA_START
462 /* Note: not all uses of the `index' token (e.g. variable names and
463 structure members) have been eliminated. */
468 #pragma GCC poison bcopy bzero bcmp rindex
470 #endif /* GCC >= 3.0 */
471 #endif /* ! LIBCPP_SYSTEM_H */