Skip to content

Commit fda70bc

Browse files
[clang] Move warning about memset/memcpy to NonTriviallyCopyable types to its own flag
Namely -Wnontrivial-memcall, implied by -Wnontricial-memaccess This is a followup to llvm#111434
1 parent 2fe947b commit fda70bc

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ Modified Compiler Flags
404404
to utilize these vector libraries. The behavior for all other vector function
405405
libraries remains unchanged.
406406

407-
- The ``-Wnontrivial-memaccess`` warning has been updated to also warn about
407+
- The ``-Wnontrivial-memcall`` warning has been updated to also warn about
408408
passing non-trivially-copyable destrination parameter to ``memcpy``,
409409
``memset`` and similar functions for which it is a documented undefined
410410
behavior.

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,13 @@ def SizeofArrayDecay : DiagGroup<"sizeof-array-decay">;
683683
def SizeofPointerMemaccess : DiagGroup<"sizeof-pointer-memaccess">;
684684
def MemsetTransposedArgs : DiagGroup<"memset-transposed-args">;
685685
def DynamicClassMemaccess : DiagGroup<"dynamic-class-memaccess">;
686-
def NonTrivialMemaccess : DiagGroup<"nontrivial-memaccess">;
686+
def NonTrivialMemcall : DiagGroup<"nontrivial-memcall">;
687+
def NonTrivialMemaccess : DiagGroup<"nontrivial-memaccess", [NonTrivialMemcall]>;
687688
def SuspiciousBzero : DiagGroup<"suspicious-bzero">;
688689
def SuspiciousMemaccess : DiagGroup<"suspicious-memaccess",
689690
[SizeofPointerMemaccess, DynamicClassMemaccess,
690-
NonTrivialMemaccess, MemsetTransposedArgs, SuspiciousBzero]>;
691+
NonTrivialMemaccess, NonTrivialMemcall, MemsetTransposedArgs,
692+
SuspiciousBzero]>;
691693
def StaticInInline : DiagGroup<"static-in-inline">;
692694
def StaticLocalInInline : DiagGroup<"static-local-in-inline">;
693695
def GNUStaticFloatInit : DiagGroup<"gnu-static-float-init">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ def warn_cstruct_memaccess : Warning<
798798
def warn_cxxstruct_memaccess : Warning<
799799
"first argument in call to "
800800
"%0 is a pointer to non-trivially copyable type %1">,
801-
InGroup<NonTrivialMemaccess>;
801+
InGroup<NonTrivialMemcall>;
802802
def note_nontrivial_field : Note<
803803
"field is non-trivial to %select{copy|default-initialize}0">;
804804
def err_non_trivial_c_union_in_invalid_context : Error<

clang/test/SemaCXX/warn-memcall.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wnontrivial-memcall %s
2+
3+
extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
4+
5+
class TriviallyCopyable {};
6+
class NonTriviallyCopyable { NonTriviallyCopyable(const NonTriviallyCopyable&);};
7+
struct Incomplete;
8+
9+
void test_memcpy(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
10+
NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1,
11+
Incomplete *i0, Incomplete *i1) {
12+
// OK
13+
memcpy(tc0, tc1, sizeof(*tc0));
14+
15+
// OK
16+
memcpy(i0, i1, 10);
17+
18+
// expected-warning@+2{{first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
19+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
20+
memcpy(ntc0, ntc1, sizeof(*ntc0));
21+
22+
// ~ OK
23+
memcpy((void*)ntc0, ntc1, sizeof(*ntc0));
24+
25+
// OK
26+
memcpy((void*)ntc0, (void*)ntc1, sizeof(*ntc0));
27+
}

0 commit comments

Comments
 (0)