2011-06-03 14:23:57 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wdynamic-class-memaccess -verify %s
|
2011-04-27 15:05:31 +08:00
|
|
|
|
2011-05-04 02:11:37 +08:00
|
|
|
extern "C" void *memset(void *, int, unsigned);
|
2011-05-04 04:37:33 +08:00
|
|
|
extern "C" void *memmove(void *s1, const void *s2, unsigned n);
|
|
|
|
extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
|
2011-08-20 04:40:18 +08:00
|
|
|
extern "C" void *memcmp(void *s1, const void *s2, unsigned n);
|
2011-04-27 15:05:31 +08:00
|
|
|
|
2012-12-31 01:23:09 +08:00
|
|
|
|
|
|
|
// Redeclare without the extern "C" to test that we still figure out that this
|
|
|
|
// is the "real" memset.
|
|
|
|
void *memset(void *, int, unsigned);
|
|
|
|
|
2011-06-03 14:23:57 +08:00
|
|
|
// Several types that should not warn.
|
2011-04-27 15:05:31 +08:00
|
|
|
struct S1 {} s1;
|
|
|
|
struct S2 { int x; } s2;
|
|
|
|
struct S3 { float x, y; S1 s[4]; void (*f)(S1**); } s3;
|
|
|
|
|
2011-04-29 17:46:08 +08:00
|
|
|
class C1 {
|
|
|
|
int x, y, z;
|
|
|
|
public:
|
|
|
|
void foo() {}
|
|
|
|
} c1;
|
|
|
|
|
2015-03-22 01:56:44 +08:00
|
|
|
struct X1 { virtual void f(); } x1, x1arr[2];
|
2011-06-03 14:23:57 +08:00
|
|
|
struct X2 : virtual S1 {} x2;
|
2011-04-27 15:05:31 +08:00
|
|
|
|
2014-06-28 07:58:21 +08:00
|
|
|
struct ContainsDynamic { X1 dynamic; } contains_dynamic;
|
|
|
|
struct DeepContainsDynamic { ContainsDynamic m; } deep_contains_dynamic;
|
|
|
|
struct ContainsArrayDynamic { X1 dynamic[1]; } contains_array_dynamic;
|
|
|
|
struct ContainsPointerDynamic { X1 *dynamic; } contains_pointer_dynamic;
|
|
|
|
|
2011-04-27 15:05:31 +08:00
|
|
|
void test_warn() {
|
|
|
|
memset(&x1, 0, sizeof x1); // \
|
2011-05-04 04:37:33 +08:00
|
|
|
// expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
|
2011-04-27 15:05:31 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
2015-03-22 01:56:44 +08:00
|
|
|
memset(x1arr, 0, sizeof x1arr); // \
|
|
|
|
// expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memset((void*)x1arr, 0, sizeof x1arr);
|
2011-06-03 14:23:57 +08:00
|
|
|
memset(&x2, 0, sizeof x2); // \
|
2011-05-04 04:37:33 +08:00
|
|
|
// expected-warning {{destination for this 'memset' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
|
|
|
|
memmove(&x1, 0, sizeof x1); // \
|
2014-06-28 07:58:21 +08:00
|
|
|
// expected-warning{{destination for this 'memmove' call is a pointer to dynamic class 'X1'; vtable pointer will be overwritten}} \
|
2011-05-04 04:37:33 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memmove(0, &x1, sizeof x1); // \
|
2014-06-28 07:58:21 +08:00
|
|
|
// expected-warning{{source of this 'memmove' call is a pointer to dynamic class 'X1'; vtable pointer will be moved}} \
|
2011-05-04 04:37:33 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memcpy(&x1, 0, sizeof x1); // \
|
2014-06-28 07:58:21 +08:00
|
|
|
// expected-warning{{destination for this 'memcpy' call is a pointer to dynamic class 'X1'; vtable pointer will be overwritten}} \
|
2011-05-04 04:37:33 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memcpy(0, &x1, sizeof x1); // \
|
2014-06-28 07:58:21 +08:00
|
|
|
// expected-warning{{source of this 'memcpy' call is a pointer to dynamic class 'X1'; vtable pointer will be copied}} \
|
2011-08-20 04:40:18 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memcmp(&x1, 0, sizeof x1); // \
|
2014-06-28 07:58:21 +08:00
|
|
|
// expected-warning{{first operand of this 'memcmp' call is a pointer to dynamic class 'X1'; vtable pointer will be compared}} \
|
2011-08-20 04:40:18 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memcmp(0, &x1, sizeof x1); // \
|
2014-06-28 07:58:21 +08:00
|
|
|
// expected-warning{{second operand of this 'memcmp' call is a pointer to dynamic class 'X1'; vtable pointer will be compared}} \
|
2011-04-27 15:05:31 +08:00
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
2011-06-17 01:56:04 +08:00
|
|
|
|
|
|
|
__builtin_memset(&x1, 0, sizeof x1); // \
|
|
|
|
// expected-warning {{destination for this '__builtin_memset' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin_memset(&x2, 0, sizeof x2); // \
|
|
|
|
// expected-warning {{destination for this '__builtin_memset' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
|
|
|
|
__builtin_memmove(&x1, 0, sizeof x1); // \
|
|
|
|
// expected-warning{{destination for this '__builtin_memmove' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin_memmove(0, &x1, sizeof x1); // \
|
|
|
|
// expected-warning{{source of this '__builtin_memmove' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin_memcpy(&x1, 0, sizeof x1); // \
|
|
|
|
// expected-warning{{destination for this '__builtin_memcpy' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin_memcpy(0, &x1, sizeof x1); // \
|
|
|
|
// expected-warning{{source of this '__builtin_memcpy' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
|
|
|
|
__builtin___memset_chk(&x1, 0, sizeof x1, sizeof x1); // \
|
|
|
|
// expected-warning {{destination for this '__builtin___memset_chk' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin___memset_chk(&x2, 0, sizeof x2, sizeof x2); // \
|
|
|
|
// expected-warning {{destination for this '__builtin___memset_chk' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
|
|
|
|
__builtin___memmove_chk(&x1, 0, sizeof x1, sizeof x1); // \
|
|
|
|
// expected-warning{{destination for this '__builtin___memmove_chk' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin___memmove_chk(0, &x1, sizeof x1, sizeof x1); // \
|
|
|
|
// expected-warning{{source of this '__builtin___memmove_chk' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin___memcpy_chk(&x1, 0, sizeof x1, sizeof x1); // \
|
|
|
|
// expected-warning{{destination for this '__builtin___memcpy_chk' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
|
|
|
__builtin___memcpy_chk(0, &x1, sizeof x1, sizeof x1); // \
|
|
|
|
// expected-warning{{source of this '__builtin___memcpy_chk' call is a pointer to dynamic class}} \
|
|
|
|
// expected-note {{explicitly cast the pointer to silence this warning}}
|
2014-06-28 07:58:21 +08:00
|
|
|
|
|
|
|
// expected-warning@+2 {{destination for this 'memset' call is a pointer to class containing a dynamic class 'X1'}}
|
|
|
|
// expected-note@+1 {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memset(&contains_dynamic, 0, sizeof(contains_dynamic));
|
|
|
|
// expected-warning@+2 {{destination for this 'memset' call is a pointer to class containing a dynamic class 'X1'}}
|
|
|
|
// expected-note@+1 {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memset(&deep_contains_dynamic, 0, sizeof(deep_contains_dynamic));
|
|
|
|
// expected-warning@+2 {{destination for this 'memset' call is a pointer to class containing a dynamic class 'X1'}}
|
|
|
|
// expected-note@+1 {{explicitly cast the pointer to silence this warning}}
|
|
|
|
memset(&contains_array_dynamic, 0, sizeof(contains_array_dynamic));
|
2011-04-27 15:05:31 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 02:48:59 +08:00
|
|
|
void test_nowarn(void *void_ptr) {
|
|
|
|
int i, *iptr;
|
|
|
|
float y;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
memset(&i, 0, sizeof i);
|
|
|
|
memset(&iptr, 0, sizeof iptr);
|
|
|
|
memset(&y, 0, sizeof y);
|
|
|
|
memset(&c, 0, sizeof c);
|
|
|
|
memset(void_ptr, 0, 42);
|
2011-04-27 15:05:31 +08:00
|
|
|
memset(&s1, 0, sizeof s1);
|
|
|
|
memset(&s2, 0, sizeof s2);
|
|
|
|
memset(&s3, 0, sizeof s3);
|
2011-04-29 17:46:08 +08:00
|
|
|
memset(&c1, 0, sizeof c1);
|
2011-04-27 15:05:31 +08:00
|
|
|
|
2014-06-28 07:58:21 +08:00
|
|
|
memset(&contains_pointer_dynamic, 0, sizeof(contains_pointer_dynamic));
|
|
|
|
|
2011-04-27 15:05:31 +08:00
|
|
|
// Unevaluated code shouldn't warn.
|
|
|
|
(void)sizeof memset(&x1, 0, sizeof x1);
|
|
|
|
|
|
|
|
// Dead code shouldn't warn.
|
|
|
|
if (false) memset(&x1, 0, sizeof x1);
|
|
|
|
}
|
2011-05-04 02:11:37 +08:00
|
|
|
|
|
|
|
namespace N {
|
|
|
|
void *memset(void *, int, unsigned);
|
|
|
|
void test_nowarn() {
|
|
|
|
N::memset(&x1, 0, sizeof x1);
|
|
|
|
}
|
|
|
|
}
|