Handle trailing underscores on modernize-loop-convert variable namer. Summary: https://llvm.org/bugs/show_bug.cgi?id=24961. Reviewers: klimek Subscribers: cfe-commits, alexfh Differential Revision: http://reviews.llvm.org/D13381 llvm-svn: 249127 GitOrigin-RevId: 88d204442be24dd691ad95078e86565cb0321831
diff --git a/clang-tidy/modernize/LoopConvertUtils.cpp b/clang-tidy/modernize/LoopConvertUtils.cpp index 70af93d..5592657 100644 --- a/clang-tidy/modernize/LoopConvertUtils.cpp +++ b/clang-tidy/modernize/LoopConvertUtils.cpp
@@ -819,6 +819,14 @@ size_t Len = ContainerName.size(); if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) { IteratorName = ContainerName.substr(0, Len - 1); + // E.g.: (auto thing : things) + if (!declarationExists(IteratorName)) + return IteratorName; + } + + if (Len > 2 && ContainerName.endswith(Style == NS_UpperCase ? "S_" : "s_")) { + IteratorName = ContainerName.substr(0, Len - 2); + // E.g.: (auto thing : things_) if (!declarationExists(IteratorName)) return IteratorName; } @@ -835,14 +843,17 @@ case NS_UpperCase: Elem = "ELEM"; } + // E.g.: (auto elem : container) if (!declarationExists(Elem)) return Elem; IteratorName = AppendWithStyle(ContainerName, OldIndex->getName()); + // E.g.: (auto container_i : container) if (!declarationExists(IteratorName)) return IteratorName; IteratorName = AppendWithStyle(ContainerName, Elem); + // E.g.: (auto container_elem : container) if (!declarationExists(IteratorName)) return IteratorName;
diff --git a/test/clang-tidy/modernize-loop-convert-lowercase.cpp b/test/clang-tidy/modernize-loop-convert-lowercase.cpp index 0b3d735..d1ca71a 100644 --- a/test/clang-tidy/modernize-loop-convert-lowercase.cpp +++ b/test/clang-tidy/modernize-loop-convert-lowercase.cpp
@@ -7,6 +7,7 @@ const int n = 10; int arr[n]; int nums[n]; +int nums_[n]; void naming() { for (int i = 0; i < n; ++i) { @@ -23,6 +24,13 @@ // CHECK-FIXES: for (auto & num : nums) // CHECK-FIXES-NEXT: printf("%d\n", num); + for (int i = 0; i < n; ++i) { + printf("%d\n", nums_[i]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & num : nums_) + // CHECK-FIXES-NEXT: printf("%d\n", num); + int num = 0; for (int i = 0; i < n; ++i) { printf("%d\n", nums[i] + num);
diff --git a/test/clang-tidy/modernize-loop-convert-uppercase.cpp b/test/clang-tidy/modernize-loop-convert-uppercase.cpp index c58ae16..1f172a4 100644 --- a/test/clang-tidy/modernize-loop-convert-uppercase.cpp +++ b/test/clang-tidy/modernize-loop-convert-uppercase.cpp
@@ -7,6 +7,7 @@ const int N = 10; int ARR[N]; int NUMS[N]; +int NUMS_[N]; void naming() { for (int I = 0; I < N; ++I) { @@ -23,6 +24,13 @@ // CHECK-FIXES: for (auto & NUM : NUMS) // CHECK-FIXES-NEXT: printf("%d\n", NUM); + for (int I = 0; I < N; ++I) { + printf("%d\n", NUMS_[I]); + } + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for (auto & NUM : NUMS_) + // CHECK-FIXES-NEXT: printf("%d\n", NUM); + int NUM = 0; for (int I = 0; I < N; ++I) { printf("%d\n", NUMS[I] + NUM);