diff options
author | Alexander Kornienko <alexfh@google.com> | 2018-06-11 12:46:48 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2018-06-11 12:46:48 +0000 |
commit | 1e047364636890f52a1c480f5aa79600d7e1acd4 (patch) | |
tree | 6e0acf2cc144c099a2f1ac68188eedc01232d734 /clang-tidy/performance | |
parent | 00eda17f4b7899a8da6957d402e7a74bfe595774 (diff) |
Add support for arrays in performance-implicit-conversion-in-loop
Summary:
Add support for arrays (and structure that use naked pointers for their iterator, like std::array) in performance-implicit-conversion-in-loop
Reviewers: alexfh
Reviewed By: alexfh
Subscribers: cfe-commits
Patch by Alex Pilkiewicz.
Differential Revision: https://reviews.llvm.org/D47945
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@334400 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/performance')
-rw-r--r-- | clang-tidy/performance/ImplicitConversionInLoopCheck.cpp | 22 | ||||
-rw-r--r-- | clang-tidy/performance/ImplicitConversionInLoopCheck.h | 2 |
2 files changed, 15 insertions, 9 deletions
diff --git a/clang-tidy/performance/ImplicitConversionInLoopCheck.cpp b/clang-tidy/performance/ImplicitConversionInLoopCheck.cpp index 2acbca34..1a5605f5 100644 --- a/clang-tidy/performance/ImplicitConversionInLoopCheck.cpp +++ b/clang-tidy/performance/ImplicitConversionInLoopCheck.cpp @@ -28,7 +28,7 @@ namespace performance { static bool IsNonTrivialImplicitCast(const Stmt *ST) { if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) { return (ICE->getCastKind() != CK_NoOp) || - IsNonTrivialImplicitCast(ICE->getSubExpr()); + IsNonTrivialImplicitCast(ICE->getSubExpr()); } return false; } @@ -39,7 +39,9 @@ void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) { // conversion. The check on the implicit conversion is done in check() because // we can't access implicit conversion subnode via matchers: has() skips casts // and materialize! We also bind on the call to operator* to get the proper - // type in the diagnostic message. + // type in the diagnostic message. We use both cxxOperatorCallExpr for user + // defined operator and unaryOperator when the iterator is a pointer, like + // for arrays or std::array. // // Note that when the implicit conversion is done through a user defined // conversion operator, the node is a CXXMemberCallExpr, not a @@ -47,10 +49,14 @@ void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) { // cxxOperatorCallExpr() matcher. Finder->addMatcher( cxxForRangeStmt(hasLoopVariable( - varDecl(hasType(qualType(references(qualType(isConstQualified())))), - hasInitializer(expr(hasDescendant(cxxOperatorCallExpr().bind( - "operator-call"))) - .bind("init"))) + varDecl( + hasType(qualType(references(qualType(isConstQualified())))), + hasInitializer( + expr(anyOf(hasDescendant( + cxxOperatorCallExpr().bind("operator-call")), + hasDescendant(unaryOperator(hasOperatorName("*")) + .bind("operator-call")))) + .bind("init"))) .bind("faulty-var"))), this); } @@ -60,7 +66,7 @@ void ImplicitConversionInLoopCheck::check( const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var"); const auto *Init = Result.Nodes.getNodeAs<Expr>("init"); const auto *OperatorCall = - Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call"); + Result.Nodes.getNodeAs<Expr>("operator-call"); if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init)) Init = Cleanup->getSubExpr(); @@ -79,7 +85,7 @@ void ImplicitConversionInLoopCheck::check( void ImplicitConversionInLoopCheck::ReportAndFix( const ASTContext *Context, const VarDecl *VD, - const CXXOperatorCallExpr *OperatorCall) { + const Expr *OperatorCall) { // We only match on const ref, so we should print a const ref version of the // type. QualType ConstType = OperatorCall->getType().withConst(); diff --git a/clang-tidy/performance/ImplicitConversionInLoopCheck.h b/clang-tidy/performance/ImplicitConversionInLoopCheck.h index 8a459ab0..55cb84c3 100644 --- a/clang-tidy/performance/ImplicitConversionInLoopCheck.h +++ b/clang-tidy/performance/ImplicitConversionInLoopCheck.h @@ -28,7 +28,7 @@ public: private: void ReportAndFix(const ASTContext *Context, const VarDecl *VD, - const CXXOperatorCallExpr *OperatorCall); + const Expr *OperatorCall); }; } // namespace performance |