Skip to content

[clang][Interp] Fix array subscript eval order #101804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2024
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
24 changes: 17 additions & 7 deletions clang/lib/AST/Interp/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,21 +1282,31 @@ bool Compiler<Emitter>::VisitImplicitValueInitExpr(

template <class Emitter>
bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
const Expr *Base = E->getBase();
const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
const Expr *Index = E->getIdx();

if (DiscardResult)
return this->discard(Base) && this->discard(Index);
return this->discard(LHS) && this->discard(RHS);

// Take pointer of LHS, add offset from RHS.
// What's left on the stack after this is a pointer.
if (!this->visit(Base))
return false;
// C++17's rules require us to evaluate the LHS first, regardless of which
// side is the base.
bool Success = true;
for (const Expr *SubExpr : {LHS, RHS}) {
if (!this->visit(SubExpr))
Success = false;
}

if (!this->visit(Index))
if (!Success)
return false;

PrimType IndexT = classifyPrim(Index->getType());
// If the index is first, we need to change that.
if (LHS == Index) {
if (!this->emitFlip(PT_Ptr, IndexT, E))
return false;
}

return this->emitArrayElemPtrPop(IndexT, E);
}

Expand Down
15 changes: 15 additions & 0 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,21 @@ bool Pop(InterpState &S, CodePtr OpPC) {
return true;
}

/// [Value1, Value2] -> [Value2, Value1]
template <PrimType TopName, PrimType BottomName>
bool Flip(InterpState &S, CodePtr OpPC) {
using TopT = typename PrimConv<TopName>::T;
using BottomT = typename PrimConv<BottomName>::T;

const auto &Top = S.Stk.pop<TopT>();
const auto &Bottom = S.Stk.pop<BottomT>();

S.Stk.push<TopT>(Top);
S.Stk.push<BottomT>(Bottom);

return true;
}

//===----------------------------------------------------------------------===//
// Const
//===----------------------------------------------------------------------===//
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/Interp/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ def Dup : Opcode {
let HasGroup = 1;
}

def Flip : Opcode {
let Types = [AllTypeClass, AllTypeClass];
let HasGroup = 1;
}

// [] -> []
def Invalid : Opcode {}
def Unsupported : Opcode {}
Expand Down
9 changes: 4 additions & 5 deletions clang/test/AST/Interp/eval-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace EvalOrder {
}
template <typename T> constexpr T &&b(T &&v) {
if (!done_a)
throw "wrong"; // expected-note 7{{not valid}}
throw "wrong"; // expected-note 5{{not valid}}
done_b = true;
return (T &&)v;
}
Expand Down Expand Up @@ -95,10 +95,9 @@ namespace EvalOrder {
constexpr int arr[3] = {};
SEQ(A(arr)[B(0)]);
SEQ(A(+arr)[B(0)]);
SEQ(A(0)[B(arr)]); // expected-error {{not an integral constant expression}} FIXME \
// expected-note 2{{in call to}}
SEQ(A(0)[B(+arr)]); // expected-error {{not an integral constant expression}} FIXME \
// expected-note 2{{in call to}}
SEQ(A(0)[B(arr)]);
SEQ(A(0)[B(+arr)]);

SEQ(A(ud)[B(0)]);

// Rule 7: a << b
Expand Down
Loading