Update.
[glibc.git] / isomac.c
blobfc6de89558311ff9d93834215a576f2327aca780
1 /* Check system header files for ISO 9899:1990 (ISO C) compliance.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jens Schweikhardt <schweikh@noc.dfn.de>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License
8 as published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place -
19 Suite 330, Boston, MA 02111-1307, USA. */
21 /* This is a simple minded program that tries to find illegal macro
22 definitions in system header files. Illegal macro definitions are
23 those not from the implementation namespace (i.e. not starting with
24 an underscore) or not matching any identifier mandated by The
25 Standard. Some common macro names are considered okay, e.g. all those
26 beginning with E (which may be defined in <errno.h>) or ending in
27 _MAX. See the arrays prefix[] and suffix[] below for details.
29 In a compliant implementation no other macros can be defined, because
30 you could write strictly conforming programs that may fail to compile
31 due to syntax errors: suppose <stdio.h> defines PIPE_BUF, then the
32 conforming
34 #include <assert.h>
35 #include <stdio.h> <- or where the bogus macro is defined
36 #include <string.h>
37 #define STR(x) #x
38 #define XSTR(x) STR(x)
39 int main (void)
41 int PIPE_BUF = 0;
42 assert (strcmp ("PIPE_BUF", XSTR (PIPE_BUF)) == 0);
43 return 0;
46 is expected to compile and meet the assertion. If it does not, your
47 compiler compiles some other language than Standard C.
49 REQUIREMENTS:
50 This program calls gcc to get the list of defined macros. If you
51 don't have gcc you're probably out of luck unless your compiler or
52 preprocessor has something similar to gcc's -dM option. Tune
53 PRINT_MACROS in this case. This program assumes headers are found
54 under /usr/include and that there is a writable /tmp directory.
55 Tune SYSTEM_INCLUDE and TMPFILE if your system differs.
56 #define BROKEN_SYSTEM if system(NULL) bombs -- one more violation
57 of ISO C, by the way.
59 OUTPUT:
60 Each header file name is printed, followed by illegal macro names
61 and their definition. For the above example, you would see
62 ...
63 /usr/include/stdio.h
64 #define PIPE_BUF 5120
65 ...
66 If your implementation does not yet incorporate Amendment 1 you
67 will see messages about iso646.h, wctype.h and wchar.h not being
68 found. */
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
74 #define TMPFILE "/tmp/macros"
75 #define HEADER_MAX 256
77 /* ISO C header names including Amendment 1 (without ".h" suffix). */
78 static char *header[] =
80 "assert", "ctype", "errno", "float", "iso646", "limits", "locale",
81 "math", "setjmp", "signal", "stdarg", "stddef", "stdio", "stdlib",
82 "string", "time", "wchar", "wctype"
85 /* Macros with these prefixes are considered okay. */
86 static char *prefix[] =
88 "_", "E", "is", "str", "mem", "SIG", "FLT_", "DBL_", "LDBL_",
89 "LC_", "wmem", "wcs"
92 /* Macros with these suffixes are considered okay. Will not work for
93 parametrized macros with arguments. */
94 static char *suffix[] =
96 "_MAX", "_MIN"
99 /* These macros are considered okay. In fact, these are just more prefixes. */
100 static char *macros[] =
102 "BUFSIZ", "CHAR_BIT", "CHAR_MAX", "CHAR_MIN", "CLOCKS_PER_SEC",
103 "DBL_DIG", "DBL_EPSILON", "DBL_MANT_DIG", "DBL_MAX",
104 "DBL_MAX_10_EXP", "DBL_MAX_EXP", "DBL_MIN", "DBL_MIN_10_EXP",
105 "DBL_MIN_EXP", "EDOM", "EILSEQ", "EOF", "ERANGE", "EXIT_FAILURE",
106 "EXIT_SUCCESS", "FILENAME_MAX", "FLT_DIG", "FLT_EPSILON",
107 "FLT_MANT_DIG", "FLT_MAX", "FLT_MAX_10_EXP", "FLT_MAX_EXP",
108 "FLT_MIN", "FLT_MIN_10_EXP", "FLT_MIN_EXP", "FLT_RADIX",
109 "FLT_ROUNDS", "FOPEN_MAX", "HUGE_VAL", "INT_MAX", "INT_MIN",
110 "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC",
111 "LC_TIME", "LDBL_DIG", "LDBL_EPSILON", "LDBL_MANT_DIG", "LDBL_MAX",
112 "LDBL_MAX_10_EXP", "LDBL_MAX_EXP", "LDBL_MIN", "LDBL_MIN_10_EXP",
113 "LDBL_MIN_EXP", "LONG_MAX", "LONG_MIN", "L_tmpnam", "MB_CUR_MAX",
114 "MB_LEN_MAX", "NDEBUG", "NULL", "RAND_MAX", "SCHAR_MAX",
115 "SCHAR_MIN", "SEEK_CUR", "SEEK_END", "SEEK_SET", "SHRT_MAX",
116 "SHRT_MIN", "SIGABRT", "SIGFPE", "SIGILL", "SIGINT", "SIGSEGV",
117 "SIGTERM", "SIG_DFL", "SIG_ERR", "SIG_IGN", "TMP_MAX", "UCHAR_MAX",
118 "UINT_MAX", "ULONG_MAX", "USHRT_MAX", "WCHAR_MAX", "WCHAR_MIN",
119 "WEOF", "_IOFBF", "_IOLBF", "_IONBF", "abort", "abs", "acos",
120 "acosf", "acosl", "and", "and_eq", "asctime", "asin", "asinf",
121 "asinl", "assert", "atan", "atan2", "atan2f", "atan2l", "atanf",
122 "atanl", "atexit", "atof", "atoi", "atol", "bitand", "bitor",
123 "bsearch", "btowc", "calloc", "ceil", "ceilf", "ceill", "clearerr",
124 "clock", "clock_t", "compl", "cos", "cosf", "cosh", "coshf",
125 "coshl", "cosl", "ctime", "difftime", "div", "div_t", "errno",
126 "exit", "exp", "expf", "expl", "fabs", "fabsf", "fabsl", "fclose",
127 "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets", "fgetwc",
128 "fgetws", "floor", "floorf", "floorl", "fmod", "fmodf", "fmodl",
129 "fopen", "fprintf", "fputc", "fputs", "fputwc", "fputws", "fread",
130 "free", "freopen", "frexp", "frexpf", "frexpl", "fscanf", "fseek",
131 "fsetpos", "ftell", "fwide", "fwprintf", "fwrite", "fwscanf",
132 "getc", "getchar", "getenv", "gets", "getwc", "getwchar", "gmtime",
133 "isalnum", "isalpha", "iscntrl", "isdigit", "isgraph", "islower",
134 "isprint", "ispunct", "isspace", "isupper", "iswalnum", "iswalpha",
135 "iswcntrl", "iswctype", "iswdigit", "iswgraph", "iswlower",
136 "iswprint", "iswpunct", "iswspace", "iswupper", "iswxdigit",
137 "isxdigit", "labs", "ldexp", "ldexpf", "ldexpl", "ldiv", "ldiv_t",
138 "localeconv", "localtime", "log", "log10", "log10f", "log10l",
139 "logf", "logl", "longjmp", "malloc", "mblen", "mbrlen", "mbrtowc",
140 "mbsinit", "mbsrtowcs", "mbstate_t", "mbstowcs", "mbtowc", "memchr",
141 "memcmp", "memcpy", "memmove", "memset", "mktime", "modf", "modff",
142 "modfl", "not", "not_eq", "offsetof", "or", "or_eq", "perror",
143 "pow", "powf", "powl", "printf", "ptrdiff_t", "putc", "putchar",
144 "puts", "putwc", "putwchar", "qsort", "raise", "rand", "realloc",
145 "remove", "rename", "rewind", "scanf", "setbuf", "setjmp",
146 "setlocale", "setvbuf", "sig_atomic_t", "signal", "sin", "sinf",
147 "sinh", "sinhf", "sinhl", "sinl", "size_t", "sprintf", "sqrt",
148 "sqrtf", "sqrtl", "srand", "sscanf", "stderr", "stdin", "stdout",
149 "strcat", "strchr", "strcmp", "strcoll", "strcpy", "strcspn",
150 "strerror", "strftime", "strlen", "strncat", "strncmp", "strncpy",
151 "strpbrk", "strrchr", "strspn", "strstr", "strtod", "strtok",
152 "strtol", "strtoul", "strxfrm", "swprintf", "swscanf", "system",
153 "tan", "tanf", "tanh", "tanhf", "tanhl", "tanl", "time", "time_t",
154 "tmpfile", "tmpnam", "tolower", "toupper", "towctrans", "towlower",
155 "towupper", "ungetc", "ungetwc", "va_arg", "va_end", "va_start",
156 "vfprintf", "vfwprintf", "vprintf", "vsprintf", "vswprintf",
157 "vwprintf", "wchar_t", "wcrtomb", "wcscat", "wcschr", "wcscmp",
158 "wcscoll", "wcscpy", "wcscspn", "wcsftime", "wcslen", "wcsncat",
159 "wcsncmp", "wcsncpy", "wcspbrk", "wcsrchr", "wcsrtombs", "wcsspn",
160 "wcsstr", "wcstod", "wcstok", "wcstol", "wcstombs", "wcstoul",
161 "wcsxfrm", "wctob", "wctomb", "wctrans", "wctrans_t", "wctype",
162 "wctype_t", "wint_t", "wmemchr", "wmemcmp", "wmemcpy", "wmemmove",
163 "wmemset", "wprintf", "wscanf", "xor", "xor_eq"
166 #define NUMBER_OF_HEADERS (sizeof header / sizeof *header)
167 #define NUMBER_OF_PREFIXES (sizeof prefix / sizeof *prefix)
168 #define NUMBER_OF_SUFFIXES (sizeof suffix / sizeof *suffix)
169 #define NUMBER_OF_MACROS (sizeof macros / sizeof *macros)
171 /* The compiler we use (given on the command line). */
172 char *CC;
173 /* The -I parameters for CC to find all headers. */
174 char *INC;
176 static int check_header (const char *);
179 main (int argc, char *argv[])
181 int h;
182 int result = 0;
184 CC = argc > 1 ? argv[1] : "gcc";
185 INC = argc > 2 ? argv[2] : "";
187 if (system (NULL) == 0)
189 puts ("Sorry, no command processor.");
190 return EXIT_FAILURE;
193 for (h = 0; h < NUMBER_OF_HEADERS; ++h)
195 char file_name[HEADER_MAX];
196 sprintf (file_name, "%s.h", header[h]);
197 result |= check_header (file_name);
201 #if 0
202 /* The test suite should return errors but for now this is not
203 practical. Give a warning and ask the user to correct the bugs. */
204 return result;
205 #else
206 if (result)
207 fputs ("\
208 ##########################################################################\n\
209 # The test suite found some problems with your system (see the generated #\n\
210 # isomac.out file). These are all violations of the ISO C rules and #\n\
211 # should be corrected. If the problem is in the libc, report it using #\n\
212 # the glibcbug script to <bugs@gnu.ai.mit.edu>. If it is a problem with #\n\
213 # your compiler, contact the compiler manufacturer. #\n\
214 ##########################################################################\n",
215 stderr);
217 return 0;
218 #endif
221 static int
222 check_header (const char *file_name)
224 char line[BUFSIZ], *command;
225 FILE *input;
226 int result = 0;
227 static const char fmt[] = "\
228 echo \"#include <%s>\" |\
229 %s -E -dM -ansi -pedantic %s -D_LIBC -I. -I `%s --print-prog-name=include` -\
230 > %s";
232 command = malloc (sizeof fmt + strlen (file_name) + 2 * strlen (CC)
233 + strlen (INC) + strlen (TMPFILE));
235 if (command == NULL)
237 puts ("No more memory.");
238 exit (1);
241 puts (file_name);
242 sprintf (command, fmt, file_name, CC, INC, CC, TMPFILE);
244 if (system (command))
246 puts ("system() returned nonzero");
247 result = 1;
249 free (command);
250 input = fopen (TMPFILE, "r");
252 if (input == NULL)
254 printf ("Could not read %s: ", TMPFILE);
255 perror (NULL);
256 return 1;
259 while (fgets (line, sizeof line, input) != NULL)
261 int i, okay = 0;
262 size_t endmac;
263 if (strlen (line) < 9 || line[7] != ' ')
264 { /* "#define A" */
265 printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n",
266 line);
267 result = 1;
268 continue;
270 for (i = 0; i < NUMBER_OF_PREFIXES; ++i)
272 if (!strncmp (line+8, prefix[i], strlen (prefix[i]))) {
273 ++okay;
274 break;
277 if (okay)
278 continue;
279 for (i = 0; i < NUMBER_OF_MACROS; ++i)
281 if (!strncmp (line+8, macros[i], strlen (macros[i])))
283 ++okay;
284 break;
287 if (okay)
288 continue;
289 /* Find next char after the macro identifier; this can be either
290 a space or an open parenthesis. */
291 endmac = strcspn (line + 8, " (");
292 if (line[8+endmac] == '\0')
294 printf ("malformed input, expected '#define MACRO VALUE'\n"
295 "got '%s'\n", line);
296 result = 1;
297 continue;
299 for (i = 0; i < NUMBER_OF_SUFFIXES; ++i)
301 size_t len = strlen (suffix[i]);
302 if (!strncmp (line + 8 + endmac - len, suffix[i], len))
304 ++okay;
305 break;
308 if (!okay)
310 fputs (line, stdout);
311 result = 2;
314 fclose (input);
315 remove (TMPFILE);
317 return result;
320 /* EOF */