math: array parameters can be NULL
[smatch.git] / predefine.c
blobd05b1018fdce46f1030e7be80c2f55b603b4ddab
1 // SPDX-License-Identifier: MIT
2 // Copyright (C) 2017-2020 Luc Van Oostenryck.
4 #include <stdio.h>
6 #include "lib.h"
7 #include "machine.h"
8 #include "symbol.h"
10 #define PTYPE_SIZEOF (1U << 0)
11 #define PTYPE_T (1U << 1)
12 #define PTYPE_MAX (1U << 2)
13 #define PTYPE_MIN (1U << 3)
14 #define PTYPE_WIDTH (1U << 4)
15 #define PTYPE_TYPE (1U << 5)
16 #define PTYPE_ALL (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH)
17 #define PTYPE_ALL_T (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH|PTYPE_T)
20 static void predefined_sizeof(const char *name, const char *suffix, unsigned bits)
22 char buf[32];
24 snprintf(buf, sizeof(buf), "__SIZEOF_%s%s__", name, suffix);
25 predefine(buf, 1, "%d", bits/8);
28 static void predefined_width(const char *name, unsigned bits)
30 char buf[32];
32 snprintf(buf, sizeof(buf), "__%s_WIDTH__", name);
33 predefine(buf, 1, "%d", bits);
36 static void predefined_max(const char *name, struct symbol *type)
38 const char *suffix = builtin_type_suffix(type);
39 unsigned bits = type->bit_size - is_signed_type(type);
40 unsigned long long max = bits_mask(bits);
41 char buf[32];
43 snprintf(buf, sizeof(buf), "__%s_MAX__", name);
44 predefine(buf, 1, "%#llx%s", max, suffix);
47 static void predefined_min(const char *name, struct symbol *type)
49 const char *suffix = builtin_type_suffix(type);
50 char buf[32];
52 snprintf(buf, sizeof(buf), "__%s_MIN__", name);
54 if (is_signed_type(type))
55 add_pre_buffer("#weak_define %s (-__%s_MAX__ - 1)\n", buf, name);
56 else
57 predefine(buf, 1, "0%s", suffix);
60 static void predefined_type(const char *name, struct symbol *type)
62 const char *typename = builtin_typename(type);
63 add_pre_buffer("#weak_define __%s_TYPE__ %s\n", name, typename);
66 static void predefined_ctype(const char *name, struct symbol *type, int flags)
68 unsigned bits = type->bit_size;
70 if (flags & PTYPE_SIZEOF) {
71 const char *suffix = (flags & PTYPE_T) ? "_T" : "";
72 predefined_sizeof(name, suffix, bits);
74 if (flags & PTYPE_MAX)
75 predefined_max(name, type);
76 if (flags & PTYPE_MIN)
77 predefined_min(name, type);
78 if (flags & PTYPE_TYPE)
79 predefined_type(name, type);
80 if (flags & PTYPE_WIDTH)
81 predefined_width(name, bits);
84 void predefined_macros(void)
86 predefine("__CHECKER__", 0, "1");
87 predefine("__GNUC__", 1, "%d", gcc_major);
88 predefine("__GNUC_MINOR__", 1, "%d", gcc_minor);
89 predefine("__GNUC_PATCHLEVEL__", 1, "%d", gcc_patchlevel);
91 predefine("__STDC__", 1, "1");
92 predefine("__STDC_HOSTED__", 0, fhosted ? "1" : "0");
93 switch (standard) {
94 default:
95 break;
97 case STANDARD_C94:
98 predefine("__STDC_VERSION__", 1, "199409L");
99 break;
101 case STANDARD_C99:
102 case STANDARD_GNU99:
103 predefine("__STDC_VERSION__", 1, "199901L");
104 break;
106 case STANDARD_C11:
107 case STANDARD_GNU11:
108 predefine("__STDC_VERSION__", 1, "201112L");
109 break;
110 case STANDARD_C17:
111 case STANDARD_GNU17:
112 predefine("__STDC_VERSION__", 1, "201710L");
113 break;
115 if (!(standard & STANDARD_GNU) && (standard != STANDARD_NONE))
116 predefine("__STRICT_ANSI__", 1, "1");
117 if (standard >= STANDARD_C11) {
118 predefine("__STDC_NO_ATOMICS__", 1, "1");
119 predefine("__STDC_NO_COMPLEX__", 1, "1");
120 predefine("__STDC_NO_THREADS__", 1, "1");
123 predefine("__CHAR_BIT__", 1, "%d", bits_in_char);
124 if (funsigned_char)
125 predefine("__CHAR_UNSIGNED__", 1, "1");
127 predefined_ctype("SHORT", &short_ctype, PTYPE_SIZEOF);
128 predefined_ctype("SHRT", &short_ctype, PTYPE_MAX|PTYPE_WIDTH);
129 predefined_ctype("SCHAR", &schar_ctype, PTYPE_MAX|PTYPE_WIDTH);
130 predefined_ctype("WCHAR", wchar_ctype, PTYPE_ALL_T|PTYPE_MIN|PTYPE_TYPE);
131 predefined_ctype("WINT", wint_ctype, PTYPE_ALL_T|PTYPE_MIN|PTYPE_TYPE);
132 predefined_ctype("CHAR16", &ushort_ctype, PTYPE_TYPE);
133 predefined_ctype("CHAR32", uint32_ctype, PTYPE_TYPE);
135 predefined_ctype("INT", &int_ctype, PTYPE_ALL);
136 predefined_ctype("LONG", &long_ctype, PTYPE_ALL);
137 predefined_ctype("LONG_LONG", &llong_ctype, PTYPE_ALL);
139 predefined_ctype("INT8", &schar_ctype, PTYPE_MAX|PTYPE_TYPE);
140 predefined_ctype("UINT8", &uchar_ctype, PTYPE_MAX|PTYPE_TYPE);
141 predefined_ctype("INT16", &short_ctype, PTYPE_MAX|PTYPE_TYPE);
142 predefined_ctype("UINT16", &ushort_ctype, PTYPE_MAX|PTYPE_TYPE);
143 predefined_ctype("INT32", int32_ctype, PTYPE_MAX|PTYPE_TYPE);
144 predefined_ctype("UINT32", uint32_ctype, PTYPE_MAX|PTYPE_TYPE);
145 predefined_ctype("INT64", int64_ctype, PTYPE_MAX|PTYPE_TYPE);
146 predefined_ctype("UINT64", uint64_ctype, PTYPE_MAX|PTYPE_TYPE);
148 predefined_ctype("INTMAX", intmax_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
149 predefined_ctype("UINTMAX", uintmax_ctype, PTYPE_MAX|PTYPE_TYPE);
150 predefined_ctype("INTPTR", ssize_t_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
151 predefined_ctype("UINTPTR", size_t_ctype, PTYPE_MAX|PTYPE_TYPE);
152 predefined_ctype("PTRDIFF", ssize_t_ctype, PTYPE_ALL_T|PTYPE_TYPE);
153 predefined_ctype("SIZE", size_t_ctype, PTYPE_ALL_T|PTYPE_TYPE);
154 predefined_ctype("POINTER", &ptr_ctype, PTYPE_SIZEOF);
156 predefined_sizeof("FLOAT", "", bits_in_float);
157 predefined_sizeof("DOUBLE", "", bits_in_double);
158 predefined_sizeof("LONG_DOUBLE", "", bits_in_longdouble);
160 if (arch_target->has_int128)
161 predefined_sizeof("INT128", "", 128);
163 predefine("__ORDER_LITTLE_ENDIAN__", 1, "1234");
164 predefine("__ORDER_BIG_ENDIAN__", 1, "4321");
165 predefine("__ORDER_PDP_ENDIAN__", 1, "3412");
166 if (arch_big_endian) {
167 predefine("__BIG_ENDIAN__", 1, "1");
168 predefine("__BYTE_ORDER__", 1, "__ORDER_BIG_ENDIAN__");
169 } else {
170 predefine("__LITTLE_ENDIAN__", 1, "1");
171 predefine("__BYTE_ORDER__", 1, "__ORDER_LITTLE_ENDIAN__");
174 if (optimize_level)
175 predefine("__OPTIMIZE__", 0, "1");
176 if (optimize_size)
177 predefine("__OPTIMIZE_SIZE__", 0, "1");
179 predefine("__PRAGMA_REDEFINE_EXTNAME", 1, "1");
181 // Temporary hacks
182 predefine("__extension__", 0, NULL);
183 predefine("__pragma__", 0, NULL);
185 switch (arch_m64) {
186 case ARCH_LP32:
187 break;
188 case ARCH_X32:
189 predefine("__ILP32__", 1, "1");
190 predefine("_ILP32", 1, "1");
191 break;
192 case ARCH_LP64:
193 predefine("__LP64__", 1, "1");
194 predefine("_LP64", 1, "1");
195 break;
196 case ARCH_LLP64:
197 predefine("__LLP64__", 1, "1");
198 break;
201 if (fpic) {
202 predefine("__pic__", 0, "%d", fpic);
203 predefine("__PIC__", 0, "%d", fpic);
205 if (fpie) {
206 predefine("__pie__", 0, "%d", fpie);
207 predefine("__PIE__", 0, "%d", fpie);
210 if (arch_target->predefine)
211 arch_target->predefine(arch_target);
213 if (arch_os >= OS_UNIX) {
214 predefine("__unix__", 1, "1");
215 predefine("__unix", 1, "1");
216 predefine_nostd("unix");
219 if (arch_os == OS_SUNOS) {
220 predefine("__sun__", 1, "1");
221 predefine("__sun", 1, "1");
222 predefine_nostd("sun");
223 predefine("__svr4__", 1, "1");