Skip to content
Closed
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
33 changes: 33 additions & 0 deletions unittests/CppInterOp/VariableReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,39 @@ TEST(VariableReflectionTest, DatamembersWithAnonymousStructOrUnion) {
#endif
}

TEST(VariableReflectionTest, GetTypeAsString) {
if (llvm::sys::RunningOnValgrind())
GTEST_SKIP() << "XFAIL due to Valgrind report";

std::string code = R"(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::string" is directly included [misc-include-cleaner]

unittests/CppInterOp/VariableReflectionTest.cpp:6:

+ #include <string>

namespace my_namespace {

struct Container {
int value;
};

struct Wrapper {
Container item;
};

}
)";

Cpp::CreateInterpreter();
EXPECT_EQ(Cpp::Declare(code.c_str()), 0);

Cpp::TCppScope_t wrapper =
Cpp::GetScopeFromCompleteName("my_namespace::Wrapper");
EXPECT_TRUE(wrapper);

std::vector<Cpp::TCppScope_t> datamembers;
Cpp::GetDatamembers(wrapper, datamembers);
EXPECT_EQ(datamembers.size(), 1);

EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetVariableType(datamembers[0])),
"my_namespace::Container");
Comment on lines +171 to +172
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion fails here with

"Container" != "my_namespace::Container"

I need the fully qualified type name.

Copy link
Contributor

@vgvassilev vgvassilev Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't GetQualifiedName or GetQualifiedCompleteName help?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the following diff in GetVariableType passed the above test for me. I may be wrong due to lack of context.

--- a/lib/Interpreter/CppInterOp.cpp
+++ b/lib/Interpreter/CppInterOp.cpp
@@ -1216,7 +1216,10 @@ namespace Cpp {
     auto D = (Decl *) var;
 
     if (auto DD = llvm::dyn_cast_or_null<DeclaratorDecl>(D)) {
-      return DD->getType().getAsOpaquePtr();
+      QualType QT = DD->getType();
+
+      // Ensure the type includes the namespace.
+      QT = QT.getCanonicalType();
+      return QT.getAsOpaquePtr();
     }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the change makes the test pass that's a good sign.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, let me run all the tests and get back to you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vgvassilev I ran all the tests now, all tests pass but the typedef int I; failed in TypeReflectionTest.cpp, so changing the above diff makes all tests pass. What is your opinion here ?

TCppType_t GetVariableType(TCppScope_t var)
{
    auto D = (Decl *) var;

    if (auto DD = llvm::dyn_cast_or_null<DeclaratorDecl>(D)) {
        QualType QT = DD->getType();

        // Check if the type is a typedef type
        if (QT->isTypedefNameType()) {
            return QT.getAsOpaquePtr();
        }

        // Else, return the canonical type
        QT = QT.getCanonicalType();
        return QT.getAsOpaquePtr();
    }

    return 0;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faze-geek, I did not test your changes locally. But if all existing tests and the test from this PR pass, you can open a PR of this branch (create a new branch from this branch). I will take a look at it. We may extend the test with other combinations.

Thanks for looking into this. 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure ! Will make the changes soon.

}

TEST(VariableReflectionTest, LookupDatamember) {
std::vector<Decl*> Decls;
std::string code = R"(
Expand Down
Loading