diff --git a/lib/CppInterOp/CppInterOp.cpp b/lib/CppInterOp/CppInterOp.cpp index a8bf9c064..d108059d2 100755 --- a/lib/CppInterOp/CppInterOp.cpp +++ b/lib/CppInterOp/CppInterOp.cpp @@ -1518,6 +1518,7 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D, cling::Interpreter::PushTransactionRAII RAII(&getInterp()); #endif // CPPINTEROP_USE_CLING getSema().InstantiateVariableDefinition(SourceLocation(), VD); + VD = VD->getDefinition(); } if (VD->hasInit() && (VD->isConstexpr() || VD->getType().isConstQualified())) { diff --git a/unittests/CppInterOp/VariableReflectionTest.cpp b/unittests/CppInterOp/VariableReflectionTest.cpp index 1dfba0066..f6abfebe0 100644 --- a/unittests/CppInterOp/VariableReflectionTest.cpp +++ b/unittests/CppInterOp/VariableReflectionTest.cpp @@ -322,6 +322,28 @@ TEST(VariableReflectionTest, GetVariableOffset) { EXPECT_EQ(Cpp::GetVariableOffset(datamembers[0]), offsetof(K, x)); EXPECT_EQ(Cpp::GetVariableOffset(datamembers[1]), offsetof(K, y)); EXPECT_EQ(Cpp::GetVariableOffset(datamembers[2]), offsetof(K, z)); + + Cpp::Declare(R"( + template struct ClassWithStatic { + static T const ref_value; + }; + template T constexpr ClassWithStatic::ref_value = 42; + )"); + + Cpp::TCppScope_t klass = Cpp::GetNamed("ClassWithStatic"); + EXPECT_TRUE(klass); + + ASTContext& C = Interp->getCI()->getASTContext(); + std::vector template_args = { + {C.IntTy.getAsOpaquePtr()}}; + Cpp::TCppScope_t klass_instantiated = Cpp::InstantiateTemplate( + klass, template_args.data(), template_args.size()); + EXPECT_TRUE(klass_instantiated); + + Cpp::TCppScope_t var = Cpp::GetNamed("ref_value", klass_instantiated); + EXPECT_TRUE(var); + + EXPECT_TRUE(Cpp::GetVariableOffset(var)); } #define CODE \