diff options
author | Angel Garcia Gomez <angelgarcia@google.com> | 2015-11-03 16:38:31 +0000 |
---|---|---|
committer | Angel Garcia Gomez <angelgarcia@google.com> | 2015-11-03 16:38:31 +0000 |
commit | 432ff5e205f28f5e8a05ca5a043e05f4a9f20d2c (patch) | |
tree | 27657bd6f016f2a10cef12672fc4e8506c10ff0d /clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp | |
parent | bc78a65adcf6c21443ebd646abb058c545adefb8 (diff) | |
download | bcm5719-llvm-432ff5e205f28f5e8a05ca5a043e05f4a9f20d2c.tar.gz bcm5719-llvm-432ff5e205f28f5e8a05ca5a043e05f4a9f20d2c.zip |
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
Diffstat (limited to 'clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp index 168afade8fc..c09e3278589 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp @@ -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; |