@@ -330,14 +330,10 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
330330 }
331331 }
332332
333- // Is the struct packed, without any alignment specification?
334- if (type.get_bool (ID_C_packed) &&
335- type.find (ID_C_alignment).is_nil ())
336- return ; // done
337-
338333 mp_integer offset=0 ;
339334 mp_integer max_alignment=0 ;
340335 std::size_t bit_field_bits=0 ;
336+ const bool struct_is_packed = type.get_bool (ID_C_packed);
341337
342338 for (struct_typet::componentst::iterator
343339 it=components.begin ();
@@ -347,9 +343,6 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
347343 const typet it_type=it->type ();
348344 mp_integer a=1 ;
349345
350- const bool packed=it_type.get_bool (ID_C_packed) ||
351- ns.follow (it_type).get_bool (ID_C_packed);
352-
353346 if (it_type.id ()==ID_c_bit_field)
354347 {
355348 a = alignment (to_c_bit_field_type (it_type).underlying_type (), ns);
@@ -392,13 +385,19 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
392385 bit_field_bits == 0 , " padding ensures offset at byte boundaries" );
393386
394387 // check minimum alignment
395- if (a<config.ansi_c .alignment && !packed)
388+ if (
389+ a < config.ansi_c .alignment && !it_type.get_bool (ID_C_packed) &&
390+ !ns.follow (it_type).get_bool (ID_C_packed))
391+ {
396392 a=config.ansi_c .alignment ;
393+ }
397394
398395 if (max_alignment<a)
399396 max_alignment=a;
400397
401- if (a!=1 )
398+ if (
399+ a != 1 &&
400+ (!struct_is_packed || it_type.find (ID_C_alignment).is_not_nil ()))
402401 {
403402 // we may need to align it
404403 const mp_integer displacement = offset % a;
@@ -433,7 +432,7 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
433432 }
434433 }
435434 // Is the struct packed, without any alignment specification?
436- else if (type. get_bool (ID_C_packed) )
435+ else if (struct_is_packed )
437436 return ; // done
438437
439438 // There may be a need for 'end of struct' padding.
0 commit comments