[clang] [clang][bytecode] Diagnose integral source/dest in memcpy (PR #132715)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 24 04:08:08 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Like the current interpreter does.
---
Full diff: https://github.com/llvm/llvm-project/pull/132715.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+11)
- (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+6)
``````````diff
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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/132715
More information about the cfe-commits
mailing list