[clang] [clang] Move warning about memset/memcpy to NonTriviallyCopyable type… (PR #117387)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 28 01:48:22 PST 2024


================
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wnontrivial-memcall %s
+
+extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
+
+class TriviallyCopyable {};
+class NonTriviallyCopyable { NonTriviallyCopyable(const NonTriviallyCopyable&);};
+struct Incomplete;
+
+void test_memcpy(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
+                 NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1,
+                 Incomplete *i0, Incomplete *i1) {
+  // OK
+  memcpy(tc0, tc1, sizeof(*tc0));
+
+  // OK
+  memcpy(i0, i1, 10);
+
+  // expected-warning at +2{{first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
+  // expected-note at +1{{explicitly cast the pointer to silence this warning}}
+  memcpy(ntc0, ntc1, sizeof(*ntc0));
+
+  // ~ OK
+  memcpy((void*)ntc0, ntc1, sizeof(*ntc0));
+
+  // OK
+  memcpy((void*)ntc0, (void*)ntc1, sizeof(*ntc0));
+}
----------------
zmodem wrote:

But it looks to me that clang/test/SemaCXX/warn-memaccess.cpp also only covers -Wnontrivial-memcall, i.e. it's just checking calls to bzero/memset/memmove/memcpy. Can you just change the flag there?

https://github.com/llvm/llvm-project/pull/117387


More information about the cfe-commits mailing list