[clang] [clang] Fix 71315698c9 in presence of incomplete types (PR #114095)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 29 10:35:51 PDT 2024
https://github.com/serge-sans-paille created https://github.com/llvm/llvm-project/pull/114095
Incomplete types are not considered trivially copyable by clang but we don't want to warn about invalid argument for memcpy / memset in that case because we cannot prove they are not Trivially Copyable.
>From d5807d319f565cbeaf6cf02ea7ca187691ead4e5 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Tue, 29 Oct 2024 18:31:13 +0100
Subject: [PATCH] [clang] Fix 71315698c9 in presence of incomplete types
Incomplete types are not considered trivially copyable by clang but we
don't want to warn about invalid argument for memcpy / memset in that
case because we cannot prove they are not Trivially Copyable.
---
clang/lib/Sema/SemaChecking.cpp | 7 ++++---
clang/test/SemaCXX/warn-memaccess.cpp | 25 +++++++++++++++++++++----
2 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3308b898a5b68f..577218ac4f0ab2 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8900,7 +8900,8 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
<< Call->getCallee()->getSourceRange());
else if (const auto *RT = PointeeTy->getAs<RecordType>()) {
- bool IsTriviallyCopyableCXXRecord =
+ bool MayBeTriviallyCopyableCXXRecord =
+ RT->isIncompleteType() ||
RT->desugar().isTriviallyCopyableType(Context);
if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
@@ -8910,7 +8911,7 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
<< ArgIdx << FnName << PointeeTy << 0);
SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
} else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
- !IsTriviallyCopyableCXXRecord && ArgIdx == 0) {
+ !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) {
// FIXME: Limiting this warning to dest argument until we decide
// whether it's valid for source argument too.
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
@@ -8923,7 +8924,7 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
<< ArgIdx << FnName << PointeeTy << 1);
SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
} else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
- !IsTriviallyCopyableCXXRecord && ArgIdx == 0) {
+ !MayBeTriviallyCopyableCXXRecord && 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/SemaCXX/warn-memaccess.cpp b/clang/test/SemaCXX/warn-memaccess.cpp
index b4b7f6a6905b23..070b44891a91aa 100644
--- a/clang/test/SemaCXX/warn-memaccess.cpp
+++ b/clang/test/SemaCXX/warn-memaccess.cpp
@@ -7,12 +7,17 @@ extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
class TriviallyCopyable {};
class NonTriviallyCopyable { NonTriviallyCopyable(const NonTriviallyCopyable&);};
+struct Incomplete;
void test_bzero(TriviallyCopyable* tc,
- NonTriviallyCopyable *ntc) {
+ NonTriviallyCopyable *ntc,
+ Incomplete* i) {
// OK
bzero(tc, sizeof(*tc));
+ // OK
+ bzero(i, 10);
+
// expected-warning at +2{{first argument in call to 'bzero' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
// expected-note at +1{{explicitly cast the pointer to silence this warning}}
bzero(ntc, sizeof(*ntc));
@@ -22,10 +27,14 @@ void test_bzero(TriviallyCopyable* tc,
}
void test_memset(TriviallyCopyable* tc,
- NonTriviallyCopyable *ntc) {
+ NonTriviallyCopyable *ntc,
+ Incomplete* i) {
// OK
memset(tc, 0, sizeof(*tc));
+ // OK
+ memset(i, 0, 10);
+
// expected-warning at +2{{first argument in call to 'memset' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
// expected-note at +1{{explicitly cast the pointer to silence this warning}}
memset(ntc, 0, sizeof(*ntc));
@@ -36,10 +45,14 @@ void test_memset(TriviallyCopyable* tc,
void test_memcpy(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
- NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1) {
+ 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));
@@ -52,10 +65,14 @@ void test_memcpy(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
}
void test_memmove(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
- NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1) {
+ NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1,
+ Incomplete *i0, Incomplete *i1) {
// OK
memmove(tc0, tc1, sizeof(*tc0));
+ // OK
+ memmove(i0, i1, 10);
+
// expected-warning at +2{{first argument in call to 'memmove' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
// expected-note at +1{{explicitly cast the pointer to silence this warning}}
memmove(ntc0, ntc1, sizeof(*ntc0));
More information about the cfe-commits
mailing list