Skip to content

Commit 9ab3b6a

Browse files
authored
[clang][bytecode] Diagnose integral source/dest in memcpy (llvm#132715)
Like the current interpreter does.
1 parent bf2d30e commit 9ab3b6a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,17 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
17921792
return false;
17931793
}
17941794

1795+
// Diagnose integral src/dest pointers specially.
1796+
if (SrcPtr.isIntegralPointer() || DestPtr.isIntegralPointer()) {
1797+
std::string DiagVal = "(void *)";
1798+
DiagVal += SrcPtr.isIntegralPointer()
1799+
? std::to_string(SrcPtr.getIntegerRepresentation())
1800+
: std::to_string(DestPtr.getIntegerRepresentation());
1801+
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
1802+
<< Move << false << DestPtr.isIntegralPointer() << DiagVal;
1803+
return false;
1804+
}
1805+
17951806
// Can't read from dummy pointers.
17961807
if (DestPtr.isDummy() || SrcPtr.isDummy())
17971808
return false;

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,12 @@ namespace BuiltinMemcpy {
12901290
return Result1 && Result2;
12911291
}
12921292
static_assert(memmoveOverlapping());
1293+
1294+
#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
1295+
static_assert(__builtin_memcpy(&global, fold((wchar_t*)123), sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1296+
// both-note {{source of 'memcpy' is (void *)123}}
1297+
static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1298+
// both-note {{destination of 'memcpy' is (void *)123}}
12931299
}
12941300

12951301
namespace Memcmp {

0 commit comments

Comments
 (0)