ObjFW  Check-in [b31e47c23e]

Overview
Comment:OFMethodSignature: Fix align inside structs on x86
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b31e47c23ee0375a753332dd0fa9f664f5a064bc140916707ef6f88c357fd9eb
User & Date: js on 2018-02-11 21:06:53
Other Links: manifest | tags
Context
2018-02-11
23:21
Add .travis.yml check-in: 9607870d0b user: js tags: trunk
21:06
OFMethodSignature: Fix align inside structs on x86 check-in: b31e47c23e user: js tags: trunk
15:17
Update buildsys check-in: 7482940215 user: js tags: trunk
Changes

Modified src/OFMethodSignature.m from [ba6ce52679] to [c051dfe1af].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

size_t alignofEncoding(const char **type, size_t *length);
size_t sizeofEncoding(const char **type, size_t *length);

size_t
alignofArray(const char **type, size_t *length)
{
	size_t align;

	assert(*length > 0);

	(*type)++;
	(*length)--;

	while (*length > 0 && isdigit(**type)) {
		(*type)++;
		(*length)--;
	}

	align = alignofEncoding(type, length);

	if (*length == 0 || **type != ']')
		@throw [OFInvalidFormatException exception];

	(*type)++;
	(*length)--;








|

















|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

size_t alignofEncoding(const char **type, size_t *length, bool inStruct);
size_t sizeofEncoding(const char **type, size_t *length);

size_t
alignofArray(const char **type, size_t *length)
{
	size_t align;

	assert(*length > 0);

	(*type)++;
	(*length)--;

	while (*length > 0 && isdigit(**type)) {
		(*type)++;
		(*length)--;
	}

	align = alignofEncoding(type, length, true);

	if (*length == 0 || **type != ']')
		@throw [OFInvalidFormatException exception];

	(*type)++;
	(*length)--;

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		@throw [OFInvalidFormatException exception];

	/* Skip '=' */
	(*type)++;
	(*length)--;

	while (*length > 0 && **type != '}') {
		size_t fieldAlign = alignofEncoding(type, length);

#if defined(OF_POWERPC) && defined(OF_MACOS)
		if (!first && fieldAlign > 4)
			fieldAlign = 4;

		first = false;
#endif







|







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		@throw [OFInvalidFormatException exception];

	/* Skip '=' */
	(*type)++;
	(*length)--;

	while (*length > 0 && **type != '}') {
		size_t fieldAlign = alignofEncoding(type, length, true);

#if defined(OF_POWERPC) && defined(OF_MACOS)
		if (!first && fieldAlign > 4)
			fieldAlign = 4;

		first = false;
#endif
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
		@throw [OFInvalidFormatException exception];

	/* Skip '=' */
	(*type)++;
	(*length)--;

	while (*length > 0 && **type != ')') {
		size_t fieldAlign = alignofEncoding(type, length);

		if (fieldAlign > align)
			align = fieldAlign;
	}

	if (*length == 0 || **type != ')')
		@throw [OFInvalidFormatException exception];

	(*type)++;
	(*length)--;

	return align;
}

size_t
alignofEncoding(const char **type, size_t *length)
{
	size_t align;

	if (*length == 0)
		@throw [OFInvalidFormatException exception];

	if (**type == 'r') {







|















|







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
		@throw [OFInvalidFormatException exception];

	/* Skip '=' */
	(*type)++;
	(*length)--;

	while (*length > 0 && **type != ')') {
		size_t fieldAlign = alignofEncoding(type, length, true);

		if (fieldAlign > align)
			align = fieldAlign;
	}

	if (*length == 0 || **type != ')')
		@throw [OFInvalidFormatException exception];

	(*type)++;
	(*length)--;

	return align;
}

size_t
alignofEncoding(const char **type, size_t *length, bool inStruct)
{
	size_t align;

	if (*length == 0)
		@throw [OFInvalidFormatException exception];

	if (**type == 'r') {
177
178
179
180
181
182
183





184
185
186
187
188
189
190
191
192
193
194
195





196
197
198





199
200
201
202
203
204
205
206
		break;
	case 'l':
	case 'L':
		align = OF_ALIGNOF(long);
		break;
	case 'q':
	case 'Q':





		align = OF_ALIGNOF(long long);
		break;
#ifdef __SIZEOF_INT128__
	case 't':
	case 'T':
		align = __extension__ OF_ALIGNOF(__int128);
		break;
#endif
	case 'f':
		align = OF_ALIGNOF(float);
		break;
	case 'd':





		align = OF_ALIGNOF(double);
		break;
	case 'D':





		align = OF_ALIGNOF(long double);
		break;
	case 'B':
		align = OF_ALIGNOF(_Bool);
		break;
	case 'v':
		align = 0;
		break;







>
>
>
>
>
|











>
>
>
>
>
|


>
>
>
>
>
|







177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
		break;
	case 'l':
	case 'L':
		align = OF_ALIGNOF(long);
		break;
	case 'q':
	case 'Q':
#ifdef OF_X86
		if (inStruct)
			align = 4;
		else
#endif
			align = OF_ALIGNOF(long long);
		break;
#ifdef __SIZEOF_INT128__
	case 't':
	case 'T':
		align = __extension__ OF_ALIGNOF(__int128);
		break;
#endif
	case 'f':
		align = OF_ALIGNOF(float);
		break;
	case 'd':
#ifdef OF_X86
		if (inStruct)
			align = 4;
		else
#endif
			align = OF_ALIGNOF(double);
		break;
	case 'D':
#ifdef OF_X86
		if (inStruct)
			align = 4;
		else
#endif
			align = OF_ALIGNOF(long double);
		break;
	case 'B':
		align = OF_ALIGNOF(_Bool);
		break;
	case 'v':
		align = 0;
		break;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244





245
246
247
248
249
250
251
252
		return alignofStruct(type, length);
	case '(':
		return alignofUnion(type, length);
	case '^':
		/* Just to skip over the rest */
		(*type)++;
		(*length)--;
		alignofEncoding(type, length);

		return OF_ALIGNOF(void *);
#ifndef __STDC_NO_COMPLEX__
	case 'j':
		(*type)++;
		(*length)--;

		if (*length == 0)
			@throw [OFInvalidFormatException exception];

		switch (**type) {
		case 'f':
			align = OF_ALIGNOF(float _Complex);
			break;
		case 'd':





			align = OF_ALIGNOF(double _Complex);
			break;
		case 'D':
			align = OF_ALIGNOF(long double _Complex);
			break;
		default:
			@throw [OFInvalidFormatException exception];
		}







|















>
>
>
>
>
|







237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
		return alignofStruct(type, length);
	case '(':
		return alignofUnion(type, length);
	case '^':
		/* Just to skip over the rest */
		(*type)++;
		(*length)--;
		alignofEncoding(type, length, false);

		return OF_ALIGNOF(void *);
#ifndef __STDC_NO_COMPLEX__
	case 'j':
		(*type)++;
		(*length)--;

		if (*length == 0)
			@throw [OFInvalidFormatException exception];

		switch (**type) {
		case 'f':
			align = OF_ALIGNOF(float _Complex);
			break;
		case 'd':
# ifdef OF_X86
			if (inStruct)
				align = 4;
			else
#endif
				align = OF_ALIGNOF(double _Complex);
			break;
		case 'D':
			align = OF_ALIGNOF(long double _Complex);
			break;
		default:
			@throw [OFInvalidFormatException exception];
		}
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

	while (*length > 0 && **type != '}') {
		size_t fieldSize, fieldAlign;

		typeCopy = *type;
		lengthCopy = *length;
		fieldSize = sizeofEncoding(type, length);
		fieldAlign = alignofEncoding(&typeCopy, &lengthCopy);

#if defined(OF_POWERPC) && defined(OF_MACOS)
		if (!first && fieldAlign > 4)
			fieldAlign = 4;

		first = false;
#endif







|







349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

	while (*length > 0 && **type != '}') {
		size_t fieldSize, fieldAlign;

		typeCopy = *type;
		lengthCopy = *length;
		fieldSize = sizeofEncoding(type, length);
		fieldAlign = alignofEncoding(&typeCopy, &lengthCopy, true);

#if defined(OF_POWERPC) && defined(OF_MACOS)
		if (!first && fieldAlign > 4)
			fieldAlign = 4;

		first = false;
#endif
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
	return ret;
}

size_t
of_alignof_type_encoding(const char *type)
{
	size_t length = strlen(type);
	size_t ret = alignofEncoding(&type, &length);

	if (length > 0)
		@throw [OFInvalidFormatException exception];

	return ret;
}








|







563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
	return ret;
}

size_t
of_alignof_type_encoding(const char *type)
{
	size_t length = strlen(type);
	size_t ret = alignofEncoding(&type, &length, false);

	if (length > 0)
		@throw [OFInvalidFormatException exception];

	return ret;
}

Modified tests/OFMethodSignatureTests.m from [8cbc481dee] to [c008280f35].

14
15
16
17
18
19
20




21
22
23
24
25
26
27
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <string.h>





#import "OFMethodSignature.h"
#import "OFAutoreleasePool.h"

#import "OFInvalidFormatException.h"

#import "TestsAppDelegate.h"







>
>
>
>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <string.h>

#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H)
# include <complex.h>
#endif

#import "OFMethodSignature.h"
#import "OFAutoreleasePool.h"

#import "OFInvalidFormatException.h"

#import "TestsAppDelegate.h"
42
43
44
45
46
47
48







49
50
51
52
53
54
55
	} st;
	union {
		char c;
		int i;
	} u;
	double d;
};








union test3_union {
	char c;
	int i;
	double d;
};








>
>
>
>
>
>
>







46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	} st;
	union {
		char c;
		int i;
	} u;
	double d;
};

#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H)
struct test3_struct {
	char c;
	complex double cd;
};
#endif

union test3_union {
	char c;
	int i;
	double d;
};

119
120
121
122
123
124
125

126





127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

146





147
148
149
150
151
152
153
154
155
156
157
158
159
160
	    of_sizeof_type_encoding(@encode(struct test1_struct)) ==
	    sizeof(struct test1_struct))

	TEST(@"of_sizeof_type_encoding() #2",
	    of_sizeof_type_encoding(@encode(struct test2_struct)) ==
	    sizeof(struct test2_struct))


	TEST(@"of_sizeof_type_encoding() #3",





	    of_sizeof_type_encoding(@encode(union test3_union)) ==
	    sizeof(union test3_union))

	TEST(@"of_sizeof_type_encoding() #4",
	    of_sizeof_type_encoding(@encode(union test4_union)) ==
	    sizeof(union test4_union))

	TEST(@"of_sizeof_type_encoding() #5",
	    of_sizeof_type_encoding(@encode(struct test1_struct [5])) ==
	    sizeof(struct test1_struct [5]))

	TEST(@"of_alignof_type_encoding() #1",
	    of_alignof_type_encoding(@encode(struct test1_struct)) ==
	    OF_ALIGNOF(struct test1_struct))

	TEST(@"of_alignof_type_encoding() #2",
	    of_alignof_type_encoding(@encode(struct test2_struct)) ==
	    OF_ALIGNOF(struct test2_struct))


	TEST(@"of_alignof_type_encoding() #3",





	    of_alignof_type_encoding(@encode(union test3_union)) ==
	    OF_ALIGNOF(union test3_union))

	TEST(@"of_alignof_type_encoding() #4",
	    of_alignof_type_encoding(@encode(union test4_union)) ==
	    OF_ALIGNOF(union test4_union))

	TEST(@"of_alignof_type_encoding() #5",
	    of_alignof_type_encoding(@encode(struct test1_struct [5])) ==
	    OF_ALIGNOF(struct test1_struct [5]))

	[pool drain];
}
@end







>

>
>
>
>
>



|



|











>

>
>
>
>
>



|



|






130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	    of_sizeof_type_encoding(@encode(struct test1_struct)) ==
	    sizeof(struct test1_struct))

	TEST(@"of_sizeof_type_encoding() #2",
	    of_sizeof_type_encoding(@encode(struct test2_struct)) ==
	    sizeof(struct test2_struct))

#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H)
	TEST(@"of_sizeof_type_encoding() #3",
	    of_sizeof_type_encoding(@encode(struct test3_struct)) ==
	    sizeof(struct test3_struct))
#endif

	TEST(@"of_sizeof_type_encoding() #4",
	    of_sizeof_type_encoding(@encode(union test3_union)) ==
	    sizeof(union test3_union))

	TEST(@"of_sizeof_type_encoding() #5",
	    of_sizeof_type_encoding(@encode(union test4_union)) ==
	    sizeof(union test4_union))

	TEST(@"of_sizeof_type_encoding() #6",
	    of_sizeof_type_encoding(@encode(struct test1_struct [5])) ==
	    sizeof(struct test1_struct [5]))

	TEST(@"of_alignof_type_encoding() #1",
	    of_alignof_type_encoding(@encode(struct test1_struct)) ==
	    OF_ALIGNOF(struct test1_struct))

	TEST(@"of_alignof_type_encoding() #2",
	    of_alignof_type_encoding(@encode(struct test2_struct)) ==
	    OF_ALIGNOF(struct test2_struct))

#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H)
	TEST(@"of_alignof_type_encoding() #3",
	    of_alignof_type_encoding(@encode(struct test3_struct)) ==
	    OF_ALIGNOF(struct test3_struct))
#endif

	TEST(@"of_alignof_type_encoding() #4",
	    of_alignof_type_encoding(@encode(union test3_union)) ==
	    OF_ALIGNOF(union test3_union))

	TEST(@"of_alignof_type_encoding() #5",
	    of_alignof_type_encoding(@encode(union test4_union)) ==
	    OF_ALIGNOF(union test4_union))

	TEST(@"of_alignof_type_encoding() #6",
	    of_alignof_type_encoding(@encode(struct test1_struct [5])) ==
	    OF_ALIGNOF(struct test1_struct [5]))

	[pool drain];
}
@end