[clang] cb80b26 - [clang] Use *Set::insert_range (NFC) (#133357)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 27 20:14:28 PDT 2025


Author: Kazu Hirata
Date: 2025-03-27T20:14:25-07:00
New Revision: cb80b26e3731e7b10ff516d66761a3cfbc55bd20

URL: https://github.com/llvm/llvm-project/commit/cb80b26e3731e7b10ff516d66761a3cfbc55bd20
DIFF: https://github.com/llvm/llvm-project/commit/cb80b26e3731e7b10ff516d66761a3cfbc55bd20.diff

LOG: [clang] Use *Set::insert_range (NFC) (#133357)

We can use *Set::insert_range to collapse:

  for (auto Elem : Range)
    Set.insert(E);

down to:

  Set.insert_range(Range);

In some cases, we can further fold that into the set declaration.

Added: 
    

Modified: 
    clang/lib/Analysis/FlowSensitive/ASTOps.cpp
    clang/lib/Basic/TargetID.cpp
    clang/lib/CodeGen/CGDeclCXX.cpp
    clang/lib/Sema/SemaChecking.cpp
    clang/lib/Sema/SemaObjCProperty.cpp
    clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/FlowSensitive/ASTOps.cpp b/clang/lib/Analysis/FlowSensitive/ASTOps.cpp
index 9a46e79cf4a67..431b1f2038357 100644
--- a/clang/lib/Analysis/FlowSensitive/ASTOps.cpp
+++ b/clang/lib/Analysis/FlowSensitive/ASTOps.cpp
@@ -64,8 +64,7 @@ static void getFieldsFromClassHierarchy(QualType Type, FieldSet &Fields) {
       !Type->isRecordType())
     return;
 
-  for (const FieldDecl *Field : Type->getAsRecordDecl()->fields())
-    Fields.insert(Field);
+  Fields.insert_range(Type->getAsRecordDecl()->fields());
   if (auto *CXXRecord = Type->getAsCXXRecordDecl())
     for (const CXXBaseSpecifier &Base : CXXRecord->bases())
       getFieldsFromClassHierarchy(Base.getType(), Fields);
@@ -260,15 +259,13 @@ class ReferencedDeclsVisitor : public AnalysisASTVisitor {
 
   bool VisitInitListExpr(InitListExpr *InitList) override {
     if (InitList->getType()->isRecordType())
-      for (const auto *FD : getFieldsForInitListExpr(InitList))
-        Referenced.Fields.insert(FD);
+      Referenced.Fields.insert_range(getFieldsForInitListExpr(InitList));
     return true;
   }
 
   bool VisitCXXParenListInitExpr(CXXParenListInitExpr *ParenInitList) override {
     if (ParenInitList->getType()->isRecordType())
-      for (const auto *FD : getFieldsForInitListExpr(ParenInitList))
-        Referenced.Fields.insert(FD);
+      Referenced.Fields.insert_range(getFieldsForInitListExpr(ParenInitList));
     return true;
   }
 

diff  --git a/clang/lib/Basic/TargetID.cpp b/clang/lib/Basic/TargetID.cpp
index b42d1f07013c2..2a2f23409e896 100644
--- a/clang/lib/Basic/TargetID.cpp
+++ b/clang/lib/Basic/TargetID.cpp
@@ -113,9 +113,8 @@ parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
   if (Processor.empty())
     return std::nullopt;
 
-  llvm::SmallSet<llvm::StringRef, 4> AllFeatures;
-  for (auto &&F : getAllPossibleTargetIDFeatures(T, Processor))
-    AllFeatures.insert(F);
+  llvm::SmallSet<llvm::StringRef, 4> AllFeatures(
+      llvm::from_range, getAllPossibleTargetIDFeatures(T, Processor));
 
   for (auto &&F : *FeatureMap)
     if (!AllFeatures.count(F.first()))

diff  --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp
index 1ad34ae61f96a..a01fa157c2b26 100644
--- a/clang/lib/CodeGen/CGDeclCXX.cpp
+++ b/clang/lib/CodeGen/CGDeclCXX.cpp
@@ -703,8 +703,7 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
   for (auto I : Primary->Exports)
     AllImports.insert(I.getPointer());
   // Ones that we only import.
-  for (Module *M : Primary->Imports)
-    AllImports.insert(M);
+  AllImports.insert_range(Primary->Imports);
   // Ones that we import in the global module fragment or the private module
   // fragment.
   for (Module *SubM : Primary->submodules()) {
@@ -714,8 +713,7 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
     assert(SubM->Exports.empty() &&
            "The global mdoule fragments and the private module fragments are "
            "not allowed to export import modules.");
-    for (Module *M : SubM->Imports)
-      AllImports.insert(M);
+    AllImports.insert_range(SubM->Imports);
   }
 
   SmallVector<llvm::Function *, 8> ModuleInits;

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 12a8894cc7f47..5a4fa97366809 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14731,9 +14731,8 @@ static bool isLayoutCompatibleStruct(const ASTContext &C, const RecordDecl *RD1,
 /// (C++11 [class.mem] p18)
 static bool isLayoutCompatibleUnion(const ASTContext &C, const RecordDecl *RD1,
                                     const RecordDecl *RD2) {
-  llvm::SmallPtrSet<const FieldDecl *, 8> UnmatchedFields;
-  for (auto *Field2 : RD2->fields())
-    UnmatchedFields.insert(Field2);
+  llvm::SmallPtrSet<const FieldDecl *, 8> UnmatchedFields(llvm::from_range,
+                                                          RD2->fields());
 
   for (auto *Field1 : RD1->fields()) {
     auto I = UnmatchedFields.begin();

diff  --git a/clang/lib/Sema/SemaObjCProperty.cpp b/clang/lib/Sema/SemaObjCProperty.cpp
index 93a17e8459811..6db2c246de791 100644
--- a/clang/lib/Sema/SemaObjCProperty.cpp
+++ b/clang/lib/Sema/SemaObjCProperty.cpp
@@ -2075,10 +2075,9 @@ void SemaObjC::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl *IMPDecl,
   for (const auto *I : IMPDecl->property_impls())
     PropImplMap.insert(I->getPropertyDecl());
 
-  llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
   // Collect property accessors implemented in current implementation.
-  for (const auto *I : IMPDecl->methods())
-    InsMap.insert(I);
+  llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap(llvm::from_range,
+                                                      IMPDecl->methods());
 
   ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
   ObjCInterfaceDecl *PrimaryClass = nullptr;
@@ -2089,8 +2088,7 @@ void SemaObjC::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl *IMPDecl,
         // When reporting on missing setter/getters, do not report when
         // setter/getter is implemented in category's primary class
         // implementation.
-        for (const auto *I : IMP->methods())
-          InsMap.insert(I);
+        InsMap.insert_range(IMP->methods());
       }
 
   for (ObjCContainerDecl::PropertyMap::iterator

diff  --git a/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp b/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
index f78b09a755591..720afea8b0965 100644
--- a/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
+++ b/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -1175,9 +1175,7 @@ StringRef OpenCLBuiltinFileEmitterBase::emitTypeExtensionGuards(
       // The TypeExtensions are space-separated in the .td file.
       SmallVector<StringRef, 2> ExtVec;
       TypeExt.split(ExtVec, " ");
-      for (const auto Ext : ExtVec) {
-        ExtSet.insert(Ext);
-      }
+      ExtSet.insert_range(ExtVec);
     }
   }
 


        


More information about the cfe-commits mailing list