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
14 changes: 12 additions & 2 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,11 +1881,21 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
bool IsWide =
(ID == Builtin::BIwmemcmp || ID == Builtin::BI__builtin_wmemcmp);

auto getElemType = [](const Pointer &P) -> QualType {
const Descriptor *Desc = P.getFieldDesc();
QualType T = Desc->getType();
if (T->isPointerType())
return T->getAs<PointerType>()->getPointeeType();
if (Desc->isArray())
return Desc->getElemQualType();
return T;
};

const ASTContext &ASTCtx = S.getASTContext();
// FIXME: This is an arbitrary limitation the current constant interpreter
// had. We could remove this.
if (!IsWide && (!isOneByteCharacterType(PtrA.getType()) ||
!isOneByteCharacterType(PtrB.getType()))) {
if (!IsWide && (!isOneByteCharacterType(getElemType(PtrA)) ||
!isOneByteCharacterType(getElemType(PtrB)))) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_memcmp_unsupported)
<< ASTCtx.BuiltinInfo.getQuotedName(ID) << PtrA.getType()
Expand Down
15 changes: 15 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,21 @@ namespace Memcmp {
static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1);
static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1);
static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0);

#if __cplusplus >= 202002L
constexpr bool f() {
char *c = new char[12];
c[0] = 'b';

char n = 'a';
bool b = __builtin_memcmp(c, &n, 1) == 0;

delete[] c;
return !b;
}
static_assert(f());
#endif

}

namespace Memchr {
Expand Down