Skip to content

[MRE] cant resolve static variable offset for templated class #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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,8 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D,
#ifdef CPPINTEROP_USE_CLING
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
#endif // CPPINTEROP_USE_CLING
getSema().InstantiateVariableDefinition(SourceLocation(), VD);
getSema().InstantiateVariableDefinition(SourceLocation(), VD, true,
true);
}
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 @@ -299,6 +299,28 @@ TEST(VariableReflectionTest, GetVariableOffset) {

auto* VD_C_s_a = Cpp::GetNamed("s_a", Decls[4]); // C::s_a
EXPECT_TRUE((bool) Cpp::GetVariableOffset(VD_C_s_a));

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