Skip to content

Commit 0f73248

Browse files
authored
[clang][bytecode] Fix getting pointer element type in __builtin_memcmp (#130485)
When such a pointer is heap allocated, the type we get is a pointer type. Take the pointee type in that case.
1 parent d84dc8f commit 0f73248

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,11 +1881,21 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
18811881
bool IsWide =
18821882
(ID == Builtin::BIwmemcmp || ID == Builtin::BI__builtin_wmemcmp);
18831883

1884+
auto getElemType = [](const Pointer &P) -> QualType {
1885+
const Descriptor *Desc = P.getFieldDesc();
1886+
QualType T = Desc->getType();
1887+
if (T->isPointerType())
1888+
return T->getAs<PointerType>()->getPointeeType();
1889+
if (Desc->isArray())
1890+
return Desc->getElemQualType();
1891+
return T;
1892+
};
1893+
18841894
const ASTContext &ASTCtx = S.getASTContext();
18851895
// FIXME: This is an arbitrary limitation the current constant interpreter
18861896
// had. We could remove this.
1887-
if (!IsWide && (!isOneByteCharacterType(PtrA.getType()) ||
1888-
!isOneByteCharacterType(PtrB.getType()))) {
1897+
if (!IsWide && (!isOneByteCharacterType(getElemType(PtrA)) ||
1898+
!isOneByteCharacterType(getElemType(PtrB)))) {
18891899
S.FFDiag(S.Current->getSource(OpPC),
18901900
diag::note_constexpr_memcmp_unsupported)
18911901
<< ASTCtx.BuiltinInfo.getQuotedName(ID) << PtrA.getType()

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,21 @@ namespace Memcmp {
13521352
static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1);
13531353
static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1);
13541354
static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0);
1355+
1356+
#if __cplusplus >= 202002L
1357+
constexpr bool f() {
1358+
char *c = new char[12];
1359+
c[0] = 'b';
1360+
1361+
char n = 'a';
1362+
bool b = __builtin_memcmp(c, &n, 1) == 0;
1363+
1364+
delete[] c;
1365+
return !b;
1366+
}
1367+
static_assert(f());
1368+
#endif
1369+
13551370
}
13561371

13571372
namespace Memchr {

0 commit comments

Comments
 (0)