From 3f175af8a0644fb5fc53aa90e01c24f75855c5f7 Mon Sep 17 00:00:00 2001 From: Stephen Hines Date: Fri, 16 Sep 2011 16:26:29 -0700 Subject: Fix .rs.dtor scoping bug. BUG=5267595 This change makes sure to scope any helper variables (like rsIntIter) to the enclosing function (and not make them global). Prior to this change, if an array iterator was created, it would be marked as a global symbol. The libbcc library then sometimes removed this global as it was not able to determine that it was actually used (.rs.dtor is not guaranteed to be called). A fix should be made to libbcc as well, but the proper fix for llvm-rs-cc is to scope any helper variable declarations to the calling function. Change-Id: Ia46654cb0a114f6f5495e32ad1508d7d01e2084b --- slang_rs_object_ref_count.cpp | 29 ++++++++++++++++++----------- slang_rs_object_ref_count.h | 3 ++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/slang_rs_object_ref_count.cpp b/slang_rs_object_ref_count.cpp index e35c677..88ffc62 100644 --- a/slang_rs_object_ref_count.cpp +++ b/slang_rs_object_ref_count.cpp @@ -1230,7 +1230,7 @@ void RSObjectRefCount::Scope::InsertLocalVarDestructors() { I != E; I++) { clang::VarDecl *VD = *I; - clang::Stmt *RSClearObjectCall = ClearRSObject(VD); + clang::Stmt *RSClearObjectCall = ClearRSObject(VD, VD->getDeclContext()); if (RSClearObjectCall) { DestructorVisitor DV((*mRSO.begin())->getASTContext(), mCS, @@ -1243,10 +1243,11 @@ void RSObjectRefCount::Scope::InsertLocalVarDestructors() { return; } -clang::Stmt *RSObjectRefCount::Scope::ClearRSObject(clang::VarDecl *VD) { +clang::Stmt *RSObjectRefCount::Scope::ClearRSObject( + clang::VarDecl *VD, + clang::DeclContext *DC) { slangAssert(VD); clang::ASTContext &C = VD->getASTContext(); - clang::DeclContext *DC = VD->getDeclContext(); clang::SourceLocation Loc = VD->getLocation(); clang::SourceLocation StartLoc = VD->getInnerLocStart(); const clang::Type *T = RSExportType::GetTypeOfDecl(VD); @@ -1494,6 +1495,13 @@ clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() { clang::DeclContext *DC = mCtx.getTranslationUnitDecl(); clang::SourceLocation loc; + llvm::StringRef SR(".rs.dtor"); + clang::IdentifierInfo &II = mCtx.Idents.get(SR); + clang::DeclarationName N(&II); + clang::FunctionProtoType::ExtProtoInfo EPI; + clang::QualType T = mCtx.getFunctionType(mCtx.VoidTy, NULL, 0, EPI); + clang::FunctionDecl *FD = NULL; + // Generate rsClearObject() call chains for every global variable // (whether static or extern). std::list StmtList; @@ -1502,7 +1510,13 @@ clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() { clang::VarDecl *VD = llvm::dyn_cast(*I); if (VD) { if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(), loc)) { - clang::Stmt *RSClearObjectCall = Scope::ClearRSObject(VD); + if (!FD) { + // Only create FD if we are going to use it. + FD = clang::FunctionDecl::Create(mCtx, DC, loc, loc, N, T, NULL); + } + // Make sure to create any helpers within the function's DeclContext, + // not the one associated with the global translation unit. + clang::Stmt *RSClearObjectCall = Scope::ClearRSObject(VD, FD); StmtList.push_back(RSClearObjectCall); } } @@ -1513,13 +1527,6 @@ clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() { return NULL; } - llvm::StringRef SR(".rs.dtor"); - clang::IdentifierInfo &II = mCtx.Idents.get(SR); - clang::DeclarationName N(&II); - clang::FunctionProtoType::ExtProtoInfo EPI; - clang::QualType T = mCtx.getFunctionType(mCtx.VoidTy, NULL, 0, EPI); - clang::FunctionDecl *FD = clang::FunctionDecl::Create(mCtx, DC, loc, loc, N, - T, NULL); clang::CompoundStmt *CS = BuildCompoundStmt(mCtx, StmtList, loc); FD->setBody(CS); diff --git a/slang_rs_object_ref_count.h b/slang_rs_object_ref_count.h index 15be35f..1e87ca1 100644 --- a/slang_rs_object_ref_count.h +++ b/slang_rs_object_ref_count.h @@ -68,7 +68,8 @@ class RSObjectRefCount : public clang::StmtVisitor { void InsertLocalVarDestructors(); - static clang::Stmt *ClearRSObject(clang::VarDecl *VD); + static clang::Stmt *ClearRSObject(clang::VarDecl *VD, + clang::DeclContext *DC); }; clang::ASTContext &mCtx; -- cgit v1.2.3