* gnatvsn.ads: Bump copyright year.
[official-gcc.git] / libcpp / system.h
blob719435df949a149f0662be3e8634dd80a5576099
1 /* Get common system includes and various definitions and declarations based
2 on autoconf macros.
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
10 version.
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
15 for more details.
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. */
26 #include <stdarg.h>
28 #ifdef HAVE_STDDEF_H
29 # include <stddef.h>
30 #endif
31 #ifdef HAVE_STDINT_H
32 # include <stdint.h>
33 #endif
34 #ifdef HAVE_INTTYPES_H
35 # include <inttypes.h>
36 #endif
38 #include <stdio.h>
40 /* Define a generic NULL if one hasn't already been defined. */
41 #ifndef NULL
42 #define NULL 0
43 #endif
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. */
51 #undef fopen
52 #undef freopen
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
74 # undef putc
75 # define putc(C, Stream) putc_unlocked (C, Stream)
76 # endif
77 # ifdef HAVE_PUTCHAR_UNLOCKED
78 # undef putchar
79 # define putchar(C) putchar_unlocked (C)
80 # endif
81 # ifdef HAVE_GETC_UNLOCKED
82 # undef getc
83 # define getc(Stream) getc_unlocked (Stream)
84 # endif
85 # ifdef HAVE_GETCHAR_UNLOCKED
86 # undef getchar
87 # define getchar() getchar_unlocked ()
88 # endif
89 # ifdef HAVE_FPUTC_UNLOCKED
90 # undef fputc
91 # define fputc(C, Stream) fputc_unlocked (C, Stream)
92 # endif
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
98 # ifdef HAVE_CLEARERR_UNLOCKED
99 # undef clearerr
100 # define clearerr(Stream) clearerr_unlocked (Stream)
101 # if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
102 extern void clearerr_unlocked (FILE *);
103 # endif
104 # endif
105 # ifdef HAVE_FEOF_UNLOCKED
106 # undef feof
107 # define feof(Stream) feof_unlocked (Stream)
108 # if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
109 extern int feof_unlocked (FILE *);
110 # endif
111 # endif
112 # ifdef HAVE_FILENO_UNLOCKED
113 # undef fileno
114 # define fileno(Stream) fileno_unlocked (Stream)
115 # if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
116 extern int fileno_unlocked (FILE *);
117 # endif
118 # endif
119 # ifdef HAVE_FFLUSH_UNLOCKED
120 # undef fflush
121 # define fflush(Stream) fflush_unlocked (Stream)
122 # if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
123 extern int fflush_unlocked (FILE *);
124 # endif
125 # endif
126 # ifdef HAVE_FGETC_UNLOCKED
127 # undef fgetc
128 # define fgetc(Stream) fgetc_unlocked (Stream)
129 # if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
130 extern int fgetc_unlocked (FILE *);
131 # endif
132 # endif
133 # ifdef HAVE_FGETS_UNLOCKED
134 # undef fgets
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 *);
138 # endif
139 # endif
140 # ifdef HAVE_FPUTS_UNLOCKED
141 # undef fputs
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 *);
145 # endif
146 # endif
147 # ifdef HAVE_FERROR_UNLOCKED
148 # undef ferror
149 # define ferror(Stream) ferror_unlocked (Stream)
150 # if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
151 extern int ferror_unlocked (FILE *);
152 # endif
153 # endif
154 # ifdef HAVE_FREAD_UNLOCKED
155 # undef fread
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 *);
159 # endif
160 # endif
161 # ifdef HAVE_FWRITE_UNLOCKED
162 # undef fwrite
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 *);
166 # endif
167 # endif
168 # ifdef HAVE_FPRINTF_UNLOCKED
169 # undef fprintf
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 *, ...);
175 # endif
176 # endif
178 #ifdef __cplusplus
180 #endif
182 #endif
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>
190 #include <errno.h>
192 #if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
193 extern int errno;
194 #endif
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
201 # include <string.h>
202 # include <strings.h>
203 #else
204 # ifdef HAVE_STRING_H
205 # include <string.h>
206 # else
207 # ifdef HAVE_STRINGS_H
208 # include <strings.h>
209 # endif
210 # endif
211 #endif
213 #ifdef HAVE_STDLIB_H
214 # include <stdlib.h>
215 #endif
217 #ifdef HAVE_UNISTD_H
218 # include <unistd.h>
219 #endif
221 #if HAVE_LIMITS_H
222 # include <limits.h>
223 #endif
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. */
237 #ifndef UCHAR_MAX
238 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
239 #endif
241 #ifdef TIME_WITH_SYS_TIME
242 # include <sys/time.h>
243 # include <time.h>
244 #else
245 # if HAVE_SYS_TIME_H
246 # include <sys/time.h>
247 # else
248 # ifdef HAVE_TIME_H
249 # include <time.h>
250 # endif
251 # endif
252 #endif
254 #ifdef HAVE_FCNTL_H
255 # include <fcntl.h>
256 #else
257 # ifdef HAVE_SYS_FILE_H
258 # include <sys/file.h>
259 # endif
260 #endif
262 #ifdef HAVE_LOCALE_H
263 # include <locale.h>
264 #endif
266 #ifdef HAVE_LANGINFO_CODESET
267 # include <langinfo.h>
268 #endif
270 #ifndef HAVE_SETLOCALE
271 # define setlocale(category, locale) (locale)
272 #endif
274 #ifdef ENABLE_NLS
275 #include <libintl.h>
276 #else
277 /* Stubs. */
278 # undef dgettext
279 # define dgettext(package, msgid) (msgid)
280 #endif
282 #ifndef _
283 # define _(msgid) dgettext (PACKAGE, msgid)
284 #endif
286 #ifndef N_
287 # define N_(msgid) msgid
288 #endif
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. */
294 #undef MIN
295 #undef MAX
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_*)". */
304 #ifdef __cplusplus
305 extern "C" {
306 #endif
308 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
309 extern void abort (void);
310 #endif
312 #ifdef __cplusplus
314 #endif
316 #if HAVE_SYS_STAT_H
317 # include <sys/stat.h>
318 #endif
320 /* Test if something is a normal file. */
321 #ifndef S_ISREG
322 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
323 #endif
325 /* Test if something is a directory. */
326 #ifndef S_ISDIR
327 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
328 #endif
330 /* Test if something is a character special file. */
331 #ifndef S_ISCHR
332 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
333 #endif
335 /* Test if something is a block special file. */
336 #ifndef S_ISBLK
337 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
338 #endif
340 /* Test if something is a socket. */
341 #ifndef S_ISSOCK
342 # ifdef S_IFSOCK
343 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
344 # else
345 # define S_ISSOCK(m) 0
346 # endif
347 #endif
349 /* Test if something is a FIFO. */
350 #ifndef S_ISFIFO
351 # ifdef S_IFIFO
352 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
353 # else
354 # define S_ISFIFO(m) 0
355 # endif
356 #endif
358 /* Approximate O_NOCTTY and O_BINARY. */
359 #ifndef O_NOCTTY
360 #define O_NOCTTY 0
361 #endif
362 #ifndef O_BINARY
363 # define O_BINARY 0
364 #endif
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)
378 #ifdef __cplusplus
379 #define HAVE_DESIGNATED_INITIALIZERS 0
380 #else
381 #define HAVE_DESIGNATED_INITIALIZERS \
382 ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
383 #endif
384 #endif
386 #ifndef offsetof
387 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
388 #endif
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)
395 #endif
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))
409 #else
410 /* Include EXPR, so that unused variable warnings do not occur. */
411 #define gcc_assert(EXPR) ((void)(0 && (EXPR)))
412 #endif
414 #if CHECKING_P
415 #define gcc_checking_assert(EXPR) gcc_assert (EXPR)
416 #else
417 /* N.B.: in release build EXPR is not evaluated. */
418 #define gcc_checking_assert(EXPR) ((void)(0 && (EXPR)))
419 #endif
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. */
426 #undef bool
427 #undef true
428 #undef false
429 #undef TRUE
430 #undef FALSE
432 #ifndef __cplusplus
433 #define bool unsigned char
434 #endif
435 #define true 1
436 #define false 0
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)
443 #undef calloc
444 #undef strdup
445 #undef malloc
446 #undef realloc
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
452 #undef PTR_CONST
453 #undef LONG_DOUBLE
454 #undef VPARAMS
455 #undef VA_OPEN
456 #undef VA_FIXEDARG
457 #undef VA_CLOSE
458 #undef VA_START
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. */
464 #undef bcopy
465 #undef bzero
466 #undef bcmp
467 #undef rindex
468 #pragma GCC poison bcopy bzero bcmp rindex
470 #endif /* GCC >= 3.0 */
471 #endif /* ! LIBCPP_SYSTEM_H */