[clang] [clang][bytecode] Diagnose integral source/dest in memcpy (PR #132715)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 24 04:07:31 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/132715
Like the current interpreter does.
>From 698d310421651db1320d3a60fd7fa9a44c25627e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 24 Mar 2025 12:05:57 +0100
Subject: [PATCH] [clang][bytecode] Diagnose integral source/dest in memcpy
Like the current interpreter does.
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 11 +++++++++++
clang/test/AST/ByteCode/builtin-functions.cpp | 6 ++++++
2 files changed, 17 insertions(+)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 4a56e73239675..504e25de46aa1 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1792,6 +1792,17 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
return false;
}
+ // Diagnose integral src/dest pointers specially.
+ if (SrcPtr.isIntegralPointer() || DestPtr.isIntegralPointer()) {
+ std::string DiagVal = "(void *)";
+ DiagVal += SrcPtr.isIntegralPointer()
+ ? std::to_string(SrcPtr.getIntegerRepresentation())
+ : std::to_string(DestPtr.getIntegerRepresentation());
+ S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
+ << Move << false << DestPtr.isIntegralPointer() << DiagVal;
+ return false;
+ }
+
// Can't read from dummy pointers.
if (DestPtr.isDummy() || SrcPtr.isDummy())
return false;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 13a34f71a6354..4bff2cc6283ec 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1289,6 +1289,12 @@ namespace BuiltinMemcpy {
return Result1 && Result2;
}
static_assert(memmoveOverlapping());
+
+#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
+ static_assert(__builtin_memcpy(&global, fold((wchar_t*)123), sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
+ // both-note {{source of 'memcpy' is (void *)123}}
+ static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
+ // both-note {{destination of 'memcpy' is (void *)123}}
}
namespace Memcmp {
More information about the cfe-commits
mailing list