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/BitmaskEnum.h"
24#include "llvm/Support/ErrorHandling.h"
37struct CognitiveComplexity final {
44 enum Criteria : uint8_t {
69 IncrementNesting = 1U << 1,
79 PenalizeNesting = 1U << 2,
81 All = Increment | PenalizeNesting | IncrementNesting,
83 LLVM_MARK_AS_BITMASK_ENUM(PenalizeNesting),
89 const SourceLocation Loc;
90 const unsigned short Nesting;
93 Detail(SourceLocation SLoc,
unsigned short CurrentNesting, Criteria Crit)
94 : Loc(SLoc), Nesting(CurrentNesting), C(Crit) {}
100 std::pair<unsigned, unsigned short> process()
const {
101 assert(C != Criteria::None &&
"invalid criteria");
104 unsigned short Increment = 0;
106 if (C == Criteria::All) {
107 Increment = 1 + Nesting;
109 }
else if (C == (Criteria::Increment | Criteria::IncrementNesting)) {
112 }
else if (C == Criteria::Increment) {
115 }
else if (C == Criteria::IncrementNesting) {
119 llvm_unreachable(
"should not get to here.");
122 return {MsgId, Increment};
127 static constexpr unsigned DefaultLimit = 25U;
133 static_assert(
sizeof(Detail) <= 8,
134 "Since we use SmallVector to minimize the amount of "
135 "allocations, we also need to consider the price we pay for "
136 "that in terms of stack usage. "
137 "Thus, it is good to minimize the size of the Detail struct.");
138 SmallVector<Detail, DefaultLimit> Details;
147 void account(SourceLocation Loc,
unsigned short Nesting, Criteria C);
156static constexpr std::array<StringRef, 4>
Msgs = {{
158 "+%0, including nesting penalty of %1, nesting level increased to %2",
161 "+%0, nesting level increased to %2",
167 "nesting level increased to %2",
170void CognitiveComplexity::account(SourceLocation Loc,
unsigned short Nesting,
173 assert(C != Criteria::None &&
"invalid criteria");
175 Details.emplace_back(Loc, Nesting, C);
176 const Detail &D = Details.back();
179 unsigned short Increase = 0;
180 std::tie(MsgId, Increase) = D.process();
187class FunctionASTVisitor final
188 :
public RecursiveASTVisitor<FunctionASTVisitor> {
189 using Base = RecursiveASTVisitor<FunctionASTVisitor>;
192 const bool IgnoreMacros;
195 unsigned short CurrentNestingLevel = 0;
200 using OBO = std::optional<BinaryOperator::Opcode>;
201 std::stack<OBO, SmallVector<OBO, 4>> BinaryOperatorsStack;
204 explicit FunctionASTVisitor(
const bool IgnoreMacros)
205 : IgnoreMacros(IgnoreMacros) {}
207 bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) {
208 ++CurrentNestingLevel;
209 const bool ShouldContinue = Base::TraverseStmt(Node);
210 --CurrentNestingLevel;
211 return ShouldContinue;
214 bool traverseDeclWithIncreasedNestingLevel(Decl *Node) {
215 ++CurrentNestingLevel;
216 const bool ShouldContinue = Base::TraverseDecl(Node);
217 --CurrentNestingLevel;
218 return ShouldContinue;
221 bool TraverseIfStmt(IfStmt *Node,
bool InElseIf =
false) {
223 return Base::TraverseIfStmt(Node);
226 CognitiveComplexity::Criteria Reasons =
227 CognitiveComplexity::Criteria::None;
230 Reasons |= CognitiveComplexity::Criteria::Increment;
232 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
237 Reasons |= CognitiveComplexity::Criteria::PenalizeNesting;
240 CC.account(Node->getIfLoc(), CurrentNestingLevel, Reasons);
249 if (!TraverseStmt(Node->getInit()))
252 if (!TraverseStmt(Node->getCond()))
255 if (!traverseStmtWithIncreasedNestingLevel(Node->getInit()))
258 if (!traverseStmtWithIncreasedNestingLevel(Node->getCond()))
263 if (!traverseStmtWithIncreasedNestingLevel(Node->getThen()))
266 if (!Node->getElse())
269 if (
auto *E = dyn_cast<IfStmt>(Node->getElse()))
270 return TraverseIfStmt(E,
true);
273 CognitiveComplexity::Criteria Reasons =
274 CognitiveComplexity::Criteria::None;
277 Reasons |= CognitiveComplexity::Criteria::Increment;
279 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
283 CC.account(Node->getElseLoc(), CurrentNestingLevel, Reasons);
287 return traverseStmtWithIncreasedNestingLevel(Node->getElse());
291#define CurrentBinaryOperator BinaryOperatorsStack.top()
295 bool TraverseBinaryOperator(BinaryOperator *Op) {
296 if (!Op || !Op->isLogicalOp())
297 return Base::TraverseBinaryOperator(Op);
300 if (BinaryOperatorsStack.empty())
301 BinaryOperatorsStack.emplace();
306 CC.account(Op->getOperatorLoc(), CurrentNestingLevel,
307 CognitiveComplexity::Criteria::Increment);
311 const std::optional<BinaryOperator::Opcode> BinOpCopy(
316 const bool ShouldContinue = Base::TraverseBinaryOperator(Op);
321 return ShouldContinue;
326 bool TraverseCallExpr(CallExpr *Node) {
330 return Base::TraverseCallExpr(Node);
333 BinaryOperatorsStack.emplace();
334 const bool ShouldContinue = Base::TraverseCallExpr(Node);
336 BinaryOperatorsStack.pop();
338 return ShouldContinue;
341#undef CurrentBinaryOperator
343 bool TraverseStmt(Stmt *Node) {
345 return Base::TraverseStmt(Node);
347 if (IgnoreMacros && Node->getBeginLoc().isMacroID())
353 CognitiveComplexity::Criteria Reasons = CognitiveComplexity::Criteria::None;
354 SourceLocation Location = Node->getBeginLoc();
358 switch (Node->getStmtClass()) {
361 case Stmt::ConditionalOperatorClass:
362 case Stmt::SwitchStmtClass:
363 case Stmt::ForStmtClass:
364 case Stmt::CXXForRangeStmtClass:
365 case Stmt::WhileStmtClass:
366 case Stmt::DoStmtClass:
367 case Stmt::CXXCatchStmtClass:
368 case Stmt::GotoStmtClass:
369 case Stmt::IndirectGotoStmtClass:
370 Reasons |= CognitiveComplexity::Criteria::Increment;
381 switch (Node->getStmtClass()) {
384 case Stmt::ConditionalOperatorClass:
385 case Stmt::SwitchStmtClass:
386 case Stmt::ForStmtClass:
387 case Stmt::CXXForRangeStmtClass:
388 case Stmt::WhileStmtClass:
389 case Stmt::DoStmtClass:
390 case Stmt::CXXCatchStmtClass:
391 case Stmt::LambdaExprClass:
392 case Stmt::StmtExprClass:
393 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
402 switch (Node->getStmtClass()) {
404 case Stmt::ConditionalOperatorClass:
405 case Stmt::SwitchStmtClass:
406 case Stmt::ForStmtClass:
407 case Stmt::CXXForRangeStmtClass:
408 case Stmt::WhileStmtClass:
409 case Stmt::DoStmtClass:
410 case Stmt::CXXCatchStmtClass:
411 Reasons |= CognitiveComplexity::Criteria::PenalizeNesting;
417 if (Node->getStmtClass() == Stmt::ConditionalOperatorClass) {
421 Location = cast<ConditionalOperator>(Node)->getQuestionLoc();
425 if (Reasons & CognitiveComplexity::Criteria::All)
426 CC.account(Location, CurrentNestingLevel, Reasons);
429 if (!(Reasons & CognitiveComplexity::Criteria::IncrementNesting))
430 return Base::TraverseStmt(Node);
432 return traverseStmtWithIncreasedNestingLevel(Node);
444 bool TraverseDecl(Decl *Node,
bool MainAnalyzedFunction =
false) {
445 if (!Node || MainAnalyzedFunction)
446 return Base::TraverseDecl(Node);
450 switch (Node->getKind()) {
452 case Decl::CXXMethod:
453 case Decl::CXXConstructor:
454 case Decl::CXXDestructor:
459 return Base::TraverseDecl(Node);
463 CC.account(Node->getBeginLoc(), CurrentNestingLevel,
464 CognitiveComplexity::Criteria::IncrementNesting);
466 return traverseDeclWithIncreasedNestingLevel(Node);
469 CognitiveComplexity CC;
477 Threshold(Options.get(
"Threshold", CognitiveComplexity::DefaultLimit)),
478 DescribeBasicIncrements(Options.get(
"DescribeBasicIncrements", true)),
479 IgnoreMacros(Options.get(
"IgnoreMacros", false)) {}
483 Options.store(Opts,
"Threshold", Threshold);
484 Options.store(Opts,
"DescribeBasicIncrements", DescribeBasicIncrements);
485 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
490 functionDecl(isDefinition(),
491 unless(anyOf(isDefaulted(), isDeleted(), isWeak())))
494 Finder->addMatcher(lambdaExpr().bind(
"lambda"),
this);
498 const MatchFinder::MatchResult &Result) {
499 FunctionASTVisitor Visitor(IgnoreMacros);
502 const auto *TheDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
503 const auto *TheLambdaExpr = Result.Nodes.getNodeAs<LambdaExpr>(
"lambda");
505 assert(TheDecl->hasBody() &&
506 "The matchers should only match the functions that "
507 "have user-provided body.");
508 Loc = TheDecl->getLocation();
509 Visitor.TraverseDecl(
const_cast<FunctionDecl *
>(TheDecl),
true);
511 Loc = TheLambdaExpr->getBeginLoc();
512 Visitor.TraverseLambdaExpr(
const_cast<LambdaExpr *
>(TheLambdaExpr));
515 if (Visitor.CC.Total <= Threshold)
519 diag(Loc,
"function %0 has cognitive complexity of %1 (threshold %2)")
520 << TheDecl << Visitor.CC.Total << Threshold;
522 diag(Loc,
"lambda has cognitive complexity of %0 (threshold %1)")
523 << Visitor.CC.Total << Threshold;
525 if (!DescribeBasicIncrements)
529 for (
const auto &Detail : Visitor.CC.Details) {
531 unsigned short Increase = 0;
532 std::tie(MsgId, Increase) = Detail.process();
533 assert(MsgId <
Msgs.size() &&
"MsgId should always be valid");
536 diag(Detail.Loc,
Msgs[MsgId], DiagnosticIDs::Note)
537 << Increase << Detail.Nesting << 1 + Detail.Nesting;
#define CurrentBinaryOperator
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
FunctionCognitiveComplexityCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
static constexpr std::array< StringRef, 4 > Msgs
llvm::StringMap< ClangTidyValue > OptionMap