From ec5eab7e87cb472ddf6e2b444b6109d08636e244 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 20 Aug 2022 21:18:32 -0700 Subject: [PATCH] Use range-based for loops (NFC) --- clang/lib/AST/ASTContext.cpp | 9 ++++----- clang/lib/Driver/Compilation.cpp | 6 +++--- clang/lib/Driver/ToolChain.cpp | 6 +++--- clang/lib/Parse/ParseOpenMP.cpp | 8 ++++---- clang/lib/Sema/SemaLookup.cpp | 15 ++++++--------- clang/lib/Sema/TreeTransform.h | 7 +++---- lldb/source/Utility/ArchSpec.cpp | 20 ++++++++------------ .../lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp | 10 +++++----- llvm/lib/Support/ARMAttributeParser.cpp | 8 +++----- llvm/lib/Support/CSKYAttributeParser.cpp | 7 +++---- llvm/lib/Support/RISCVAttributeParser.cpp | 7 +++---- .../AArch64/MCTargetDesc/AArch64InstPrinter.cpp | 7 +++---- llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp | 6 +++--- llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp | 9 ++++----- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp | 3 +-- llvm/tools/llvm-config/llvm-config.cpp | 17 +++++++---------- 16 files changed, 63 insertions(+), 82 deletions(-) diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 2c2f466..0786c1e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -7065,12 +7065,11 @@ QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { uint64_t FromSize = getTypeSize(BT); QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, LongLongTy, UnsignedLongLongTy }; - for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { - uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); + for (const auto &PT : PromoteTypes) { + uint64_t ToSize = getTypeSize(PT); if (FromSize < ToSize || - (FromSize == ToSize && - FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) - return PromoteTypes[Idx]; + (FromSize == ToSize && FromIsSigned == PT->isSignedIntegerType())) + return PT; } llvm_unreachable("char type should fit into long long"); } diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp index b4367a0..ecf3da2 100644 --- a/clang/lib/Driver/Compilation.cpp +++ b/clang/lib/Driver/Compilation.cpp @@ -283,9 +283,9 @@ void Compilation::initCompilationForDiagnostics() { options::OPT_o, options::OPT_MD, options::OPT_MMD, options::OPT_M, options::OPT_MM, options::OPT_MF, options::OPT_MG, options::OPT_MJ, options::OPT_MQ, options::OPT_MT, options::OPT_MV}; - for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) { - if (TranslatedArgs->hasArg(OutputOpts[i])) - TranslatedArgs->eraseArg(OutputOpts[i]); + for (const auto &Opt : OutputOpts) { + if (TranslatedArgs->hasArg(Opt)) + TranslatedArgs->eraseArg(Opt); } TranslatedArgs->ClaimAllArgs(); diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp index bc86e5b..3434b5c 100644 --- a/clang/lib/Driver/ToolChain.cpp +++ b/clang/lib/Driver/ToolChain.cpp @@ -185,11 +185,11 @@ static const DriverSuffix *FindDriverSuffix(StringRef ProgName, size_t &Pos) { {"clang-dxc", "--driver-mode=dxc"}, }; - for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) { - StringRef Suffix(DriverSuffixes[i].Suffix); + for (const auto &DS : DriverSuffixes) { + StringRef Suffix(DS.Suffix); if (ProgName.endswith(Suffix)) { Pos = ProgName.size() - Suffix.size(); - return &DriverSuffixes[i]; + return &DS; } } return nullptr; diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp index 2421f8d5..e0707c6 100644 --- a/clang/lib/Parse/ParseOpenMP.cpp +++ b/clang/lib/Parse/ParseOpenMP.cpp @@ -198,8 +198,8 @@ static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) { if (DKind == OMPD_unknown) return OMPD_unknown; - for (unsigned I = 0; I < llvm::array_lengthof(F); ++I) { - if (DKind != F[I][0]) + for (const auto &I : F) { + if (DKind != I[0]) continue; Tok = P.getPreprocessor().LookAhead(0); @@ -210,9 +210,9 @@ static OpenMPDirectiveKindExWrapper parseOpenMPDirectiveKind(Parser &P) { if (SDKind == OMPD_unknown) continue; - if (SDKind == F[I][1]) { + if (SDKind == I[1]) { P.ConsumeToken(); - DKind = F[I][2]; + DKind = I[2]; } } return unsigned(DKind) < llvm::omp::Directive_enumSize diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index d163ad3..1d8df94 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -5023,9 +5023,8 @@ static void AddKeywordsToConsumer(Sema &SemaRef, "extern", "inline", "static", "typedef" }; - const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs); - for (unsigned I = 0; I != NumCTypeSpecs; ++I) - Consumer.addKeywordResult(CTypeSpecs[I]); + for (const auto *CTS : CTypeSpecs) + Consumer.addKeywordResult(CTS); if (SemaRef.getLangOpts().C99) Consumer.addKeywordResult("restrict"); @@ -5077,9 +5076,8 @@ static void AddKeywordsToConsumer(Sema &SemaRef, static const char *const CXXExprs[] = { "delete", "new", "operator", "throw", "typeid" }; - const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs); - for (unsigned I = 0; I != NumCXXExprs; ++I) - Consumer.addKeywordResult(CXXExprs[I]); + for (const auto *CE : CXXExprs) + Consumer.addKeywordResult(CE); if (isa(SemaRef.CurContext) && cast(SemaRef.CurContext)->isInstance()) @@ -5103,9 +5101,8 @@ static void AddKeywordsToConsumer(Sema &SemaRef, // Statements. static const char *const CStmts[] = { "do", "else", "for", "goto", "if", "return", "switch", "while" }; - const unsigned NumCStmts = llvm::array_lengthof(CStmts); - for (unsigned I = 0; I != NumCStmts; ++I) - Consumer.addKeywordResult(CStmts[I]); + for (const auto *CS : CStmts) + Consumer.addKeywordResult(CS); if (SemaRef.getLangOpts().CPlusPlus) { Consumer.addKeywordResult("catch"); diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 142e00a..c5337de 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -14536,11 +14536,10 @@ TreeTransform::RebuildArrayType(QualType ElementType, SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy, SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty }; - const unsigned NumTypes = llvm::array_lengthof(Types); QualType SizeType; - for (unsigned I = 0; I != NumTypes; ++I) - if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) { - SizeType = Types[I]; + for (const auto &T : Types) + if (Size->getBitWidth() == SemaRef.Context.getIntWidth(T)) { + SizeType = T; break; } diff --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp index 348a62d..1cc8ab7 100644 --- a/lldb/source/Utility/ArchSpec.cpp +++ b/lldb/source/Utility/ArchSpec.cpp @@ -255,13 +255,13 @@ struct ArchDefinition { }; void ArchSpec::ListSupportedArchNames(StringList &list) { - for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) - list.AppendString(g_core_definitions[i].name); + for (const auto &def : g_core_definitions) + list.AppendString(def.name); } void ArchSpec::AutoComplete(CompletionRequest &request) { - for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) - request.TryCompleteCurrentArg(g_core_definitions[i].name); + for (const auto &def : g_core_definitions) + request.TryCompleteCurrentArg(def.name); } #define CPU_ANY (UINT32_MAX) @@ -446,16 +446,12 @@ static const ArchDefinition g_coff_arch_def = { static const ArchDefinition *g_arch_definitions[] = { &g_macho_arch_def, &g_elf_arch_def, &g_coff_arch_def}; -static const size_t k_num_arch_definitions = - llvm::array_lengthof(g_arch_definitions); - //===----------------------------------------------------------------------===// // Static helper functions. // Get the architecture definition for a given object type. static const ArchDefinition *FindArchDefinition(ArchitectureType arch_type) { - for (unsigned int i = 0; i < k_num_arch_definitions; ++i) { - const ArchDefinition *def = g_arch_definitions[i]; + for (const ArchDefinition *def : g_arch_definitions) { if (def->type == arch_type) return def; } @@ -464,9 +460,9 @@ static const ArchDefinition *FindArchDefinition(ArchitectureType arch_type) { // Get an architecture definition by name. static const CoreDefinition *FindCoreDefinition(llvm::StringRef name) { - for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) { - if (name.equals_insensitive(g_core_definitions[i].name)) - return &g_core_definitions[i]; + for (const auto &def : g_core_definitions) { + if (name.equals_insensitive(def.name)) + return &def; } return nullptr; } diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp index 5944dee..55f9851 100644 --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp @@ -2579,11 +2579,11 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N, NewElts](SmallVectorImpl &Mask) { SetVector UniqueInputs; SetVector UniqueConstantInputs; - for (unsigned I = 0; I < array_lengthof(Inputs); ++I) { - if (IsConstant(Inputs[I])) - UniqueConstantInputs.insert(Inputs[I]); - else if (!Inputs[I].isUndef()) - UniqueInputs.insert(Inputs[I]); + for (const auto &I : Inputs) { + if (IsConstant(I)) + UniqueConstantInputs.insert(I); + else if (!I.isUndef()) + UniqueInputs.insert(I); } // Adjust mask in case of reused inputs. Also, need to insert constant // inputs at first, otherwise it affects the final outcome. diff --git a/llvm/lib/Support/ARMAttributeParser.cpp b/llvm/lib/Support/ARMAttributeParser.cpp index 69a969f..af79a58c 100644 --- a/llvm/lib/Support/ARMAttributeParser.cpp +++ b/llvm/lib/Support/ARMAttributeParser.cpp @@ -463,11 +463,9 @@ Error ARMAttributeParser::also_compatible_with(AttrType tag) { Error ARMAttributeParser::handler(uint64_t tag, bool &handled) { handled = false; - for (unsigned AHI = 0, AHE = array_lengthof(displayRoutines); AHI != AHE; - ++AHI) { - if (uint64_t(displayRoutines[AHI].attribute) == tag) { - if (Error e = - (this->*displayRoutines[AHI].routine)(static_cast(tag))) + for (const auto &AH : displayRoutines) { + if (uint64_t(AH.attribute) == tag) { + if (Error e = (this->*AH.routine)(static_cast(tag))) return e; handled = true; break; diff --git a/llvm/lib/Support/CSKYAttributeParser.cpp b/llvm/lib/Support/CSKYAttributeParser.cpp index ea1ac92..1310eed 100644 --- a/llvm/lib/Support/CSKYAttributeParser.cpp +++ b/llvm/lib/Support/CSKYAttributeParser.cpp @@ -69,10 +69,9 @@ const CSKYAttributeParser::DisplayHandler Error CSKYAttributeParser::handler(uint64_t tag, bool &handled) { handled = false; - for (unsigned AHI = 0, AHE = array_lengthof(displayRoutines); AHI != AHE; - ++AHI) { - if (uint64_t(displayRoutines[AHI].attribute) == tag) { - if (Error e = (this->*displayRoutines[AHI].routine)(tag)) + for (const auto &AH : displayRoutines) { + if (uint64_t(AH.attribute) == tag) { + if (Error e = (this->*AH.routine)(tag)) return e; handled = true; break; diff --git a/llvm/lib/Support/RISCVAttributeParser.cpp b/llvm/lib/Support/RISCVAttributeParser.cpp index 393861c..b9fdfe0 100644 --- a/llvm/lib/Support/RISCVAttributeParser.cpp +++ b/llvm/lib/Support/RISCVAttributeParser.cpp @@ -53,10 +53,9 @@ Error RISCVAttributeParser::stackAlign(unsigned tag) { Error RISCVAttributeParser::handler(uint64_t tag, bool &handled) { handled = false; - for (unsigned AHI = 0, AHE = array_lengthof(displayRoutines); AHI != AHE; - ++AHI) { - if (uint64_t(displayRoutines[AHI].attribute) == tag) { - if (Error e = (this->*displayRoutines[AHI].routine)(tag)) + for (const auto &AH : displayRoutines) { + if (uint64_t(AH.attribute) == tag) { + if (Error e = (this->*AH.routine)(tag)) return e; handled = true; break; diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp index 5d2ba7e..33869c4 100644 --- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp +++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.cpp @@ -705,10 +705,9 @@ static const LdStNInstrDesc LdStNInstInfo[] = { }; static const LdStNInstrDesc *getLdStNInstrDesc(unsigned Opcode) { - unsigned Idx; - for (Idx = 0; Idx != array_lengthof(LdStNInstInfo); ++Idx) - if (LdStNInstInfo[Idx].Opcode == Opcode) - return &LdStNInstInfo[Idx]; + for (const auto &Info : LdStNInstInfo) + if (Info.Opcode == Opcode) + return &Info; return nullptr; } diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp index 92aa103..79fda24 100644 --- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp +++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp @@ -2468,9 +2468,9 @@ static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = { }; unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) { - for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i) - if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc) - return AddSubFlagsOpcodeMap[i].MachineOpc; + for (const auto &Entry : AddSubFlagsOpcodeMap) + if (OldOpc == Entry.PseudoOpc) + return Entry.MachineOpc; return 0; } diff --git a/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp b/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp index d943507..9c06c46 100644 --- a/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp +++ b/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp @@ -301,8 +301,8 @@ SystemZELFFrameLowering::SystemZELFFrameLowering() // Create a mapping from register number to save slot offset. // These offsets are relative to the start of the register save area. RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); - for (unsigned I = 0, E = array_lengthof(ELFSpillOffsetTable); I != E; ++I) - RegSpillOffsets[ELFSpillOffsetTable[I].Reg] = ELFSpillOffsetTable[I].Offset; + for (const auto &Entry : ELFSpillOffsetTable) + RegSpillOffsets[Entry.Reg] = Entry.Offset; } // Add GPR64 to the save instruction being built by MIB, which is in basic @@ -906,9 +906,8 @@ SystemZXPLINKFrameLowering::SystemZXPLINKFrameLowering() // Create a mapping from register number to save slot offset. // These offsets are relative to the start of the local are area. RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); - for (unsigned I = 0, E = array_lengthof(XPLINKSpillOffsetTable); I != E; ++I) - RegSpillOffsets[XPLINKSpillOffsetTable[I].Reg] = - XPLINKSpillOffsetTable[I].Offset; + for (const auto &Entry : XPLINKSpillOffsetTable) + RegSpillOffsets[Entry.Reg] = Entry.Offset; } // Checks if the function is a potential candidate for being a XPLeaf routine. diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp index c303307..ca6c156 100644 --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -4267,8 +4267,7 @@ void LSRInstance::GenerateCrossUseConstantOffsets() { ImmMapTy::const_iterator OtherImms[] = { Imms.begin(), std::prev(Imms.end()), Imms.lower_bound(Avg)}; - for (size_t i = 0, e = array_lengthof(OtherImms); i != e; ++i) { - ImmMapTy::const_iterator M = OtherImms[i]; + for (const auto &M : OtherImms) { if (M == J || M == JE) continue; // Compute the difference between the two. diff --git a/llvm/tools/llvm-config/llvm-config.cpp b/llvm/tools/llvm-config/llvm-config.cpp index 18958e8..8b28a00 100644 --- a/llvm/tools/llvm-config/llvm-config.cpp +++ b/llvm/tools/llvm-config/llvm-config.cpp @@ -170,10 +170,8 @@ static std::vector ComputeLibsForComponents( // Build a map of component names to information. StringMap ComponentMap; - for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) { - AvailableComponent *AC = &AvailableComponents[i]; - ComponentMap[AC->Name] = AC; - } + for (auto &AC : AvailableComponents) + ComponentMap[AC.Name] = &AC; // Visit the components. for (unsigned i = 0, e = Components.size(); i != e; ++i) { @@ -546,15 +544,14 @@ int main(int argc, char **argv) { /// built, print LLVM_DYLIB_COMPONENTS instead of everything /// in the manifest. std::vector Components; - for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) { + for (const auto &AC : AvailableComponents) { // Only include non-installed components when in a development tree. - if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree) + if (!AC.IsInstalled && !IsInDevelopmentTree) continue; - Components.push_back(AvailableComponents[j].Name); - if (AvailableComponents[j].Library && !IsInDevelopmentTree) { - std::string path( - GetComponentLibraryPath(AvailableComponents[j].Library, false)); + Components.push_back(AC.Name); + if (AC.Library && !IsInDevelopmentTree) { + std::string path(GetComponentLibraryPath(AC.Library, false)); if (DirSep == "\\") { std::replace(path.begin(), path.end(), '/', '\\'); } -- 2.7.4