Skip to content

Commit

Permalink
Avoid naming conflicts with the old index in modernize-loop-convert.
Browse files Browse the repository at this point in the history
Summary: 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.

Reviewers: klimek

Subscribers: cfe-commits, alexfh

Differential Revision: http://reviews.llvm.org/D14437

llvm-svn: 252303
  • Loading branch information
angargo committed Nov 6, 2015
1 parent 712229e commit 7e1d4ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 6 additions & 5 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
Expand Up @@ -820,14 +820,14 @@ std::string VariableNamer::createIndexName() {
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;
}

Expand All @@ -849,12 +849,12 @@ std::string VariableNamer::createIndexName() {

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...
Expand All @@ -875,7 +875,8 @@ std::string VariableNamer::createIndexName() {
int Attempt = 0;
do {
IteratorName = GiveMeName + std::to_string(Attempt++);
} while (declarationExists(IteratorName));
} while (declarationExists(IteratorName) ||
IteratorName == OldIndex->getName());

return IteratorName;
}
Expand Down
17 changes: 17 additions & 0 deletions clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp
Expand Up @@ -325,6 +325,23 @@ void sameNames() {
// 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) {
Expand Down

0 comments on commit 7e1d4ae

Please sign in to comment.