Author: klimek Date: Wed Sep 23 13:40:47 2015 New Revision: 248418 URL: http://llvm.org/viewvc/llvm-project?rev=248418&view=rev Log: Fix loop-convert for const references to containers.
Previously we would use a non-const loop variable in the range-based loop for: void f(const std::vector<int> &v) { for (size_t i = 0; i < v.size(); ++i) { Now we use const auto&. Note that we'll also want to use a copy at least for simple types. Modified: clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-basic.cpp Modified: clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp?rev=248418&r1=248417&r2=248418&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp Wed Sep 23 13:40:47 2015 @@ -391,6 +391,12 @@ static bool containerIsConst(const Expr return false; CType = CType->getPointeeType(); } + // If VDec is a reference to a container, Dereference is false, + // but we still need to check the const-ness of the underlying container + // type. + if (const auto &RT = CType->getAs<ReferenceType>()) { + CType = RT->getPointeeType(); + } return CType.isConstQualified(); } return false; Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-basic.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-basic.cpp?rev=248418&r1=248417&r2=248418&view=diff ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-basic.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-basic.cpp Wed Sep 23 13:40:47 2015 @@ -544,6 +544,18 @@ void constness() { // CHECK-FIXES-NEXT: sum += elem + 2; } +void ConstRef(const dependent<int>& ConstVRef) { + int sum = 0; + // FIXME: This does not work with size_t (probably due to the implementation + // of dependent); make dependent work exactly like a std container type. + for (int i = 0; i < ConstVRef.size(); ++i) { + sum += ConstVRef[i]; + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (const auto & elem : ConstVRef) + // CHECK-FIXES-NEXT: sum += elem; +} + // Check for loops that don't mention containers. void noContainer() { for (auto i = 0; i < v.size(); ++i) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits