[PATCH] D14437: Avoid naming conflicts with the old index in modernize-loop-convert.

Angel Garcia via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 05:45:06 PST 2015


angelgarcia created this revision.
angelgarcia added a reviewer: klimek.
angelgarcia added subscribers: alexfh, cfe-commits.

The old index declaration is going to be removed anyway, so we can reuse its name if it is the best candidate for the new index.

http://reviews.llvm.org/D14437

Files:
  clang-tidy/modernize/LoopConvertUtils.cpp
  test/clang-tidy/modernize-loop-convert-extra.cpp

Index: test/clang-tidy/modernize-loop-convert-extra.cpp
===================================================================
--- test/clang-tidy/modernize-loop-convert-extra.cpp
+++ test/clang-tidy/modernize-loop-convert-extra.cpp
@@ -325,6 +325,23 @@
   // CHECK-FIXES-NEXT: (void)NumsI;
 }
 
+void oldIndexConflict() {
+  for (int Num = 0; Num < N; ++Num) {
+    printf("Num: %d\n", Nums[Num]);
+  }
+  // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead
+  // CHECK-FIXES: for (int Num : Nums)
+  // CHECK-FIXES-NEXT: printf("Num: %d\n", Num);
+
+  S Things;
+  for (S::iterator Thing = Things.begin(), End = Things.end(); Thing != End; ++Thing) {
+    printf("Thing: %d %d\n", Thing->X, (*Thing).X);
+  }
+  // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead
+  // CHECK-FIXES: for (auto & Thing : Things)
+  // CHECK-FIXES-NEXT: printf("Thing: %d %d\n", Thing.X, Thing.X);
+}
+
 void macroConflict() {
   S MAXs;
   for (S::iterator It = MAXs.begin(), E = MAXs.end(); It != E; ++It) {
Index: clang-tidy/modernize/LoopConvertUtils.cpp
===================================================================
--- clang-tidy/modernize/LoopConvertUtils.cpp
+++ clang-tidy/modernize/LoopConvertUtils.cpp
@@ -820,14 +820,14 @@
   if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) {
     IteratorName = ContainerName.substr(0, Len - 1);
     // E.g.: (auto thing : things)
-    if (!declarationExists(IteratorName))
+    if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
       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))
+    if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
       return IteratorName;
   }
 
@@ -849,12 +849,12 @@
 
   IteratorName = AppendWithStyle(ContainerName, OldIndex->getName());
   // E.g.: (auto container_i : container)
-  if (!declarationExists(IteratorName))
+  if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
     return IteratorName;
 
   IteratorName = AppendWithStyle(ContainerName, Elem);
   // E.g.: (auto container_elem : container)
-  if (!declarationExists(IteratorName))
+  if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
     return IteratorName;
 
   // Someone defeated my naming scheme...
@@ -875,7 +875,8 @@
   int Attempt = 0;
   do {
     IteratorName = GiveMeName + std::to_string(Attempt++);
-  } while (declarationExists(IteratorName));
+  } while (declarationExists(IteratorName) ||
+           IteratorName == OldIndex->getName());
 
   return IteratorName;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14437.39516.patch
Type: text/x-patch
Size: 2793 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151106/2f92c3fd/attachment.bin>


More information about the cfe-commits mailing list