blob: e0abdbd2aff27c18e65832463cd93f6a660a9141 [file] [log] [blame]
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00001// RUN: %clang_cc1 -fsyntax-only -Wnon-pod-memaccess -verify %s
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002
Douglas Gregore452c782011-05-03 18:11:37 +00003extern "C" void *memset(void *, int, unsigned);
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00004extern "C" void *memmove(void *s1, const void *s2, unsigned n);
5extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00006
7// Several POD types that should not warn.
8struct S1 {} s1;
9struct S2 { int x; } s2;
10struct S3 { float x, y; S1 s[4]; void (*f)(S1**); } s3;
11
Chandler Carruth43fa33b2011-04-29 09:46:08 +000012// We use the C++11 concept of POD for this warning, so ensure a non-aggregate
13// still warns.
14class C1 {
15 int x, y, z;
16public:
17 void foo() {}
18} c1;
19
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000020// Non-POD types that should warn.
21struct X1 { X1(); } x1;
22struct X2 { ~X2(); } x2;
23struct X3 { virtual void f(); } x3;
24struct X4 : X2 {} x4;
25struct X5 : virtual S1 {} x5;
26
27void test_warn() {
28 memset(&x1, 0, sizeof x1); // \
Douglas Gregor06bc9eb2011-05-03 20:37:33 +000029 // expected-warning {{destination for this 'memset' call is a pointer to non-POD type}} \
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000030 // expected-note {{explicitly cast the pointer to silence this warning}}
31 memset(&x2, 0, sizeof x2); // \
Douglas Gregor06bc9eb2011-05-03 20:37:33 +000032 // expected-warning {{destination for this 'memset' call is a pointer to non-POD type}} \
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000033 // expected-note {{explicitly cast the pointer to silence this warning}}
34 memset(&x3, 0, sizeof x3); // \
Douglas Gregor06bc9eb2011-05-03 20:37:33 +000035 // expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000036 // expected-note {{explicitly cast the pointer to silence this warning}}
37 memset(&x4, 0, sizeof x4); // \
Douglas Gregor06bc9eb2011-05-03 20:37:33 +000038 // expected-warning {{destination for this 'memset' call is a pointer to non-POD type}} \
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000039 // expected-note {{explicitly cast the pointer to silence this warning}}
40 memset(&x5, 0, sizeof x5); // \
Douglas Gregor06bc9eb2011-05-03 20:37:33 +000041 // expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
42 // expected-note {{explicitly cast the pointer to silence this warning}}
43
44 memmove(&x1, 0, sizeof x1); // \
45 // expected-warning{{destination for this 'memmove' call is a pointer to non-POD type 'struct X1'}} \
46 // expected-note {{explicitly cast the pointer to silence this warning}}
47 memmove(0, &x1, sizeof x1); // \
48 // expected-warning{{source of this 'memmove' call is a pointer to non-POD type 'struct X1'}} \
49 // expected-note {{explicitly cast the pointer to silence this warning}}
50 memcpy(&x1, 0, sizeof x1); // \
51 // expected-warning{{destination for this 'memcpy' call is a pointer to non-POD type 'struct X1'}} \
52 // expected-note {{explicitly cast the pointer to silence this warning}}
53 memcpy(0, &x1, sizeof x1); // \
54 // expected-warning{{source of this 'memcpy' call is a pointer to non-POD type 'struct X1'}} \
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000055 // expected-note {{explicitly cast the pointer to silence this warning}}
56}
57
Chandler Carruth134cb442011-04-27 18:48:59 +000058void test_nowarn(void *void_ptr) {
59 int i, *iptr;
60 float y;
61 char c;
62
63 memset(&i, 0, sizeof i);
64 memset(&iptr, 0, sizeof iptr);
65 memset(&y, 0, sizeof y);
66 memset(&c, 0, sizeof c);
67 memset(void_ptr, 0, 42);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000068 memset(&s1, 0, sizeof s1);
69 memset(&s2, 0, sizeof s2);
70 memset(&s3, 0, sizeof s3);
Chandler Carruth43fa33b2011-04-29 09:46:08 +000071 memset(&c1, 0, sizeof c1);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000072
73 // Unevaluated code shouldn't warn.
74 (void)sizeof memset(&x1, 0, sizeof x1);
75
76 // Dead code shouldn't warn.
77 if (false) memset(&x1, 0, sizeof x1);
78}
Douglas Gregore452c782011-05-03 18:11:37 +000079
80namespace N {
81 void *memset(void *, int, unsigned);
82 void test_nowarn() {
83 N::memset(&x1, 0, sizeof x1);
84 }
85}