From 2acddf9793a734abd530dc423a62a4ac213504d0 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 2 Jun 2017 20:43:25 +0200 Subject: [PATCH] Range-ify usages of front-end Arrays --- driver/archiver.cpp | 5 +++-- driver/linker-gcc.cpp | 14 ++++++++------ driver/linker-msvc.cpp | 14 ++++++++------ driver/main.cpp | 4 ++-- gen/asmstmt.cpp | 3 +-- gen/dibuilder.cpp | 8 +++----- gen/inlineir.cpp | 7 +++---- gen/llvmhelpers.cpp | 13 ++++++------- gen/mangling.cpp | 4 ++-- gen/modules.cpp | 1 + gen/statements.cpp | 2 +- gen/toconstelem.cpp | 8 ++++---- gen/toir.cpp | 14 +++++++------- gen/typinf.cpp | 3 +-- 14 files changed, 50 insertions(+), 50 deletions(-) diff --git a/driver/archiver.cpp b/driver/archiver.cpp index 2c156ceb191..8a8aacbb21a 100644 --- a/driver/archiver.cpp +++ b/driver/archiver.cpp @@ -346,8 +346,9 @@ int createStaticLibrary() { } // object files - for (unsigned i = 0; i < global.params.objfiles->dim; i++) - args.push_back((*global.params.objfiles)[i]); + for (auto objfile : *global.params.objfiles) { + args.push_back(objfile); + } // .res/.def files for lib.exe if (isTargetMSVC) { diff --git a/driver/linker-gcc.cpp b/driver/linker-gcc.cpp index c12f602526a..0d7a8e33ebf 100644 --- a/driver/linker-gcc.cpp +++ b/driver/linker-gcc.cpp @@ -173,8 +173,9 @@ void ArgsBuilder::addLTOLinkFlags() { void ArgsBuilder::build(llvm::StringRef outputPath, llvm::cl::boolOrDefault fullyStaticFlag) { // object files - for (unsigned i = 0; i < global.params.objfiles->dim; i++) - args.push_back((*global.params.objfiles)[i]); + for (auto objfile : *global.params.objfiles) { + args.push_back(objfile); + } // Link with profile-rt library when generating an instrumented binary. // profile-rt uses Phobos (MD5 hashing) and therefore must be passed on the @@ -191,8 +192,9 @@ void ArgsBuilder::build(llvm::StringRef outputPath, } // user libs - for (unsigned i = 0; i < global.params.libfiles->dim; i++) - args.push_back((*global.params.libfiles)[i]); + for (auto libfile : *global.params.libfiles) { + args.push_back(libfile); + } if (global.params.dll) { args.push_back("-shared"); @@ -217,8 +219,8 @@ void ArgsBuilder::build(llvm::StringRef outputPath, addUserSwitches(); // libs added via pragma(lib, libname) - for (unsigned i = 0; i < global.params.linkswitches->dim; i++) { - args.push_back((*global.params.linkswitches)[i]); + for (auto ls : *global.params.linkswitches) { + args.push_back(ls); } if (global.params.targetTriple->getOS() == llvm::Triple::Linux) { diff --git a/driver/linker-msvc.cpp b/driver/linker-msvc.cpp index 3624b3646a6..6ead29f5e2c 100644 --- a/driver/linker-msvc.cpp +++ b/driver/linker-msvc.cpp @@ -100,8 +100,9 @@ int linkObjToBinaryMSVC(llvm::StringRef outputPath, bool useInternalLinker, args.push_back(("/OUT:" + outputPath).str()); // object files - for (unsigned i = 0; i < global.params.objfiles->dim; i++) - args.push_back((*global.params.objfiles)[i]); + for (auto objfile : *global.params.objfiles) { + args.push_back(objfile); + } // .res/.def files if (global.params.resfile) @@ -118,8 +119,9 @@ int linkObjToBinaryMSVC(llvm::StringRef outputPath, bool useInternalLinker, } // user libs - for (unsigned i = 0; i < global.params.libfiles->dim; i++) - args.push_back((*global.params.libfiles)[i]); + for (auto libfile : *global.params.libfiles) { + args.push_back(libfile); + } // additional linker switches auto addSwitch = [&](std::string str) { @@ -138,8 +140,8 @@ int linkObjToBinaryMSVC(llvm::StringRef outputPath, bool useInternalLinker, addSwitch(str); } - for (unsigned i = 0; i < global.params.linkswitches->dim; i++) { - addSwitch(global.params.linkswitches->data[i]); + for (auto ls : *global.params.linkswitches) { + addSwitch(ls); } // default libs diff --git a/driver/main.cpp b/driver/main.cpp index 40a9b7527ae..7e4454147ad 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -404,8 +404,8 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, const auto toWinPaths = [](Strings *paths) { if (!paths) return; - for (unsigned i = 0; i < paths->dim; ++i) - (*paths)[i] = dupPathString((*paths)[i]); + for (auto &path : *paths) + path = dupPathString(path); }; toWinPaths(global.params.imppath); toWinPaths(global.params.fileImppath); diff --git a/gen/asmstmt.cpp b/gen/asmstmt.cpp index ccba4aa3e71..0a91efcfede 100644 --- a/gen/asmstmt.cpp +++ b/gen/asmstmt.cpp @@ -479,8 +479,7 @@ void CompoundAsmStatement_toIR(CompoundAsmStatement *stmt, IRState *p) { p->asmBlock = asmblock; // do asm statements - for (unsigned i = 0; i < stmt->statements->dim; i++) { - Statement *s = (*stmt->statements)[i]; + for (auto s : *stmt->statements) { if (s) { Statement_toIR(s, p); } diff --git a/gen/dibuilder.cpp b/gen/dibuilder.cpp index 79267b82483..03fca6330cf 100644 --- a/gen/dibuilder.cpp +++ b/gen/dibuilder.cpp @@ -1177,11 +1177,9 @@ void ldc::DIBuilder::EmitLocalVariable(llvm::Value *ll, VarDeclaration *vd, size_t argNo = 0; if (fd->vthis != vd) { assert(fd->parameters); - for (argNo = 0; argNo < fd->parameters->dim; argNo++) { - if ((*fd->parameters)[argNo] == vd) - break; - } - assert(argNo < fd->parameters->dim); + auto it = std::find(fd->parameters->begin(), fd->parameters->end(), vd); + assert(it != fd->parameters->end()); + argNo = it - fd->parameters->begin(); if (fd->vthis) argNo++; } diff --git a/gen/inlineir.cpp b/gen/inlineir.cpp index bb87e1bfd6a..8a7f286c05c 100644 --- a/gen/inlineir.cpp +++ b/gen/inlineir.cpp @@ -165,11 +165,10 @@ DValue *DtoInlineIRExpr(Loc &loc, FuncDeclaration *fdecl, fun->setCallingConv(llvm::CallingConv::C); // Build the runtime arguments - size_t n = arguments->dim; llvm::SmallVector args; - args.reserve(n); - for (size_t i = 0; i < n; i++) { - args.push_back(DtoRVal((*arguments)[i])); + args.reserve(arguments->dim); + for (auto arg : *arguments) { + args.push_back(DtoRVal(arg)); } llvm::Value *rv = gIR->ir->CreateCall(fun, args); diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 6e8909f6f4b..ba85b92e2b3 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -988,10 +988,9 @@ DValue *DtoDeclarationExp(Dsymbol *declaration) { } else if (AttribDeclaration *a = declaration->isAttribDeclaration()) { Logger::println("AttribDeclaration"); // choose the right set in case this is a conditional declaration - Dsymbols *d = a->include(nullptr, nullptr); - if (d) { - for (unsigned i = 0; i < d->dim; ++i) { - DtoDeclarationExp((*d)[i]); + if (auto symbols = a->include(nullptr, nullptr)) { + for (auto symbol : *symbols) { + DtoDeclarationExp(symbol); } } } else if (TemplateMixin *m = declaration->isTemplateMixin()) { @@ -1003,9 +1002,9 @@ DValue *DtoDeclarationExp(Dsymbol *declaration) { Logger::println("TupleDeclaration"); assert(tupled->isexp && "Non-expression tuple decls not handled yet."); assert(tupled->objects); - for (unsigned i = 0; i < tupled->objects->dim; ++i) { - DsymbolExp *exp = static_cast(tupled->objects->data[i]); - DtoDeclarationExp(exp->s); + for (auto object : *tupled->objects) { + auto symExp = static_cast(object); + DtoDeclarationExp(symExp->s); } } else { // Do nothing for template/alias/enum declarations and static diff --git a/gen/mangling.cpp b/gen/mangling.cpp index ef312009e41..a9c76898b7f 100644 --- a/gen/mangling.cpp +++ b/gen/mangling.cpp @@ -58,8 +58,8 @@ std::string hashSymbolName(llvm::StringRef name, Dsymbol *symb) { auto moddecl = symb->getModule()->md; assert(moddecl); if (auto packages = moddecl->packages) { - for (size_t i = 0; i < packages->dim; ++i) { - llvm::StringRef str = (*packages)[i]->toChars(); + for (auto package : *packages) { + llvm::StringRef str = package->toChars(); ret += std::to_string(str.size()); ret += str; } diff --git a/gen/modules.cpp b/gen/modules.cpp index f4105a4867f..a3428cd9a06 100644 --- a/gen/modules.cpp +++ b/gen/modules.cpp @@ -718,6 +718,7 @@ void codegenModule(IRState *irs, Module *m) { } // process module members + // NOTE: m->members may grow during codegen for (unsigned k = 0; k < m->members->dim; k++) { Dsymbol *dsym = (*m->members)[k]; assert(dsym); diff --git a/gen/statements.cpp b/gen/statements.cpp index 4b160741c67..056e81cb2a7 100644 --- a/gen/statements.cpp +++ b/gen/statements.cpp @@ -1210,7 +1210,7 @@ class ToIRVisitor : public Visitor { } // do statements - Statement **stmts = static_cast(stmt->statements->data); + Statement **stmts = stmt->statements->data; for (size_t i = 0; i < nstmt; i++) { Statement *s = stmts[i]; diff --git a/gen/toconstelem.cpp b/gen/toconstelem.cpp index 481e50205b6..a6652fba182 100644 --- a/gen/toconstelem.cpp +++ b/gen/toconstelem.cpp @@ -591,8 +591,8 @@ class ToConstElemVisitor : public Visitor { std::map varInits; const size_t nexprs = e->elements->dim; for (size_t i = 0; i < nexprs; i++) { - if ((*e->elements)[i]) { - LLConstant *c = toConstElem((*e->elements)[i]); + if (auto elem = (*e->elements)[i]) { + LLConstant *c = toConstElem(elem); // extend i1 to i8 if (c->getType() == LLType::getInt1Ty(p->context())) c = llvm::ConstantExpr::getZExt(c, LLType::getInt8Ty(p->context())); @@ -641,12 +641,12 @@ class ToConstElemVisitor : public Visitor { cur = classHierachy.top(); classHierachy.pop(); for (size_t j = 0; j < cur->fields.dim; ++j) { - if ((*value->elements)[i]) { + if (auto elem = (*value->elements)[i]) { VarDeclaration *field = cur->fields[j]; IF_LOG Logger::println("Getting initializer for: %s", field->toChars()); LOG_SCOPE; - varInits[field] = toConstElem((*value->elements)[i]); + varInits[field] = toConstElem(elem); } ++i; } diff --git a/gen/toir.cpp b/gen/toir.cpp index f970952c629..b4764e0a3d5 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1466,8 +1466,8 @@ class ToElemVisitor : public Visitor { size_t ndims = e->arguments->dim; std::vector dims; dims.reserve(ndims); - for (size_t i = 0; i < ndims; ++i) { - dims.push_back(toElem((*e->arguments)[i])); + for (auto arg : *e->arguments) { + dims.push_back(toElem(arg)); } result = DtoNewMulDimDynArray(e->loc, e->newtype, &dims[0], ndims); } @@ -2272,7 +2272,7 @@ class ToElemVisitor : public Visitor { auto global = new llvm::GlobalVariable( gIR->module, init->getType(), true, llvm::GlobalValue::InternalLinkage, init, ".immutablearray"); - result = new DSliceValue(arrayType, DtoConstSize_t(e->elements->dim), + result = new DSliceValue(arrayType, DtoConstSize_t(len), DtoBitCast(global, getPtrToType(llElemType))); } else { DSliceValue *dynSlice = DtoNewDynArray( @@ -2431,8 +2431,8 @@ class ToElemVisitor : public Visitor { keysInits.reserve(e->keys->dim); valuesInits.reserve(e->keys->dim); for (size_t i = 0, n = e->keys->dim; i < n; ++i) { - Expression *ekey = e->keys->tdata()[i]; - Expression *eval = e->values->tdata()[i]; + Expression *ekey = (*e->keys)[i]; + Expression *eval = (*e->values)[i]; IF_LOG Logger::println("(%llu) aa[%s] = %s", static_cast(i), ekey->toChars(), eval->toChars()); @@ -2585,8 +2585,8 @@ class ToElemVisitor : public Visitor { std::vector types; types.reserve(e->exps->dim); - for (size_t i = 0; i < e->exps->dim; i++) { - types.push_back(DtoMemType((*e->exps)[i]->type)); + for (auto exp : *e->exps) { + types.push_back(DtoMemType(exp->type)); } LLValue *val = DtoRawAlloca(LLStructType::get(gIR->context(), types), 0, ".tuple"); diff --git a/gen/typinf.cpp b/gen/typinf.cpp index a63cbf02422..5747b017e9b 100644 --- a/gen/typinf.cpp +++ b/gen/typinf.cpp @@ -486,8 +486,7 @@ class LLVMDefineVisitor : public Visitor { LLType *tiTy = DtoType(Type::dtypeinfo->type); - for (size_t i = 0; i < dim; i++) { - Parameter *arg = static_cast(tu->arguments->data[i]); + for (auto arg : *tu->arguments) { arrInits.push_back(DtoTypeInfoOf(arg->type, true)); }