[clang] [clang][bytecode] Consider unknown-size arrays in memcpy/memcmp (PR #121462)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 2 01:33:23 PST 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/121462
When emitting diagnostics for the number of elements.
>From 26b2225c92cf30d75e91a1a43bdf55e6e624da5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 2 Jan 2025 10:27:30 +0100
Subject: [PATCH] [clang][bytecode] Consider unknown-size arrays in
memcpy/memcmp
When emitting diagnostics for the number of elements.
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 8 ++++++--
clang/test/AST/ByteCode/builtin-functions.cpp | 9 +++++++++
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index e9f3303f958d3e..b5849553d0bf53 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1871,7 +1871,9 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
size_t RemainingDestElems;
if (DestPtr.getFieldDesc()->isArray()) {
DestElemType = DestPtr.getFieldDesc()->getElemQualType();
- RemainingDestElems = (DestPtr.getNumElems() - DestPtr.getIndex());
+ RemainingDestElems = DestPtr.isUnknownSizeArray()
+ ? 0
+ : (DestPtr.getNumElems() - DestPtr.getIndex());
} else {
DestElemType = DestPtr.getType();
RemainingDestElems = 1;
@@ -1890,7 +1892,9 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
size_t RemainingSrcElems;
if (SrcPtr.getFieldDesc()->isArray()) {
SrcElemType = SrcPtr.getFieldDesc()->getElemQualType();
- RemainingSrcElems = (SrcPtr.getNumElems() - SrcPtr.getIndex());
+ RemainingSrcElems = SrcPtr.isUnknownSizeArray()
+ ? 0
+ : (SrcPtr.getNumElems() - SrcPtr.getIndex());
} else {
SrcElemType = SrcPtr.getType();
RemainingSrcElems = 1;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 0188e8297db528..723764010d9a3a 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1263,6 +1263,15 @@ namespace BuiltinMemcpy {
return arr[0].ok() && arr[1].ok() && arr[2].ok();
}
static_assert(test_trivial());
+
+ // Check that an incomplete array is rejected.
+ constexpr int test_incomplete_array_type() { // both-error {{never produces a constant}}
+ extern int arr[];
+ __builtin_memmove(arr, arr, 4 * sizeof(arr[0]));
+ // both-note at -1 2{{'memmove' not supported: source is not a contiguous array of at least 4 elements of type 'int'}}
+ return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
+ }
+ static_assert(test_incomplete_array_type() == 1234); // both-error {{constant}} both-note {{in call}}
}
namespace Memcmp {
More information about the cfe-commits
mailing list