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
9 changes: 8 additions & 1 deletion clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,15 @@ bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT))
return false;

QualType ElemType = LHS->getType()->getPointeeType();
CharUnits ElemTypeSize;
if (ElemType->isVoidType() || ElemType->isFunctionType())
ElemTypeSize = CharUnits::One();
else
ElemTypeSize = Ctx.getASTContext().getTypeSizeInChars(ElemType);

PrimType IntT = classifyPrim(E->getType());
if (!this->emitSubPtr(IntT, E))
if (!this->emitSubPtr(IntT, ElemTypeSize.isZero(), E))
return false;
return DiscardResult ? this->emitPop(IntT, E) : true;
}
Expand Down
32 changes: 15 additions & 17 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,7 @@ static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
/// 2) Pops another Pointer from the stack.
/// 3) Pushes the difference of the indices of the two pointers on the stack.
template <PrimType Name, class T = typename PrimConv<Name>::T>
inline bool SubPtr(InterpState &S, CodePtr OpPC) {
inline bool SubPtr(InterpState &S, CodePtr OpPC, bool ElemSizeIsZero) {
const Pointer &LHS = S.Stk.pop<Pointer>();
const Pointer &RHS = S.Stk.pop<Pointer>();

Expand All @@ -2402,25 +2402,23 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
return false;
}

if (LHS == RHS) {
S.Stk.push<T>();
return true;
}
if (ElemSizeIsZero) {
QualType PtrT = LHS.getType();
while (auto *AT = dyn_cast<ArrayType>(PtrT))
PtrT = AT->getElementType();

for (const Pointer &P : {LHS, RHS}) {
if (P.isZeroSizeArray()) {
QualType PtrT = P.getType();
while (auto *AT = dyn_cast<ArrayType>(PtrT))
PtrT = AT->getElementType();
QualType ArrayTy = S.getASTContext().getConstantArrayType(
PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_pointer_subtraction_zero_size)
<< ArrayTy;

QualType ArrayTy = S.getASTContext().getConstantArrayType(
PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_pointer_subtraction_zero_size)
<< ArrayTy;
return false;
}

return false;
}
if (LHS == RHS) {
S.Stk.push<T>();
return true;
}

int64_t A64 =
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ def SubOffset : AluOpcode;
// [Pointer, Pointer] -> [Integral]
def SubPtr : Opcode {
let Types = [IntegerTypeClass];
let Args = [ArgBool];
let HasGroup = 1;
}

Expand Down
4 changes: 4 additions & 0 deletions clang/test/AST/ByteCode/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ namespace ZeroSizeTypes {
// both-note {{subtraction of pointers to type 'int[0]' of zero size}} \
// both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}

constexpr int k2 = p1 - p1; // both-error {{constexpr variable 'k2' must be initialized by a constant expression}} \
// both-note {{subtraction of pointers to type 'int[0]' of zero size}} \
// both-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}

int arr[5][0];
constexpr int f() { // both-error {{never produces a constant expression}}
return &arr[3] - &arr[0]; // both-note {{subtraction of pointers to type 'int[0]' of zero size}} \
Expand Down
10 changes: 10 additions & 0 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,16 @@ namespace HugeAllocation {
}
#endif

namespace ZeroSizeArray {
constexpr int foo() {
int *A = new int[0];
int diff = A - (&A[0]);
delete[] A;
return diff;
}
static_assert(foo() == 0);
}

#else
/// Make sure we reject this prior to C++20
constexpr int a() { // both-error {{never produces a constant expression}}
Expand Down
Loading