mirror of
https://github.com/intel/llvm.git
synced 2026-01-17 14:48:27 +08:00
Handle correctly containers that are data members in modernize-loop-convert.
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
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -37,7 +37,7 @@ private:
|
||||
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);
|
||||
|
||||
@@ -425,7 +425,7 @@ public:
|
||||
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 @@ private:
|
||||
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;
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user