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

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@kazutakahirata kazutakahirata requested a review from nikic September 8, 2024 15:07
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Sep 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 8, 2024

@llvm/pr-subscribers-clang

Author: Kazu Hirata (kazutakahirata)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/107757.diff

1 Files Affected:

  • (modified) clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp (+48-60)
diff --git a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp
index 326920c7915537..8cdb463e2c99f2 100644
--- a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp
+++ b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp
@@ -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:
     //
@@ -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];
@@ -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:
       //
@@ -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());
@@ -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;
@@ -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;
@@ -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);
@@ -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;

@kazutakahirata kazutakahirata merged commit 668cbd8 into llvm:main Sep 8, 2024
10 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_range_for_RewriteModernObjC branch September 8, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants