Skip to content

Commit

Permalink
[clang][Interp] Handle discarding ConstantExprs
Browse files Browse the repository at this point in the history
Assume no side-effects in the presence of a cashed result in the form
of an APValue. This is also what the current interpreter does.
  • Loading branch information
tbaederr committed Feb 6, 2024
1 parent 54c29e0 commit 26db3c3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 9 additions & 6 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,14 +1030,17 @@ bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr(

template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
// Try to emit the APValue directly, without visiting the subexpr.
// This will only fail if we can't emit the APValue, so won't emit any
// diagnostics or any double values.
std::optional<PrimType> T = classify(E->getType());
if (T && E->hasAPValueResult() &&
this->visitAPValue(E->getAPValueResult(), *T, E))
return true;
if (T && E->hasAPValueResult()) {
// Try to emit the APValue directly, without visiting the subexpr.
// This will only fail if we can't emit the APValue, so won't emit any
// diagnostics or any double values.
if (DiscardResult)
return true;

if (this->visitAPValue(E->getAPValueResult(), *T, E))
return true;
}
return this->delegate(E->getSubExpr());
}

Expand Down
13 changes: 13 additions & 0 deletions clang/test/AST/Interp/cxx20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,16 @@ namespace TryCatch {
}
static_assert(foo() == 11);
}

namespace IgnoredConstantExpr {
consteval int immediate() { return 0;}
struct ReferenceToNestedMembers {
int m;
int a = ((void)immediate(), m);
int b = ((void)immediate(), this->m);
};
struct ReferenceToNestedMembersTest {
void* m = nullptr;
ReferenceToNestedMembers j{0};
} test_reference_to_nested_members;
}
2 changes: 2 additions & 0 deletions clang/test/SemaCXX/cxx11-default-member-initializers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %clang_cc1 -std=c++11 -verify %s -pedantic
// RUN: %clang_cc1 -std=c++11 -verify %s -pedantic -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++20 -verify %s -pedantic
// RUN: %clang_cc1 -std=c++20 -verify %s -pedantic -fexperimental-new-constant-interpreter


namespace PR31692 {
Expand Down

0 comments on commit 26db3c3

Please sign in to comment.