blob: 9023793bd52d19c9342ef793e3d9a95f2251a7d5 [file] [log] [blame]
Chandler Carruth202e1232011-04-29 20:58:14 +00001// RUN: %clang_cc1 -fsyntax-only -Wnon-pod-memset -verify %s
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002
Douglas Gregore452c782011-05-03 18:11:37 +00003extern "C" void *memset(void *, int, unsigned);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00004
5// Several POD types that should not warn.
6struct S1 {} s1;
7struct S2 { int x; } s2;
8struct S3 { float x, y; S1 s[4]; void (*f)(S1**); } s3;
9
Chandler Carruth43fa33b2011-04-29 09:46:08 +000010// We use the C++11 concept of POD for this warning, so ensure a non-aggregate
11// still warns.
12class C1 {
13 int x, y, z;
14public:
15 void foo() {}
16} c1;
17
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000018// Non-POD types that should warn.
19struct X1 { X1(); } x1;
20struct X2 { ~X2(); } x2;
21struct X3 { virtual void f(); } x3;
22struct X4 : X2 {} x4;
23struct X5 : virtual S1 {} x5;
24
25void test_warn() {
26 memset(&x1, 0, sizeof x1); // \
27 // expected-warning {{destination for this memset call is a pointer to a non-POD type}} \
28 // expected-note {{explicitly cast the pointer to silence this warning}}
29 memset(&x2, 0, sizeof x2); // \
30 // expected-warning {{destination for this memset call is a pointer to a non-POD type}} \
31 // expected-note {{explicitly cast the pointer to silence this warning}}
32 memset(&x3, 0, sizeof x3); // \
33 // expected-warning {{destination for this memset call is a pointer to a non-POD type}} \
34 // expected-note {{explicitly cast the pointer to silence this warning}}
35 memset(&x4, 0, sizeof x4); // \
36 // expected-warning {{destination for this memset call is a pointer to a non-POD type}} \
37 // expected-note {{explicitly cast the pointer to silence this warning}}
38 memset(&x5, 0, sizeof x5); // \
39 // expected-warning {{destination for this memset call is a pointer to a non-POD type}} \
40 // expected-note {{explicitly cast the pointer to silence this warning}}
41}
42
Chandler Carruth134cb442011-04-27 18:48:59 +000043void test_nowarn(void *void_ptr) {
44 int i, *iptr;
45 float y;
46 char c;
47
48 memset(&i, 0, sizeof i);
49 memset(&iptr, 0, sizeof iptr);
50 memset(&y, 0, sizeof y);
51 memset(&c, 0, sizeof c);
52 memset(void_ptr, 0, 42);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000053 memset(&s1, 0, sizeof s1);
54 memset(&s2, 0, sizeof s2);
55 memset(&s3, 0, sizeof s3);
Chandler Carruth43fa33b2011-04-29 09:46:08 +000056 memset(&c1, 0, sizeof c1);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +000057
58 // Unevaluated code shouldn't warn.
59 (void)sizeof memset(&x1, 0, sizeof x1);
60
61 // Dead code shouldn't warn.
62 if (false) memset(&x1, 0, sizeof x1);
63}
Douglas Gregore452c782011-05-03 18:11:37 +000064
65namespace N {
66 void *memset(void *, int, unsigned);
67 void test_nowarn() {
68 N::memset(&x1, 0, sizeof x1);
69 }
70}