1 //===--- FunctionCognitiveComplexityCheck.cpp - clang-tidy ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "FunctionCognitiveComplexityCheck.h"
10 #include "../ClangTidyDiagnosticConsumer.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclBase.h"
13 #include "clang/AST/Expr.h"
14 #include "clang/AST/RecursiveASTVisitor.h"
15 #include "clang/AST/Stmt.h"
16 #include "clang/ASTMatchers/ASTMatchFinder.h"
17 #include "clang/ASTMatchers/ASTMatchers.h"
18 #include "clang/ASTMatchers/ASTMatchersInternal.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/DiagnosticIDs.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
31 #include <type_traits>
34 using namespace clang::ast_matchers;
38 namespace readability {
41 struct CognitiveComplexity final {
42 // Any increment is based on some combination of reasons.
43 // For details you can look at the Specification at
44 // https://www.sonarsource.com/docs/CognitiveComplexity.pdf
45 // or user-facing docs at
46 // http://clang.llvm.org/extra/clang-tidy/checks/readability-function-cognitive-complexity.html
47 // Here are all the possible reasons:
48 enum Criteria : uint8_t {
51 // B1, increases cognitive complexity (by 1)
53 // * if, else if, else, ConditionalOperator (not BinaryConditionalOperator)
55 // * ForStmt, CXXForRangeStmt
56 // * WhileStmt, DoStmt
58 // * GotoStmt, IndirectGotoStmt (but not BreakStmt, ContinueStmt)
59 // * sequences of binary logical operators (BinOpLAnd, BinOpLOr)
60 // * each method in a recursion cycle (not implemented)
63 // B2, increases current nesting level (by 1)
65 // * if, else if, else, ConditionalOperator (not BinaryConditionalOperator)
67 // * ForStmt, CXXForRangeStmt
68 // * WhileStmt, DoStmt
70 // * nested CXXConstructor, CXXDestructor, CXXMethod (incl. C++11 Lambda)
71 // * GNU Statement Expression
72 // * Apple Block declaration
73 IncrementNesting = 1U << 1,
75 // B3, increases cognitive complexity by the current nesting level
76 // Applied before IncrementNesting
78 // * IfStmt, ConditionalOperator (not BinaryConditionalOperator)
80 // * ForStmt, CXXForRangeStmt
81 // * WhileStmt, DoStmt
83 PenalizeNesting = 1U << 2,
85 All = Increment | PenalizeNesting | IncrementNesting,
88 // The helper struct used to record one increment occurrence, with all the
91 const SourceLocation Loc; // What caused the increment?
92 const unsigned short Nesting; // How deeply nested is Loc located?
93 const Criteria C; // The criteria of the increment
95 Detail(SourceLocation SLoc, unsigned short CurrentNesting, Criteria Crit)
96 : Loc(SLoc), Nesting(CurrentNesting), C(Crit) {}
98 // To minimize the sizeof(Detail), we only store the minimal info there.
99 // This function is used to convert from the stored info into the usable
100 // information - what message to output, how much of an increment did this
101 // occurrence actually result in.
102 std::pair<unsigned, unsigned short> process() const {
103 assert(C != Criteria::None && "invalid criteria");
105 unsigned MsgId; // The id of the message to output.
106 unsigned short Increment; // How much of an increment?
108 if (C == Criteria::All) {
109 Increment = 1 + Nesting;
111 } else if (C == (Criteria::Increment | Criteria::IncrementNesting)) {
114 } else if (C == Criteria::Increment) {
117 } else if (C == Criteria::IncrementNesting) {
118 Increment = 0; // Unused in this message.
121 llvm_unreachable("should not get to here.");
123 return std::make_pair(MsgId, Increment);
127 // Limit of 25 is the "upstream"'s default.
128 static constexpr unsigned DefaultLimit = 25U;
130 // Based on the publicly-avaliable numbers for some big open-source projects
131 // https://sonarcloud.io/projects?languages=c%2Ccpp&size=5 we can estimate:
132 // value ~20 would result in no allocs for 98% of functions, ~12 for 96%, ~10
133 // for 91%, ~8 for 88%, ~6 for 84%, ~4 for 77%, ~2 for 64%, and ~1 for 37%.
134 static_assert(sizeof(Detail) <= 8,
135 "Since we use SmallVector to minimize the amount of "
136 "allocations, we also need to consider the price we pay for "
137 "that in terms of stack usage. "
138 "Thus, it is good to minimize the size of the Detail struct.");
139 SmallVector<Detail, DefaultLimit> Details; // 25 elements is 200 bytes.
140 // Yes, 25 is a magic number. This is the seemingly-sane default for the
141 // upper limit for function cognitive complexity. Thus it would make sense
142 // to avoid allocations for any function that does not violate the limit.
144 // The grand total Cognitive Complexity of the function.
147 // The function used to store new increment, calculate the total complexity.
148 void account(SourceLocation Loc, unsigned short Nesting, Criteria C);
151 // All the possible messages that can be output. The choice of the message
152 // to use is based of the combination of the CognitiveComplexity::Criteria.
153 // It would be nice to have it in CognitiveComplexity struct, but then it is
155 static const std::array<const StringRef, 4> Msgs = {{
157 "+%0, including nesting penalty of %1, nesting level increased to %2",
160 "+%0, nesting level increased to %2",
166 "nesting level increased to %2",
169 // Criteria is a bitset, thus a few helpers are needed.
170 CognitiveComplexity::Criteria operator|(CognitiveComplexity::Criteria LHS,
171 CognitiveComplexity::Criteria RHS) {
172 return static_cast<CognitiveComplexity::Criteria>(
173 static_cast<std::underlying_type<CognitiveComplexity::Criteria>::type>(
175 static_cast<std::underlying_type<CognitiveComplexity::Criteria>::type>(
178 CognitiveComplexity::Criteria operator&(CognitiveComplexity::Criteria LHS,
179 CognitiveComplexity::Criteria RHS) {
180 return static_cast<CognitiveComplexity::Criteria>(
181 static_cast<std::underlying_type<CognitiveComplexity::Criteria>::type>(
183 static_cast<std::underlying_type<CognitiveComplexity::Criteria>::type>(
186 CognitiveComplexity::Criteria &operator|=(CognitiveComplexity::Criteria &LHS,
187 CognitiveComplexity::Criteria RHS) {
188 LHS = operator|(LHS, RHS);
191 CognitiveComplexity::Criteria &operator&=(CognitiveComplexity::Criteria &LHS,
192 CognitiveComplexity::Criteria RHS) {
193 LHS = operator&(LHS, RHS);
197 void CognitiveComplexity::account(SourceLocation Loc, unsigned short Nesting,
200 assert(C != Criteria::None && "invalid criteria");
202 Details.emplace_back(Loc, Nesting, C);
203 const Detail &D = Details.back();
206 unsigned short Increase;
207 std::tie(MsgId, Increase) = D.process();
212 class FunctionASTVisitor final
213 : public RecursiveASTVisitor<FunctionASTVisitor> {
214 using Base = RecursiveASTVisitor<FunctionASTVisitor>;
216 // The current nesting level (increased by Criteria::IncrementNesting).
217 unsigned short CurrentNestingLevel = 0;
219 // Used to efficiently know the last type of the binary sequence operator
220 // that was encountered. It would make sense for the function call to start
221 // the new sequence, thus it is a stack.
222 using OBO = Optional<BinaryOperator::Opcode>;
223 std::stack<OBO, SmallVector<OBO, 4>> BinaryOperatorsStack;
226 bool TraverseStmtWithIncreasedNestingLevel(Stmt *Node) {
227 ++CurrentNestingLevel;
228 bool ShouldContinue = Base::TraverseStmt(Node);
229 --CurrentNestingLevel;
230 return ShouldContinue;
233 bool TraverseDeclWithIncreasedNestingLevel(Decl *Node) {
234 ++CurrentNestingLevel;
235 bool ShouldContinue = Base::TraverseDecl(Node);
236 --CurrentNestingLevel;
237 return ShouldContinue;
240 bool TraverseIfStmt(IfStmt *Node, bool InElseIf = false) {
242 return Base::TraverseIfStmt(Node);
245 CognitiveComplexity::Criteria Reasons;
247 Reasons = CognitiveComplexity::Criteria::None;
249 // "If" increases cognitive complexity.
250 Reasons |= CognitiveComplexity::Criteria::Increment;
251 // "If" increases nesting level.
252 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
255 // "If" receives a nesting increment commensurate with it's nested
256 // depth, if it is not part of "else if".
257 Reasons |= CognitiveComplexity::Criteria::PenalizeNesting;
260 CC.account(Node->getIfLoc(), CurrentNestingLevel, Reasons);
263 // If this IfStmt is *NOT* "else if", then only the body (i.e. "Then" and
264 // "Else") is traversed with increased Nesting level.
265 // However if this IfStmt *IS* "else if", then Nesting level is increased
266 // for the whole IfStmt (i.e. for "Init", "Cond", "Then" and "Else").
269 if (!TraverseStmt(Node->getInit()))
272 if (!TraverseStmt(Node->getCond()))
275 if (!TraverseStmtWithIncreasedNestingLevel(Node->getInit()))
278 if (!TraverseStmtWithIncreasedNestingLevel(Node->getCond()))
282 // "Then" always increases nesting level.
283 if (!TraverseStmtWithIncreasedNestingLevel(Node->getThen()))
286 if (!Node->getElse())
289 if (auto *E = dyn_cast<IfStmt>(Node->getElse()))
290 return TraverseIfStmt(E, true);
293 CognitiveComplexity::Criteria Reasons;
295 Reasons = CognitiveComplexity::Criteria::None;
297 // "Else" increases cognitive complexity.
298 Reasons |= CognitiveComplexity::Criteria::Increment;
299 // "Else" increases nesting level.
300 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
301 // "Else" DOES NOT receive a nesting increment commensurate with it's
304 CC.account(Node->getElseLoc(), CurrentNestingLevel, Reasons);
307 // "Else" always increases nesting level.
308 return TraverseStmtWithIncreasedNestingLevel(Node->getElse());
311 // The currently-being-processed stack entry, which is always the top.
312 #define CurrentBinaryOperator BinaryOperatorsStack.top()
314 // In a sequence of binary logical operators, if the new operator is different
315 // from the previous one, then the cognitive complexity is increased.
316 bool TraverseBinaryOperator(BinaryOperator *Op) {
317 if (!Op || !Op->isLogicalOp())
318 return Base::TraverseBinaryOperator(Op);
320 // Make sure that there is always at least one frame in the stack.
321 if (BinaryOperatorsStack.empty())
322 BinaryOperatorsStack.emplace();
324 // If this is the first binary operator that we are processing, or the
325 // previous binary operator was different, there is an increment.
326 if (!CurrentBinaryOperator || Op->getOpcode() != CurrentBinaryOperator)
327 CC.account(Op->getOperatorLoc(), CurrentNestingLevel,
328 CognitiveComplexity::Criteria::Increment);
330 // We might encounter a function call, which starts a new sequence, thus
331 // we need to save the current previous binary operator.
332 const Optional<BinaryOperator::Opcode> BinOpCopy(CurrentBinaryOperator);
334 // Record the operator that we are currently processing and traverse it.
335 CurrentBinaryOperator = Op->getOpcode();
336 bool ShouldContinue = Base::TraverseBinaryOperator(Op);
338 // And restore the previous binary operator, which might be nonexistent.
339 CurrentBinaryOperator = BinOpCopy;
341 return ShouldContinue;
344 // It would make sense for the function call to start the new binary
345 // operator sequence, thus let's make sure that it creates a new stack frame.
346 bool TraverseCallExpr(CallExpr *Node) {
347 // If we are not currently processing any binary operator sequence, then
348 // no Node-handling is needed.
349 if (!Node || BinaryOperatorsStack.empty() || !CurrentBinaryOperator)
350 return Base::TraverseCallExpr(Node);
352 // Else, do add [uninitialized] frame to the stack, and traverse call.
353 BinaryOperatorsStack.emplace();
354 bool ShouldContinue = Base::TraverseCallExpr(Node);
355 // And remove the top frame.
356 BinaryOperatorsStack.pop();
358 return ShouldContinue;
361 #undef CurrentBinaryOperator
363 bool TraverseStmt(Stmt *Node) {
365 return Base::TraverseStmt(Node);
367 // Three following switch()'es have huge duplication, but it is better to
368 // keep them separate, to simplify comparing them with the Specification.
370 CognitiveComplexity::Criteria Reasons = CognitiveComplexity::Criteria::None;
371 SourceLocation Location = Node->getBeginLoc();
374 // There is an increment for each of the following:
375 switch (Node->getStmtClass()) {
376 // if, else if, else are handled in TraverseIfStmt(),
377 // FIXME: "each method in a recursion cycle" Increment is not implemented.
378 case Stmt::ConditionalOperatorClass:
379 case Stmt::SwitchStmtClass:
380 case Stmt::ForStmtClass:
381 case Stmt::CXXForRangeStmtClass:
382 case Stmt::WhileStmtClass:
383 case Stmt::DoStmtClass:
384 case Stmt::CXXCatchStmtClass:
385 case Stmt::GotoStmtClass:
386 case Stmt::IndirectGotoStmtClass:
387 Reasons |= CognitiveComplexity::Criteria::Increment;
390 // break LABEL, continue LABEL increase cognitive complexity,
391 // but they are not supported in C++ or C.
392 // Regular break/continue do not increase cognitive complexity.
397 // The following structures increment the nesting level:
398 switch (Node->getStmtClass()) {
399 // if, else if, else are handled in TraverseIfStmt(),
400 // Nested methods and such are handled in TraverseDecl.
401 case Stmt::ConditionalOperatorClass:
402 case Stmt::SwitchStmtClass:
403 case Stmt::ForStmtClass:
404 case Stmt::CXXForRangeStmtClass:
405 case Stmt::WhileStmtClass:
406 case Stmt::DoStmtClass:
407 case Stmt::CXXCatchStmtClass:
408 case Stmt::LambdaExprClass:
409 case Stmt::StmtExprClass:
410 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
416 // B3. Nesting increments
417 // The following structures receive a nesting increment
418 // commensurate with their nested depth inside B2 structures:
419 switch (Node->getStmtClass()) {
420 // if, else if, else are handled in TraverseIfStmt().
421 case Stmt::ConditionalOperatorClass:
422 case Stmt::SwitchStmtClass:
423 case Stmt::ForStmtClass:
424 case Stmt::CXXForRangeStmtClass:
425 case Stmt::WhileStmtClass:
426 case Stmt::DoStmtClass:
427 case Stmt::CXXCatchStmtClass:
428 Reasons |= CognitiveComplexity::Criteria::PenalizeNesting;
434 if (Node->getStmtClass() == Stmt::ConditionalOperatorClass) {
435 // A little beautification.
436 // For conditional operator "cond ? true : false" point at the "?"
438 ConditionalOperator *COp = dyn_cast<ConditionalOperator>(Node);
439 Location = COp->getQuestionLoc();
442 // If we have found any reasons, let's account it.
443 if (Reasons & CognitiveComplexity::Criteria::All)
444 CC.account(Location, CurrentNestingLevel, Reasons);
446 // Did we decide that the nesting level should be increased?
447 if (!(Reasons & CognitiveComplexity::Criteria::IncrementNesting))
448 return Base::TraverseStmt(Node);
450 return TraverseStmtWithIncreasedNestingLevel(Node);
453 // The parameter MainAnalyzedFunction is needed to differentiate between the
454 // cases where TraverseDecl() is the entry point from
455 // FunctionCognitiveComplexityCheck::check() and the cases where it was called
456 // from the FunctionASTVisitor itself. Explanation: if we get a function
457 // definition (e.g. constructor, destructor, method), the Cognitive Complexity
458 // specification states that the Nesting level shall be increased. But if this
459 // function is the entry point, then the Nesting level should not be
460 // increased. Thus that parameter is there and is used to fall-through
461 // directly to traversing if this is the main function that is being analyzed.
462 bool TraverseDecl(Decl *Node, bool MainAnalyzedFunction = false) {
463 if (!Node || MainAnalyzedFunction)
464 return Base::TraverseDecl(Node);
467 // The following structures increment the nesting level:
468 switch (Node->getKind()) {
470 case Decl::CXXMethod:
471 case Decl::CXXConstructor:
472 case Decl::CXXDestructor:
476 // If this is something else, we use early return!
477 return Base::TraverseDecl(Node);
481 CC.account(Node->getBeginLoc(), CurrentNestingLevel,
482 CognitiveComplexity::Criteria::IncrementNesting);
484 return TraverseDeclWithIncreasedNestingLevel(Node);
487 CognitiveComplexity CC;
492 FunctionCognitiveComplexityCheck::FunctionCognitiveComplexityCheck(
493 StringRef Name, ClangTidyContext *Context)
494 : ClangTidyCheck(Name, Context),
495 Threshold(Options.get("Threshold", CognitiveComplexity::DefaultLimit)) {}
497 void FunctionCognitiveComplexityCheck::storeOptions(
498 ClangTidyOptions::OptionMap &Opts) {
499 Options.store(Opts, "Threshold", Threshold);
502 void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) {
505 allOf(isDefinition(), unless(anyOf(isDefaulted(), isDeleted(),
506 isImplicit(), isInstantiated()))))
511 void FunctionCognitiveComplexityCheck::check(
512 const MatchFinder::MatchResult &Result) {
513 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
514 assert(Func->hasBody() && "The matchers should only match the functions that "
515 "have user-provided body.");
517 FunctionASTVisitor Visitor;
518 Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func), true);
520 if (Visitor.CC.Total <= Threshold)
523 diag(Func->getLocation(),
524 "function %0 has cognitive complexity of %1 (threshold %2)")
525 << Func << Visitor.CC.Total << Threshold;
527 // Output all the basic increments of complexity.
528 for (const auto &Detail : Visitor.CC.Details) {
529 unsigned MsgId; // The id of the message to output.
530 unsigned short Increase; // How much of an increment?
531 std::tie(MsgId, Increase) = Detail.process();
532 assert(MsgId < Msgs.size() && "MsgId should always be valid");
533 // Increase, on the other hand, can be 0.
535 diag(Detail.Loc, Msgs[MsgId], DiagnosticIDs::Note)
536 << (unsigned)Increase << (unsigned)Detail.Nesting << 1 + Detail.Nesting;
540 } // namespace readability