Skip to content

fix GetVariableOffset for templated static variable #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
22 changes: 22 additions & 0 deletions unittests/CppInterOp/VariableReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T> struct ClassWithStatic {
static T const ref_value;
};
template <typename T> T constexpr ClassWithStatic<T>::ref_value = 42;
)");

Cpp::TCppScope_t klass = Cpp::GetNamed("ClassWithStatic");
EXPECT_TRUE(klass);

ASTContext& C = Interp->getCI()->getASTContext();
std::vector<Cpp::TemplateArgInfo> 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 \
Expand Down
Loading