diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.h b/clang/lib/AST/Interp/ByteCodeExprGen.h --- a/clang/lib/AST/Interp/ByteCodeExprGen.h +++ b/clang/lib/AST/Interp/ByteCodeExprGen.h @@ -87,6 +87,7 @@ bool VisitCharacterLiteral(const CharacterLiteral *E); bool VisitCompoundAssignOperator(const CompoundAssignOperator *E); bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E); + bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E); bool VisitExprWithCleanups(const ExprWithCleanups *E); bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -685,6 +685,41 @@ return this->emitStore(*LT, E); } +template +bool ByteCodeExprGen::VisitPointerCompoundAssignOperator( + const CompoundAssignOperator *E) { + BinaryOperatorKind Op = E->getOpcode(); + const Expr *LHS = E->getLHS(); + const Expr *RHS = E->getRHS(); + std::optional LT = classify(LHS->getType()); + std::optional RT = classify(RHS->getType()); + + if (Op != BO_AddAssign && Op != BO_SubAssign) + return false; + + if (!LT || !RT) + return false; + assert(*LT == PT_Ptr); + + if (!visit(LHS)) + return false; + + if (!this->emitLoadPtr(LHS)) + return false; + + if (!visit(RHS)) + return false; + + if (Op == BO_AddAssign) + this->emitAddOffset(*RT, E); + else + this->emitSubOffset(*RT, E); + + if (DiscardResult) + return this->emitStorePopPtr(E); + return this->emitStorePtr(E); +} + template bool ByteCodeExprGen::VisitCompoundAssignOperator( const CompoundAssignOperator *E) { @@ -694,6 +729,9 @@ if (E->getType()->isFloatingType()) return VisitFloatCompoundAssignOperator(E); + if (E->getType()->isPointerType()) + return VisitPointerCompoundAssignOperator(E); + const Expr *LHS = E->getLHS(); const Expr *RHS = E->getRHS(); std::optional LHSComputationT = @@ -705,9 +743,7 @@ if (!LT || !RT || !ResultT || !LHSComputationT) return false; - assert(!E->getType()->isPointerType() && - "Support pointer arithmethic in compound assignment operators"); - + assert(!E->getType()->isPointerType() && "Handled above"); assert(!E->getType()->isFloatingType() && "Handled above"); // Get LHS pointer, load its value and get RHS value. diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp --- a/clang/test/AST/Interp/literals.cpp +++ b/clang/test/AST/Interp/literals.cpp @@ -669,5 +669,42 @@ // expected-note {{in call to 'IntMul}} \ // ref-error {{not an integral constant expression}} \ // ref-note {{in call to 'IntMul}} + constexpr int arr[] = {1,2,3}; + constexpr int ptrInc1() { + const int *p = arr; + p += 2; + return *p; + } + static_assert(ptrInc1() == 3, ""); + + constexpr int ptrInc2() { + const int *p = arr; + return *(p += 1); + } + static_assert(ptrInc2() == 2, ""); + + constexpr int ptrInc3() { // expected-error {{never produces a constant expression}} \ + // ref-error {{never produces a constant expression}} + const int *p = arr; + p += 12; // expected-note {{cannot refer to element 12 of array of 3 elements}} \ + // ref-note {{cannot refer to element 12 of array of 3 elements}} + return *p; + } + + constexpr int ptrIncDec1() { + const int *p = arr; + p += 2; + p -= 1; + return *p; + } + static_assert(ptrIncDec1() == 2, ""); + + constexpr int ptrDec1() { // expected-error {{never produces a constant expression}} \ + // ref-error {{never produces a constant expression}} + const int *p = arr; + p -= 1; // expected-note {{cannot refer to element -1 of array of 3 elements}} \ + // ref-note {{cannot refer to element -1 of array of 3 elements}} + return *p; + } }; #endif