| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
|
| 6 |
name_map = { |
| 7 |
'uint8_t': 'uint8', |
| 8 |
|
| 9 |
'int16_t': 'int16', |
| 10 |
'uint16_t': 'uint16', |
| 11 |
|
| 12 |
'uint32_t': 'uint32', |
| 13 |
|
| 14 |
'gid_t': 'gid', |
| 15 |
'uid_t': 'uid', |
| 16 |
|
| 17 |
'size_t': 'size', |
| 18 |
'ssize_t': 'ssize', |
| 19 |
|
| 20 |
'int': 'int', |
| 21 |
'unsigned int': 'uint', |
| 22 |
|
| 23 |
'long': 'long', |
| 24 |
'unsigned long': 'ulong', |
| 25 |
|
| 26 |
'long long': 'longlong', |
| 27 |
'unsigned long long': 'ulonglong', |
| 28 |
} |
| 29 |
|
| 30 |
def is_unsigned(t): |
| 31 |
if t[0] == 'u': |
| 32 |
return True |
| 33 |
|
| 34 |
|
| 35 |
if t in ('size_t', 'gid_t', 'uid_t'): |
| 36 |
return True |
| 37 |
return False |
| 38 |
|
| 39 |
def is_signed(t): |
| 40 |
if t.startswith('int'): |
| 41 |
return True |
| 42 |
if t.startswith('long'): |
| 43 |
return True |
| 44 |
if t in ('ssize_t',): |
| 45 |
return True |
| 46 |
return False |
| 47 |
|
| 48 |
def type_max(t): |
| 49 |
return { |
| 50 |
'ssize_t': 'SSIZE_MAX', |
| 51 |
'size_t': 'SIZE_MAX', |
| 52 |
'int8_t': 'INT8_MAX', |
| 53 |
'uint8_t': 'UINT8_MAX', |
| 54 |
'uint16_t': 'UINT16_MAX', |
| 55 |
'int16_t': 'INT16_MAX', |
| 56 |
'uint32_t': 'UINT32_MAX', |
| 57 |
'int': 'INT_MAX', |
| 58 |
'unsigned int': 'UINT_MAX', |
| 59 |
'uid_t': 'UID_MAX', |
| 60 |
'gid_t': 'GID_MAX', |
| 61 |
'uid_t': 'UID_MAX', |
| 62 |
'long long': 'LLONG_MAX', |
| 63 |
'unsigned long': 'ULONG_MAX', |
| 64 |
'unsigned long long': 'ULONGLONG_MAX', |
| 65 |
}[t] |
| 66 |
|
| 67 |
def main(): |
| 68 |
d = os.path.dirname(sys.argv[0]) |
| 69 |
with open(os.path.join(d, 'cast.c'), 'w') as fc: |
| 70 |
with open(os.path.join(d, 'cast.h'), 'w') as fh: |
| 71 |
codegen(fc, fh) |
| 72 |
|
| 73 |
def codegen(fc, fh): |
| 74 |
|
| 75 |
genheader = """/* |
| 76 |
* GENERATED FILE, DO NOT EDIT. Generated by mkcast.py |
| 77 |
*/ |
| 78 |
""" |
| 79 |
fh.write(genheader) |
| 80 |
fc.write(genheader) |
| 81 |
fh.write("""#include "config.h" |
| 82 |
#include <stdint.h> |
| 83 |
#include <inttypes.h> |
| 84 |
#include <limits.h> |
| 85 |
#include <stddef.h> |
| 86 |
#ifdef HAVE_SYS_TYPES_H |
| 87 |
#include <sys/types.h> |
| 88 |
#endif |
| 89 |
unsigned cast_float_unsigned(float x); |
| 90 |
uint32_t cast_double_uint32(double x); |
| 91 |
void cast_assert(int must, const char* fmt, ...) __attribute__ ((format (printf, 2, 3))); |
| 92 |
""") |
| 93 |
fc.write(""" |
| 94 |
#include "cast.h" |
| 95 |
#include <math.h> |
| 96 |
#include <stdarg.h> |
| 97 |
#include <stdio.h> |
| 98 |
#include <stdlib.h> |
| 99 |
unsigned |
| 100 |
cast_float_unsigned(float x) |
| 101 |
{ |
| 102 |
cast_assert(!isnan(x), "Tried casting %f to unsigned. It's nan", x); |
| 103 |
cast_assert(x >= 0.0f, "Tried casting %f to unsigned. It's not positive", x); |
| 104 |
// Due to rounding errors, avoid comparing close to the edge. |
| 105 |
cast_assert(x <= (float)(UINT_MAX - 1000), "Tried casting %f to unsigned. It's too big", x); |
| 106 |
return (unsigned)x; |
| 107 |
} |
| 108 |
uint32_t |
| 109 |
cast_double_uint32(double x) |
| 110 |
{ |
| 111 |
cast_assert(!isnan(x), "Tried casting %f to uint32_t. It's nan", x); |
| 112 |
cast_assert(x >= 0.0, "Tried casting %f to uint32_t. It's not positive", x); |
| 113 |
// Due to rounding errors, avoid comparing close to the edge. |
| 114 |
cast_assert(x <= (double)(UINT32_MAX - 1000), "Tried casting %f to uint32_t. It's too big", x); |
| 115 |
return (uint32_t)x; |
| 116 |
} |
| 117 |
void |
| 118 |
cast_assert(int must, const char* fmt, ...) |
| 119 |
{ |
| 120 |
va_list ap; |
| 121 |
va_start(ap, fmt); |
| 122 |
if (!must) { |
| 123 |
fprintf(stderr, "arping: runtime check: "); |
| 124 |
vfprintf(stderr, fmt, ap); |
| 125 |
fprintf(stderr, "\\n"); |
| 126 |
exit(1); |
| 127 |
} |
| 128 |
va_end(ap); |
| 129 |
} |
| 130 |
""") |
| 131 |
|
| 132 |
for src, dst in [ |
| 133 |
|
| 134 |
('int16_t', 'uint16_t'), |
| 135 |
|
| 136 |
('ssize_t', 'size_t'), |
| 137 |
('size_t', 'ssize_t'), |
| 138 |
('size_t', 'uint16_t'), |
| 139 |
('size_t', 'uint32_t'), |
| 140 |
('unsigned long', 'unsigned int'), |
| 141 |
('long', 'int16_t'), |
| 142 |
('int16_t', 'uint8_t'), |
| 143 |
('int', 'unsigned int'), |
| 144 |
('int', 'uint16_t'), |
| 145 |
('int', 'size_t'), |
| 146 |
('long', 'int'), |
| 147 |
('long', 'uint8_t'), |
| 148 |
('unsigned long', 'gid_t'), |
| 149 |
('gid_t', 'long long'), |
| 150 |
('uid_t', 'long long'), |
| 151 |
('long long', 'unsigned long long'), |
| 152 |
('long long', 'unsigned int'), |
| 153 |
('int', 'int16_t'), |
| 154 |
('long', 'unsigned long'), |
| 155 |
('long', 'unsigned int'), |
| 156 |
('int', 'unsigned long'), |
| 157 |
]: |
| 158 |
keys = { |
| 159 |
'src': src, |
| 160 |
'dst': dst, |
| 161 |
'src_name':name_map[src], |
| 162 |
'dst_name':name_map[dst], |
| 163 |
} |
| 164 |
assert is_unsigned(src) ^ is_signed(src) |
| 165 |
assert is_unsigned(dst) ^ is_signed(dst) |
| 166 |
from_signed = is_signed(src) |
| 167 |
s2u = is_signed(src) and is_unsigned(dst) |
| 168 |
u2s = is_unsigned(src) and is_signed(dst) |
| 169 |
keys['dst_max'] = type_max(dst) |
| 170 |
|
| 171 |
if from_signed: |
| 172 |
keys['errstr'] = '"cast_{src_name}_{dst_name}(%"PRIdMAX"): %s", (intmax_t)from'.format(**keys) |
| 173 |
else: |
| 174 |
keys['errstr'] = '"cast_{src_name}_{dst_name}(%"PRIuMAX"): %s", (uintmax_t)from'.format(**keys) |
| 175 |
|
| 176 |
fh.write("""{dst} cast_{src_name}_{dst_name}({src} from, const char* fmt, ...) __attribute__ ((format (printf, 2, 3))); |
| 177 |
""".format(**keys)) |
| 178 |
fc.write(""" |
| 179 |
{dst} |
| 180 |
cast_{src_name}_{dst_name}({src} from, const char* fmt, ...) |
| 181 |
{{ |
| 182 |
va_list ap; |
| 183 |
va_start(ap, fmt); |
| 184 |
""".format(**keys)) |
| 185 |
|
| 186 |
if s2u: |
| 187 |
fc.write(' cast_assert(from >= 0, {errstr}, "need >= 0");\n'.format(**keys)); |
| 188 |
if u2s: |
| 189 |
fc.write(''' |
| 190 |
#pragma GCC diagnostic push |
| 191 |
// If the `from` type cannot represent `dst_max`, then GCC warns as |
| 192 |
// it always being true. So suppressing that warning. |
| 193 |
// |
| 194 |
// Silence both GCC and clang for the same error. |
| 195 |
#pragma GCC diagnostic ignored "-Wpragmas" |
| 196 |
#pragma GCC diagnostic ignored "-Wtype-limits" |
| 197 |
#pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" |
| 198 |
cast_assert(from <= {dst_max}, {errstr}, "value won't fit in {dst}"); |
| 199 |
#pragma GCC diagnostic pop |
| 200 |
'''.format(**keys)); |
| 201 |
|
| 202 |
fc.write(" const {dst} to = ({dst})from;\n".format(**keys)) |
| 203 |
|
| 204 |
if u2s: |
| 205 |
fc.write(' cast_assert(to >= 0, {errstr}, "wrapped after casting");\n'.format(**keys)); |
| 206 |
|
| 207 |
fc.write(""" if (from != ({src})to) {{ |
| 208 |
fprintf(stderr, "arping: "); |
| 209 |
if (fmt) {{ |
| 210 |
vfprintf(stderr, fmt, ap); |
| 211 |
fprintf(stderr, ": value won't fit in {dst}"); |
| 212 |
}} else {{ |
| 213 |
fprintf(stderr, {errstr}, "value won't fit in {dst}\\n"); |
| 214 |
}} |
| 215 |
fprintf(stderr, "\\n"); |
| 216 |
exit(1); |
| 217 |
}} |
| 218 |
va_end(ap); |
| 219 |
return to; |
| 220 |
}}""".format(**keys)) |
| 221 |
|
| 222 |
if __name__ == '__main__': |
| 223 |
main() |