Skip to content

Commit 960563b

Browse files
author
Daniel Kroening
authored
Merge pull request #5310 from tautschnig/fix-alignment
C front-end: maintain alignment attribute for struct members
2 parents 240a30a + de53718 commit 960563b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#define CONCAT(a, b) a##b
2+
#define CONCAT2(a, b) CONCAT(a, b)
3+
4+
#define STATIC_ASSERT(condition) \
5+
int CONCAT2(some_array, __LINE__)[(condition) ? 1 : -1]
6+
7+
typedef struct S1
8+
{
9+
int x;
10+
} S1;
11+
12+
#ifdef __GNUC__
13+
struct S2
14+
{
15+
struct S1 __attribute__((__aligned__(((1L) << 12)))) s1;
16+
};
17+
18+
struct foo
19+
{
20+
char a;
21+
int x[2] __attribute__((packed));
22+
};
23+
#endif
24+
25+
int main()
26+
{
27+
#ifdef __GNUC__
28+
STATIC_ASSERT(sizeof(struct S1) == sizeof(int));
29+
STATIC_ASSERT(sizeof(struct S2) == (1L << 12));
30+
STATIC_ASSERT(sizeof(struct foo) == sizeof(char) + 2 * sizeof(int));
31+
#endif
32+
return 0;
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

src/ansi-c/c_typecheck_type.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
761761
original_qualifiers.is_transparent_union;
762762
remove_qualifiers.write(type);
763763

764+
bool is_packed = type.get_bool(ID_C_packed);
765+
irept alignment = type.find(ID_C_alignment);
766+
764767
if(type.find(ID_tag).is_nil())
765768
{
766769
// Anonymous? Must come with body.
@@ -867,6 +870,11 @@ void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
867870
type.swap(tag_type);
868871

869872
original_qualifiers.write(type);
873+
874+
if(is_packed)
875+
type.set(ID_C_packed, true);
876+
if(alignment.is_not_nil())
877+
type.set(ID_C_alignment, alignment);
870878
}
871879

872880
void c_typecheck_baset::typecheck_compound_body(

0 commit comments

Comments
 (0)