Skip to content

Commit 2d2b70c

Browse files
using getFirstDecl for TranslationUnitDecl for consistent pointers
refactored `GetEnums` for this change by using `collectAllContexts`
1 parent 57d09bd commit 2d2b70c

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ namespace Cpp {
498498

499499
TCppScope_t GetGlobalScope()
500500
{
501-
return getSema().getASTContext().getTranslationUnitDecl();
501+
return getSema().getASTContext().getTranslationUnitDecl()->getFirstDecl();
502502
}
503503

504504
static Decl *GetScopeFromType(QualType QT) {
@@ -604,8 +604,12 @@ namespace Cpp {
604604
if (!ParentDC)
605605
return 0;
606606

607-
return (TCppScope_t) clang::Decl::castFromDeclContext(
608-
ParentDC)->getCanonicalDecl();
607+
auto* P = clang::Decl::castFromDeclContext(ParentDC)->getCanonicalDecl();
608+
609+
if (auto* TU = llvm::dyn_cast_or_null<TranslationUnitDecl>(P))
610+
return (TCppScope_t)TU->getFirstDecl();
611+
612+
return (TCppScope_t)P;
609613
}
610614

611615
TCppIndex_t GetNumBases(TCppScope_t klass)
@@ -3074,27 +3078,22 @@ namespace Cpp {
30743078
}
30753079

30763080
void GetEnums(TCppScope_t scope, std::vector<std::string>& Result) {
3077-
auto *D = (clang::Decl *)scope;
3078-
clang::DeclContext *DC;
3079-
clang::DeclContext::decl_iterator decl;
3081+
auto* D = static_cast<clang::Decl*>(scope);
30803082

3081-
if (auto *TD = dyn_cast_or_null<TagDecl>(D)) {
3082-
DC = clang::TagDecl::castToDeclContext(TD);
3083-
decl = DC->decls_begin();
3084-
decl++;
3085-
} else if (auto *ND = dyn_cast_or_null<NamespaceDecl>(D)) {
3086-
DC = clang::NamespaceDecl::castToDeclContext(ND);
3087-
decl = DC->decls_begin();
3088-
} else if (auto *TUD = dyn_cast_or_null<TranslationUnitDecl>(D)) {
3089-
DC = clang::TranslationUnitDecl::castToDeclContext(TUD);
3090-
decl = DC->decls_begin();
3091-
} else {
3083+
if (!llvm::isa_and_nonnull<clang::DeclContext>(D))
30923084
return;
3093-
}
30943085

3095-
for (/* decl set above */; decl != DC->decls_end(); decl++) {
3096-
if (auto *ND = llvm::dyn_cast_or_null<EnumDecl>(*decl)) {
3097-
Result.push_back(ND->getNameAsString());
3086+
auto* DC = llvm::dyn_cast<clang::DeclContext>(D);
3087+
3088+
llvm::SmallVector<clang::DeclContext*, 4> DCs;
3089+
DC->collectAllContexts(DCs);
3090+
3091+
// FIXME: We should use a lookup based approach instead of brute force
3092+
for (auto* DC : DCs) {
3093+
for (auto decl = DC->decls_begin(); decl != DC->decls_end(); decl++) {
3094+
if (auto* ND = llvm::dyn_cast_or_null<EnumDecl>(*decl)) {
3095+
Result.push_back(ND->getNameAsString());
3096+
}
30983097
}
30993098
}
31003099
}

0 commit comments

Comments
 (0)