xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-bad-memaccess.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wdynamic-class-memaccess -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc extern "C" void *memset(void *, int, unsigned);
4f4a2713aSLionel Sambuc extern "C" void *memmove(void *s1, const void *s2, unsigned n);
5f4a2713aSLionel Sambuc extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
6f4a2713aSLionel Sambuc extern "C" void *memcmp(void *s1, const void *s2, unsigned n);
7f4a2713aSLionel Sambuc 
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc // Redeclare without the extern "C" to test that we still figure out that this
10f4a2713aSLionel Sambuc // is the "real" memset.
11f4a2713aSLionel Sambuc void *memset(void *, int, unsigned);
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc // Several types that should not warn.
14f4a2713aSLionel Sambuc struct S1 {} s1;
15f4a2713aSLionel Sambuc struct S2 { int x; } s2;
16f4a2713aSLionel Sambuc struct S3 { float x, y; S1 s[4]; void (*f)(S1**); } s3;
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc class C1 {
19f4a2713aSLionel Sambuc   int x, y, z;
20f4a2713aSLionel Sambuc public:
foo()21f4a2713aSLionel Sambuc   void foo() {}
22f4a2713aSLionel Sambuc } c1;
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc struct X1 { virtual void f(); } x1;
25f4a2713aSLionel Sambuc struct X2 : virtual S1 {} x2;
26f4a2713aSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc struct ContainsDynamic { X1 dynamic; } contains_dynamic;
28*0a6a1f1dSLionel Sambuc struct DeepContainsDynamic { ContainsDynamic m; } deep_contains_dynamic;
29*0a6a1f1dSLionel Sambuc struct ContainsArrayDynamic { X1 dynamic[1]; } contains_array_dynamic;
30*0a6a1f1dSLionel Sambuc struct ContainsPointerDynamic { X1 *dynamic; } contains_pointer_dynamic;
31*0a6a1f1dSLionel Sambuc 
test_warn()32f4a2713aSLionel Sambuc void test_warn() {
33f4a2713aSLionel Sambuc   memset(&x1, 0, sizeof x1); // \
34f4a2713aSLionel Sambuc       // expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
35f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
36f4a2713aSLionel Sambuc   memset(&x2, 0, sizeof x2); // \
37f4a2713aSLionel Sambuc       // expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
38f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc   memmove(&x1, 0, sizeof x1); // \
41*0a6a1f1dSLionel Sambuc       // expected-warning{{destination for this 'memmove' call is a pointer to dynamic class 'X1'; vtable pointer will be overwritten}} \
42f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
43f4a2713aSLionel Sambuc   memmove(0, &x1, sizeof x1); // \
44*0a6a1f1dSLionel Sambuc       // expected-warning{{source of this 'memmove' call is a pointer to dynamic class 'X1'; vtable pointer will be moved}} \
45f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
46f4a2713aSLionel Sambuc   memcpy(&x1, 0, sizeof x1); // \
47*0a6a1f1dSLionel Sambuc       // expected-warning{{destination for this 'memcpy' call is a pointer to dynamic class 'X1'; vtable pointer will be overwritten}} \
48f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
49f4a2713aSLionel Sambuc   memcpy(0, &x1, sizeof x1); // \
50*0a6a1f1dSLionel Sambuc       // expected-warning{{source of this 'memcpy' call is a pointer to dynamic class 'X1'; vtable pointer will be copied}} \
51f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
52f4a2713aSLionel Sambuc   memcmp(&x1, 0, sizeof x1); // \
53*0a6a1f1dSLionel Sambuc       // expected-warning{{first operand of this 'memcmp' call is a pointer to dynamic class 'X1'; vtable pointer will be compared}} \
54f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
55f4a2713aSLionel Sambuc   memcmp(0, &x1, sizeof x1); // \
56*0a6a1f1dSLionel Sambuc       // expected-warning{{second operand of this 'memcmp' call is a pointer to dynamic class 'X1'; vtable pointer will be compared}} \
57f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc   __builtin_memset(&x1, 0, sizeof x1); // \
60f4a2713aSLionel Sambuc       // expected-warning {{destination for this '__builtin_memset' call is a pointer to dynamic class}} \
61f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
62f4a2713aSLionel Sambuc   __builtin_memset(&x2, 0, sizeof x2); // \
63f4a2713aSLionel Sambuc       // expected-warning {{destination for this '__builtin_memset' call is a pointer to dynamic class}} \
64f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   __builtin_memmove(&x1, 0, sizeof x1); // \
67f4a2713aSLionel Sambuc       // expected-warning{{destination for this '__builtin_memmove' call is a pointer to dynamic class}} \
68f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
69f4a2713aSLionel Sambuc   __builtin_memmove(0, &x1, sizeof x1); // \
70f4a2713aSLionel Sambuc       // expected-warning{{source of this '__builtin_memmove' call is a pointer to dynamic class}} \
71f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
72f4a2713aSLionel Sambuc   __builtin_memcpy(&x1, 0, sizeof x1); // \
73f4a2713aSLionel Sambuc       // expected-warning{{destination for this '__builtin_memcpy' call is a pointer to dynamic class}} \
74f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
75f4a2713aSLionel Sambuc   __builtin_memcpy(0, &x1, sizeof x1); // \
76f4a2713aSLionel Sambuc       // expected-warning{{source of this '__builtin_memcpy' call is a pointer to dynamic class}} \
77f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   __builtin___memset_chk(&x1, 0, sizeof x1, sizeof x1); //                    \
80f4a2713aSLionel Sambuc       // expected-warning {{destination for this '__builtin___memset_chk' call is a pointer to dynamic class}} \
81f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
82f4a2713aSLionel Sambuc   __builtin___memset_chk(&x2, 0, sizeof x2, sizeof x2); //                    \
83f4a2713aSLionel Sambuc       // expected-warning {{destination for this '__builtin___memset_chk' call is a pointer to dynamic class}} \
84f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   __builtin___memmove_chk(&x1, 0, sizeof x1, sizeof x1); //                   \
87f4a2713aSLionel Sambuc       // expected-warning{{destination for this '__builtin___memmove_chk' call is a pointer to dynamic class}} \
88f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
89f4a2713aSLionel Sambuc   __builtin___memmove_chk(0, &x1, sizeof x1, sizeof x1); //                   \
90f4a2713aSLionel Sambuc       // expected-warning{{source of this '__builtin___memmove_chk' call is a pointer to dynamic class}} \
91f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
92f4a2713aSLionel Sambuc   __builtin___memcpy_chk(&x1, 0, sizeof x1, sizeof x1); //                    \
93f4a2713aSLionel Sambuc       // expected-warning{{destination for this '__builtin___memcpy_chk' call is a pointer to dynamic class}} \
94f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
95f4a2713aSLionel Sambuc   __builtin___memcpy_chk(0, &x1, sizeof x1, sizeof x1); //                    \
96f4a2713aSLionel Sambuc       // expected-warning{{source of this '__builtin___memcpy_chk' call is a pointer to dynamic class}} \
97f4a2713aSLionel Sambuc       // expected-note {{explicitly cast the pointer to silence this warning}}
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc   // expected-warning@+2 {{destination for this 'memset' call is a pointer to class containing a dynamic class 'X1'}}
100*0a6a1f1dSLionel Sambuc   // expected-note@+1 {{explicitly cast the pointer to silence this warning}}
101*0a6a1f1dSLionel Sambuc   memset(&contains_dynamic, 0, sizeof(contains_dynamic));
102*0a6a1f1dSLionel Sambuc   // expected-warning@+2 {{destination for this 'memset' call is a pointer to class containing a dynamic class 'X1'}}
103*0a6a1f1dSLionel Sambuc   // expected-note@+1 {{explicitly cast the pointer to silence this warning}}
104*0a6a1f1dSLionel Sambuc   memset(&deep_contains_dynamic, 0, sizeof(deep_contains_dynamic));
105*0a6a1f1dSLionel Sambuc   // expected-warning@+2 {{destination for this 'memset' call is a pointer to class containing a dynamic class 'X1'}}
106*0a6a1f1dSLionel Sambuc   // expected-note@+1 {{explicitly cast the pointer to silence this warning}}
107*0a6a1f1dSLionel Sambuc   memset(&contains_array_dynamic, 0, sizeof(contains_array_dynamic));
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc 
test_nowarn(void * void_ptr)110f4a2713aSLionel Sambuc void test_nowarn(void *void_ptr) {
111f4a2713aSLionel Sambuc   int i, *iptr;
112f4a2713aSLionel Sambuc   float y;
113f4a2713aSLionel Sambuc   char c;
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc   memset(&i, 0, sizeof i);
116f4a2713aSLionel Sambuc   memset(&iptr, 0, sizeof iptr);
117f4a2713aSLionel Sambuc   memset(&y, 0, sizeof y);
118f4a2713aSLionel Sambuc   memset(&c, 0, sizeof c);
119f4a2713aSLionel Sambuc   memset(void_ptr, 0, 42);
120f4a2713aSLionel Sambuc   memset(&s1, 0, sizeof s1);
121f4a2713aSLionel Sambuc   memset(&s2, 0, sizeof s2);
122f4a2713aSLionel Sambuc   memset(&s3, 0, sizeof s3);
123f4a2713aSLionel Sambuc   memset(&c1, 0, sizeof c1);
124f4a2713aSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc   memset(&contains_pointer_dynamic, 0, sizeof(contains_pointer_dynamic));
126*0a6a1f1dSLionel Sambuc 
127f4a2713aSLionel Sambuc   // Unevaluated code shouldn't warn.
128f4a2713aSLionel Sambuc   (void)sizeof memset(&x1, 0, sizeof x1);
129f4a2713aSLionel Sambuc 
130f4a2713aSLionel Sambuc   // Dead code shouldn't warn.
131f4a2713aSLionel Sambuc   if (false) memset(&x1, 0, sizeof x1);
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc namespace N {
135f4a2713aSLionel Sambuc   void *memset(void *, int, unsigned);
test_nowarn()136f4a2713aSLionel Sambuc   void test_nowarn() {
137f4a2713aSLionel Sambuc     N::memset(&x1, 0, sizeof x1);
138f4a2713aSLionel Sambuc   }
139f4a2713aSLionel Sambuc }
140