1 /* Get common system includes and various definitions and declarations based
3 Copyright (C) 1998-2024 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>
44 /* Define a generic NULL if one hasn't already been defined. */
49 /* Use the unlocked open routines from libiberty. */
51 /* Some of these are #define on some systems, e.g. on AIX to redirect
52 the names to 64bit capable functions for LARGE_FILES support. These
53 redefs are pointless here so we can override them. */
58 #define fopen(PATH,MODE) fopen_unlocked(PATH,MODE)
59 #define fdopen(FILDES,MODE) fdopen_unlocked(FILDES,MODE)
60 #define freopen(PATH,MODE,STREAM) freopen_unlocked(PATH,MODE,STREAM)
62 /* The compiler is not a multi-threaded application and therefore we
63 do not have to use the locking functions. In fact, using the locking
64 functions can cause the compiler to be significantly slower under
65 I/O bound conditions (such as -g -O0 on very large source files).
67 HAVE_DECL_PUTC_UNLOCKED actually indicates whether or not the stdio
68 code is multi-thread safe by default. If it is set to 0, then do
69 not worry about using the _unlocked functions.
71 fputs_unlocked, fwrite_unlocked, and fprintf_unlocked are
72 extensions and need to be prototyped by hand (since we do not
73 define _GNU_SOURCE). */
75 #if defined HAVE_DECL_PUTC_UNLOCKED && HAVE_DECL_PUTC_UNLOCKED
77 # ifdef HAVE_PUTC_UNLOCKED
79 # define putc(C, Stream) putc_unlocked (C, Stream)
81 # ifdef HAVE_PUTCHAR_UNLOCKED
83 # define putchar(C) putchar_unlocked (C)
85 # ifdef HAVE_GETC_UNLOCKED
87 # define getc(Stream) getc_unlocked (Stream)
89 # ifdef HAVE_GETCHAR_UNLOCKED
91 # define getchar() getchar_unlocked ()
93 # ifdef HAVE_FPUTC_UNLOCKED
95 # define fputc(C, Stream) fputc_unlocked (C, Stream)
102 # ifdef HAVE_CLEARERR_UNLOCKED
104 # define clearerr(Stream) clearerr_unlocked (Stream)
105 # if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
106 extern void clearerr_unlocked (FILE *);
109 # ifdef HAVE_FEOF_UNLOCKED
111 # define feof(Stream) feof_unlocked (Stream)
112 # if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
113 extern int feof_unlocked (FILE *);
116 # ifdef HAVE_FILENO_UNLOCKED
118 # define fileno(Stream) fileno_unlocked (Stream)
119 # if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
120 extern int fileno_unlocked (FILE *);
123 # ifdef HAVE_FFLUSH_UNLOCKED
125 # define fflush(Stream) fflush_unlocked (Stream)
126 # if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
127 extern int fflush_unlocked (FILE *);
130 # ifdef HAVE_FGETC_UNLOCKED
132 # define fgetc(Stream) fgetc_unlocked (Stream)
133 # if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
134 extern int fgetc_unlocked (FILE *);
137 # ifdef HAVE_FGETS_UNLOCKED
139 # define fgets(S, n, Stream) fgets_unlocked (S, n, Stream)
140 # if defined (HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
141 extern char *fgets_unlocked (char *, int, FILE *);
144 # ifdef HAVE_FPUTS_UNLOCKED
146 # define fputs(String, Stream) fputs_unlocked (String, Stream)
147 # if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
148 extern int fputs_unlocked (const char *, FILE *);
151 # ifdef HAVE_FERROR_UNLOCKED
153 # define ferror(Stream) ferror_unlocked (Stream)
154 # if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
155 extern int ferror_unlocked (FILE *);
158 # ifdef HAVE_FREAD_UNLOCKED
160 # define fread(Ptr, Size, N, Stream) fread_unlocked (Ptr, Size, N, Stream)
161 # if defined (HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
162 extern size_t fread_unlocked (void *, size_t, size_t, FILE *);
165 # ifdef HAVE_FWRITE_UNLOCKED
167 # define fwrite(Ptr, Size, N, Stream) fwrite_unlocked (Ptr, Size, N, Stream)
168 # if defined (HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
169 extern size_t fwrite_unlocked (const void *, size_t, size_t, FILE *);
172 # ifdef HAVE_FPRINTF_UNLOCKED
174 /* We can't use a function-like macro here because we don't know if
175 we have varargs macros. */
176 # define fprintf fprintf_unlocked
177 # if defined (HAVE_DECL_FPRINTF_UNLOCKED) && !HAVE_DECL_FPRINTF_UNLOCKED
178 extern int fprintf_unlocked (FILE *, const char *, ...);
188 /* ??? Glibc's fwrite/fread_unlocked macros cause
189 "warning: signed and unsigned type in conditional expression". */
190 #undef fread_unlocked
191 #undef fwrite_unlocked
193 #include <sys/types.h>
196 #if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
200 /* Some of glibc's string inlines cause warnings. Plus we'd rather
201 rely on (and therefore test) GCC's string builtins. */
202 #define __NO_STRING_INLINES
204 #ifdef STRING_WITH_STRINGS
206 # include <strings.h>
208 # ifdef HAVE_STRING_H
211 # ifdef HAVE_STRINGS_H
212 # include <strings.h>
229 /* Infrastructure for defining missing _MAX and _MIN macros. Note that
230 macros defined with these cannot be used in #if. */
232 /* The extra casts work around common compiler bugs. */
233 #define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
234 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
235 It is necessary at least when t == time_t. */
236 #define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
237 ? (t) 1 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
238 #define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
240 /* Use that infrastructure to provide a few constants. */
242 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
245 #ifdef TIME_WITH_SYS_TIME
246 # include <sys/time.h>
250 # include <sys/time.h>
261 # ifdef HAVE_SYS_FILE_H
262 # include <sys/file.h>
270 #ifdef HAVE_LANGINFO_CODESET
271 # include <langinfo.h>
274 #ifndef HAVE_SETLOCALE
275 # define setlocale(category, locale) (locale)
283 # define dgettext(package, msgid) (msgid)
287 # define _(msgid) dgettext (PACKAGE, msgid)
291 # define N_(msgid) msgid
294 /* Some systems define these in, e.g., param.h. We undefine these names
295 here to avoid the warnings. We prefer to use our definitions since we
296 know they are correct. */
300 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
301 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
303 /* The HAVE_DECL_* macros are three-state, undefined, 0 or 1. If they
304 are defined to 0 then we must provide the relevant declaration
305 here. These checks will be in the undefined state while configure
306 is running so be careful to test "defined (HAVE_DECL_*)". */
312 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
313 extern void abort (void);
321 # include <sys/stat.h>
324 /* Test if something is a normal file. */
326 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
329 /* Test if something is a directory. */
331 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
334 /* Test if something is a character special file. */
336 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
339 /* Test if something is a block special file. */
341 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
344 /* Test if something is a socket. */
347 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
349 # define S_ISSOCK(m) 0
353 /* Test if something is a FIFO. */
356 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
358 # define S_ISFIFO(m) 0
362 /* Approximate O_NOCTTY and O_BINARY. */
370 /* Filename handling macros. */
371 #include "filenames.h"
373 /* Get libiberty declarations. */
374 #include "libiberty.h"
375 #include "safe-ctype.h"
377 /* 1 if we have C99 designated initializers.
379 ??? C99 designated initializers are not supported by most C++
380 compilers, including G++. -- gdr, 2005-05-18 */
381 #if !defined(HAVE_DESIGNATED_INITIALIZERS)
383 #define HAVE_DESIGNATED_INITIALIZERS 0
385 #define HAVE_DESIGNATED_INITIALIZERS \
386 ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
391 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
394 /* __builtin_expect(A, B) evaluates to A, but notifies the compiler that
395 the most likely value of A is B. This feature was added at some point
396 between 2.95 and 3.0. Let's use 3.0 as the lower bound for now. */
397 #if (GCC_VERSION < 3000)
398 #define __builtin_expect(a, b) (a)
401 /* Redefine abort to report an internal error w/o coredump, and
402 reporting the location of the error in the source file. */
403 extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN
;
404 #define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
406 /* Use gcc_assert(EXPR) to test invariants. */
407 #if ENABLE_ASSERT_CHECKING
408 #define gcc_assert(EXPR) \
409 ((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
410 #elif (GCC_VERSION >= 4005)
411 #define gcc_assert(EXPR) \
412 ((void)(__builtin_expect (!(EXPR), 0) ? __builtin_unreachable (), 0 : 0))
414 /* Include EXPR, so that unused variable warnings do not occur. */
415 #define gcc_assert(EXPR) ((void)(0 && (EXPR)))
419 #define gcc_checking_assert(EXPR) gcc_assert (EXPR)
421 /* N.B.: in release build EXPR is not evaluated. */
422 #define gcc_checking_assert(EXPR) ((void)(0 && (EXPR)))
425 #ifdef __has_cpp_attribute
426 # if __has_cpp_attribute(likely)
427 # define ATTR_LIKELY [[likely]]
428 # elif __has_cpp_attribute(__likely__)
429 # define ATTR_LIKELY [[__likely__]]
437 /* Poison identifiers we do not want to use. */
438 #if (GCC_VERSION >= 3000)
443 #pragma GCC poison calloc strdup
444 #pragma GCC poison malloc realloc
446 /* Libiberty macros that are no longer used in GCC. */
447 #undef ANSI_PROTOTYPES
455 #pragma GCC poison ANSI_PROTOTYPES PTR_CONST LONG_DOUBLE VPARAMS VA_OPEN \
456 VA_FIXEDARG VA_CLOSE VA_START
458 /* Note: not all uses of the `index' token (e.g. variable names and
459 structure members) have been eliminated. */
464 #pragma GCC poison bcopy bzero bcmp rindex
466 #endif /* GCC >= 3.0 */
467 #endif /* ! LIBCPP_SYSTEM_H */