[clang] 6bb4ce0 - Silence spurious -Wnontrivial-memcall warnings in C mode (#137429)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 29 18:38:01 PDT 2025
Author: Akira Hatanaka
Date: 2025-04-29T18:37:57-07:00
New Revision: 6bb4ce0f6ff462b58bacc8dbc47719b7009f9b18
URL: https://github.com/llvm/llvm-project/commit/6bb4ce0f6ff462b58bacc8dbc47719b7009f9b18
DIFF: https://github.com/llvm/llvm-project/commit/6bb4ce0f6ff462b58bacc8dbc47719b7009f9b18.diff
LOG: Silence spurious -Wnontrivial-memcall warnings in C mode (#137429)
clang currently issues a warning when memset is used on a struct that
contains an address-discriminated pointer field, even though this is
entirely valid behavior.
For example:
```
struct S {
int * __ptrauth(1, 1, 100) p;
} s;
memset(&s, 0, sizeof(struct S));
```
Only allow the warning to be emitted in C++ mode to silence the warning.
rdar://142495870
Added:
clang/test/Sema/warn-nontrivial-struct-memaccess-ptrauth.c
Modified:
clang/lib/Sema/SemaChecking.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 018d121ad8ab5..d2cad8a6fd608 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -9735,9 +9735,9 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
// completed later. GCC does not diagnose such code, but we may want to
// consider diagnosing it in the future, perhaps under a
diff erent, but
// related, diagnostic group.
- bool MayBeTriviallyCopyableCXXRecord =
- RT->isIncompleteType() ||
- RT->desugar().isTriviallyCopyableType(Context);
+ bool NonTriviallyCopyableCXXRecord =
+ getLangOpts().CPlusPlus && !RT->isIncompleteType() &&
+ !RT->desugar().isTriviallyCopyableType(Context);
if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) {
@@ -9746,7 +9746,7 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
<< ArgIdx << FnName << PointeeTy << 0);
SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
} else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
- !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) {
+ NonTriviallyCopyableCXXRecord && ArgIdx == 0) {
// FIXME: Limiting this warning to dest argument until we decide
// whether it's valid for source argument too.
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
@@ -9759,7 +9759,7 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
<< ArgIdx << FnName << PointeeTy << 1);
SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
} else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
- !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) {
+ NonTriviallyCopyableCXXRecord && ArgIdx == 0) {
// FIXME: Limiting this warning to dest argument until we decide
// whether it's valid for source argument too.
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
diff --git a/clang/test/Sema/warn-nontrivial-struct-memaccess-ptrauth.c b/clang/test/Sema/warn-nontrivial-struct-memaccess-ptrauth.c
new file mode 100644
index 0000000000000..9cdb98e55458b
--- /dev/null
+++ b/clang/test/Sema/warn-nontrivial-struct-memaccess-ptrauth.c
@@ -0,0 +1,60 @@
+// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics -fsyntax-only -verify=c,expected %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics -fsyntax-only -verify=c,expected %s
+// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics -fsyntax-only -x c++ -verify=cxx,expected %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics -fsyntax-only -x c++ -verify=cxx,expected %s
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+void *memset(void *, int, __SIZE_TYPE__);
+void bzero(void *, __SIZE_TYPE__);
+void *memcpy(void *, const void *, __SIZE_TYPE__);
+void *memmove(void *, const void *, __SIZE_TYPE__);
+
+#if defined __cplusplus
+}
+#endif
+
+#define AQ __ptrauth(1,1,50)
+#define IQ __ptrauth(1,0,50)
+
+struct PtrAuthTrivial {
+ int f0;
+ int * IQ f1;
+};
+
+struct PtrAuthNonTrivial0 {
+ int f0;
+ int * AQ f1; // c-note 2 {{non-trivial to copy}}
+ int f2;
+};
+
+struct PtrAuthNonTrivial1 {
+ int * AQ f0; // c-note 2 {{non-trivial to copy}}
+ int f1;
+ struct PtrAuthNonTrivial0 f2;
+};
+
+void testPtrAuthTrivial(struct PtrAuthTrivial *d, struct PtrAuthTrivial *s) {
+ memset(d, 0, sizeof(struct PtrAuthTrivial));
+ memset(d, 1, sizeof(struct PtrAuthTrivial));
+ bzero(d, sizeof(struct PtrAuthTrivial));
+ memcpy(d, s, sizeof(struct PtrAuthTrivial));
+ memmove(d, s, sizeof(struct PtrAuthTrivial));
+}
+
+void testPtrAuthNonTrivial1(struct PtrAuthNonTrivial1 *d,
+ struct PtrAuthNonTrivial1 *s) {
+ memset(d, 0, sizeof(struct PtrAuthNonTrivial1)); // cxx-warning {{is a pointer to non-trivially copyable type 'struct PtrAuthNonTrivial1'}} // cxx-note {{explicitly cast the pointer to silence}}
+ memset(d, 1, sizeof(struct PtrAuthNonTrivial1)); // cxx-warning {{is a pointer to non-trivially copyable type 'struct PtrAuthNonTrivial1'}} // cxx-note {{explicitly cast the pointer to silence}}
+ bzero(d, sizeof(struct PtrAuthNonTrivial1)); // cxx-warning {{is a pointer to non-trivially copyable type 'struct PtrAuthNonTrivial1'}} // cxx-note {{explicitly cast the pointer to silence}}
+ memcpy(d, s, sizeof(struct PtrAuthNonTrivial1));
+ // c-warning at -1 {{that is not trivial to primitive-copy}}
+ // cxx-warning at -2 {{is a pointer to non-trivially copyable type 'struct PtrAuthNonTrivial1'}}
+ // expected-note at -3 {{explicitly cast the pointer to silence}}
+ memmove(d, s, sizeof(struct PtrAuthNonTrivial1));
+ // c-warning at -1 {{that is not trivial to primitive-copy}}
+ // cxx-warning at -2 {{is a pointer to non-trivially copyable type 'struct PtrAuthNonTrivial1'}}
+ // expected-note at -3 {{explicitly cast the pointer to silence}}
+}
More information about the cfe-commits
mailing list