Skip to content

Commit 517b64d

Browse files
[MRE] cant resolve static variable offset for templated class
1 parent 59a5e99 commit 517b64d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,8 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D,
14921492
#ifdef CPPINTEROP_USE_CLING
14931493
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
14941494
#endif // CPPINTEROP_USE_CLING
1495-
getSema().InstantiateVariableDefinition(SourceLocation(), VD);
1495+
getSema().InstantiateVariableDefinition(SourceLocation(), VD, true,
1496+
true);
14961497
}
14971498
if (VD->hasInit() &&
14981499
(VD->isConstexpr() || VD->getType().isConstQualified())) {

unittests/CppInterOp/VariableReflectionTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ TEST(VariableReflectionTest, GetVariableOffset) {
318318
EXPECT_EQ(Cpp::GetVariableOffset(datamembers[0]), offsetof(K, x));
319319
EXPECT_EQ(Cpp::GetVariableOffset(datamembers[1]), offsetof(K, y));
320320
EXPECT_EQ(Cpp::GetVariableOffset(datamembers[2]), offsetof(K, z));
321+
322+
Cpp::Declare(R"(
323+
template <typename T> struct ClassWithStatic {
324+
static T const ref_value;
325+
};
326+
template <typename T> T constexpr ClassWithStatic<T>::ref_value = 42;
327+
)");
328+
329+
Cpp::TCppScope_t klass = Cpp::GetNamed("ClassWithStatic");
330+
EXPECT_TRUE(klass);
331+
332+
ASTContext& C = Interp->getCI()->getASTContext();
333+
std::vector<Cpp::TemplateArgInfo> template_args = {
334+
{C.IntTy.getAsOpaquePtr()}};
335+
Cpp::TCppScope_t klass_instantiated = Cpp::InstantiateTemplate(
336+
klass, template_args.data(), template_args.size());
337+
EXPECT_TRUE(klass_instantiated);
338+
339+
Cpp::TCppScope_t var = Cpp::GetNamed("ref_value", klass_instantiated);
340+
EXPECT_TRUE(var);
341+
342+
EXPECT_TRUE(Cpp::GetVariableOffset(var));
321343
}
322344

323345
#define CODE \

0 commit comments

Comments
 (0)