[PATCH] D96131: [clang-tidy] Simplify function complexity check
This revision was automatically updated to reflect the committed changes. Closed by commit rG6852a29a3b5b: [clang-tidy] Simplify function complexity check (authored by stephenkelly). Changed prior to commit: https://reviews.llvm.org/D96131?vs=325226=325241#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D96131/new/ https://reviews.llvm.org/D96131 Files: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,7 +666,7 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES: :[[@LINE-6]]:14: warning: function 'operator()' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] +// CHECK-NOTES: :[[@LINE-6]]:14: warning: lambda has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] // CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} } Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h @@ -31,6 +31,9 @@ void storeOptions(ClangTidyOptions::OptionMap ) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; + llvm::Optional getCheckTraversalKind() const override { +return TK_IgnoreUnlessSpelledInSource; + } private: const unsigned Threshold; Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -502,27 +502,40 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( functionDecl(isDefinition(), - unless(anyOf(isDefaulted(), isDeleted(), isImplicit(), -isInstantiated(), isWeak( + unless(anyOf(isDefaulted(), isDeleted(), isWeak( .bind("func"), this); + Finder->addMatcher(lambdaExpr().bind("lambda"), this); } void FunctionCognitiveComplexityCheck::check( const MatchFinder::MatchResult ) { - const auto *Func = Result.Nodes.getNodeAs("func"); - assert(Func->hasBody() && "The matchers should only match the functions that " -"have user-provided body."); FunctionASTVisitor Visitor; - Visitor.TraverseDecl(const_cast(Func), true); + SourceLocation Loc; + + const auto *TheDecl = Result.Nodes.getNodeAs("func"); + const auto *TheLambdaExpr = Result.Nodes.getNodeAs("lambda"); + if (TheDecl) { +assert(TheDecl->hasBody() && + "The matchers should only match the functions that " + "have user-provided body."); +Loc = TheDecl->getLocation(); +Visitor.TraverseDecl(const_cast(TheDecl), true); + } else { +Loc = TheLambdaExpr->getBeginLoc(); +Visitor.TraverseLambdaExpr(const_cast(TheLambdaExpr)); + } if (Visitor.CC.Total <= Threshold) return; - diag(Func->getLocation(), - "function %0 has cognitive complexity of %1 (threshold %2)") - << Func << Visitor.CC.Total << Threshold; + if (TheDecl) +diag(Loc, "function %0 has cognitive complexity of %1 (threshold %2)") +<< TheDecl << Visitor.CC.Total << Threshold; + else +diag(Loc, "lambda has cognitive complexity of %0 (threshold %1)") +<< Visitor.CC.Total << Threshold; // Output all the basic increments of complexity. for (const auto : Visitor.CC.Details) { Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,7 +666,7 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES:
[PATCH] D96131: [clang-tidy] Simplify function complexity check
steveire updated this revision to Diff 325226. steveire added a comment. Fix test Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D96131/new/ https://reviews.llvm.org/D96131 Files: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,8 +666,8 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES: :[[@LINE-6]]:14: warning: function 'operator()' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] -// CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} + // CHECK-NOTES: :[[@LINE-6]]:14: warning: lambda has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] + // CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} } void unittest_b2_09() { Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h @@ -31,6 +31,9 @@ void storeOptions(ClangTidyOptions::OptionMap ) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; + llvm::Optional getCheckTraversalKind() const override { +return TK_IgnoreUnlessSpelledInSource; + } private: const unsigned Threshold; Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -502,27 +502,40 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( functionDecl(isDefinition(), - unless(anyOf(isDefaulted(), isDeleted(), isImplicit(), -isInstantiated(), isWeak( + unless(anyOf(isDefaulted(), isDeleted(), isWeak( .bind("func"), this); + Finder->addMatcher(lambdaExpr().bind("lambda"), this); } void FunctionCognitiveComplexityCheck::check( const MatchFinder::MatchResult ) { - const auto *Func = Result.Nodes.getNodeAs("func"); - assert(Func->hasBody() && "The matchers should only match the functions that " -"have user-provided body."); FunctionASTVisitor Visitor; - Visitor.TraverseDecl(const_cast(Func), true); + SourceLocation Loc; + + const auto *TheDecl = Result.Nodes.getNodeAs("func"); + const auto *TheLambdaExpr = Result.Nodes.getNodeAs("lambda"); + if (TheDecl) { +assert(TheDecl->hasBody() && + "The matchers should only match the functions that " + "have user-provided body."); +Loc = TheDecl->getLocation(); +Visitor.TraverseDecl(const_cast(TheDecl), true); + } else { +Loc = TheLambdaExpr->getBeginLoc(); +Visitor.TraverseLambdaExpr(const_cast(TheLambdaExpr)); + } if (Visitor.CC.Total <= Threshold) return; - diag(Func->getLocation(), - "function %0 has cognitive complexity of %1 (threshold %2)") - << Func << Visitor.CC.Total << Threshold; + if (TheDecl) +diag(Loc, "function %0 has cognitive complexity of %1 (threshold %2)") +<< TheDecl << Visitor.CC.Total << Threshold; + else +diag(Loc, "lambda has cognitive complexity of %0 (threshold %1)") +<< Visitor.CC.Total << Threshold; // Output all the basic increments of complexity. for (const auto : Visitor.CC.Details) { Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,8 +666,8 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES: :[[@LINE-6]]:14: warning: function
[PATCH] D96131: [clang-tidy] Simplify function complexity check
steveire updated this revision to Diff 325216. steveire added a comment. Trigger rebuild Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D96131/new/ https://reviews.llvm.org/D96131 Files: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,8 +666,8 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES: :[[@LINE-6]]:14: warning: function 'operator()' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] -// CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} + // CHECK-NOTES: :[[@LINE-6]]:14: warning: lambda has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] + // CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} } void unittest_b2_09() { Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h @@ -31,6 +31,9 @@ void storeOptions(ClangTidyOptions::OptionMap ) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; + llvm::Optional getCheckTraversalKind() const override { +return TK_IgnoreUnlessSpelledInSource; + } private: const unsigned Threshold; Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -501,28 +501,38 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - functionDecl(isDefinition(), - unless(anyOf(isDefaulted(), isDeleted(), isImplicit(), -isInstantiated(), isWeak( - .bind("func"), - this); + functionDecl(isDefinition(), unless(isWeak())).bind("func"), this); + Finder->addMatcher(lambdaExpr().bind("lambda"), this); } void FunctionCognitiveComplexityCheck::check( const MatchFinder::MatchResult ) { - const auto *Func = Result.Nodes.getNodeAs("func"); - assert(Func->hasBody() && "The matchers should only match the functions that " -"have user-provided body."); FunctionASTVisitor Visitor; - Visitor.TraverseDecl(const_cast(Func), true); + SourceLocation Loc; + + const auto *TheDecl = Result.Nodes.getNodeAs("func"); + const auto *TheLambdaExpr = Result.Nodes.getNodeAs("lambda"); + if (TheDecl) { +assert(TheDecl->hasBody() && + "The matchers should only match the functions that " + "have user-provided body."); +Loc = TheDecl->getLocation(); +Visitor.TraverseDecl(const_cast(TheDecl), true); + } else { +Loc = TheLambdaExpr->getBeginLoc(); +Visitor.TraverseLambdaExpr(const_cast(TheLambdaExpr)); + } if (Visitor.CC.Total <= Threshold) return; - diag(Func->getLocation(), - "function %0 has cognitive complexity of %1 (threshold %2)") - << Func << Visitor.CC.Total << Threshold; + if (TheDecl) +diag(Loc, "function %0 has cognitive complexity of %1 (threshold %2)") +<< TheDecl << Visitor.CC.Total << Threshold; + else +diag(Loc, "lambda has cognitive complexity of %0 (threshold %1)") +<< Visitor.CC.Total << Threshold; // Output all the basic increments of complexity. for (const auto : Visitor.CC.Details) { Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,8 +666,8 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES: :[[@LINE-6]]:14: warning:
[PATCH] D96131: [clang-tidy] Simplify function complexity check
njames93 added a comment. Can you have a look at the failed tests, they seem to be related. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D96131/new/ https://reviews.llvm.org/D96131 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D96131: [clang-tidy] Simplify function complexity check
steveire added inline comments. Comment at: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp:505 + functionDecl(isDefinition(), unless(isWeak())).bind("func"), this); + Finder->addMatcher(lambdaExpr().bind("lambda"), this); } njames93 wrote: > Am I right in assuming lambdas need to be explicitly matched because in the > traversal mode their operator() isn't matched as a functionDecl? Yes. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D96131/new/ https://reviews.llvm.org/D96131 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D96131: [clang-tidy] Simplify function complexity check
njames93 added inline comments. Comment at: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp:505 + functionDecl(isDefinition(), unless(isWeak())).bind("func"), this); + Finder->addMatcher(lambdaExpr().bind("lambda"), this); } Am I right in assuming lambdas need to be explicitly matched because in the traversal mode their operator() isn't matched as a functionDecl? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D96131/new/ https://reviews.llvm.org/D96131 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D96131: [clang-tidy] Simplify function complexity check
steveire created this revision. steveire added reviewers: aaron.ballman, njames93. Herald added subscribers: nullptr.cpp, lebedev.ri, xazax.hun. Herald added a reviewer: lebedev.ri. steveire requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. Update test to note use of lambda instead of the invisible operator(). Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D96131 Files: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp @@ -666,8 +666,8 @@ // CHECK-NOTES: :[[@LINE-1]]:5: note: +2, including nesting penalty of 1, nesting level increased to 2{{$}} } }; -// CHECK-NOTES: :[[@LINE-6]]:14: warning: function 'operator()' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] -// CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} + // CHECK-NOTES: :[[@LINE-6]]:14: warning: lambda has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] + // CHECK-NOTES: :[[@LINE-5]]:5: note: +1, including nesting penalty of 0, nesting level increased to 1{{$}} } void unittest_b2_09() { Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h @@ -31,6 +31,9 @@ void storeOptions(ClangTidyOptions::OptionMap ) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; + llvm::Optional getCheckTraversalKind() const override { +return TK_IgnoreUnlessSpelledInSource; + } private: const unsigned Threshold; Index: clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp === --- clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp +++ clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp @@ -501,28 +501,38 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - functionDecl(isDefinition(), - unless(anyOf(isDefaulted(), isDeleted(), isImplicit(), -isInstantiated(), isWeak( - .bind("func"), - this); + functionDecl(isDefinition(), unless(isWeak())).bind("func"), this); + Finder->addMatcher(lambdaExpr().bind("lambda"), this); } void FunctionCognitiveComplexityCheck::check( const MatchFinder::MatchResult ) { - const auto *Func = Result.Nodes.getNodeAs("func"); - assert(Func->hasBody() && "The matchers should only match the functions that " -"have user-provided body."); FunctionASTVisitor Visitor; - Visitor.TraverseDecl(const_cast(Func), true); + SourceLocation Loc; + + const auto *TheDecl = Result.Nodes.getNodeAs("func"); + const auto *TheLambdaExpr = Result.Nodes.getNodeAs("lambda"); + if (TheDecl) { +assert(TheDecl->hasBody() && + "The matchers should only match the functions that " + "have user-provided body."); +Loc = TheDecl->getLocation(); +Visitor.TraverseDecl(const_cast(TheDecl), true); + } else { +Loc = TheLambdaExpr->getBeginLoc(); +Visitor.TraverseLambdaExpr(const_cast(TheLambdaExpr)); + } if (Visitor.CC.Total <= Threshold) return; - diag(Func->getLocation(), - "function %0 has cognitive complexity of %1 (threshold %2)") - << Func << Visitor.CC.Total << Threshold; + if (TheDecl) +diag(Loc, "function %0 has cognitive complexity of %1 (threshold %2)") +<< TheDecl << Visitor.CC.Total << Threshold; + else +diag(Loc, "lambda has cognitive complexity of %0 (threshold %1)") +<< Visitor.CC.Total << Threshold; // Output all the basic increments of complexity. for (const auto : Visitor.CC.Details) { Index: clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp === --- clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp +++