Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wdynamic-class-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 | |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 7 | // Several types that should not warn. |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 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 | class C1 { |
| 13 | int x, y, z; |
| 14 | public: |
| 15 | void foo() {} |
| 16 | } c1; |
| 17 | |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 18 | struct X1 { virtual void f(); } x1; |
| 19 | struct X2 : virtual S1 {} x2; |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 20 | |
| 21 | void test_warn() { |
| 22 | memset(&x1, 0, sizeof x1); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 23 | // 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] | 24 | // expected-note {{explicitly cast the pointer to silence this warning}} |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 25 | memset(&x2, 0, sizeof x2); // \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 26 | // expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \ |
| 27 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 28 | |
| 29 | memmove(&x1, 0, sizeof x1); // \ |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 30 | // expected-warning{{destination for this 'memmove' call is a pointer to dynamic class}} \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 31 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 32 | memmove(0, &x1, sizeof x1); // \ |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 33 | // expected-warning{{source of this 'memmove' call is a pointer to dynamic class}} \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 34 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 35 | memcpy(&x1, 0, sizeof x1); // \ |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 36 | // expected-warning{{destination for this 'memcpy' call is a pointer to dynamic class}} \ |
Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 37 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 38 | memcpy(0, &x1, sizeof x1); // \ |
Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame^] | 39 | // expected-warning{{source of this 'memcpy' call is a pointer to dynamic class}} \ |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 40 | // expected-note {{explicitly cast the pointer to silence this warning}} |
| 41 | } |
| 42 | |
Chandler Carruth | 134cb44 | 2011-04-27 18:48:59 +0000 | [diff] [blame] | 43 | void 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 Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 53 | memset(&s1, 0, sizeof s1); |
| 54 | memset(&s2, 0, sizeof s2); |
| 55 | memset(&s3, 0, sizeof s3); |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 56 | memset(&c1, 0, sizeof c1); |
Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 57 | |
| 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 Gregor | e452c78 | 2011-05-03 18:11:37 +0000 | [diff] [blame] | 64 | |
| 65 | namespace N { |
| 66 | void *memset(void *, int, unsigned); |
| 67 | void test_nowarn() { |
| 68 | N::memset(&x1, 0, sizeof x1); |
| 69 | } |
| 70 | } |