Skip to content

Commit

Permalink
[FrontEnd] Use SetVector (NFC) (llvm#107743)
Browse files Browse the repository at this point in the history
We could also use range-based for loops at several places, but I'm
leaving that to a subsequent patch.
  • Loading branch information
kazutakahirata authored Sep 8, 2024
1 parent 45c8766 commit e29e7e7
Showing 1 changed file with 31 additions and 47 deletions.
78 changes: 31 additions & 47 deletions clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ namespace {
SmallVector<DeclRefExpr *, 32> BlockDeclRefs;

// Block related declarations.
SmallVector<ValueDecl *, 8> BlockByCopyDecls;
llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet;
SmallVector<ValueDecl *, 8> BlockByRefDecls;
llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet;
llvm::SmallSetVector<ValueDecl *, 8> BlockByCopyDecls;
llvm::SmallSetVector<ValueDecl *, 8> BlockByRefDecls;
llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo;
llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls;
Expand Down Expand Up @@ -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<ValueDecl *>::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;
Expand All @@ -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<ValueDecl *>::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:
//
Expand Down Expand Up @@ -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*/);";
Expand All @@ -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*/);";
Expand All @@ -4190,8 +4188,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,

if (BlockDeclRefs.size()) {
// Output all "by copy" declarations.
for (SmallVectorImpl<ValueDecl *>::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;
Expand Down Expand Up @@ -4219,8 +4217,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
S += FieldName + ";\n";
}
// Output all "by ref" declarations.
for (SmallVectorImpl<ValueDecl *>::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;
Expand All @@ -4238,8 +4236,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
Constructor += ", int flags=0)";
// Initialize all "by copy" arguments.
bool firsTime = true;
for (SmallVectorImpl<ValueDecl *>::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 += " : ";
Expand All @@ -4253,8 +4251,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
Constructor += Name + "(_" + Name + ")";
}
// Initialize all "by ref" arguments.
for (SmallVectorImpl<ValueDecl *>::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 += " : ";
Expand Down Expand Up @@ -4340,13 +4338,11 @@ void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
ValueDecl *VD = Exp->getDecl();
BlockDeclRefs.push_back(Exp);
if (!VD->hasAttr<BlocksAttr>()) {
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.
Expand Down Expand Up @@ -4376,9 +4372,7 @@ void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,

BlockDeclRefs.clear();
BlockByRefDecls.clear();
BlockByRefDeclsPtrSet.clear();
BlockByCopyDecls.clear();
BlockByCopyDeclsPtrSet.clear();
ImportedBlockDecls.clear();
}
if (RewriteSC) {
Expand Down Expand Up @@ -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<BlocksAttr>()) {
if (BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()).second)
BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl());
}
if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>())
BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
// Unique all "by ref" declarations.
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
if (BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()).second)
BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl());
}
if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>())
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<BlocksAttr>() ||
Expand Down Expand Up @@ -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<BlocksAttr>() && !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<BlocksAttr>() && 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<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) {
if (VD->hasAttr<BlocksAttr>() && 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.
Expand Down Expand Up @@ -5291,8 +5277,8 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
if (BlockDeclRefs.size()) {
Expr *Exp;
// Output all "by copy" declarations.
for (SmallVectorImpl<ValueDecl *>::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());
Expand Down Expand Up @@ -5329,8 +5315,8 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
InitExprs.push_back(Exp);
}
// Output all "by ref" declarations.
for (SmallVectorImpl<ValueDecl *>::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;
Expand Down Expand Up @@ -5398,9 +5384,7 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,

BlockDeclRefs.clear();
BlockByRefDecls.clear();
BlockByRefDeclsPtrSet.clear();
BlockByCopyDecls.clear();
BlockByCopyDeclsPtrSet.clear();
ImportedBlockDecls.clear();
return NewRep;
}
Expand Down

0 comments on commit e29e7e7

Please sign in to comment.