[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / address-packed.cpp
blobf0d1496fd8928614ebb9994aa7c5169a60c5481e
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 extern void f1(int *);
3 extern void f2(char *);
5 struct __attribute__((packed)) Arguable {
6 int x;
7 char c;
8 static void foo();
9 };
11 extern void f3(void());
13 namespace Foo {
14 struct __attribute__((packed)) Arguable {
15 char c;
16 int x;
17 static void foo();
21 struct Arguable *get_arguable();
23 void f4(int &);
25 void to_void(void *);
27 template <typename... T>
28 void sink(T...);
30 void g0() {
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
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);
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
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
60 struct __attribute__((packed)) A {
61 int x;
62 char c;
64 int *f0() {
65 return &this->x; // expected-warning {{packed member 'x' of class or structure 'A'}}
68 int *g0() {
69 return &x; // expected-warning {{packed member 'x' of class or structure 'A'}}
72 char *h0() {
73 return &c; // no-warning
77 struct B : A {
78 int *f1() {
79 return &this->x; // expected-warning {{packed member 'x' of class or structure 'A'}}
82 int *g1() {
83 return &x; // expected-warning {{packed member 'x' of class or structure 'A'}}
86 char *h1() {
87 return &c; // no-warning
91 template <typename Ty>
92 class __attribute__((packed)) S {
93 Ty X;
95 public:
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>'}}
102 template <typename Ty>
103 void h(Ty *);
105 void g1() {
106 S<int> s1;
107 s1.get(); // expected-note {{in instantiation of member function 'S<int>::get'}}
109 S<char> s2;
110 s2.get();
112 S<float> s3;
113 s3.get(); // expected-note {{in instantiation of member function 'S<float>::get'}}
116 // PR35509
117 typedef long L1;
118 struct Incomplete;
119 struct S2 {
120 L1 d;
121 Incomplete *e() const;
122 } __attribute__((packed));
123 Incomplete *S2::e() const { return (Incomplete *)&d; } // no-warning