Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1792,14 +1792,18 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
Pointer DestPtr = getParam<Pointer>(Frame, 0);
const ASTContext &ASTCtx = S.getASTContext();
const Pointer &SrcPtr = getParam<Pointer>(Frame, 1);
const APSInt &Size =
peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)));
APSInt Size = peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)));
assert(!Size.isSigned() && "memcpy and friends take an unsigned size");

if (ID == Builtin::BImemcpy || ID == Builtin::BImemmove)
diagnoseNonConstexprBuiltin(S, OpPC, ID);

bool Move = (ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove);
bool Move =
(ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove ||
ID == Builtin::BI__builtin_wmemmove || ID == Builtin::BIwmemmove);
bool WChar = ID == Builtin::BIwmemcpy || ID == Builtin::BIwmemmove ||
ID == Builtin::BI__builtin_wmemcpy ||
ID == Builtin::BI__builtin_wmemmove;

// If the size is zero, we treat this as always being a valid no-op.
if (Size.isZero()) {
Expand All @@ -1810,7 +1814,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
if (SrcPtr.isZero() || DestPtr.isZero()) {
Pointer DiagPtr = (SrcPtr.isZero() ? SrcPtr : DestPtr);
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
<< /*IsMove=*/Move << /*IsWchar=*/false << !SrcPtr.isZero()
<< /*IsMove=*/Move << /*IsWchar=*/WChar << !SrcPtr.isZero()
<< DiagPtr.toDiagnosticString(ASTCtx);
return false;
}
Expand All @@ -1822,7 +1826,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
? 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;
<< Move << WChar << DestPtr.isIntegralPointer() << DiagVal;
return false;
}

Expand All @@ -1841,11 +1845,17 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
}
unsigned DestElemSize = ASTCtx.getTypeSizeInChars(DestElemType).getQuantity();

if (WChar) {
uint64_t WCharSize =
ASTCtx.getTypeSizeInChars(ASTCtx.getWCharType()).getQuantity();
Size *= APSInt(APInt(Size.getBitWidth(), WCharSize, /*IsSigned=*/false),
/*IsUnsigend=*/true);
}

if (Size.urem(DestElemSize) != 0) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_memcpy_unsupported)
<< Move << /*IsWchar=*/false << 0 << DestElemType << Size
<< DestElemSize;
<< Move << WChar << 0 << DestElemType << Size << DestElemSize;
return false;
}

Expand Down Expand Up @@ -1873,7 +1883,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
APInt N = Size.udiv(DestElemSize);
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_memcpy_unsupported)
<< Move << /*IsWChar*/ false << (Size.ugt(RemainingSrcBytes) ? 1 : 2)
<< Move << WChar << (Size.ugt(RemainingSrcBytes) ? 1 : 2)
<< DestElemType << toString(N, 10, /*Signed=*/false);
return false;
}
Expand Down Expand Up @@ -2591,8 +2601,12 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,

case Builtin::BI__builtin_memcpy:
case Builtin::BImemcpy:
case Builtin::BI__builtin_wmemcpy:
case Builtin::BIwmemcpy:
case Builtin::BI__builtin_memmove:
case Builtin::BImemmove:
case Builtin::BI__builtin_wmemmove:
case Builtin::BIwmemmove:
if (!interp__builtin_memcpy(S, OpPC, Frame, F, Call))
return false;
break;
Expand Down
62 changes: 62 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" {
extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
extern int wcscmp(const wchar_t *s1, const wchar_t *s2);
extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n);
extern wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n);
}

namespace strcmp {
Expand Down Expand Up @@ -1592,6 +1593,67 @@ namespace WMemChr {
// both-note {{non-constexpr function 'wcschr' cannot be used in a constant expression}}
}

namespace WMemCpy {
template<typename T>
constexpr T result(T (&arr)[4]) {
return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
}
constexpr int test_wmemcpy(int a, int b, int n) {
wchar_t arr[4] = {1, 2, 3, 4};
__builtin_wmemcpy(arr + a, arr + b, n);
// both-note@-1 2{{overlapping memory regions}}
// both-note@-2 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
// both-note@-3 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
return result(arr);
}
static_assert(test_wmemcpy(1, 2, 1) == 1334);
static_assert(test_wmemcpy(2, 1, 1) == 1224);
static_assert(test_wmemcpy(0, 1, 2) == 2334); // both-error {{constant}} both-note {{in call}}
static_assert(test_wmemcpy(1, 0, 2) == 1124); // both-error {{constant}} both-note {{in call}}
static_assert(test_wmemcpy(1, 2, 1) == 1334);
static_assert(test_wmemcpy(0, 3, 1) == 4234);
static_assert(test_wmemcpy(0, 3, 2) == 4234); // both-error {{constant}} both-note {{in call}}
static_assert(test_wmemcpy(2, 0, 3) == 4234); // both-error {{constant}} both-note {{in call}}

wchar_t global;
constexpr wchar_t *null = 0;
static_assert(__builtin_wmemcpy(&global, null, sizeof(wchar_t))); // both-error {{}} \
// both-note {{source of 'wmemcpy' is nullptr}}
static_assert(__builtin_wmemcpy(null, &global, sizeof(wchar_t))); // both-error {{}} \
// both-note {{destination of 'wmemcpy' is nullptr}}
}

namespace WMemMove {
template<typename T>
constexpr T result(T (&arr)[4]) {
return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
}

constexpr int test_wmemmove(int a, int b, int n) {
wchar_t arr[4] = {1, 2, 3, 4};
__builtin_wmemmove(arr + a, arr + b, n);
// both-note@-1 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
// both-note@-2 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
return result(arr);
}

static_assert(test_wmemmove(1, 2, 1) == 1334);
static_assert(test_wmemmove(2, 1, 1) == 1224);
static_assert(test_wmemmove(0, 1, 2) == 2334);
static_assert(test_wmemmove(1, 0, 2) == 1124);
static_assert(test_wmemmove(1, 2, 1) == 1334);
static_assert(test_wmemmove(0, 3, 1) == 4234);
static_assert(test_wmemmove(0, 3, 2) == 4234); // both-error {{constant}} both-note {{in call}}
static_assert(test_wmemmove(2, 0, 3) == 4234); // both-error {{constant}} both-note {{in call}}

wchar_t global;
constexpr wchar_t *null = 0;
static_assert(__builtin_wmemmove(&global, null, sizeof(wchar_t))); // both-error {{}} \
// both-note {{source of 'wmemmove' is nullptr}}
static_assert(__builtin_wmemmove(null, &global, sizeof(wchar_t))); // both-error {{}} \
// both-note {{destination of 'wmemmove' is nullptr}}
}

namespace Invalid {
constexpr int test() { // both-error {{never produces a constant expression}}
__builtin_abort(); // both-note 2{{subexpression not valid in a constant expression}}
Expand Down
Loading