Skip to content

Commit 26db3c3

Browse files
committed
[clang][Interp] Handle discarding ConstantExprs
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.
1 parent 54c29e0 commit 26db3c3

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,14 +1030,17 @@ bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr(
10301030

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

1041+
if (this->visitAPValue(E->getAPValueResult(), *T, E))
1042+
return true;
1043+
}
10411044
return this->delegate(E->getSubExpr());
10421045
}
10431046

clang/test/AST/Interp/cxx20.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,16 @@ namespace TryCatch {
752752
}
753753
static_assert(foo() == 11);
754754
}
755+
756+
namespace IgnoredConstantExpr {
757+
consteval int immediate() { return 0;}
758+
struct ReferenceToNestedMembers {
759+
int m;
760+
int a = ((void)immediate(), m);
761+
int b = ((void)immediate(), this->m);
762+
};
763+
struct ReferenceToNestedMembersTest {
764+
void* m = nullptr;
765+
ReferenceToNestedMembers j{0};
766+
} test_reference_to_nested_members;
767+
}

clang/test/SemaCXX/cxx11-default-member-initializers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang_cc1 -std=c++11 -verify %s -pedantic
2+
// RUN: %clang_cc1 -std=c++11 -verify %s -pedantic -fexperimental-new-constant-interpreter
23
// RUN: %clang_cc1 -std=c++20 -verify %s -pedantic
4+
// RUN: %clang_cc1 -std=c++20 -verify %s -pedantic -fexperimental-new-constant-interpreter
35

46

57
namespace PR31692 {

0 commit comments

Comments
 (0)