Clang Project

clang_source_code/test/SemaCXX/address-packed.cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2extern void f1(int *);
3extern void f2(char *);
4
5struct __attribute__((packed)) Arguable {
6  int x;
7  char c;
8  static void foo();
9};
10
11extern void f3(void());
12
13namespace Foo {
14struct __attribute__((packed)) Arguable {
15  char c;
16  int x;
17  static void foo();
18};
19}
20
21struct Arguable *get_arguable();
22
23void f4(int &);
24
25void to_void(void *);
26
27template <typename... T>
28void sink(T...);
29
30void g0() {
31  {
32    Foo::Arguable arguable;
33    f1(&arguable.x);   // expected-warning {{packed member 'x' of class or structure 'Foo::Arguable'}}
34    f2(&arguable.c);   // no-warning
35    f3(&arguable.foo); // no-warning
36
37    to_void(&arguable.x);                             // no-warning
38    void *p1 = &arguable.x;                           // no-warning
39    void *p2 = static_cast<void *>(&arguable.x);      // no-warning
40    void *p3 = reinterpret_cast<void *>(&arguable.x); // no-warning
41    void *p4 = (void *)&arguable.x;                   // no-warning
42    sink(p1, p2, p3, p4);
43  }
44  {
45    Arguable arguable1;
46    Arguable &arguable(arguable1);
47    f1(&arguable.x);   // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
48    f2(&arguable.c);   // no-warning
49    f3(&arguable.foo); // no-warning
50  }
51  {
52    Arguable *arguable1;
53    Arguable *&arguable(arguable1);
54    f1(&arguable->x);   // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
55    f2(&arguable->c);   // no-warning
56    f3(&arguable->foo); // no-warning
57  }
58}
59
60struct __attribute__((packed)) A {
61  int x;
62  char c;
63
64  int *f0() {
65    return &this->x; // expected-warning {{packed member 'x' of class or structure 'A'}}
66  }
67
68  int *g0() {
69    return &x; // expected-warning {{packed member 'x' of class or structure 'A'}}
70  }
71
72  char *h0() {
73    return &c; // no-warning
74  }
75};
76
77struct B : A {
78  int *f1() {
79    return &this->x; // expected-warning {{packed member 'x' of class or structure 'A'}}
80  }
81
82  int *g1() {
83    return &x; // expected-warning {{packed member 'x' of class or structure 'A'}}
84  }
85
86  char *h1() {
87    return &c; // no-warning
88  }
89};
90
91template <typename Ty>
92class __attribute__((packed)) S {
93  Ty X;
94
95public:
96  const Ty *get() const {
97    return &X; // expected-warning {{packed member 'X' of class or structure 'S<int>'}}
98               // expected-warning@-1 {{packed member 'X' of class or structure 'S<float>'}}
99  }
100};
101
102template <typename Ty>
103void h(Ty *);
104
105void g1() {
106  S<int> s1;
107  s1.get(); // expected-note {{in instantiation of member function 'S<int>::get'}}
108
109  S<char> s2;
110  s2.get();
111
112  S<float> s3;
113  s3.get(); // expected-note {{in instantiation of member function 'S<float>::get'}}
114}
115
116// PR35509
117typedef long L1;
118struct Incomplete;
119struct S2 {
120  L1 d;
121  Incomplete *e() const;
122} __attribute__((packed));
123Incomplete *S2::e() const { return (Incomplete *)&d; } // no-warning
124