diff options
author | Piotr Zegar <me@piotrzegar.pl> | 2024-03-31 15:06:49 +0000 |
---|---|---|
committer | Piotr Zegar <me@piotrzegar.pl> | 2024-03-31 15:06:49 +0000 |
commit | 11a411a49b62c129bba551df4587dd446fcdc660 (patch) | |
tree | 73fdddefb9f0b8d044f805cb8fc5b4d2654a7e01 /clang-tools-extra/clang-tidy/readability | |
parent | b6f6be4b500ff64c23a5103ac3311cb74519542f (diff) | |
download | llvm-11a411a49b62c129bba551df4587dd446fcdc660.zip llvm-11a411a49b62c129bba551df4587dd446fcdc660.tar.gz llvm-11a411a49b62c129bba551df4587dd446fcdc660.tar.bz2 |
Revert "[clang-tidy][NFC] Remove duplicated code"
This reverts commit b6f6be4b500ff64c23a5103ac3311cb74519542f.
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability')
7 files changed, 92 insertions, 50 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp index 5b1bf01..1284df6 100644 --- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp +++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "ConvertMemberFunctionsToStatic.h" -#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" @@ -19,12 +18,40 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { -namespace { +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } + +AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); } AST_MATCHER(CXXMethodDecl, isOverloadedOperator) { return Node.isOverloadedOperator(); } +AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) { + return Node.hasAnyDependentBases(); +} + +AST_MATCHER(CXXMethodDecl, isTemplate) { + return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate; +} + +AST_MATCHER(CXXMethodDecl, isDependentContext) { + return Node.isDependentContext(); +} + +AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) { + const ASTContext &Ctxt = Finder->getASTContext(); + return clang::Lexer::makeFileCharRange( + clang::CharSourceRange::getCharRange( + Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()), + Ctxt.getSourceManager(), Ctxt.getLangOpts()) + .isInvalid(); +} + +AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, + ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) { + return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder); +} + AST_MATCHER(CXXMethodDecl, usesThis) { class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> { public: @@ -47,27 +74,22 @@ AST_MATCHER(CXXMethodDecl, usesThis) { return UsageOfThis.Used; } -} // namespace - void ConvertMemberFunctionsToStatic::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxMethodDecl( isDefinition(), isUserProvided(), unless(anyOf( - isExpansionInSystemHeader(), isVirtual(), - matchers::isStaticMethod(), matchers::hasTrivialBody(), - isOverloadedOperator(), cxxConstructorDecl(), cxxDestructorDecl(), - cxxConversionDecl(), matchers::isTemplate(), - matchers::isDependentContext(), + isExpansionInSystemHeader(), isVirtual(), isStatic(), + hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(), + cxxDestructorDecl(), cxxConversionDecl(), isTemplate(), + isDependentContext(), ofClass(anyOf( isLambda(), - matchers::hasAnyDependentBases()) // Method might become - // virtual depending on - // template base class. + hasAnyDependentBases()) // Method might become virtual + // depending on template base class. ), - matchers::isInsideMacroDefinition(), - matchers::hasCanonicalDecl(matchers::isInsideMacroDefinition()), - usesThis()))) + isInsideMacroDefinition(), + hasCanonicalDecl(isInsideMacroDefinition()), usesThis()))) .bind("x"), this); } diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp index 261c02e..759cdd4 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -202,9 +202,9 @@ void CognitiveComplexity::account(SourceLocation Loc, unsigned short Nesting, Total += Increase; } -class CognitiveComplexityFunctionASTVisitor final - : public RecursiveASTVisitor<CognitiveComplexityFunctionASTVisitor> { - using Base = RecursiveASTVisitor<CognitiveComplexityFunctionASTVisitor>; +class FunctionASTVisitor final + : public RecursiveASTVisitor<FunctionASTVisitor> { + using Base = RecursiveASTVisitor<FunctionASTVisitor>; // If set to true, macros are ignored during analysis. const bool IgnoreMacros; @@ -219,7 +219,7 @@ class CognitiveComplexityFunctionASTVisitor final std::stack<OBO, SmallVector<OBO, 4>> BinaryOperatorsStack; public: - explicit CognitiveComplexityFunctionASTVisitor(const bool IgnoreMacros) + explicit FunctionASTVisitor(const bool IgnoreMacros) : IgnoreMacros(IgnoreMacros) {} bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) { @@ -453,13 +453,12 @@ public: // The parameter MainAnalyzedFunction is needed to differentiate between the // cases where TraverseDecl() is the entry point from // FunctionCognitiveComplexityCheck::check() and the cases where it was called - // from the CognitiveComplexityFunctionASTVisitor itself. Explanation: if we - // get a function definition (e.g. constructor, destructor, method), the - // Cognitive Complexity specification states that the Nesting level shall be - // increased. But if this function is the entry point, then the Nesting level - // should not be increased. Thus that parameter is there and is used to - // fall-through directly to traversing if this is the main function that is - // being analyzed. + // from the FunctionASTVisitor itself. Explanation: if we get a function + // definition (e.g. constructor, destructor, method), the Cognitive Complexity + // specification states that the Nesting level shall be increased. But if this + // function is the entry point, then the Nesting level should not be + // increased. Thus that parameter is there and is used to fall-through + // directly to traversing if this is the main function that is being analyzed. bool TraverseDecl(Decl *Node, bool MainAnalyzedFunction = false) { if (!Node || MainAnalyzedFunction) return Base::TraverseDecl(Node); @@ -516,7 +515,7 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) { void FunctionCognitiveComplexityCheck::check( const MatchFinder::MatchResult &Result) { - CognitiveComplexityFunctionASTVisitor Visitor(IgnoreMacros); + FunctionASTVisitor Visitor(IgnoreMacros); SourceLocation Loc; const auto *TheDecl = Result.Nodes.getNodeAs<FunctionDecl>("func"); diff --git a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp index c828ef3..d42fcba 100644 --- a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "MakeMemberFunctionConstCheck.h" -#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/AST/ParentMapContext.h" #include "clang/AST/RecursiveASTVisitor.h" @@ -18,6 +17,36 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } + +AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); } + +AST_MATCHER(CXXRecordDecl, hasAnyDependentBases) { + return Node.hasAnyDependentBases(); +} + +AST_MATCHER(CXXMethodDecl, isTemplate) { + return Node.getTemplatedKind() != FunctionDecl::TK_NonTemplate; +} + +AST_MATCHER(CXXMethodDecl, isDependentContext) { + return Node.isDependentContext(); +} + +AST_MATCHER(CXXMethodDecl, isInsideMacroDefinition) { + const ASTContext &Ctxt = Finder->getASTContext(); + return clang::Lexer::makeFileCharRange( + clang::CharSourceRange::getCharRange( + Node.getTypeSourceInfo()->getTypeLoc().getSourceRange()), + Ctxt.getSourceManager(), Ctxt.getLangOpts()) + .isInvalid(); +} + +AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, + ast_matchers::internal::Matcher<CXXMethodDecl>, InnerMatcher) { + return InnerMatcher.matches(*Node.getCanonicalDecl(), Finder, Builder); +} + enum UsageKind { Unused, Const, NonConst }; class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> { @@ -176,7 +205,6 @@ public: } }; -namespace { AST_MATCHER(CXXMethodDecl, usesThisAsConst) { FindUsageOfThis UsageOfThis(Finder->getASTContext()); @@ -186,8 +214,6 @@ AST_MATCHER(CXXMethodDecl, usesThisAsConst) { return UsageOfThis.Usage == Const; } -} // namespace - void MakeMemberFunctionConstCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( traverse( @@ -196,18 +222,15 @@ void MakeMemberFunctionConstCheck::registerMatchers(MatchFinder *Finder) { isDefinition(), isUserProvided(), unless(anyOf( isExpansionInSystemHeader(), isVirtual(), isConst(), - matchers::isStaticMethod(), matchers::hasTrivialBody(), - cxxConstructorDecl(), cxxDestructorDecl(), - matchers::isTemplate(), matchers::isDependentContext(), - ofClass(anyOf( - isLambda(), - matchers::hasAnyDependentBases()) // Method might become + isStatic(), hasTrivialBody(), cxxConstructorDecl(), + cxxDestructorDecl(), isTemplate(), isDependentContext(), + ofClass(anyOf(isLambda(), + hasAnyDependentBases()) // Method might become // virtual depending on // template base class. ), - matchers::isInsideMacroDefinition(), - matchers::hasCanonicalDecl( - matchers::isInsideMacroDefinition()))), + isInsideMacroDefinition(), + hasCanonicalDecl(isInsideMacroDefinition()))), usesThisAsConst()) .bind("x")), this); diff --git a/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp index bfd4dca..7850a6f 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp @@ -15,13 +15,10 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { -namespace { AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) { return Node.doesDeclarationForceExternallyVisibleDefinition(); } -} // namespace - RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp index 86d3c7e..8837ac1 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp @@ -15,8 +15,7 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { namespace { -clang::ast_matchers::internal::Matcher<Expr> -callToGet(const clang::ast_matchers::internal::Matcher<Decl> &OnClass) { +internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) { return expr( anyOf(cxxMemberCallExpr( on(expr(anyOf(hasType(OnClass), @@ -44,7 +43,7 @@ callToGet(const clang::ast_matchers::internal::Matcher<Decl> &OnClass) { .bind("redundant_get"); } -clang::ast_matchers::internal::Matcher<Decl> knownSmartptr() { +internal::Matcher<Decl> knownSmartptr() { return recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr")); } diff --git a/clang-tools-extra/clang-tidy/readability/ReferenceToConstructedTemporaryCheck.cpp b/clang-tools-extra/clang-tidy/readability/ReferenceToConstructedTemporaryCheck.cpp index 9abe0f0..587ae8e 100644 --- a/clang-tools-extra/clang-tidy/readability/ReferenceToConstructedTemporaryCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReferenceToConstructedTemporaryCheck.cpp @@ -20,8 +20,7 @@ namespace { // Predicate structure to check if lifetime of temporary is not extended by // ValueDecl pointed out by ID struct NotExtendedByDeclBoundToPredicate { - bool - operator()(const clang::ast_matchers::internal::BoundNodesMap &Nodes) const { + bool operator()(const internal::BoundNodesMap &Nodes) const { const auto *Other = Nodes.getNodeAs<ValueDecl>(ID); if (!Other) return true; diff --git a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp index 77b8f5e..65356cc 100644 --- a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "StaticAccessedThroughInstanceCheck.h" -#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/ADT/StringRef.h" @@ -16,6 +15,10 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { +namespace { +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } +} // namespace + static unsigned getNameSpecifierNestingLevel(const QualType &QType) { if (const auto *ElType = QType->getAs<ElaboratedType>()) { if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) { @@ -39,7 +42,7 @@ void StaticAccessedThroughInstanceCheck::storeOptions( void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - memberExpr(hasDeclaration(anyOf(cxxMethodDecl(matchers::isStaticMethod()), + memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStatic()), varDecl(hasStaticStorageDuration()), enumConstantDecl()))) .bind("memberExpression"), |