Skip to content

[Frontend] Use range-based for loops (NFC) #107757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 48 additions & 60 deletions clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4080,19 +4080,17 @@ 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 (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
++I) {
for (ValueDecl *VD : BlockByRefDecls) {
S += " ";
std::string Name = (*I)->getNameAsString();
std::string Name = VD->getNameAsString();
std::string TypeString;
RewriteByRefString(TypeString, Name, (*I));
RewriteByRefString(TypeString, Name, VD);
TypeString += " *";
Name = TypeString + Name;
S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
S += Name + " = __cself->" + VD->getNameAsString() + "; // bound by ref\n";
}
// Next, emit a declaration for all "by copy" declarations.
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
++I) {
for (ValueDecl *VD : BlockByCopyDecls) {
S += " ";
// Handle nested closure invocation. For example:
//
Expand All @@ -4104,21 +4102,20 @@ std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
// myImportedClosure(); // import and invoke the closure
// };
//
if (isTopLevelBlockPointerType((*I)->getType())) {
RewriteBlockPointerTypeVariable(S, (*I));
if (isTopLevelBlockPointerType(VD->getType())) {
RewriteBlockPointerTypeVariable(S, VD);
S += " = (";
RewriteBlockPointerType(S, (*I)->getType());
RewriteBlockPointerType(S, VD->getType());
S += ")";
S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
}
else {
std::string Name = (*I)->getNameAsString();
QualType QT = (*I)->getType();
if (HasLocalVariableExternalStorage(*I))
S += "__cself->" + VD->getNameAsString() + "; // bound by copy\n";
} else {
std::string Name = VD->getNameAsString();
QualType QT = VD->getType();
if (HasLocalVariableExternalStorage(VD))
QT = Context->getPointerType(QT);
QT.getAsStringInternal(Name, Context->getPrintingPolicy());
S += Name + " = __cself->" +
(*I)->getNameAsString() + "; // bound by copy\n";
S += Name + " = __cself->" + VD->getNameAsString() +
"; // bound by copy\n";
}
}
std::string RewrittenStr = RewrittenBlockExprs[CE];
Expand Down Expand Up @@ -4188,10 +4185,9 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,

if (BlockDeclRefs.size()) {
// Output all "by copy" declarations.
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
++I) {
for (ValueDecl *VD : BlockByCopyDecls) {
S += " ";
std::string FieldName = (*I)->getNameAsString();
std::string FieldName = VD->getNameAsString();
std::string ArgName = "_" + FieldName;
// Handle nested closure invocation. For example:
//
Expand All @@ -4203,12 +4199,12 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
// myImportedBlock(); // import and invoke the closure
// };
//
if (isTopLevelBlockPointerType((*I)->getType())) {
if (isTopLevelBlockPointerType(VD->getType())) {
S += "struct __block_impl *";
Constructor += ", void *" + ArgName;
} else {
QualType QT = (*I)->getType();
if (HasLocalVariableExternalStorage(*I))
QualType QT = VD->getType();
if (HasLocalVariableExternalStorage(VD))
QT = Context->getPointerType(QT);
QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
Expand All @@ -4217,14 +4213,13 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
S += FieldName + ";\n";
}
// Output all "by ref" declarations.
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
++I) {
for (ValueDecl *VD : BlockByRefDecls) {
S += " ";
std::string FieldName = (*I)->getNameAsString();
std::string FieldName = VD->getNameAsString();
std::string ArgName = "_" + FieldName;
{
std::string TypeString;
RewriteByRefString(TypeString, FieldName, (*I));
RewriteByRefString(TypeString, FieldName, VD);
TypeString += " *";
FieldName = TypeString + FieldName;
ArgName = TypeString + ArgName;
Expand All @@ -4236,24 +4231,21 @@ std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE,
Constructor += ", int flags=0)";
// Initialize all "by copy" arguments.
bool firsTime = true;
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
++I) {
std::string Name = (*I)->getNameAsString();
if (firsTime) {
Constructor += " : ";
firsTime = false;
}
else
Constructor += ", ";
if (isTopLevelBlockPointerType((*I)->getType()))
Constructor += Name + "((struct __block_impl *)_" + Name + ")";
else
Constructor += Name + "(_" + Name + ")";
for (const ValueDecl *VD : BlockByCopyDecls) {
std::string Name = VD->getNameAsString();
if (firsTime) {
Constructor += " : ";
firsTime = false;
} else
Constructor += ", ";
if (isTopLevelBlockPointerType(VD->getType()))
Constructor += Name + "((struct __block_impl *)_" + Name + ")";
else
Constructor += Name + "(_" + Name + ")";
}
// Initialize all "by ref" arguments.
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
++I) {
std::string Name = (*I)->getNameAsString();
for (const ValueDecl *VD : BlockByRefDecls) {
std::string Name = VD->getNameAsString();
if (firsTime) {
Constructor += " : ";
firsTime = false;
Expand Down Expand Up @@ -5277,47 +5269,43 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
if (BlockDeclRefs.size()) {
Expr *Exp;
// Output all "by copy" declarations.
for (auto I = BlockByCopyDecls.begin(), E = BlockByCopyDecls.end(); I != E;
++I) {
if (isObjCType((*I)->getType())) {
for (ValueDecl *VD : BlockByCopyDecls) {
if (isObjCType(VD->getType())) {
// FIXME: Conform to ABI ([[obj retain] autorelease]).
FD = SynthBlockInitFunctionDecl((*I)->getName());
FD = SynthBlockInitFunctionDecl(VD->getName());
Exp = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
VK_LValue, SourceLocation());
if (HasLocalVariableExternalStorage(*I)) {
QualType QT = (*I)->getType();
if (HasLocalVariableExternalStorage(VD)) {
QualType QT = VD->getType();
QT = Context->getPointerType(QT);
Exp = UnaryOperator::Create(const_cast<ASTContext &>(*Context), Exp,
UO_AddrOf, QT, VK_PRValue, OK_Ordinary,
SourceLocation(), false,
FPOptionsOverride());
}
} else if (isTopLevelBlockPointerType((*I)->getType())) {
FD = SynthBlockInitFunctionDecl((*I)->getName());
} else if (isTopLevelBlockPointerType(VD->getType())) {
FD = SynthBlockInitFunctionDecl(VD->getName());
Arg = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
VK_LValue, SourceLocation());
Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
CK_BitCast, Arg);
} else {
FD = SynthBlockInitFunctionDecl((*I)->getName());
FD = SynthBlockInitFunctionDecl(VD->getName());
Exp = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
VK_LValue, SourceLocation());
if (HasLocalVariableExternalStorage(*I)) {
QualType QT = (*I)->getType();
if (HasLocalVariableExternalStorage(VD)) {
QualType QT = VD->getType();
QT = Context->getPointerType(QT);
Exp = UnaryOperator::Create(const_cast<ASTContext &>(*Context), Exp,
UO_AddrOf, QT, VK_PRValue, OK_Ordinary,
SourceLocation(), false,
FPOptionsOverride());
}

}
InitExprs.push_back(Exp);
}
// Output all "by ref" declarations.
for (auto I = BlockByRefDecls.begin(), E = BlockByRefDecls.end(); I != E;
++I) {
ValueDecl *ND = (*I);
for (ValueDecl *ND : BlockByRefDecls) {
std::string Name(ND->getNameAsString());
std::string RecName;
RewriteByRefString(RecName, Name, ND, true);
Expand All @@ -5329,7 +5317,7 @@ Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl");
QualType castT = Context->getPointerType(Context->getTagDeclType(RD));

FD = SynthBlockInitFunctionDecl((*I)->getName());
FD = SynthBlockInitFunctionDecl(ND->getName());
Exp = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
VK_LValue, SourceLocation());
bool isNestedCapturedVar = false;
Expand Down
Loading