Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wnon-pod-memaccess -verify %s |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2 | |
Douglas Gregor | e452c78 | 2011-05-03 18:11:37 +0000 | [diff] [blame] | 3 | extern "C" void *memset(void *, int, unsigned); |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 4 | extern "C" void *memmove(void *s1, const void *s2, unsigned n); |
| 5 | extern "C" void *memcpy(void *s1, const void *s2, unsigned n); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 6 | |
| 7 | // Several POD types that should not warn. |
| 8 | struct S1 {} s1; |
| 9 | struct S2 { int x; } s2; |
| 10 | struct S3 { float x, y; S1 s[4]; void (*f)(S1**); } s3; |
| 11 | |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 12 | // We use the C++11 concept of POD for this warning, so ensure a non-aggregate |
| 13 | // still warns. |
| 14 | class C1 { |
| 15 | int x, y, z; |
| 16 | public: |
| 17 | void foo() {} |
| 18 | } c1; |
| 19 | |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 20 | // Non-POD types that should warn. |
| 21 | struct X1 { X1(); } x1; |
| 22 | struct X2 { ~X2(); } x2; |
| 23 | struct X3 { virtual void f(); } x3; |
| 24 | struct X4 : X2 {} x4; |
| 25 | struct X5 : virtual S1 {} x5; |
| 26 | |
| 27 | void test_warn() { |
| 28 | memset(&x1, 0, sizeof x1); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 29 | // expected-warning {{destination for this 'memset' call is a pointer to non-POD type}} \ |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 30 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 31 | memset(&x2, 0, sizeof x2); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 32 | // expected-warning {{destination for this 'memset' call is a pointer to non-POD type}} \ |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 33 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 34 | memset(&x3, 0, sizeof x3); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 35 | // expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \ |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 36 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 37 | memset(&x4, 0, sizeof x4); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 38 | // expected-warning {{destination for this 'memset' call is a pointer to non-POD type}} \ |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 39 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 40 | memset(&x5, 0, sizeof x5); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame^] | 41 | // 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 Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 55 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 56 | } |
| 57 | |
Chandler Carruth | 134cb44 | 2011-04-27 18:48:59 +0000 | [diff] [blame] | 58 | void 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 Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 68 | memset(&s1, 0, sizeof s1); |
| 69 | memset(&s2, 0, sizeof s2); |
| 70 | memset(&s3, 0, sizeof s3); |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 71 | memset(&c1, 0, sizeof c1); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 72 | |
| 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 Gregor | e452c78 | 2011-05-03 18:11:37 +0000 | [diff] [blame] | 79 | |
| 80 | namespace N { |
| 81 | void *memset(void *, int, unsigned); |
| 82 | void test_nowarn() { |
| 83 | N::memset(&x1, 0, sizeof x1); |
| 84 | } |
| 85 | } |