diff options
author | Alexander Kornienko <alexfh@google.com> | 2016-01-29 15:21:32 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2016-01-29 15:21:32 +0000 |
commit | 40d307d12089fb7ee05dd5900cea6362fce89f1f (patch) | |
tree | 76db2abc04733078aea63644c1ff0d624c9628aa /clang-tools-extra/clang-tidy/performance | |
parent | 865a7d8aab2d2ba12a5f2526fc746e580878ebaf (diff) | |
download | bcm5719-llvm-40d307d12089fb7ee05dd5900cea6362fce89f1f.tar.gz bcm5719-llvm-40d307d12089fb7ee05dd5900cea6362fce89f1f.zip |
[clang-tidy] Move implicit-cast-in-loop check to upstream.
Summary: This is implemented originally by Alex Pilkiewicz (pilki@google.com).
Reviewers: alexfh
Subscribers: cfe-commits
Patch by Haojian Wu!
Differential Revision: http://reviews.llvm.org/D16721
llvm-svn: 259195
Diffstat (limited to 'clang-tools-extra/clang-tidy/performance')
4 files changed, 147 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 97225781356..322b0007943 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyPerformanceModule + ImplicitCastInLoopCheck.cpp PerformanceTidyModule.cpp UnnecessaryCopyInitialization.cpp diff --git a/clang-tools-extra/clang-tidy/performance/ImplicitCastInLoopCheck.cpp b/clang-tools-extra/clang-tidy/performance/ImplicitCastInLoopCheck.cpp new file mode 100644 index 00000000000..1752e2d0f10 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/ImplicitCastInLoopCheck.cpp @@ -0,0 +1,106 @@ +//===--- ImplicitCastInLoopCheck.cpp - clang-tidy--------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ImplicitCastInLoopCheck.h" + +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +namespace clang { + +using namespace ast_matchers; + +namespace tidy { +namespace performance { + +namespace { +// Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp. +// The subtelty is that in some cases (user defined conversions), we can +// get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this +// case we skip the first cast expr. +bool IsNonTrivialImplicitCast(const Stmt* ST) { + if (const auto* ICE = dyn_cast<ImplicitCastExpr>(ST)) { + return (ICE->getCastKind() != CK_NoOp) || + IsNonTrivialImplicitCast(ICE->getSubExpr()); + } + return false; +} +} // namespace + +void ImplicitCastInLoopCheck::registerMatchers( + ast_matchers::MatchFinder* Finder) { + // We look for const ref loop variables that (optionally inside an + // ExprWithCleanup) materialize a temporary, and contain a implicit cast. The + // check on the implicit cast is done in check() because we can't access + // implicit cast 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. + // Note that when the implicit cast is done through a user defined cast + // operator, the node is a CXXMemberCallExpr, not a CXXOperatorCallExpr, so + // it should not get caught by the cxxOperatorCallExpr() matcher. + Finder->addMatcher( + cxxForRangeStmt(hasLoopVariable( + varDecl(hasType(qualType(references(qualType(isConstQualified())))), + hasInitializer(expr(hasDescendant(cxxOperatorCallExpr().bind( + "operator-call"))) + .bind("init"))) + .bind("faulty-var"))), + this); +} + +void ImplicitCastInLoopCheck::check( + const ast_matchers::MatchFinder::MatchResult &Result) { + 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"); + + if (const auto* Cleanup = dyn_cast<ExprWithCleanups>(Init)) { + Init = Cleanup->getSubExpr(); + } + const auto* Materialized = dyn_cast<MaterializeTemporaryExpr>(Init); + if (!Materialized) { + return; + } + + // We ignore NoOp casts. Those are generated if the * operator on the + // iterator returns a value instead of a reference, and the loop variable + // is a reference. This situation is fine (it probably produces the same + // code at the end). + if (IsNonTrivialImplicitCast(Materialized->getTemporary())) { + ReportAndFix(Result.Context, VD, OperatorCall); + } +} + +void ImplicitCastInLoopCheck::ReportAndFix( + const ASTContext *Context, const VarDecl *VD, + const CXXOperatorCallExpr *OperatorCall) { + // We only match on const ref, so we should print a const ref version of the + // type. + QualType ConstType = OperatorCall->getType().withConst(); + QualType ConstRefType = Context->getLValueReferenceType(ConstType); + const char Message[] = + "the type of the loop variable '%0' is different from the one returned " + "by the iterator and generates an implicit cast; you can either " + "change the type to the correct one ('%1' but 'const auto&' is always a " + "valid option) or remove the reference to make it explicit that you are " + "creating a new value"; + PrintingPolicy Policy(Context->getLangOpts()); + Policy.SuppressTagKeyword = true; + + diag(VD->getLocStart(), Message) << VD->getName() + << ConstRefType.getAsString(Policy); +} + +} // namespace performance +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/performance/ImplicitCastInLoopCheck.h b/clang-tools-extra/clang-tidy/performance/ImplicitCastInLoopCheck.h new file mode 100644 index 00000000000..5131cf405b1 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/ImplicitCastInLoopCheck.h @@ -0,0 +1,37 @@ +//===--- ImplicitCastInLoopCheck.h - clang-tidy------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_IMPLICIT_CAST_IN_LOOP_CHECK_H_ +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_IMPLICIT_CAST_IN_LOOP_CHECK_H_ + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace performance { + +// Checks that in a for range loop, if the provided type is a reference, then +// the underlying type is the one returned by the iterator (i.e. that there +// isn't any implicit conversion). +class ImplicitCastInLoopCheck : public ClangTidyCheck { + public: + using ClangTidyCheck::ClangTidyCheck; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + + private: + void ReportAndFix(const ASTContext *Context, const VarDecl *VD, + const CXXOperatorCallExpr *OperatorCall); +}; + +} // namespace performance +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_IMPLICIT_CAST_IN_LOOP_CHECK_H_ diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index ab0dbc8012a..977e6a86b64 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "ImplicitCastInLoopCheck.h" #include "UnnecessaryCopyInitialization.h" namespace clang { @@ -20,6 +21,8 @@ namespace performance { class PerformanceModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck<ImplicitCastInLoopCheck>( + "performance-implicit-cast-in-loop"); CheckFactories.registerCheck<UnnecessaryCopyInitialization>( "performance-unnecessary-copy-initialization"); } |