system.h 11 KB

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