1/* Get common system includes and various definitions and declarations based
2 on autoconf macros.
3 Copyright (C) 1998-2023 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22#ifndef LIBCPP_SYSTEM_H
23#define LIBCPP_SYSTEM_H
24
25/* We must include stdarg.h before stdio.h. */
26#include <stdarg.h>
27
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
37
38#include <stdio.h>
39
40#ifdef __cplusplus
41#include <new>
42#endif
43
44/* Define a generic NULL if one hasn't already been defined. */
45#ifndef NULL
46#define NULL 0
47#endif
48
49/* Use the unlocked open routines from libiberty. */
50
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. */
54
55#undef fopen
56#undef freopen
57
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)
61
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).
66
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.
70
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). */
74
75#if defined HAVE_DECL_PUTC_UNLOCKED && HAVE_DECL_PUTC_UNLOCKED
76
77# ifdef HAVE_PUTC_UNLOCKED
78# undef putc
79# define putc(C, Stream) putc_unlocked (C, Stream)
80# endif
81# ifdef HAVE_PUTCHAR_UNLOCKED
82# undef putchar
83# define putchar(C) putchar_unlocked (C)
84# endif
85# ifdef HAVE_GETC_UNLOCKED
86# undef getc
87# define getc(Stream) getc_unlocked (Stream)
88# endif
89# ifdef HAVE_GETCHAR_UNLOCKED
90# undef getchar
91# define getchar() getchar_unlocked ()
92# endif
93# ifdef HAVE_FPUTC_UNLOCKED
94# undef fputc
95# define fputc(C, Stream) fputc_unlocked (C, Stream)
96# endif
97
98#ifdef __cplusplus
99extern "C" {
100#endif
101
102# ifdef HAVE_CLEARERR_UNLOCKED
103# undef clearerr
104# define clearerr(Stream) clearerr_unlocked (Stream)
105# if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
106extern void clearerr_unlocked (FILE *);
107# endif
108# endif
109# ifdef HAVE_FEOF_UNLOCKED
110# undef feof
111# define feof(Stream) feof_unlocked (Stream)
112# if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
113extern int feof_unlocked (FILE *);
114# endif
115# endif
116# ifdef HAVE_FILENO_UNLOCKED
117# undef fileno
118# define fileno(Stream) fileno_unlocked (Stream)
119# if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
120extern int fileno_unlocked (FILE *);
121# endif
122# endif
123# ifdef HAVE_FFLUSH_UNLOCKED
124# undef fflush
125# define fflush(Stream) fflush_unlocked (Stream)
126# if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
127extern int fflush_unlocked (FILE *);
128# endif
129# endif
130# ifdef HAVE_FGETC_UNLOCKED
131# undef fgetc
132# define fgetc(Stream) fgetc_unlocked (Stream)
133# if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
134extern int fgetc_unlocked (FILE *);
135# endif
136# endif
137# ifdef HAVE_FGETS_UNLOCKED
138# undef fgets
139# define fgets(S, n, Stream) fgets_unlocked (S, n, Stream)
140# if defined (HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
141extern char *fgets_unlocked (char *, int, FILE *);
142# endif
143# endif
144# ifdef HAVE_FPUTS_UNLOCKED
145# undef fputs
146# define fputs(String, Stream) fputs_unlocked (String, Stream)
147# if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
148extern int fputs_unlocked (const char *, FILE *);
149# endif
150# endif
151# ifdef HAVE_FERROR_UNLOCKED
152# undef ferror
153# define ferror(Stream) ferror_unlocked (Stream)
154# if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
155extern int ferror_unlocked (FILE *);
156# endif
157# endif
158# ifdef HAVE_FREAD_UNLOCKED
159# undef fread
160# define fread(Ptr, Size, N, Stream) fread_unlocked (Ptr, Size, N, Stream)
161# if defined (HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
162extern size_t fread_unlocked (void *, size_t, size_t, FILE *);
163# endif
164# endif
165# ifdef HAVE_FWRITE_UNLOCKED
166# undef fwrite
167# define fwrite(Ptr, Size, N, Stream) fwrite_unlocked (Ptr, Size, N, Stream)
168# if defined (HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
169extern size_t fwrite_unlocked (const void *, size_t, size_t, FILE *);
170# endif
171# endif
172# ifdef HAVE_FPRINTF_UNLOCKED
173# undef fprintf
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
178extern int fprintf_unlocked (FILE *, const char *, ...);
179# endif
180# endif
181
182#ifdef __cplusplus
183}
184#endif
185
186#endif
187
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
192
193#include <sys/types.h>
194#include <errno.h>
195
196#if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
197extern int errno;
198#endif
199
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
203
204#ifdef STRING_WITH_STRINGS
205# include <string.h>
206# include <strings.h>
207#else
208# ifdef HAVE_STRING_H
209# include <string.h>
210# else
211# ifdef HAVE_STRINGS_H
212# include <strings.h>
213# endif
214# endif
215#endif
216
217#ifdef HAVE_STDLIB_H
218# include <stdlib.h>
219#endif
220
221#ifdef HAVE_UNISTD_H
222# include <unistd.h>
223#endif
224
225#if HAVE_LIMITS_H
226# include <limits.h>
227#endif
228
229/* Infrastructure for defining missing _MAX and _MIN macros. Note that
230 macros defined with these cannot be used in #if. */
231
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)))
239
240/* Use that infrastructure to provide a few constants. */
241#ifndef UCHAR_MAX
242# define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
243#endif
244
245#ifdef TIME_WITH_SYS_TIME
246# include <sys/time.h>
247# include <time.h>
248#else
249# if HAVE_SYS_TIME_H
250# include <sys/time.h>
251# else
252# ifdef HAVE_TIME_H
253# include <time.h>
254# endif
255# endif
256#endif
257
258#ifdef HAVE_FCNTL_H
259# include <fcntl.h>
260#else
261# ifdef HAVE_SYS_FILE_H
262# include <sys/file.h>
263# endif
264#endif
265
266#ifdef HAVE_LOCALE_H
267# include <locale.h>
268#endif
269
270#ifdef HAVE_LANGINFO_CODESET
271# include <langinfo.h>
272#endif
273
274#ifndef HAVE_SETLOCALE
275# define setlocale(category, locale) (locale)
276#endif
277
278#ifdef ENABLE_NLS
279#include <libintl.h>
280#else
281/* Stubs. */
282# undef dgettext
283# define dgettext(package, msgid) (msgid)
284#endif
285
286#ifndef _
287# define _(msgid) dgettext (PACKAGE, msgid)
288#endif
289
290#ifndef N_
291# define N_(msgid) msgid
292#endif
293
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. */
297
298#undef MIN
299#undef MAX
300#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
301#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
302
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_*)". */
307
308#ifdef __cplusplus
309extern "C" {
310#endif
311
312#if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
313extern void abort (void);
314#endif
315
316#ifdef __cplusplus
317}
318#endif
319
320#if HAVE_SYS_STAT_H
321# include <sys/stat.h>
322#endif
323
324/* Test if something is a normal file. */
325#ifndef S_ISREG
326#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
327#endif
328
329/* Test if something is a directory. */
330#ifndef S_ISDIR
331#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
332#endif
333
334/* Test if something is a character special file. */
335#ifndef S_ISCHR
336#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
337#endif
338
339/* Test if something is a block special file. */
340#ifndef S_ISBLK
341#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
342#endif
343
344/* Test if something is a socket. */
345#ifndef S_ISSOCK
346# ifdef S_IFSOCK
347# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
348# else
349# define S_ISSOCK(m) 0
350# endif
351#endif
352
353/* Test if something is a FIFO. */
354#ifndef S_ISFIFO
355# ifdef S_IFIFO
356# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
357# else
358# define S_ISFIFO(m) 0
359# endif
360#endif
361
362/* Approximate O_NOCTTY and O_BINARY. */
363#ifndef O_NOCTTY
364#define O_NOCTTY 0
365#endif
366#ifndef O_BINARY
367# define O_BINARY 0
368#endif
369
370/* Filename handling macros. */
371#include "filenames.h"
372
373/* Get libiberty declarations. */
374#include "libiberty.h"
375#include "safe-ctype.h"
376
377/* 1 if we have C99 designated initializers.
378
379 ??? C99 designated initializers are not supported by most C++
380 compilers, including G++. -- gdr, 2005-05-18 */
381#if !defined(HAVE_DESIGNATED_INITIALIZERS)
382#ifdef __cplusplus
383#define HAVE_DESIGNATED_INITIALIZERS 0
384#else
385#define HAVE_DESIGNATED_INITIALIZERS \
386 ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
387#endif
388#endif
389
390#ifndef offsetof
391#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
392#endif
393
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)
399#endif
400
401/* Redefine abort to report an internal error w/o coredump, and
402 reporting the location of the error in the source file. */
403extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
404#define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
405
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))
413#else
414/* Include EXPR, so that unused variable warnings do not occur. */
415#define gcc_assert(EXPR) ((void)(0 && (EXPR)))
416#endif
417
418#if CHECKING_P
419#define gcc_checking_assert(EXPR) gcc_assert (EXPR)
420#else
421/* N.B.: in release build EXPR is not evaluated. */
422#define gcc_checking_assert(EXPR) ((void)(0 && (EXPR)))
423#endif
424
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__]]
430# else
431# define ATTR_LIKELY
432# endif
433#else
434# define ATTR_LIKELY
435#endif
436
437/* Poison identifiers we do not want to use. */
438#if (GCC_VERSION >= 3000)
439#undef calloc
440#undef strdup
441#undef malloc
442#undef realloc
443 #pragma GCC poison calloc strdup
444 #pragma GCC poison malloc realloc
445
446/* Libiberty macros that are no longer used in GCC. */
447#undef ANSI_PROTOTYPES
448#undef PTR_CONST
449#undef LONG_DOUBLE
450#undef VPARAMS
451#undef VA_OPEN
452#undef VA_FIXEDARG
453#undef VA_CLOSE
454#undef VA_START
455 #pragma GCC poison ANSI_PROTOTYPES PTR_CONST LONG_DOUBLE VPARAMS VA_OPEN \
456 VA_FIXEDARG VA_CLOSE VA_START
457
458/* Note: not all uses of the `index' token (e.g. variable names and
459 structure members) have been eliminated. */
460#undef bcopy
461#undef bzero
462#undef bcmp
463#undef rindex
464 #pragma GCC poison bcopy bzero bcmp rindex
465
466#endif /* GCC >= 3.0 */
467#endif /* ! LIBCPP_SYSTEM_H */
468

source code of libcpp/system.h