Index: docs/ProgrammersManual.rst =================================================================== --- docs/ProgrammersManual.rst +++ docs/ProgrammersManual.rst @@ -2258,10 +2258,10 @@ .. code-block:: c++ // func is a pointer to a Function instance - for (Function::iterator i = func->begin(), e = func->end(); i != e; ++i) + for (BasicBlock &BB : Func) // Print out the name of the basic block if it has one, and then the // number of instructions that it contains - errs() << "Basic block (name=" << i->getName() << ") has " + errs() << "Basic block (name=" << BB.getName() << ") has " << i->size() << " instructions.\n"; Note that i can be used as if it were a pointer for the purposes of invoking @@ -2282,10 +2282,10 @@ .. code-block:: c++ // blk is a pointer to a BasicBlock instance - for (BasicBlock::iterator i = blk->begin(), e = blk->end(); i != e; ++i) + for (Instruction &I : Block) // The next statement works since operator<<(ostream&,...) // is overloaded for Instruction& - errs() << *i << "\n"; + errs() << I << "\n"; However, this isn't really the best way to print out the contents of a @@ -2425,13 +2425,13 @@ OurFunctionPass(): callCounter(0) { } virtual runOnFunction(Function& F) { - for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) { - for (BasicBlock::iterator i = b->begin(), ie = b->end(); i != ie; ++i) { - if (CallInst* callInst = dyn_cast(&*i)) { + for (BasicBlock &B : F) { + for (Instruction &I: B) { + if (auto *CallInst = dyn_cast(&I)) { // We know we've encountered a call instruction, so we // need to determine if it's a call to the // function pointed to by m_func or not. - if (callInst->getCalledFunction() == targetFunc) + if (CallInst->getCalledFunction() == targetFunc) ++callCounter; } } @@ -2524,12 +2524,11 @@ #include "llvm/IR/CFG.h" BasicBlock *BB = ...; - for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { - BasicBlock *Pred = *PI; + for (BasicBlock *Pred : predecessors(BB)) { // ... } -Similarly, to iterate over successors use ``succ_iterator/succ_begin/succ_end``. +Similarly, to iterate over successors use ``successors``. .. _simplechanges: @@ -2554,7 +2553,7 @@ .. code-block:: c++ - AllocaInst* ai = new AllocaInst(Type::Int32Ty); + auto *ai = new AllocaInst(Type::Int32Ty); will create an ``AllocaInst`` instance that represents the allocation of one integer in the current stack frame, at run time. Each ``Instruction`` subclass @@ -2579,7 +2578,7 @@ .. code-block:: c++ - AllocaInst* pa = new AllocaInst(Type::Int32Ty, 0, "indexLoc"); + auto *pa = new AllocaInst(Type::Int32Ty, 0, "indexLoc"); where ``indexLoc`` is now the logical name of the instruction's execution value, which is a pointer to an integer on the run time stack. @@ -2599,7 +2598,7 @@ BasicBlock *pb = ...; Instruction *pi = ...; - Instruction *newInst = new Instruction(...); + auto *newInst = new Instruction(...); pb->getInstList().insert(pi, newInst); // Inserts newInst before pi in pb @@ -2611,7 +2610,7 @@ .. code-block:: c++ BasicBlock *pb = ...; - Instruction *newInst = new Instruction(...); + auto *newInst = new Instruction(...); pb->getInstList().push_back(newInst); // Appends newInst to pb @@ -2620,7 +2619,7 @@ .. code-block:: c++ BasicBlock *pb = ...; - Instruction *newInst = new Instruction(..., pb); + auto *newInst = new Instruction(..., pb); which is much cleaner, especially if you are creating long instruction streams. @@ -2635,7 +2634,7 @@ .. code-block:: c++ Instruction *pi = ...; - Instruction *newInst = new Instruction(...); + auto *newInst = new Instruction(...); pi->getParent()->getInstList().insert(pi, newInst); @@ -2651,7 +2650,7 @@ .. code-block:: c++ Instruction* pi = ...; - Instruction* newInst = new Instruction(..., pi); + auto *newInst = new Instruction(..., pi); which is much cleaner, especially if you're creating a lot of instructions and adding them to ``BasicBlock``\ s.