Skip to content

Commit e8c9c41

Browse files
fix GetVariableOffset for templated static variable (#629)
1 parent 55b47ba commit e8c9c41

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D,
15181518
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
15191519
#endif // CPPINTEROP_USE_CLING
15201520
getSema().InstantiateVariableDefinition(SourceLocation(), VD);
1521+
VD = VD->getDefinition();
15211522
}
15221523
if (VD->hasInit() &&
15231524
(VD->isConstexpr() || VD->getType().isConstQualified())) {

unittests/CppInterOp/VariableReflectionTest.cpp

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

327349
#define CODE \

0 commit comments

Comments
 (0)