Skip to content

Commit e29e7e7

Browse files
[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.
1 parent 45c8766 commit e29e7e7

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,8 @@ namespace {
138138
SmallVector<DeclRefExpr *, 32> BlockDeclRefs;
139139

140140
// Block related declarations.
141-
SmallVector<ValueDecl *, 8> BlockByCopyDecls;
142-
llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet;
143-
SmallVector<ValueDecl *, 8> BlockByRefDecls;
144-
llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet;
141+
llvm::SmallSetVector<ValueDecl *, 8> BlockByCopyDecls;
142+
llvm::SmallSetVector<ValueDecl *, 8> BlockByRefDecls;
145143
llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo;
146144
llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
147145
llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls;
@@ -4082,8 +4080,8 @@ std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
40824080

40834081
// Create local declarations to avoid rewriting all closure decl ref exprs.
40844082
// First, emit a declaration for all "by ref" decls.
4085-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
4086-
E = BlockByRefDecls.end(); I != E; ++I) {
4083+
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
4084+
++I) {
40874085
S += " ";
40884086
std::string Name = (*I)->getNameAsString();
40894087
std::string TypeString;
@@ -4093,8 +4091,8 @@ std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
40934091
S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
40944092
}
40954093
// Next, emit a declaration for all "by copy" declarations.
4096-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
4097-
E = BlockByCopyDecls.end(); I != E; ++I) {
4094+
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
4095+
++I) {
40984096
S += " ";
40994097
// Handle nested closure invocation. For example:
41004098
//
@@ -4146,7 +4144,7 @@ std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(
41464144
S += VD->getNameAsString();
41474145
S += ", (void*)src->";
41484146
S += VD->getNameAsString();
4149-
if (BlockByRefDeclsPtrSet.count(VD))
4147+
if (BlockByRefDecls.count(VD))
41504148
S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
41514149
else if (VD->getType()->isBlockPointerType())
41524150
S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
@@ -4163,7 +4161,7 @@ std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(
41634161
for (ValueDecl *VD : ImportedBlockDecls) {
41644162
S += "_Block_object_dispose((void*)src->";
41654163
S += VD->getNameAsString();
4166-
if (BlockByRefDeclsPtrSet.count(VD))
4164+
if (BlockByRefDecls.count(VD))
41674165
S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
41684166
else if (VD->getType()->isBlockPointerType())
41694167
S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
@@ -4190,8 +4188,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
41904188

41914189
if (BlockDeclRefs.size()) {
41924190
// Output all "by copy" declarations.
4193-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
4194-
E = BlockByCopyDecls.end(); I != E; ++I) {
4191+
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
4192+
++I) {
41954193
S += " ";
41964194
std::string FieldName = (*I)->getNameAsString();
41974195
std::string ArgName = "_" + FieldName;
@@ -4219,8 +4217,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
42194217
S += FieldName + ";\n";
42204218
}
42214219
// Output all "by ref" declarations.
4222-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
4223-
E = BlockByRefDecls.end(); I != E; ++I) {
4220+
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
4221+
++I) {
42244222
S += " ";
42254223
std::string FieldName = (*I)->getNameAsString();
42264224
std::string ArgName = "_" + FieldName;
@@ -4238,8 +4236,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
42384236
Constructor += ", int flags=0)";
42394237
// Initialize all "by copy" arguments.
42404238
bool firsTime = true;
4241-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
4242-
E = BlockByCopyDecls.end(); I != E; ++I) {
4239+
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
4240+
++I) {
42434241
std::string Name = (*I)->getNameAsString();
42444242
if (firsTime) {
42454243
Constructor += " : ";
@@ -4253,8 +4251,8 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
42534251
Constructor += Name + "(_" + Name + ")";
42544252
}
42554253
// Initialize all "by ref" arguments.
4256-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
4257-
E = BlockByRefDecls.end(); I != E; ++I) {
4254+
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
4255+
++I) {
42584256
std::string Name = (*I)->getNameAsString();
42594257
if (firsTime) {
42604258
Constructor += " : ";
@@ -4340,13 +4338,11 @@ void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
43404338
ValueDecl *VD = Exp->getDecl();
43414339
BlockDeclRefs.push_back(Exp);
43424340
if (!VD->hasAttr<BlocksAttr>()) {
4343-
if (BlockByCopyDeclsPtrSet.insert(VD).second)
4344-
BlockByCopyDecls.push_back(VD);
4341+
BlockByCopyDecls.insert(VD);
43454342
continue;
43464343
}
43474344

4348-
if (BlockByRefDeclsPtrSet.insert(VD).second)
4349-
BlockByRefDecls.push_back(VD);
4345+
BlockByRefDecls.insert(VD);
43504346

43514347
// imported objects in the inner blocks not used in the outer
43524348
// blocks must be copied/disposed in the outer block as well.
@@ -4376,9 +4372,7 @@ void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
43764372

43774373
BlockDeclRefs.clear();
43784374
BlockByRefDecls.clear();
4379-
BlockByRefDeclsPtrSet.clear();
43804375
BlockByCopyDecls.clear();
4381-
BlockByCopyDeclsPtrSet.clear();
43824376
ImportedBlockDecls.clear();
43834377
}
43844378
if (RewriteSC) {
@@ -5156,16 +5150,12 @@ void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) {
51565150
if (BlockDeclRefs.size()) {
51575151
// Unique all "by copy" declarations.
51585152
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5159-
if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
5160-
if (BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()).second)
5161-
BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl());
5162-
}
5153+
if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>())
5154+
BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
51635155
// Unique all "by ref" declarations.
51645156
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5165-
if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
5166-
if (BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()).second)
5167-
BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl());
5168-
}
5157+
if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>())
5158+
BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl());
51695159
// Find any imported blocks...they will need special attention.
51705160
for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
51715161
if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
@@ -5197,20 +5187,16 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
51975187
for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) {
51985188
DeclRefExpr *Exp = InnerBlockDeclRefs[i];
51995189
ValueDecl *VD = Exp->getDecl();
5200-
if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) {
5201-
// We need to save the copied-in variables in nested
5202-
// blocks because it is needed at the end for some of the API generations.
5203-
// See SynthesizeBlockLiterals routine.
5190+
if (!VD->hasAttr<BlocksAttr>() && BlockByCopyDecls.insert(VD)) {
5191+
// We need to save the copied-in variables in nested
5192+
// blocks because it is needed at the end for some of the API
5193+
// generations. See SynthesizeBlockLiterals routine.
52045194
InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
52055195
BlockDeclRefs.push_back(Exp);
5206-
BlockByCopyDeclsPtrSet.insert(VD);
5207-
BlockByCopyDecls.push_back(VD);
52085196
}
5209-
if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) {
5197+
if (VD->hasAttr<BlocksAttr>() && BlockByRefDecls.insert(VD)) {
52105198
InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
52115199
BlockDeclRefs.push_back(Exp);
5212-
BlockByRefDeclsPtrSet.insert(VD);
5213-
BlockByRefDecls.push_back(VD);
52145200
}
52155201
}
52165202
// Find any imported blocks...they will need special attention.
@@ -5291,8 +5277,8 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
52915277
if (BlockDeclRefs.size()) {
52925278
Expr *Exp;
52935279
// Output all "by copy" declarations.
5294-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
5295-
E = BlockByCopyDecls.end(); I != E; ++I) {
5280+
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
5281+
++I) {
52965282
if (isObjCType((*I)->getType())) {
52975283
// FIXME: Conform to ABI ([[obj retain] autorelease]).
52985284
FD = SynthBlockInitFunctionDecl((*I)->getName());
@@ -5329,8 +5315,8 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
53295315
InitExprs.push_back(Exp);
53305316
}
53315317
// Output all "by ref" declarations.
5332-
for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
5333-
E = BlockByRefDecls.end(); I != E; ++I) {
5318+
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
5319+
++I) {
53345320
ValueDecl *ND = (*I);
53355321
std::string Name(ND->getNameAsString());
53365322
std::string RecName;
@@ -5398,9 +5384,7 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
53985384

53995385
BlockDeclRefs.clear();
54005386
BlockByRefDecls.clear();
5401-
BlockByRefDeclsPtrSet.clear();
54025387
BlockByCopyDecls.clear();
5403-
BlockByCopyDeclsPtrSet.clear();
54045388
ImportedBlockDecls.clear();
54055389
return NewRep;
54065390
}

0 commit comments

Comments
 (0)