Skip to content

Commit 24f9876

Browse files
sudo-pandavgvassilev
authored andcommitted
Fix typedef hiding DeclContext of parent in GetNamed
1 parent 24fc840 commit 24f9876

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,28 @@ namespace Cpp {
494494
return GetScope(name.substr(start, end), curr_scope);
495495
}
496496

497+
static CXXRecordDecl *GetScopeFromType(QualType QT) {
498+
if (auto *Type = QT.getTypePtrOrNull()) {
499+
Type = Type->getPointeeOrArrayElementType();
500+
Type = Type->getUnqualifiedDesugaredType();
501+
return Type->getAsCXXRecordDecl();
502+
}
503+
return 0;
504+
}
505+
506+
TCppScope_t GetScopeFromType(TCppType_t type) {
507+
QualType QT = QualType::getFromOpaquePtr(type);
508+
return (TCppScope_t)GetScopeFromType(QT);
509+
}
510+
497511
TCppScope_t GetNamed(const std::string &name,
498512
TCppScope_t parent /*= nullptr*/)
499513
{
500514
clang::DeclContext *Within = 0;
501515
if (parent) {
502516
auto *D = (clang::Decl *)parent;
517+
if (auto *TD = dyn_cast<TypedefNameDecl>(D))
518+
D = GetScopeFromType(TD->getUnderlyingType());
503519
Within = llvm::dyn_cast<clang::DeclContext>(D);
504520
}
505521

@@ -527,22 +543,6 @@ namespace Cpp {
527543
ParentDC)->getCanonicalDecl();
528544
}
529545

530-
namespace {
531-
CXXRecordDecl *GetScopeFromType(QualType QT) {
532-
if (auto *Type = QT.getTypePtrOrNull()) {
533-
Type = Type->getPointeeOrArrayElementType();
534-
Type = Type->getUnqualifiedDesugaredType();
535-
return Type->getAsCXXRecordDecl();
536-
}
537-
return 0;
538-
}
539-
} // namespace
540-
541-
TCppScope_t GetScopeFromType(TCppType_t type) {
542-
QualType QT = QualType::getFromOpaquePtr(type);
543-
return (TCppScope_t)GetScopeFromType(QT);
544-
}
545-
546546
TCppIndex_t GetNumBases(TCppScope_t klass)
547547
{
548548
auto *D = (Decl *) klass;

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@ TEST(ScopeReflectionTest, GetNamed) {
397397
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetNamed("A", cl_C)), "N1::N2::C::A");
398398
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetNamed("B", cl_C)), "N1::N2::C::B");
399399
EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetNamed("S", cl_C)), "N1::N2::C::S");
400+
401+
Interp->process("#include <string>");
402+
Cpp::TCppScope_t std_ns = Cpp::GetNamed("std", nullptr);
403+
Cpp::TCppScope_t std_string_class = Cpp::GetNamed("string", std_ns);
404+
Cpp::TCppScope_t std_string_npos_var = Cpp::GetNamed("npos", std_string_class);
405+
EXPECT_EQ(Cpp::GetQualifiedName(std_ns), "std");
406+
EXPECT_EQ(Cpp::GetQualifiedName(std_string_class), "std::string");
407+
EXPECT_EQ(Cpp::GetQualifiedName(std_string_npos_var), "std::basic_string<char>::npos");
400408
}
401409

402410
TEST(ScopeReflectionTest, GetParentScope) {

0 commit comments

Comments
 (0)