comparison ir/irstruct.cpp @ 1254:747fdd9245d7

Added checks for overlapping union initializers, as shown in bug #259 .
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Wed, 22 Apr 2009 01:18:21 +0200
parents 752bed475b75
children ec1d9dc1d32a
comparison
equal deleted inserted replaced
1253:752bed475b75 1254:747fdd9245d7
254 n = si->vars.dim; 254 n = si->vars.dim;
255 for (size_t i = 0; i < n; i++) 255 for (size_t i = 0; i < n; i++)
256 { 256 {
257 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i]; 257 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i];
258 Initializer* ini = (Initializer*)si->value.data[i]; 258 Initializer* ini = (Initializer*)si->value.data[i];
259 Loc loc = ini ? ini->loc : si->loc;
259 260
260 size_t idx = datamap[i]; 261 size_t idx = datamap[i];
261 262
263 // check for duplicate initialization
262 if (data[idx].first != NULL) 264 if (data[idx].first != NULL)
263 { 265 {
264 Loc l = ini ? ini->loc : si->loc; 266 Loc l = ini ? ini->loc : si->loc;
265 error(l, "duplicate initialization of %s", vd->toChars()); 267 error(l, "duplicate initialization of %s", vd->toChars());
266 continue; 268 continue;
269 }
270
271 // check for overlapping initialization
272 for (size_t j = 0; j < i; j++)
273 {
274 size_t idx2 = datamap[j];
275 assert(data[idx2].first);
276
277 VarDeclarationIter it(aggrdecl->fields, idx2);
278
279 unsigned f_begin = it->offset;
280 unsigned f_end = f_begin + it->type->size();
281
282 if (vd->offset >= f_end || (vd->offset + vd->type->size()) <= f_begin)
283 continue;
284
285 error(loc, "initializer for %s overlaps previous initialization of %s", vd->toChars(), it->toChars());
267 } 286 }
268 287
269 IF_LOG Logger::println("Explicit initializer: %s @+%u", vd->toChars(), vd->offset); 288 IF_LOG Logger::println("Explicit initializer: %s @+%u", vd->toChars(), vd->offset);
270 LOG_SCOPE; 289 LOG_SCOPE;
271 290