Skip to content

Commit

Permalink
Handle correctly containers that are data members in modernize-loop-c…
Browse files Browse the repository at this point in the history
…onvert.

Summary:
I recently found that the variable naming wasn't working as expected with containers that are data members. The new index always received the name "Elem" (or equivalent) regardless of the container's name.
The check was assuming that the container's declaration was a VarDecl, which cannot be converted to a FieldDecl (a data member), and then it could never retrieve its name.

This also fixes some cases where the check failed to find the container at all (so it didn't do any fix) because of the same reason.

Reviewers: klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 251943
  • Loading branch information
angargo committed Nov 3, 2015
1 parent bc78a65 commit 432ff5e
Showing 7 changed files with 67 additions and 36 deletions.
13 changes: 8 additions & 5 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
Original file line number Diff line number Diff line change
@@ -353,10 +353,12 @@ static StringRef getStringFromRange(SourceManager &SourceMgr,
}

/// \brief If the given expression is actually a DeclRefExpr, find and return
/// the underlying VarDecl; otherwise, return NULL.
static const VarDecl *getReferencedVariable(const Expr *E) {
/// the underlying ValueDecl; otherwise, return NULL.
static const ValueDecl *getReferencedVariable(const Expr *E) {
if (const DeclRefExpr *DRE = getDeclRef(E))
return dyn_cast<VarDecl>(DRE->getDecl());
if (const auto *Mem = dyn_cast<MemberExpr>(E))
return dyn_cast<FieldDecl>(Mem->getMemberDecl());
return nullptr;
}

@@ -500,9 +502,10 @@ void LoopConvertCheck::getAliasRange(SourceManager &SM, SourceRange &Range) {
/// \brief Computes the changes needed to convert a given for loop, and
/// applies them.
void LoopConvertCheck::doConversion(
ASTContext *Context, const VarDecl *IndexVar, const VarDecl *MaybeContainer,
const UsageResult &Usages, const DeclStmt *AliasDecl, bool AliasUseRequired,
bool AliasFromForInit, const ForStmt *Loop, RangeDescriptor Descriptor) {
ASTContext *Context, const VarDecl *IndexVar,
const ValueDecl *MaybeContainer, const UsageResult &Usages,
const DeclStmt *AliasDecl, bool AliasUseRequired, bool AliasFromForInit,
const ForStmt *Loop, RangeDescriptor Descriptor) {
auto Diag = diag(Loop->getForLoc(), "use range-based for loop instead");

std::string VarName;
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.h
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ class LoopConvertCheck : public ClangTidyCheck {
void getAliasRange(SourceManager &SM, SourceRange &DeclRange);

void doConversion(ASTContext *Context, const VarDecl *IndexVar,
const VarDecl *MaybeContainer, const UsageResult &Usages,
const ValueDecl *MaybeContainer, const UsageResult &Usages,
const DeclStmt *AliasDecl, bool AliasUseRequired,
bool AliasFromForInit, const ForStmt *Loop,
RangeDescriptor Descriptor);
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h
Original file line number Diff line number Diff line change
@@ -425,7 +425,7 @@ class VariableNamer {
VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls,
const StmtParentMap *ReverseAST, const clang::Stmt *SourceStmt,
const clang::VarDecl *OldIndex,
const clang::VarDecl *TheContainer,
const clang::ValueDecl *TheContainer,
const clang::ASTContext *Context, NamingStyle Style)
: GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST),
SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer),
@@ -443,7 +443,7 @@ class VariableNamer {
const StmtParentMap *ReverseAST;
const clang::Stmt *SourceStmt;
const clang::VarDecl *OldIndex;
const clang::VarDecl *TheContainer;
const clang::ValueDecl *TheContainer;
const clang::ASTContext *Context;
const NamingStyle Style;

35 changes: 35 additions & 0 deletions clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp
Original file line number Diff line number Diff line change
@@ -168,6 +168,41 @@ struct HasArr {
}
};

struct HasIndirectArr {
HasArr HA;
void implicitThis() {
for (int I = 0; I < N; ++I) {
printf("%d", HA.Arr[I]);
}
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead
// CHECK-FIXES: for (int Elem : HA.Arr)
// CHECK-FIXES-NEXT: printf("%d", Elem);

for (int I = 0; I < N; ++I) {
printf("%d", HA.ValArr[I].X);
}
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead
// CHECK-FIXES: for (auto & Elem : HA.ValArr)
// CHECK-FIXES-NEXT: printf("%d", Elem.X);
}

void explicitThis() {
for (int I = 0; I < N; ++I) {
printf("%d", this->HA.Arr[I]);
}
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead
// CHECK-FIXES: for (int Elem : this->HA.Arr)
// CHECK-FIXES-NEXT: printf("%d", Elem);

for (int I = 0; I < N; ++I) {
printf("%d", this->HA.ValArr[I].X);
}
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead
// CHECK-FIXES: for (auto & Elem : this->HA.ValArr)
// CHECK-FIXES-NEXT: printf("%d", Elem.X);
}
};

// Loops whose bounds are value-dependent should not be converted.
template <int N>
void dependentExprBound() {
Original file line number Diff line number Diff line change
@@ -341,7 +341,7 @@ class TestInsideConstFunction {
copyArg(Ints[I]);
}
// CHECK-MESSAGES: :[[@LINE-4]]:5: warning: use range-based for loop
// CHECK-FIXES: for (int Elem : Ints)
// CHECK-FIXES: for (int Int : Ints)

for (int I = 0; I < N; ++I) {
Array[I].constMember(0);
20 changes: 20 additions & 0 deletions clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp
Original file line number Diff line number Diff line change
@@ -237,6 +237,26 @@ void refs_and_vals() {
}
}

struct MemberNaming {
const static int N = 10;
int Ints[N], Ints_[N];
void loops() {
for (int I = 0; I < N; ++I) {
printf("%d\n", Ints[I]);
}
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead
// CHECK-FIXES: for (int Int : Ints)
// CHECK-FIXES-NEXT: printf("%d\n", Int);

for (int I = 0; I < N; ++I) {
printf("%d\n", Ints_[I]);
}
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: use range-based for loop instead
// CHECK-FIXES: for (int Int : Ints_)
// CHECK-FIXES-NEXT: printf("%d\n", Int);
}
};

} // namespace NamingAlias

namespace NamingConlict {
Original file line number Diff line number Diff line change
@@ -92,33 +92,6 @@ void multipleArrays() {
}
}

struct HasArr {
int Arr[N];
Val ValArr[N];
};

struct HasIndirectArr {
HasArr HA;
void implicitThis() {
for (int I = 0; I < N; ++I) {
printf("%d", HA.Arr[I]);
}

for (int I = 0; I < N; ++I) {
printf("%d", HA.ValArr[I].X);
}
}

void explicitThis() {
for (int I = 0; I < N; ++I) {
printf("%d", this->HA.Arr[I]);
}

for (int I = 0; I < N; ++I) {
printf("%d", this->HA.ValArr[I].X);
}
}
};
}

namespace NegativeIterator {

0 comments on commit 432ff5e

Please sign in to comment.