Skip to content

Commit

Permalink
[clang][Interp] Fix post-inc/dec operator result
Browse files Browse the repository at this point in the history
We pushed the wrong value on the stack, always leaving a 0 behind.

Differential Revision: https://reviews.llvm.org/D148901
  • Loading branch information
tbaederr committed Apr 26, 2023
1 parent 4343699 commit 3185e47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
T Result;

if constexpr (DoPush == PushVal::Yes)
S.Stk.push<T>(Result);
S.Stk.push<T>(Value);

if constexpr (Op == IncDecOp::Inc) {
if (!T::increment(Value, &Result)) {
Expand Down
17 changes: 17 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,23 @@ namespace IncDec {
// ref-note {{cannot refer to element -1 of array of 3 elements}}
return *p;
}

/// This used to leave a 0 on the stack instead of the previous
/// value of a.
constexpr int bug1Inc() {
int a = 3;
int b = a++;
return b;
}
static_assert(bug1Inc() == 3);

constexpr int bug1Dec() {
int a = 3;
int b = a--;
return b;
}
static_assert(bug1Dec() == 3);

};
#endif

Expand Down

0 comments on commit 3185e47

Please sign in to comment.