diff options
author | Piotr Zegar <me@piotrzegar.pl> | 2024-03-31 14:34:25 +0000 |
---|---|---|
committer | Piotr Zegar <me@piotrzegar.pl> | 2024-03-31 14:58:27 +0000 |
commit | b6f6be4b500ff64c23a5103ac3311cb74519542f (patch) | |
tree | 68d134e3d3867627eb133d5a8a859a2b5202f689 /clang-tools-extra/clang-tidy/readability | |
parent | d12e45ad16a62f7d3ff20b90863f42c9ddb0e624 (diff) | |
download | llvm-b6f6be4b500ff64c23a5103ac3311cb74519542f.zip llvm-b6f6be4b500ff64c23a5103ac3311cb74519542f.tar.gz llvm-b6f6be4b500ff64c23a5103ac3311cb74519542f.tar.bz2 |
[clang-tidy][NFC] Remove duplicated code
Remove duplicated matchers by moving some of them to
utils/Matchers.h. Add some anonymous namespaces and
renamed some code to avoid ODR issues.
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability')
7 files changed, 50 insertions, 92 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp index 1284df6..5b1bf01 100644 --- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp +++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "ConvertMemberFunctionsToStatic.h" +#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" @@ -18,40 +19,12 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { -AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } - -AST_MATCHER(CXXMethodDecl, hasTrivialBody) { return Node.hasTrivialBody(); } +namespace { 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: @@ -74,22 +47,27 @@ AST_MATCHER(CXXMethodDecl, usesThis) { return UsageOfThis.Used; } +} // namespace + void ConvertMemberFunctionsToStatic::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxMethodDecl( isDefinition(), isUserProvided(), unless(anyOf( - isExpansionInSystemHeader(), isVirtual(), isStatic(), - hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(), - cxxDestructorDecl(), cxxConversionDecl(), isTemplate(), - isDependentContext(), + isExpansionInSystemHeader(), isVirtual(), + matchers::isStaticMethod(), matchers::hasTrivialBody(), + isOverloadedOperator(), cxxConstructorDecl(), cxxDestructorDecl(), + cxxConversionDecl(), matchers::isTemplate(), + matchers::isDependentContext(), ofClass(anyOf( isLambda(), - hasAnyDependentBases()) // Method might become virtual - // depending on template base class. + matchers::hasAnyDependentBases()) // Method might become + // virtual depending on + // template base class. ), - isInsideMacroDefinition(), - hasCanonicalDecl(isInsideMacroDefinition()), usesThis()))) + matchers::isInsideMacroDefinition(), + matchers::hasCanonicalDecl(matchers::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 759cdd4..261c02e 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 FunctionASTVisitor final - : public RecursiveASTVisitor<FunctionASTVisitor> { - using Base = RecursiveASTVisitor<FunctionASTVisitor>; +class CognitiveComplexityFunctionASTVisitor final + : public RecursiveASTVisitor<CognitiveComplexityFunctionASTVisitor> { + using Base = RecursiveASTVisitor<CognitiveComplexityFunctionASTVisitor>; // If set to true, macros are ignored during analysis. const bool IgnoreMacros; @@ -219,7 +219,7 @@ class FunctionASTVisitor final std::stack<OBO, SmallVector<OBO, 4>> BinaryOperatorsStack; public: - explicit FunctionASTVisitor(const bool IgnoreMacros) + explicit CognitiveComplexityFunctionASTVisitor(const bool IgnoreMacros) : IgnoreMacros(IgnoreMacros) {} bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) { @@ -453,12 +453,13 @@ 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 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. + // 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. bool TraverseDecl(Decl *Node, bool MainAnalyzedFunction = false) { if (!Node || MainAnalyzedFunction) return Base::TraverseDecl(Node); @@ -515,7 +516,7 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) { void FunctionCognitiveComplexityCheck::check( const MatchFinder::MatchResult &Result) { - FunctionASTVisitor Visitor(IgnoreMacros); + CognitiveComplexityFunctionASTVisitor 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 d42fcba..c828ef3 100644 --- a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "MakeMemberFunctionConstCheck.h" +#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/AST/ParentMapContext.h" #include "clang/AST/RecursiveASTVisitor.h" @@ -17,36 +18,6 @@ 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> { @@ -205,6 +176,7 @@ public: } }; +namespace { AST_MATCHER(CXXMethodDecl, usesThisAsConst) { FindUsageOfThis UsageOfThis(Finder->getASTContext()); @@ -214,6 +186,8 @@ AST_MATCHER(CXXMethodDecl, usesThisAsConst) { return UsageOfThis.Usage == Const; } +} // namespace + void MakeMemberFunctionConstCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( traverse( @@ -222,15 +196,18 @@ void MakeMemberFunctionConstCheck::registerMatchers(MatchFinder *Finder) { isDefinition(), isUserProvided(), unless(anyOf( isExpansionInSystemHeader(), isVirtual(), isConst(), - isStatic(), hasTrivialBody(), cxxConstructorDecl(), - cxxDestructorDecl(), isTemplate(), isDependentContext(), - ofClass(anyOf(isLambda(), - hasAnyDependentBases()) // Method might become + matchers::isStaticMethod(), matchers::hasTrivialBody(), + cxxConstructorDecl(), cxxDestructorDecl(), + matchers::isTemplate(), matchers::isDependentContext(), + ofClass(anyOf( + isLambda(), + matchers::hasAnyDependentBases()) // Method might become // virtual depending on // template base class. ), - isInsideMacroDefinition(), - hasCanonicalDecl(isInsideMacroDefinition()))), + matchers::isInsideMacroDefinition(), + matchers::hasCanonicalDecl( + matchers::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 7850a6f..bfd4dca 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp @@ -15,10 +15,13 @@ 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 8837ac1..86d3c7e 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp @@ -15,7 +15,8 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { namespace { -internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) { +clang::ast_matchers::internal::Matcher<Expr> +callToGet(const clang::ast_matchers::internal::Matcher<Decl> &OnClass) { return expr( anyOf(cxxMemberCallExpr( on(expr(anyOf(hasType(OnClass), @@ -43,7 +44,7 @@ internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) { .bind("redundant_get"); } -internal::Matcher<Decl> knownSmartptr() { +clang::ast_matchers::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 587ae8e..9abe0f0 100644 --- a/clang-tools-extra/clang-tidy/readability/ReferenceToConstructedTemporaryCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReferenceToConstructedTemporaryCheck.cpp @@ -20,7 +20,8 @@ namespace { // Predicate structure to check if lifetime of temporary is not extended by // ValueDecl pointed out by ID struct NotExtendedByDeclBoundToPredicate { - bool operator()(const internal::BoundNodesMap &Nodes) const { + bool + operator()(const clang::ast_matchers::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 65356cc..77b8f5e 100644 --- a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "StaticAccessedThroughInstanceCheck.h" +#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/ADT/StringRef.h" @@ -15,10 +16,6 @@ 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()) { @@ -42,7 +39,7 @@ void StaticAccessedThroughInstanceCheck::storeOptions( void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStatic()), + memberExpr(hasDeclaration(anyOf(cxxMethodDecl(matchers::isStaticMethod()), varDecl(hasStaticStorageDuration()), enumConstantDecl()))) .bind("memberExpression"), |