[PATCH] Use 'auto const&' for iterators whose deref operator returns a const var

Edwin Vane edwin.vane at intel.com
Mon Apr 8 14:24:25 PDT 2013


  This code makes the assumption that dereferencing the iterator returned by container.begin() produces a type that's convertible to a const version of the type returned by dereferencing the iterator type of the loop variable. Remember in our discussions there were three cases:

  # The type returned by dereferencing the iterator type of the variable declaration and the type returned by dereferencing the iterator type of the loop variable's initializer are **identical**.
  # As above except we allow the former type to be const.
  # The types otherwise are not the same.

  Case #3 should not be transformed at all. Case #2 is what you're trying to handle by adding `const`. Case #1 is ok to transform.


================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1008
@@ +1007,3 @@
+      // returns a const reference or not.
+      const QualType *IteratorType =
+          Nodes.getNodeAs<QualType>(DerefByRefResultName);
----------------
This would be more accurately described as "DerefType" not iterator type.

================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1012
@@ +1011,3 @@
+        const ReferenceType *IteratorReferenceType =
+            IteratorType->getTypePtr()->getAs<ReferenceType>();
+        IteratorPointsToConst =
----------------
QualType's operator->() provides Type. Just say (*IteratorType)->getAs...

================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1013
@@ +1012,3 @@
+            IteratorType->getTypePtr()->getAs<ReferenceType>();
+        IteratorPointsToConst =
+            IteratorReferenceType->getPointeeType().isConstQualified();
----------------
Given the context perhaps a better name for this variable is then DerefByConstRef.

================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1019
@@ +1018,3 @@
+        QualType PointerType = InitVar->getType();
+        assert(PointerType.getTypePtr()->isPointerType() &&
+               "Non-class iterator type is not a pointer type.");
----------------
Another case of just using QualType::operator->().

================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1017
@@ +1016,3 @@
+        // For non-class iterator types, check if the pointee type is const or
+        // not.
+        QualType PointerType = InitVar->getType();
----------------
We should talk about why this else case gets triggered. Should include the fact that the matcher won't tag an expression with either DerefByValue or DerefByRef if the iterator is a plain pointer.

================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1018
@@ +1017,3 @@
+        // not.
+        QualType PointerType = InitVar->getType();
+        assert(PointerType.getTypePtr()->isPointerType() &&
----------------
I'd also call this "InitVarType".

================
Comment at: cpp11-migrate/LoopConvert/LoopActions.cpp:1022
@@ +1021,3 @@
+        QualType CanonicalPointerType = PointerType.getCanonicalType();
+        IteratorPointsToConst = CanonicalPointerType.getTypePtr()
+            ->getPointeeType().isConstQualified();
----------------
Another QualType::operator->() use case.


http://llvm-reviews.chandlerc.com/D641



More information about the cfe-commits mailing list