From e29e7e726614d59cd5adae1f81266de947ee2f3b Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sun, 8 Sep 2024 07:48:30 -0700 Subject: [PATCH] [FrontEnd] Use SetVector (NFC) (#107743) We could also use range-based for loops at several places, but I'm leaving that to a subsequent patch. --- .../Frontend/Rewrite/RewriteModernObjC.cpp | 78 ++++++++----------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp index 31ec86e2e4f096..326920c7915537 100644 --- a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp +++ b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp @@ -138,10 +138,8 @@ namespace { SmallVector BlockDeclRefs; // Block related declarations. - SmallVector BlockByCopyDecls; - llvm::SmallPtrSet BlockByCopyDeclsPtrSet; - SmallVector BlockByRefDecls; - llvm::SmallPtrSet BlockByRefDeclsPtrSet; + llvm::SmallSetVector BlockByCopyDecls; + llvm::SmallSetVector BlockByRefDecls; llvm::DenseMap BlockByRefDeclNo; llvm::SmallPtrSet ImportedBlockDecls; llvm::SmallPtrSet ImportedLocalExternalDecls; @@ -4082,8 +4080,8 @@ std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, // Create local declarations to avoid rewriting all closure decl ref exprs. // First, emit a declaration for all "by ref" decls. - for (SmallVectorImpl::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { + for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E; + ++I) { S += " "; std::string Name = (*I)->getNameAsString(); std::string TypeString; @@ -4093,8 +4091,8 @@ std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; } // Next, emit a declaration for all "by copy" declarations. - for (SmallVectorImpl::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { + for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E; + ++I) { S += " "; // Handle nested closure invocation. For example: // @@ -4146,7 +4144,7 @@ std::string RewriteModernObjC::SynthesizeBlockHelperFuncs( S += VD->getNameAsString(); S += ", (void*)src->"; S += VD->getNameAsString(); - if (BlockByRefDeclsPtrSet.count(VD)) + if (BlockByRefDecls.count(VD)) S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; else if (VD->getType()->isBlockPointerType()) S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; @@ -4163,7 +4161,7 @@ std::string RewriteModernObjC::SynthesizeBlockHelperFuncs( for (ValueDecl *VD : ImportedBlockDecls) { S += "_Block_object_dispose((void*)src->"; S += VD->getNameAsString(); - if (BlockByRefDeclsPtrSet.count(VD)) + if (BlockByRefDecls.count(VD)) S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; else if (VD->getType()->isBlockPointerType()) S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);"; @@ -4190,8 +4188,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, if (BlockDeclRefs.size()) { // Output all "by copy" declarations. - for (SmallVectorImpl::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { + for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E; + ++I) { S += " "; std::string FieldName = (*I)->getNameAsString(); std::string ArgName = "_" + FieldName; @@ -4219,8 +4217,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, S += FieldName + ";\n"; } // Output all "by ref" declarations. - for (SmallVectorImpl::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { + for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E; + ++I) { S += " "; std::string FieldName = (*I)->getNameAsString(); std::string ArgName = "_" + FieldName; @@ -4238,8 +4236,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, Constructor += ", int flags=0)"; // Initialize all "by copy" arguments. bool firsTime = true; - for (SmallVectorImpl::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { + for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E; + ++I) { std::string Name = (*I)->getNameAsString(); if (firsTime) { Constructor += " : "; @@ -4253,8 +4251,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, Constructor += Name + "(_" + Name + ")"; } // Initialize all "by ref" arguments. - for (SmallVectorImpl::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { + for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E; + ++I) { std::string Name = (*I)->getNameAsString(); if (firsTime) { Constructor += " : "; @@ -4340,13 +4338,11 @@ void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, ValueDecl *VD = Exp->getDecl(); BlockDeclRefs.push_back(Exp); if (!VD->hasAttr()) { - if (BlockByCopyDeclsPtrSet.insert(VD).second) - BlockByCopyDecls.push_back(VD); + BlockByCopyDecls.insert(VD); continue; } - if (BlockByRefDeclsPtrSet.insert(VD).second) - BlockByRefDecls.push_back(VD); + BlockByRefDecls.insert(VD); // imported objects in the inner blocks not used in the outer // blocks must be copied/disposed in the outer block as well. @@ -4376,9 +4372,7 @@ void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, BlockDeclRefs.clear(); BlockByRefDecls.clear(); - BlockByRefDeclsPtrSet.clear(); BlockByCopyDecls.clear(); - BlockByCopyDeclsPtrSet.clear(); ImportedBlockDecls.clear(); } if (RewriteSC) { @@ -5156,16 +5150,12 @@ void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { if (BlockDeclRefs.size()) { // Unique all "by copy" declarations. for (unsigned i = 0; i < BlockDeclRefs.size(); i++) - if (!BlockDeclRefs[i]->getDecl()->hasAttr()) { - if (BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()).second) - BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); - } + if (!BlockDeclRefs[i]->getDecl()->hasAttr()) + BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); // Unique all "by ref" declarations. for (unsigned i = 0; i < BlockDeclRefs.size(); i++) - if (BlockDeclRefs[i]->getDecl()->hasAttr()) { - if (BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()).second) - BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); - } + if (BlockDeclRefs[i]->getDecl()->hasAttr()) + BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); // Find any imported blocks...they will need special attention. for (unsigned i = 0; i < BlockDeclRefs.size(); i++) if (BlockDeclRefs[i]->getDecl()->hasAttr() || @@ -5197,20 +5187,16 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { DeclRefExpr *Exp = InnerBlockDeclRefs[i]; ValueDecl *VD = Exp->getDecl(); - if (!VD->hasAttr() && !BlockByCopyDeclsPtrSet.count(VD)) { - // We need to save the copied-in variables in nested - // blocks because it is needed at the end for some of the API generations. - // See SynthesizeBlockLiterals routine. + if (!VD->hasAttr() && BlockByCopyDecls.insert(VD)) { + // We need to save the copied-in variables in nested + // blocks because it is needed at the end for some of the API + // generations. See SynthesizeBlockLiterals routine. InnerDeclRefs.push_back(Exp); countOfInnerDecls++; BlockDeclRefs.push_back(Exp); - BlockByCopyDeclsPtrSet.insert(VD); - BlockByCopyDecls.push_back(VD); } - if (VD->hasAttr() && !BlockByRefDeclsPtrSet.count(VD)) { + if (VD->hasAttr() && BlockByRefDecls.insert(VD)) { InnerDeclRefs.push_back(Exp); countOfInnerDecls++; BlockDeclRefs.push_back(Exp); - BlockByRefDeclsPtrSet.insert(VD); - BlockByRefDecls.push_back(VD); } } // Find any imported blocks...they will need special attention. @@ -5291,8 +5277,8 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, if (BlockDeclRefs.size()) { Expr *Exp; // Output all "by copy" declarations. - for (SmallVectorImpl::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { + for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E; + ++I) { if (isObjCType((*I)->getType())) { // FIXME: Conform to ABI ([[obj retain] autorelease]). FD = SynthBlockInitFunctionDecl((*I)->getName()); @@ -5329,8 +5315,8 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, InitExprs.push_back(Exp); } // Output all "by ref" declarations. - for (SmallVectorImpl::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { + for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E; + ++I) { ValueDecl *ND = (*I); std::string Name(ND->getNameAsString()); std::string RecName; @@ -5398,9 +5384,7 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp, BlockDeclRefs.clear(); BlockByRefDecls.clear(); - BlockByRefDeclsPtrSet.clear(); BlockByCopyDecls.clear(); - BlockByCopyDeclsPtrSet.clear(); ImportedBlockDecls.clear(); return NewRep; }