Skip to content

Commit 0f406da

Browse files
using getFirstDecl for TranslationUnitDecl for consistent pointers (#329)
refactored `GetEnums` for this change by using `collectAllContexts`
1 parent f59f2cf commit 0f406da

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)
@@ -3088,27 +3092,22 @@ namespace Cpp {
30883092
}
30893093

30903094
void GetEnums(TCppScope_t scope, std::vector<std::string>& Result) {
3091-
auto *D = (clang::Decl *)scope;
3092-
clang::DeclContext *DC;
3093-
clang::DeclContext::decl_iterator decl;
3095+
auto* D = static_cast<clang::Decl*>(scope);
30943096

3095-
if (auto *TD = dyn_cast_or_null<TagDecl>(D)) {
3096-
DC = clang::TagDecl::castToDeclContext(TD);
3097-
decl = DC->decls_begin();
3098-
decl++;
3099-
} else if (auto *ND = dyn_cast_or_null<NamespaceDecl>(D)) {
3100-
DC = clang::NamespaceDecl::castToDeclContext(ND);
3101-
decl = DC->decls_begin();
3102-
} else if (auto *TUD = dyn_cast_or_null<TranslationUnitDecl>(D)) {
3103-
DC = clang::TranslationUnitDecl::castToDeclContext(TUD);
3104-
decl = DC->decls_begin();
3105-
} else {
3097+
if (!llvm::isa_and_nonnull<clang::DeclContext>(D))
31063098
return;
3107-
}
31083099

3109-
for (/* decl set above */; decl != DC->decls_end(); decl++) {
3110-
if (auto *ND = llvm::dyn_cast_or_null<EnumDecl>(*decl)) {
3111-
Result.push_back(ND->getNameAsString());
3100+
auto* DC = llvm::dyn_cast<clang::DeclContext>(D);
3101+
3102+
llvm::SmallVector<clang::DeclContext*, 4> DCs;
3103+
DC->collectAllContexts(DCs);
3104+
3105+
// FIXME: We should use a lookup based approach instead of brute force
3106+
for (auto* DC : DCs) {
3107+
for (auto decl = DC->decls_begin(); decl != DC->decls_end(); decl++) {
3108+
if (auto* ND = llvm::dyn_cast_or_null<EnumDecl>(*decl)) {
3109+
Result.push_back(ND->getNameAsString());
3110+
}
31123111
}
31133112
}
31143113
}

0 commit comments

Comments
 (0)