[clang-tools-extra] 6852a29 - [clang-tidy] Simplify function complexity check

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 20 14:06:38 PST 2021


Author: Stephen Kelly
Date: 2021-02-20T22:06:16Z
New Revision: 6852a29a3b5b7858757c175f39e04676fb856dab

URL: https://github.com/llvm/llvm-project/commit/6852a29a3b5b7858757c175f39e04676fb856dab
DIFF: https://github.com/llvm/llvm-project/commit/6852a29a3b5b7858757c175f39e04676fb856dab.diff

LOG: [clang-tidy] Simplify function complexity check

Update test to note use of lambda instead of the invisible operator().

Differential Revision: https://reviews.llvm.org/D96131

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
index 88a422ab45a3..3a4758302406 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
@@ -502,27 +502,40 @@ void FunctionCognitiveComplexityCheck::storeOptions(
 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 &Result) {
-  const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
-  assert(Func->hasBody() && "The matchers should only match the functions that "
-                            "have user-provided body.");
 
   FunctionASTVisitor Visitor;
-  Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func), true);
+  SourceLocation Loc;
+
+  const auto *TheDecl = Result.Nodes.getNodeAs<FunctionDecl>("func");
+  const auto *TheLambdaExpr = Result.Nodes.getNodeAs<LambdaExpr>("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<FunctionDecl *>(TheDecl), true);
+  } else {
+    Loc = TheLambdaExpr->getBeginLoc();
+    Visitor.TraverseLambdaExpr(const_cast<LambdaExpr *>(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 &Detail : Visitor.CC.Details) {

diff  --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h
index 96b6723d2a6a..a21b8447029b 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h
@@ -31,6 +31,9 @@ class FunctionCognitiveComplexityCheck : public ClangTidyCheck {
   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 
 private:
   const unsigned Threshold;

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
index 79bc0c3dc4de..021330ccf9fd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-function-cognitive-complexity.cpp
@@ -666,7 +666,7 @@ void unittest_b2_08_02() {
 // 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{{$}}
 }
 


        


More information about the cfe-commits mailing list