]>
Commit | Line | Data |
---|---|---|
1eb687d0 | 1 | /* Check system header files for ISO 9899:1990 (ISO C) compliance. |
d614a753 | 2 | Copyright (C) 1996-2020 Free Software Foundation, Inc. |
1eb687d0 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Jens Schweikhardt <schweikh@noc.dfn.de>, 1996. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
1eb687d0 | 10 | |
41bdb6e2 AJ |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
1eb687d0 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
1eb687d0 UD |
19 | |
20 | /* This is a simple minded program that tries to find illegal macro | |
21 | definitions in system header files. Illegal macro definitions are | |
22 | those not from the implementation namespace (i.e. not starting with | |
23 | an underscore) or not matching any identifier mandated by The | |
24 | Standard. Some common macro names are considered okay, e.g. all those | |
25 | beginning with E (which may be defined in <errno.h>) or ending in | |
26 | _MAX. See the arrays prefix[] and suffix[] below for details. | |
27 | ||
28 | In a compliant implementation no other macros can be defined, because | |
29 | you could write strictly conforming programs that may fail to compile | |
30 | due to syntax errors: suppose <stdio.h> defines PIPE_BUF, then the | |
31 | conforming | |
32 | ||
33 | #include <assert.h> | |
34 | #include <stdio.h> <- or where the bogus macro is defined | |
35 | #include <string.h> | |
36 | #define STR(x) #x | |
37 | #define XSTR(x) STR(x) | |
38 | int main (void) | |
39 | { | |
40 | int PIPE_BUF = 0; | |
41 | assert (strcmp ("PIPE_BUF", XSTR (PIPE_BUF)) == 0); | |
42 | return 0; | |
43 | } | |
44 | ||
45 | is expected to compile and meet the assertion. If it does not, your | |
46 | compiler compiles some other language than Standard C. | |
47 | ||
48 | REQUIREMENTS: | |
49 | This program calls gcc to get the list of defined macros. If you | |
50 | don't have gcc you're probably out of luck unless your compiler or | |
51 | preprocessor has something similar to gcc's -dM option. Tune | |
52 | PRINT_MACROS in this case. This program assumes headers are found | |
53 | under /usr/include and that there is a writable /tmp directory. | |
c891b2df | 54 | Tune SYSTEM_INCLUDE if your system differs. |
1eb687d0 UD |
55 | #define BROKEN_SYSTEM if system(NULL) bombs -- one more violation |
56 | of ISO C, by the way. | |
57 | ||
58 | OUTPUT: | |
59 | Each header file name is printed, followed by illegal macro names | |
60 | and their definition. For the above example, you would see | |
61 | ... | |
62 | /usr/include/stdio.h | |
63 | #define PIPE_BUF 5120 | |
64 | ... | |
65 | If your implementation does not yet incorporate Amendment 1 you | |
66 | will see messages about iso646.h, wctype.h and wchar.h not being | |
67 | found. */ | |
68 | ||
69 | #ifndef _GNU_SOURCE | |
70 | # define _GNU_SOURCE 1 | |
71 | #endif | |
72 | ||
283c8736 | 73 | #include <ctype.h> |
1eb687d0 UD |
74 | #include <stdio.h> |
75 | #include <stdlib.h> | |
76 | #include <string.h> | |
11f382ee | 77 | #include <unistd.h> |
1eb687d0 | 78 | |
1eb687d0 UD |
79 | #define HEADER_MAX 256 |
80 | ||
5c112f1b | 81 | static char macrofile[] = "/tmp/isomac.XXXXXX"; |
c891b2df | 82 | |
1eb687d0 UD |
83 | /* ISO C header names including Amendment 1 (without ".h" suffix). */ |
84 | static char *header[] = | |
85 | { | |
86 | "assert", "ctype", "errno", "float", "iso646", "limits", "locale", | |
87 | "math", "setjmp", "signal", "stdarg", "stddef", "stdio", "stdlib", | |
88 | "string", "time", "wchar", "wctype" | |
89 | }; | |
90 | ||
91 | /* Macros with these prefixes are considered okay. */ | |
92 | static char *prefix[] = | |
93 | { | |
94 | "_", "E", "is", "str", "mem", "SIG", "FLT_", "DBL_", "LDBL_", | |
95 | "LC_", "wmem", "wcs" | |
96 | }; | |
97 | ||
98 | /* Macros with these suffixes are considered okay. Will not work for | |
99 | parametrized macros with arguments. */ | |
100 | static char *suffix[] = | |
101 | { | |
102 | "_MAX", "_MIN" | |
103 | }; | |
104 | ||
105 | /* These macros are considered okay. In fact, these are just more prefixes. */ | |
106 | static char *macros[] = | |
107 | { | |
108 | "BUFSIZ", "CHAR_BIT", "CHAR_MAX", "CHAR_MIN", "CLOCKS_PER_SEC", | |
109 | "DBL_DIG", "DBL_EPSILON", "DBL_MANT_DIG", "DBL_MAX", | |
110 | "DBL_MAX_10_EXP", "DBL_MAX_EXP", "DBL_MIN", "DBL_MIN_10_EXP", | |
111 | "DBL_MIN_EXP", "EDOM", "EILSEQ", "EOF", "ERANGE", "EXIT_FAILURE", | |
112 | "EXIT_SUCCESS", "FILENAME_MAX", "FLT_DIG", "FLT_EPSILON", | |
113 | "FLT_MANT_DIG", "FLT_MAX", "FLT_MAX_10_EXP", "FLT_MAX_EXP", | |
114 | "FLT_MIN", "FLT_MIN_10_EXP", "FLT_MIN_EXP", "FLT_RADIX", | |
115 | "FLT_ROUNDS", "FOPEN_MAX", "HUGE_VAL", "INT_MAX", "INT_MIN", | |
116 | "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC", | |
117 | "LC_TIME", "LDBL_DIG", "LDBL_EPSILON", "LDBL_MANT_DIG", "LDBL_MAX", | |
118 | "LDBL_MAX_10_EXP", "LDBL_MAX_EXP", "LDBL_MIN", "LDBL_MIN_10_EXP", | |
119 | "LDBL_MIN_EXP", "LONG_MAX", "LONG_MIN", "L_tmpnam", "MB_CUR_MAX", | |
120 | "MB_LEN_MAX", "NDEBUG", "NULL", "RAND_MAX", "SCHAR_MAX", | |
121 | "SCHAR_MIN", "SEEK_CUR", "SEEK_END", "SEEK_SET", "SHRT_MAX", | |
122 | "SHRT_MIN", "SIGABRT", "SIGFPE", "SIGILL", "SIGINT", "SIGSEGV", | |
123 | "SIGTERM", "SIG_DFL", "SIG_ERR", "SIG_IGN", "TMP_MAX", "UCHAR_MAX", | |
124 | "UINT_MAX", "ULONG_MAX", "USHRT_MAX", "WCHAR_MAX", "WCHAR_MIN", | |
125 | "WEOF", "_IOFBF", "_IOLBF", "_IONBF", "abort", "abs", "acos", | |
126 | "acosf", "acosl", "and", "and_eq", "asctime", "asin", "asinf", | |
127 | "asinl", "assert", "atan", "atan2", "atan2f", "atan2l", "atanf", | |
128 | "atanl", "atexit", "atof", "atoi", "atol", "bitand", "bitor", | |
129 | "bsearch", "btowc", "calloc", "ceil", "ceilf", "ceill", "clearerr", | |
130 | "clock", "clock_t", "compl", "cos", "cosf", "cosh", "coshf", | |
131 | "coshl", "cosl", "ctime", "difftime", "div", "div_t", "errno", | |
132 | "exit", "exp", "expf", "expl", "fabs", "fabsf", "fabsl", "fclose", | |
133 | "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets", "fgetwc", | |
134 | "fgetws", "floor", "floorf", "floorl", "fmod", "fmodf", "fmodl", | |
135 | "fopen", "fprintf", "fputc", "fputs", "fputwc", "fputws", "fread", | |
136 | "free", "freopen", "frexp", "frexpf", "frexpl", "fscanf", "fseek", | |
137 | "fsetpos", "ftell", "fwide", "fwprintf", "fwrite", "fwscanf", | |
138 | "getc", "getchar", "getenv", "gets", "getwc", "getwchar", "gmtime", | |
139 | "isalnum", "isalpha", "iscntrl", "isdigit", "isgraph", "islower", | |
140 | "isprint", "ispunct", "isspace", "isupper", "iswalnum", "iswalpha", | |
141 | "iswcntrl", "iswctype", "iswdigit", "iswgraph", "iswlower", | |
142 | "iswprint", "iswpunct", "iswspace", "iswupper", "iswxdigit", | |
143 | "isxdigit", "labs", "ldexp", "ldexpf", "ldexpl", "ldiv", "ldiv_t", | |
144 | "localeconv", "localtime", "log", "log10", "log10f", "log10l", | |
145 | "logf", "logl", "longjmp", "malloc", "mblen", "mbrlen", "mbrtowc", | |
146 | "mbsinit", "mbsrtowcs", "mbstate_t", "mbstowcs", "mbtowc", "memchr", | |
147 | "memcmp", "memcpy", "memmove", "memset", "mktime", "modf", "modff", | |
148 | "modfl", "not", "not_eq", "offsetof", "or", "or_eq", "perror", | |
149 | "pow", "powf", "powl", "printf", "ptrdiff_t", "putc", "putchar", | |
150 | "puts", "putwc", "putwchar", "qsort", "raise", "rand", "realloc", | |
151 | "remove", "rename", "rewind", "scanf", "setbuf", "setjmp", | |
152 | "setlocale", "setvbuf", "sig_atomic_t", "signal", "sin", "sinf", | |
153 | "sinh", "sinhf", "sinhl", "sinl", "size_t", "sprintf", "sqrt", | |
154 | "sqrtf", "sqrtl", "srand", "sscanf", "stderr", "stdin", "stdout", | |
155 | "strcat", "strchr", "strcmp", "strcoll", "strcpy", "strcspn", | |
156 | "strerror", "strftime", "strlen", "strncat", "strncmp", "strncpy", | |
157 | "strpbrk", "strrchr", "strspn", "strstr", "strtod", "strtok", | |
158 | "strtol", "strtoul", "strxfrm", "swprintf", "swscanf", "system", | |
159 | "tan", "tanf", "tanh", "tanhf", "tanhl", "tanl", "time", "time_t", | |
160 | "tmpfile", "tmpnam", "tolower", "toupper", "towctrans", "towlower", | |
1d8004b2 | 161 | "towupper", "ungetc", "ungetwc", "va_arg", "va_copy", "va_end", "va_start", |
1eb687d0 UD |
162 | "vfprintf", "vfwprintf", "vprintf", "vsprintf", "vswprintf", |
163 | "vwprintf", "wchar_t", "wcrtomb", "wcscat", "wcschr", "wcscmp", | |
164 | "wcscoll", "wcscpy", "wcscspn", "wcsftime", "wcslen", "wcsncat", | |
165 | "wcsncmp", "wcsncpy", "wcspbrk", "wcsrchr", "wcsrtombs", "wcsspn", | |
166 | "wcsstr", "wcstod", "wcstok", "wcstol", "wcstombs", "wcstoul", | |
167 | "wcsxfrm", "wctob", "wctomb", "wctrans", "wctrans_t", "wctype", | |
168 | "wctype_t", "wint_t", "wmemchr", "wmemcmp", "wmemcpy", "wmemmove", | |
169 | "wmemset", "wprintf", "wscanf", "xor", "xor_eq" | |
170 | }; | |
171 | ||
172 | #define NUMBER_OF_HEADERS (sizeof header / sizeof *header) | |
173 | #define NUMBER_OF_PREFIXES (sizeof prefix / sizeof *prefix) | |
174 | #define NUMBER_OF_SUFFIXES (sizeof suffix / sizeof *suffix) | |
175 | #define NUMBER_OF_MACROS (sizeof macros / sizeof *macros) | |
176 | ||
177 | ||
178 | /* Format string to build command to invoke compiler. */ | |
179 | static const char fmt[] = "\ | |
180 | echo \"#include <%s>\" |\ | |
4f41c682 SP |
181 | %s -E -dM -ansi -pedantic %s -D_LIBC -D_ISOMAC \ |
182 | -DIN_MODULE=MODULE_extramodules -I. \ | |
4959e310 | 183 | -isystem `%s --print-prog-name=include` - 2> /dev/null > %s"; |
1eb687d0 UD |
184 | |
185 | ||
186 | /* The compiler we use (given on the command line). */ | |
187 | char *CC; | |
188 | /* The -I parameters for CC to find all headers. */ | |
189 | char *INC; | |
190 | ||
191 | static char *xstrndup (const char *, size_t); | |
192 | static const char **get_null_defines (void); | |
193 | static int check_header (const char *, const char **); | |
194 | ||
195 | int | |
196 | main (int argc, char *argv[]) | |
197 | { | |
198 | int h; | |
199 | int result = 0; | |
200 | const char **ignore_list; | |
201 | ||
202 | CC = argc > 1 ? argv[1] : "gcc"; | |
203 | INC = argc > 2 ? argv[2] : ""; | |
204 | ||
205 | if (system (NULL) == 0) | |
206 | { | |
207 | puts ("Sorry, no command processor."); | |
208 | return EXIT_FAILURE; | |
209 | } | |
210 | ||
211 | /* First get list of symbols which are defined by the compiler. */ | |
212 | ignore_list = get_null_defines (); | |
213 | ||
214 | fputs ("Tested files:\n", stdout); | |
215 | ||
216 | for (h = 0; h < NUMBER_OF_HEADERS; ++h) | |
217 | { | |
218 | char file_name[HEADER_MAX]; | |
219 | sprintf (file_name, "%s.h", header[h]); | |
220 | result |= check_header (file_name, ignore_list); | |
221 | } | |
222 | ||
5c112f1b JM |
223 | remove (macrofile); |
224 | ||
1eb687d0 UD |
225 | /* The test suite should return errors but for now this is not |
226 | practical. Give a warning and ask the user to correct the bugs. */ | |
227 | return result; | |
228 | } | |
229 | ||
230 | ||
231 | static char * | |
232 | xstrndup (const char *s, size_t n) | |
233 | { | |
234 | size_t len = n; | |
235 | char *new = malloc (len + 1); | |
236 | ||
237 | if (new == NULL) | |
238 | return NULL; | |
239 | ||
240 | new[len] = '\0'; | |
241 | return memcpy (new, s, len); | |
242 | } | |
243 | ||
244 | ||
245 | static const char ** | |
246 | get_null_defines (void) | |
247 | { | |
248 | char line[BUFSIZ], *command; | |
249 | char **result = NULL; | |
250 | size_t result_len = 0; | |
251 | size_t result_max = 0; | |
252 | FILE *input; | |
253 | int first = 1; | |
254 | ||
5c112f1b JM |
255 | int fd = mkstemp (macrofile); |
256 | if (fd == -1) | |
257 | { | |
258 | printf ("mkstemp failed: %m\n"); | |
259 | exit (1); | |
260 | } | |
261 | close (fd); | |
c891b2df | 262 | |
1eb687d0 | 263 | command = malloc (sizeof fmt + sizeof "/dev/null" + 2 * strlen (CC) |
c891b2df | 264 | + strlen (INC) + strlen (macrofile)); |
1eb687d0 UD |
265 | |
266 | if (command == NULL) | |
267 | { | |
268 | puts ("No more memory."); | |
269 | exit (1); | |
270 | } | |
271 | ||
c891b2df | 272 | sprintf (command, fmt, "/dev/null", CC, INC, CC, macrofile); |
1eb687d0 UD |
273 | |
274 | if (system (command)) | |
275 | { | |
276 | puts ("system() returned nonzero"); | |
34829bc2 | 277 | free (command); |
1eb687d0 UD |
278 | return NULL; |
279 | } | |
280 | free (command); | |
c891b2df | 281 | input = fopen (macrofile, "r"); |
1eb687d0 UD |
282 | |
283 | if (input == NULL) | |
284 | { | |
c891b2df | 285 | printf ("Could not read %s: ", macrofile); |
1eb687d0 UD |
286 | perror (NULL); |
287 | return NULL; | |
288 | } | |
289 | ||
290 | while (fgets (line, sizeof line, input) != NULL) | |
291 | { | |
292 | int i, okay = 0; | |
293 | size_t endmac; | |
294 | char *start, *end; | |
295 | if (strlen (line) < 9 || line[7] != ' ') | |
296 | { /* "#define A" */ | |
297 | printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n", | |
298 | line); | |
299 | continue; | |
300 | } | |
301 | if (line[8] == '_') | |
302 | /* It's a safe identifier. */ | |
303 | continue; | |
304 | if (result_len == result_max) | |
305 | { | |
306 | result_max += 10; | |
307 | result = realloc (result, result_max * sizeof (char **)); | |
308 | if (result == NULL) | |
309 | { | |
310 | puts ("No more memory."); | |
311 | exit (1); | |
312 | } | |
313 | } | |
314 | start = &line[8]; | |
315 | for (end = start + 1; !isspace (*end) && *end != '\0'; ++end) | |
316 | ; | |
ec6170f5 | 317 | result[result_len] = xstrndup (start, end - start); |
1eb687d0 | 318 | |
4f41c682 | 319 | if (strcmp (result[result_len], "IN_MODULE") != 0) |
1eb687d0 | 320 | { |
ec6170f5 UD |
321 | if (first) |
322 | { | |
323 | fputs ("The following identifiers will be ignored since the compiler defines them\nby default:\n", stdout); | |
324 | first = 0; | |
325 | } | |
326 | puts (result[result_len]); | |
1eb687d0 | 327 | } |
ec6170f5 | 328 | ++result_len; |
1eb687d0 UD |
329 | } |
330 | if (result_len == result_max) | |
331 | { | |
332 | result_max += 1; | |
333 | result = realloc (result, result_max * sizeof (char **)); | |
334 | if (result == NULL) | |
335 | { | |
336 | puts ("No more memory."); | |
337 | exit (1); | |
338 | } | |
339 | } | |
340 | result[result_len] = NULL; | |
341 | fclose (input); | |
1eb687d0 UD |
342 | |
343 | return (const char **) result; | |
344 | } | |
345 | ||
346 | ||
347 | static int | |
348 | check_header (const char *file_name, const char **except) | |
349 | { | |
350 | char line[BUFSIZ], *command; | |
351 | FILE *input; | |
352 | int result = 0; | |
353 | ||
354 | command = malloc (sizeof fmt + strlen (file_name) + 2 * strlen (CC) | |
c891b2df | 355 | + strlen (INC) + strlen (macrofile)); |
1eb687d0 UD |
356 | |
357 | if (command == NULL) | |
358 | { | |
359 | puts ("No more memory."); | |
360 | exit (1); | |
361 | } | |
362 | ||
363 | puts (file_name); | |
c891b2df | 364 | sprintf (command, fmt, file_name, CC, INC, CC, macrofile); |
1eb687d0 UD |
365 | |
366 | if (system (command)) | |
367 | { | |
368 | puts ("system() returned nonzero"); | |
369 | result = 1; | |
370 | } | |
371 | free (command); | |
c891b2df | 372 | input = fopen (macrofile, "r"); |
1eb687d0 UD |
373 | |
374 | if (input == NULL) | |
375 | { | |
c891b2df | 376 | printf ("Could not read %s: ", macrofile); |
1eb687d0 UD |
377 | perror (NULL); |
378 | return 1; | |
379 | } | |
380 | ||
381 | while (fgets (line, sizeof line, input) != NULL) | |
382 | { | |
383 | int i, okay = 0; | |
384 | size_t endmac; | |
385 | const char **cpp; | |
386 | if (strlen (line) < 9 || line[7] != ' ') | |
387 | { /* "#define A" */ | |
388 | printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n", | |
389 | line); | |
390 | result = 1; | |
391 | continue; | |
392 | } | |
393 | for (i = 0; i < NUMBER_OF_PREFIXES; ++i) | |
394 | { | |
395 | if (!strncmp (line+8, prefix[i], strlen (prefix[i]))) { | |
396 | ++okay; | |
397 | break; | |
398 | } | |
399 | } | |
400 | if (okay) | |
401 | continue; | |
402 | for (i = 0; i < NUMBER_OF_MACROS; ++i) | |
403 | { | |
404 | if (!strncmp (line + 8, macros[i], strlen (macros[i]))) | |
405 | { | |
406 | ++okay; | |
407 | break; | |
408 | } | |
409 | } | |
410 | if (okay) | |
411 | continue; | |
412 | /* Find next char after the macro identifier; this can be either | |
413 | a space or an open parenthesis. */ | |
414 | endmac = strcspn (line + 8, " ("); | |
415 | if (line[8+endmac] == '\0') | |
416 | { | |
417 | printf ("malformed input, expected '#define MACRO VALUE'\n" | |
418 | "got '%s'\n", line); | |
419 | result = 1; | |
420 | continue; | |
421 | } | |
422 | for (i = 0; i < NUMBER_OF_SUFFIXES; ++i) | |
423 | { | |
424 | size_t len = strlen (suffix[i]); | |
425 | if (!strncmp (line + 8 + endmac - len, suffix[i], len)) | |
426 | { | |
427 | ++okay; | |
428 | break; | |
429 | } | |
430 | } | |
431 | if (okay) | |
432 | continue; | |
433 | if (except != NULL) | |
434 | for (cpp = except; *cpp != NULL; ++cpp) | |
435 | { | |
436 | size_t len = strlen (*cpp); | |
437 | if (!strncmp (line + 8, *cpp, len) && isspace (line[8 + len])) | |
438 | { | |
439 | ++okay; | |
440 | break; | |
441 | } | |
442 | } | |
443 | if (!okay) | |
444 | { | |
445 | fputs (line, stdout); | |
446 | result = 2; | |
447 | } | |
448 | } | |
449 | fclose (input); | |
1eb687d0 UD |
450 | |
451 | return result; | |
452 | } | |
453 | ||
454 | /* EOF */ |