From 2bfb7cbddbb618c6ce493b292f63c6453d2458c5 Mon Sep 17 00:00:00 2001 From: Angel Garcia Gomez Date: Thu, 1 Oct 2015 08:57:11 +0000 Subject: Add support for 'cbegin()' and 'cend()' on modernize-loop-convert. Summary: This fixes https://llvm.org/bugs/show_bug.cgi?id=22196 . Also add a non-trivially copyable type to fix some tests that were meant to be about using const-refs, but were changed in r248438. Reviewers: klimek Subscribers: alexfh, cfe-commits Differential Revision: http://reviews.llvm.org/D13292 llvm-svn: 248994 --- .../clang-tidy/modernize-loop-convert-basic.cpp | 62 ++++++++++++++-------- 1 file changed, 39 insertions(+), 23 deletions(-) (limited to 'clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp') diff --git a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp index 49acce0721c..42a5d5cdc37 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp @@ -104,6 +104,14 @@ void constArray() { // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead // CHECK-FIXES: for (auto Elem : ConstArr) // CHECK-FIXES-NEXT: printf("2 * %d = %d\n", Elem, Elem + Elem); + + const NonTriviallyCopyable NonCopy[N]{}; + for (int I = 0; I < N; ++I) { + printf("2 * %d = %d\n", NonCopy[I].X, NonCopy[I].X + NonCopy[I].X); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (const auto & Elem : NonCopy) + // CHECK-FIXES-NEXT: printf("2 * %d = %d\n", Elem.X, Elem.X + Elem.X); } struct HasArr { @@ -209,6 +217,13 @@ void f() { // CHECK-FIXES: for (auto & P : *Ps) // CHECK-FIXES-NEXT: printf("s has value %d\n", P.X); + for (S::const_iterator It = Ss.cbegin(), E = Ss.cend(); It != E; ++It) { + printf("s has value %d\n", (*It).X); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto Elem : Ss) + // CHECK-FIXES-NEXT: printf("s has value %d\n", Elem.X); + for (S::iterator It = Ss.begin(), E = Ss.end(); It != E; ++It) { printf("s has value %d\n", It->X); } @@ -459,8 +474,8 @@ namespace PseudoArray { const int N = 6; dependent V; dependent *Pv; -const dependent Constv; -const dependent *Pconstv; +const dependent Constv; +const dependent *Pconstv; transparent> Cv; @@ -516,50 +531,51 @@ void f() { // CHECK-FIXES-NEXT: Sum += Elem + 2; } +// Ensure that 'const auto &' is used with containers of non-trivial types. void constness() { int Sum = 0; for (int I = 0, E = Constv.size(); I < E; ++I) { - printf("Fibonacci number is %d\n", Constv[I]); - Sum += Constv[I] + 2; + printf("Fibonacci number is %d\n", Constv[I].X); + Sum += Constv[I].X + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto Elem : Constv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - // CHECK-FIXES-NEXT: Sum += Elem + 2; + // CHECK-FIXES: for (const auto & Elem : Constv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem.X); + // CHECK-FIXES-NEXT: Sum += Elem.X + 2; for (int I = 0, E = Constv.size(); I < E; ++I) { - printf("Fibonacci number is %d\n", Constv.at(I)); - Sum += Constv.at(I) + 2; + printf("Fibonacci number is %d\n", Constv.at(I).X); + Sum += Constv.at(I).X + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto Elem : Constv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - // CHECK-FIXES-NEXT: Sum += Elem + 2; + // CHECK-FIXES: for (const auto & Elem : Constv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem.X); + // CHECK-FIXES-NEXT: Sum += Elem.X + 2; for (int I = 0, E = Pconstv->size(); I < E; ++I) { - printf("Fibonacci number is %d\n", Pconstv->at(I)); - Sum += Pconstv->at(I) + 2; + printf("Fibonacci number is %d\n", Pconstv->at(I).X); + Sum += Pconstv->at(I).X + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto Elem : *Pconstv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - // CHECK-FIXES-NEXT: Sum += Elem + 2; + // CHECK-FIXES: for (const auto & Elem : *Pconstv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem.X); + // CHECK-FIXES-NEXT: Sum += Elem.X + 2; // This test will fail if size() isn't called repeatedly, since it // returns unsigned int, and 0 is deduced to be signed int. // FIXME: Insert the necessary explicit conversion, or write out the types // explicitly. for (int I = 0; I < Pconstv->size(); ++I) { - printf("Fibonacci number is %d\n", (*Pconstv).at(I)); - Sum += (*Pconstv)[I] + 2; + printf("Fibonacci number is %d\n", (*Pconstv).at(I).X); + Sum += (*Pconstv)[I].X + 2; } // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead - // CHECK-FIXES: for (auto Elem : *Pconstv) - // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem); - // CHECK-FIXES-NEXT: Sum += Elem + 2; + // CHECK-FIXES: for (const auto & Elem : *Pconstv) + // CHECK-FIXES-NEXT: printf("Fibonacci number is %d\n", Elem.X); + // CHECK-FIXES-NEXT: Sum += Elem.X + 2; } -void ConstRef(const dependent& ConstVRef) { +void constRef(const dependent& 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. -- cgit v1.2.3