[PATCH] D37264: [Docs] Update CodingStandards to recommend range-based for loops

David Majnemer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 29 09:46:56 PDT 2017


majnemer added inline comments.


================
Comment at: docs/CodingStandards.rst:944
 
-  for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
-    if (BinaryOperator *BO = dyn_cast<BinaryOperator>(II)) {
+  for (auto &I : BB) {
+    if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
----------------
I'd replace the `auto` with `Instruction &`.


================
Comment at: docs/CodingStandards.rst:964
 
-  for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
-    BinaryOperator *BO = dyn_cast<BinaryOperator>(II);
+  for (auto &I : BB) {
+    auto *BO = dyn_cast<BinaryOperator>(&I);
----------------
Ditto.


================
Comment at: docs/CodingStandards.rst:1338
 
-The problem with this construct is that it evaluates "``BB->end()``" every time
-through the loop.  Instead of writing the loop like this, we strongly prefer
-loops to be written so that they evaluate it once before the loop starts.  A
-convenient way to do this is like so:
+In cases where it is necessary to write an explicit iterator-based loop,
+ensure that `end()` is evaluated only once as long as the container is not
----------------
You removed text which mentioned the most common case was mutation while iteration, I think we should keep at least part of that exposition somewhere.


https://reviews.llvm.org/D37264





More information about the llvm-commits mailing list