diff options
author | Alexander Kornienko <alexfh@google.com> | 2017-11-27 13:06:28 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2017-11-27 13:06:28 +0000 |
commit | 6e39e68983045fe48fb955f24afe376b46eb3600 (patch) | |
tree | 96689a4d0c1d2385d6e9f4700744748713a0c860 /clang-tools-extra/clang-tidy/misc | |
parent | 2d4f1427267ddfae8fcab9d7764563b0d2b959cf (diff) | |
download | bcm5719-llvm-6e39e68983045fe48fb955f24afe376b46eb3600.tar.gz bcm5719-llvm-6e39e68983045fe48fb955f24afe376b46eb3600.zip |
[clang-tidy] Move checks from misc- to performance-
Summary:
rename_check.py misc-move-constructor-init performance-move-constructor-init
rename_check.py misc-inefficient-algorithm performance-inefficient-algorithm
Reviewers: hokein, aaron.ballman
Reviewed By: hokein, aaron.ballman
Subscribers: aaron.ballman, mgorny, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D40487
llvm-svn: 319023
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc')
6 files changed, 0 insertions, 361 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt index 8a4dcbd5638..8e7391ea851 100644 --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -7,13 +7,11 @@ add_clang_library(clangTidyMiscModule UnconventionalAssignOperatorCheck.cpp DefinitionsInHeadersCheck.cpp IncorrectRoundings.cpp - InefficientAlgorithmCheck.cpp MacroParenthesesCheck.cpp MacroRepeatedSideEffectsCheck.cpp MiscTidyModule.cpp MisplacedWideningCastCheck.cpp MoveConstantArgumentCheck.cpp - MoveConstructorInitCheck.cpp NewDeleteOverloadsCheck.cpp NoexceptMoveConstructorCheck.cpp NonCopyableObjects.cpp diff --git a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp b/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp deleted file mode 100644 index a5a5b2b4ff8..00000000000 --- a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp +++ /dev/null @@ -1,163 +0,0 @@ -//===--- InefficientAlgorithmCheck.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 "InefficientAlgorithmCheck.h" -#include "clang/AST/ASTContext.h" -#include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/Lex/Lexer.h" - -using namespace clang::ast_matchers; - -namespace clang { -namespace tidy { -namespace misc { - -static bool areTypesCompatible(QualType Left, QualType Right) { - if (const auto *LeftRefType = Left->getAs<ReferenceType>()) - Left = LeftRefType->getPointeeType(); - if (const auto *RightRefType = Right->getAs<ReferenceType>()) - Right = RightRefType->getPointeeType(); - return Left->getCanonicalTypeUnqualified() == - Right->getCanonicalTypeUnqualified(); -} - -void InefficientAlgorithmCheck::registerMatchers(MatchFinder *Finder) { - // Only register the matchers for C++; the functionality currently does not - // provide any benefit to other languages, despite being benign. - if (!getLangOpts().CPlusPlus) - return; - - const auto Algorithms = - hasAnyName("::std::find", "::std::count", "::std::equal_range", - "::std::lower_bound", "::std::upper_bound"); - const auto ContainerMatcher = classTemplateSpecializationDecl(hasAnyName( - "::std::set", "::std::map", "::std::multiset", "::std::multimap", - "::std::unordered_set", "::std::unordered_map", - "::std::unordered_multiset", "::std::unordered_multimap")); - - const auto Matcher = - callExpr( - callee(functionDecl(Algorithms)), - hasArgument( - 0, cxxConstructExpr(has(ignoringParenImpCasts(cxxMemberCallExpr( - callee(cxxMethodDecl(hasName("begin"))), - on(declRefExpr( - hasDeclaration(decl().bind("IneffContObj")), - anyOf(hasType(ContainerMatcher.bind("IneffCont")), - hasType(pointsTo( - ContainerMatcher.bind("IneffContPtr"))))) - .bind("IneffContExpr"))))))), - hasArgument( - 1, cxxConstructExpr(has(ignoringParenImpCasts(cxxMemberCallExpr( - callee(cxxMethodDecl(hasName("end"))), - on(declRefExpr( - hasDeclaration(equalsBoundNode("IneffContObj"))))))))), - hasArgument(2, expr().bind("AlgParam")), - unless(isInTemplateInstantiation())) - .bind("IneffAlg"); - - Finder->addMatcher(Matcher, this); -} - -void InefficientAlgorithmCheck::check(const MatchFinder::MatchResult &Result) { - const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("IneffAlg"); - const auto *IneffCont = - Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("IneffCont"); - bool PtrToContainer = false; - if (!IneffCont) { - IneffCont = - Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("IneffContPtr"); - PtrToContainer = true; - } - const llvm::StringRef IneffContName = IneffCont->getName(); - const bool Unordered = - IneffContName.find("unordered") != llvm::StringRef::npos; - const bool Maplike = IneffContName.find("map") != llvm::StringRef::npos; - - // Store if the key type of the container is compatible with the value - // that is searched for. - QualType ValueType = AlgCall->getArg(2)->getType(); - QualType KeyType = - IneffCont->getTemplateArgs()[0].getAsType().getCanonicalType(); - const bool CompatibleTypes = areTypesCompatible(KeyType, ValueType); - - // Check if the comparison type for the algorithm and the container matches. - if (AlgCall->getNumArgs() == 4 && !Unordered) { - const Expr *Arg = AlgCall->getArg(3); - const QualType AlgCmp = - Arg->getType().getUnqualifiedType().getCanonicalType(); - const unsigned CmpPosition = - (IneffContName.find("map") == llvm::StringRef::npos) ? 1 : 2; - const QualType ContainerCmp = IneffCont->getTemplateArgs()[CmpPosition] - .getAsType() - .getUnqualifiedType() - .getCanonicalType(); - if (AlgCmp != ContainerCmp) { - diag(Arg->getLocStart(), - "different comparers used in the algorithm and the container"); - return; - } - } - - const auto *AlgDecl = AlgCall->getDirectCallee(); - if (!AlgDecl) - return; - - if (Unordered && AlgDecl->getName().find("bound") != llvm::StringRef::npos) - return; - - const auto *AlgParam = Result.Nodes.getNodeAs<Expr>("AlgParam"); - const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>("IneffContExpr"); - FixItHint Hint; - - SourceManager &SM = *Result.SourceManager; - LangOptions LangOpts = getLangOpts(); - - CharSourceRange CallRange = - CharSourceRange::getTokenRange(AlgCall->getSourceRange()); - - // FIXME: Create a common utility to extract a file range that the given token - // sequence is exactly spelled at (without macro argument expansions etc.). - // We can't use Lexer::makeFileCharRange here, because for - // - // #define F(x) x - // x(a b c); - // - // it will return "x(a b c)", when given the range "a"-"c". It makes sense for - // removals, but not for replacements. - // - // This code is over-simplified, but works for many real cases. - if (SM.isMacroArgExpansion(CallRange.getBegin()) && - SM.isMacroArgExpansion(CallRange.getEnd())) { - CallRange.setBegin(SM.getSpellingLoc(CallRange.getBegin())); - CallRange.setEnd(SM.getSpellingLoc(CallRange.getEnd())); - } - - if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) { - StringRef ContainerText = Lexer::getSourceText( - CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM, - LangOpts); - StringRef ParamText = Lexer::getSourceText( - CharSourceRange::getTokenRange(AlgParam->getSourceRange()), SM, - LangOpts); - std::string ReplacementText = - (llvm::Twine(ContainerText) + (PtrToContainer ? "->" : ".") + - AlgDecl->getName() + "(" + ParamText + ")") - .str(); - Hint = FixItHint::CreateReplacement(CallRange, ReplacementText); - } - - diag(AlgCall->getLocStart(), - "this STL algorithm call should be replaced with a container method") - << Hint; -} - -} // namespace misc -} // namespace tidy -} // namespace clang diff --git a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.h b/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.h deleted file mode 100644 index 6935b455ed6..00000000000 --- a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.h +++ /dev/null @@ -1,36 +0,0 @@ -//===--- InefficientAlgorithmCheck.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_MISC_INEFFICIENTALGORITHMCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INEFFICIENTALGORITHMCHECK_H - -#include "../ClangTidy.h" - -namespace clang { -namespace tidy { -namespace misc { - -/// Warns on inefficient use of STL algorithms on associative containers. -/// -/// Associative containers implements some of the algorithms as methods which -/// should be preferred to the algorithms in the algorithm header. The methods -/// can take advanatage of the order of the elements. -class InefficientAlgorithmCheck : public ClangTidyCheck { -public: - InefficientAlgorithmCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} - void registerMatchers(ast_matchers::MatchFinder *Finder) override; - void check(const ast_matchers::MatchFinder::MatchResult &Result) override; -}; - -} // namespace misc -} // namespace tidy -} // namespace clang - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INEFFICIENTALGORITHMCHECK_H diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp index 1dddd4e2832..b38feca72cd 100644 --- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -13,14 +13,12 @@ #include "DefinitionsInHeadersCheck.h" #include "ForwardingReferenceOverloadCheck.h" #include "IncorrectRoundings.h" -#include "InefficientAlgorithmCheck.h" #include "LambdaFunctionNameCheck.h" #include "MacroParenthesesCheck.h" #include "MacroRepeatedSideEffectsCheck.h" #include "MisplacedConstCheck.h" #include "MisplacedWideningCastCheck.h" #include "MoveConstantArgumentCheck.h" -#include "MoveConstructorInitCheck.h" #include "NewDeleteOverloadsCheck.h" #include "NoexceptMoveConstructorCheck.h" #include "NonCopyableObjects.h" @@ -63,8 +61,6 @@ public: "misc-definitions-in-headers"); CheckFactories.registerCheck<IncorrectRoundings>( "misc-incorrect-roundings"); - CheckFactories.registerCheck<InefficientAlgorithmCheck>( - "misc-inefficient-algorithm"); CheckFactories.registerCheck<MacroParenthesesCheck>( "misc-macro-parentheses"); CheckFactories.registerCheck<MacroRepeatedSideEffectsCheck>( @@ -73,8 +69,6 @@ public: "misc-misplaced-widening-cast"); CheckFactories.registerCheck<MoveConstantArgumentCheck>( "misc-move-const-arg"); - CheckFactories.registerCheck<MoveConstructorInitCheck>( - "misc-move-constructor-init"); CheckFactories.registerCheck<NewDeleteOverloadsCheck>( "misc-new-delete-overloads"); CheckFactories.registerCheck<NoexceptMoveConstructorCheck>( diff --git a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp b/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp deleted file mode 100644 index 2c6af5f854c..00000000000 --- a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp +++ /dev/null @@ -1,110 +0,0 @@ -//===--- MoveConstructorInitCheck.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 "MoveConstructorInitCheck.h" -#include "../utils/Matchers.h" -#include "clang/AST/ASTContext.h" -#include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Lex/Lexer.h" -#include "clang/Lex/Preprocessor.h" - -using namespace clang::ast_matchers; - -namespace clang { -namespace tidy { -namespace misc { - -MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name, - ClangTidyContext *Context) - : ClangTidyCheck(Name, Context), - IncludeStyle(utils::IncludeSorter::parseIncludeStyle( - Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {} - -void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) { - // Only register the matchers for C++11; the functionality currently does not - // provide any benefit to other languages, despite being benign. - if (!getLangOpts().CPlusPlus11) - return; - - Finder->addMatcher( - cxxConstructorDecl( - unless(isImplicit()), - allOf(isMoveConstructor(), - hasAnyConstructorInitializer( - cxxCtorInitializer( - withInitializer(cxxConstructExpr(hasDeclaration( - cxxConstructorDecl(isCopyConstructor()) - .bind("ctor"))))) - .bind("move-init")))), - this); -} - -void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { - const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor"); - const auto *Initializer = - Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init"); - - // Do not diagnose if the expression used to perform the initialization is a - // trivially-copyable type. - QualType QT = Initializer->getInit()->getType(); - if (QT.isTriviallyCopyableType(*Result.Context)) - return; - - if (QT.isConstQualified()) - return; - - const auto *RD = QT->getAsCXXRecordDecl(); - if (RD && RD->isTriviallyCopyable()) - return; - - // Diagnose when the class type has a move constructor available, but the - // ctor-initializer uses the copy constructor instead. - const CXXConstructorDecl *Candidate = nullptr; - for (const auto *Ctor : CopyCtor->getParent()->ctors()) { - if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected && - !Ctor->isDeleted()) { - // The type has a move constructor that is at least accessible to the - // initializer. - // - // FIXME: Determine whether the move constructor is a viable candidate - // for the ctor-initializer, perhaps provide a fixit that suggests - // using std::move(). - Candidate = Ctor; - break; - } - } - - if (Candidate) { - // There's a move constructor candidate that the caller probably intended - // to call instead. - diag(Initializer->getSourceLocation(), - "move constructor initializes %0 by calling a copy constructor") - << (Initializer->isBaseInitializer() ? "base class" : "class member"); - diag(CopyCtor->getLocation(), "copy constructor being called", - DiagnosticIDs::Note); - diag(Candidate->getLocation(), "candidate move constructor here", - DiagnosticIDs::Note); - } -} - -void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) { - Inserter.reset(new utils::IncludeInserter( - Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle)); - Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); -} - -void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { - Options.store(Opts, "IncludeStyle", - utils::IncludeSorter::toString(IncludeStyle)); -} - -} // namespace misc -} // namespace tidy -} // namespace clang diff --git a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h b/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h deleted file mode 100644 index adfba029cfa..00000000000 --- a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h +++ /dev/null @@ -1,44 +0,0 @@ -//===--- MoveConstructorInitCheck.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_MISC_MOVECONSTRUCTORINITCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MOVECONSTRUCTORINITCHECK_H - -#include "../ClangTidy.h" -#include "../utils/IncludeInserter.h" - -#include <memory> - -namespace clang { -namespace tidy { -namespace misc { - -/// The check flags user-defined move constructors that have a ctor-initializer -/// initializing a member or base class through a copy constructor instead of a -/// move constructor. -/// For the user-facing documentation see: -/// http://clang.llvm.org/extra/clang-tidy/checks/misc-move-constructor-init.html -class MoveConstructorInitCheck : public ClangTidyCheck { -public: - MoveConstructorInitCheck(StringRef Name, ClangTidyContext *Context); - void registerMatchers(ast_matchers::MatchFinder *Finder) override; - void check(const ast_matchers::MatchFinder::MatchResult &Result) override; - void registerPPCallbacks(clang::CompilerInstance &Compiler) override; - void storeOptions(ClangTidyOptions::OptionMap &Opts) override; - -private: - std::unique_ptr<utils::IncludeInserter> Inserter; - const utils::IncludeSorter::IncludeStyle IncludeStyle; -}; - -} // namespace misc -} // namespace tidy -} // namespace clang - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MOVECONSTRUCTORINITCHECK_H |