Skip to content

Commit aa78f32

Browse files
[MRE] cant resolve static variable offset for templated class
1 parent d9bf9ab commit aa78f32

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
@@ -1477,7 +1477,8 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D,
14771477
#ifdef CPPINTEROP_USE_CLING
14781478
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
14791479
#endif // CPPINTEROP_USE_CLING
1480-
getSema().InstantiateVariableDefinition(SourceLocation(), VD);
1480+
getSema().InstantiateVariableDefinition(SourceLocation(), VD, true,
1481+
true);
14811482
}
14821483
if (VD->hasInit() &&
14831484
(VD->isConstexpr() || VD->getType().isConstQualified())) {

unittests/CppInterOp/VariableReflectionTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,28 @@ TEST(VariableReflectionTest, GetVariableOffset) {
299299

300300
auto* VD_C_s_a = Cpp::GetNamed("s_a", Decls[4]); // C::s_a
301301
EXPECT_TRUE((bool) Cpp::GetVariableOffset(VD_C_s_a));
302+
303+
Cpp::Declare(R"(
304+
template <typename T> struct ClassWithStatic {
305+
static T const ref_value;
306+
};
307+
template <typename T> T constexpr ClassWithStatic<T>::ref_value = 42;
308+
)");
309+
310+
Cpp::TCppScope_t klass = Cpp::GetNamed("ClassWithStatic");
311+
EXPECT_TRUE(klass);
312+
313+
ASTContext& C = Interp->getCI()->getASTContext();
314+
std::vector<Cpp::TemplateArgInfo> template_args = {
315+
{C.IntTy.getAsOpaquePtr()}};
316+
Cpp::TCppScope_t klass_instantiated = Cpp::InstantiateTemplate(
317+
klass, template_args.data(), template_args.size());
318+
EXPECT_TRUE(klass_instantiated);
319+
320+
Cpp::TCppScope_t var = Cpp::GetNamed("ref_value", klass_instantiated);
321+
EXPECT_TRUE(var);
322+
323+
EXPECT_TRUE(Cpp::GetVariableOffset(var));
302324
}
303325

304326
#define CODE \

0 commit comments

Comments
 (0)