clang-tools 23.0.0git
FunctionCognitiveComplexityCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
25#include <array>
26#include <cassert>
27#include <optional>
28#include <stack>
29#include <tuple>
30#include <utility>
31
32using namespace clang::ast_matchers;
33
35namespace {
36
37struct CognitiveComplexity final {
38 // Any increment is based on some combination of reasons.
39 // For details you can look at the Specification at
40 // https://www.sonarsource.com/docs/CognitiveComplexity.pdf
41 // or user-facing docs at
42 // https://clang.llvm.org/extra/clang-tidy/checks/readability/function-cognitive-complexity.html
43 // Here are all the possible reasons:
44 enum Criteria : uint8_t {
45 None = 0U,
46
47 // B1, increases cognitive complexity (by 1)
48 // What causes it:
49 // * if, else if, else, ConditionalOperator (not BinaryConditionalOperator)
50 // * SwitchStmt
51 // * ForStmt, CXXForRangeStmt
52 // * WhileStmt, DoStmt
53 // * CXXCatchStmt
54 // * GotoStmt, IndirectGotoStmt (but not BreakStmt, ContinueStmt)
55 // * sequences of binary logical operators (BinOpLAnd, BinOpLOr)
56 // * each method in a recursion cycle (not implemented)
57 Increment = 1U << 0,
58
59 // B2, increases current nesting level (by 1)
60 // What causes it:
61 // * if, else if, else, ConditionalOperator (not BinaryConditionalOperator)
62 // * SwitchStmt
63 // * ForStmt, CXXForRangeStmt
64 // * WhileStmt, DoStmt
65 // * CXXCatchStmt
66 // * nested CXXConstructor, CXXDestructor, CXXMethod (incl. C++11 Lambda)
67 // * GNU Statement Expression
68 // * Apple Block declaration
69 IncrementNesting = 1U << 1,
70
71 // B3, increases cognitive complexity by the current nesting level
72 // Applied before IncrementNesting
73 // What causes it:
74 // * IfStmt, ConditionalOperator (not BinaryConditionalOperator)
75 // * SwitchStmt
76 // * ForStmt, CXXForRangeStmt
77 // * WhileStmt, DoStmt
78 // * CXXCatchStmt
79 PenalizeNesting = 1U << 2,
80
81 All = Increment | PenalizeNesting | IncrementNesting,
82
83 LLVM_MARK_AS_BITMASK_ENUM(PenalizeNesting),
84 };
85
86 // The helper struct used to record one increment occurrence, with all the
87 // details necessary.
88 struct Detail {
89 const SourceLocation Loc; // What caused the increment?
90 const unsigned short Nesting; // How deeply nested is Loc located?
91 const Criteria C; // The criteria of the increment
92
93 Detail(SourceLocation SLoc, unsigned short CurrentNesting, Criteria Crit)
94 : Loc(SLoc), Nesting(CurrentNesting), C(Crit) {}
95
96 // To minimize the sizeof(Detail), we only store the minimal info there.
97 // This function is used to convert from the stored info into the usable
98 // information - what message to output, how much of an increment did this
99 // occurrence actually result in.
100 std::pair<unsigned, unsigned short> process() const {
101 assert(C != Criteria::None && "invalid criteria");
102
103 unsigned MsgId = 0; // The id of the message to output.
104 unsigned short Increment = 0; // How much of an increment?
105
106 if (C == Criteria::All) {
107 Increment = 1 + Nesting;
108 MsgId = 0;
109 } else if (C == (Criteria::Increment | Criteria::IncrementNesting)) {
110 Increment = 1;
111 MsgId = 1;
112 } else if (C == Criteria::Increment) {
113 Increment = 1;
114 MsgId = 2;
115 } else if (C == Criteria::IncrementNesting) {
116 Increment = 0; // Unused in this message.
117 MsgId = 3;
118 } else {
119 llvm_unreachable("should not get to here.");
120 }
121
122 return {MsgId, Increment};
123 }
124 };
125
126 // Limit of 25 is the "upstream"'s default.
127 static constexpr unsigned DefaultLimit = 25U;
128
129 // Based on the publicly-available numbers for some big open-source projects
130 // https://sonarcloud.io/projects?languages=c%2Ccpp&size=5 we can estimate:
131 // value ~20 would result in no allocs for 98% of functions, ~12 for 96%, ~10
132 // for 91%, ~8 for 88%, ~6 for 84%, ~4 for 77%, ~2 for 64%, and ~1 for 37%.
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; // 25 elements is 200 bytes.
139 // Yes, 25 is a magic number. This is the seemingly-sane default for the
140 // upper limit for function cognitive complexity. Thus it would make sense
141 // to avoid allocations for any function that does not violate the limit.
142
143 // The grand total Cognitive Complexity of the function.
144 unsigned Total = 0;
145
146 // The function used to store new increment, calculate the total complexity.
147 void account(SourceLocation Loc, unsigned short Nesting, Criteria C);
148};
149
150} // namespace
151
152// All the possible messages that can be output. The choice of the message
153// to use is based of the combination of the CognitiveComplexity::Criteria.
154// It would be nice to have it in CognitiveComplexity struct, but then it is
155// not static.
156static constexpr std::array<StringRef, 4> Msgs = {{
157 // B1 + B2 + B3
158 "+%0, including nesting penalty of %1, nesting level increased to %2",
159
160 // B1 + B2
161 "+%0, nesting level increased to %2",
162
163 // B1
164 "+%0",
165
166 // B2
167 "nesting level increased to %2",
168}};
169
170void CognitiveComplexity::account(SourceLocation Loc, unsigned short Nesting,
171 Criteria C) {
172 C &= Criteria::All;
173 assert(C != Criteria::None && "invalid criteria");
174
175 Details.emplace_back(Loc, Nesting, C);
176 const Detail &D = Details.back();
177
178 unsigned MsgId = 0;
179 unsigned short Increase = 0;
180 std::tie(MsgId, Increase) = D.process();
181
182 Total += Increase;
183}
184
185namespace {
186
187class FunctionASTVisitor final
188 : public RecursiveASTVisitor<FunctionASTVisitor> {
189 using Base = RecursiveASTVisitor<FunctionASTVisitor>;
190
191 // If set to true, macros are ignored during analysis.
192 const bool IgnoreMacros;
193
194 // The current nesting level (increased by Criteria::IncrementNesting).
195 unsigned short CurrentNestingLevel = 0;
196
197 // Used to efficiently know the last type of the binary sequence operator
198 // that was encountered. It would make sense for the function call to start
199 // the new sequence, thus it is a stack.
200 using OBO = std::optional<BinaryOperator::Opcode>;
201 std::stack<OBO, SmallVector<OBO, 4>> BinaryOperatorsStack;
202
203public:
204 explicit FunctionASTVisitor(const bool IgnoreMacros)
205 : IgnoreMacros(IgnoreMacros) {}
206
207 bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) {
208 ++CurrentNestingLevel;
209 const bool ShouldContinue = Base::TraverseStmt(Node);
210 --CurrentNestingLevel;
211 return ShouldContinue;
212 }
213
214 bool traverseDeclWithIncreasedNestingLevel(Decl *Node) {
215 ++CurrentNestingLevel;
216 const bool ShouldContinue = Base::TraverseDecl(Node);
217 --CurrentNestingLevel;
218 return ShouldContinue;
219 }
220
221 bool TraverseIfStmt(IfStmt *Node, bool InElseIf = false) {
222 if (!Node)
223 return Base::TraverseIfStmt(Node);
224
225 {
226 CognitiveComplexity::Criteria Reasons =
227 CognitiveComplexity::Criteria::None;
228
229 // "If" increases cognitive complexity.
230 Reasons |= CognitiveComplexity::Criteria::Increment;
231 // "If" increases nesting level.
232 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
233
234 if (!InElseIf) {
235 // "If" receives a nesting increment commensurate with it's nested
236 // depth, if it is not part of "else if".
237 Reasons |= CognitiveComplexity::Criteria::PenalizeNesting;
238 }
239
240 CC.account(Node->getIfLoc(), CurrentNestingLevel, Reasons);
241 }
242
243 // If this IfStmt is *NOT* "else if", then only the body (i.e. "Then" and
244 // "Else") is traversed with increased Nesting level.
245 // However if this IfStmt *IS* "else if", then Nesting level is increased
246 // for the whole IfStmt (i.e. for "Init", "Cond", "Then" and "Else").
247
248 if (!InElseIf) {
249 if (!TraverseStmt(Node->getInit()))
250 return false;
251
252 if (!TraverseStmt(Node->getCond()))
253 return false;
254 } else {
255 if (!traverseStmtWithIncreasedNestingLevel(Node->getInit()))
256 return false;
257
258 if (!traverseStmtWithIncreasedNestingLevel(Node->getCond()))
259 return false;
260 }
261
262 // "Then" always increases nesting level.
263 if (!traverseStmtWithIncreasedNestingLevel(Node->getThen()))
264 return false;
265
266 if (!Node->getElse())
267 return true;
268
269 if (auto *E = dyn_cast<IfStmt>(Node->getElse()))
270 return TraverseIfStmt(E, true);
271
272 {
273 CognitiveComplexity::Criteria Reasons =
274 CognitiveComplexity::Criteria::None;
275
276 // "Else" increases cognitive complexity.
277 Reasons |= CognitiveComplexity::Criteria::Increment;
278 // "Else" increases nesting level.
279 Reasons |= CognitiveComplexity::Criteria::IncrementNesting;
280 // "Else" DOES NOT receive a nesting increment commensurate with it's
281 // nested depth.
282
283 CC.account(Node->getElseLoc(), CurrentNestingLevel, Reasons);
284 }
285
286 // "Else" always increases nesting level.
287 return traverseStmtWithIncreasedNestingLevel(Node->getElse());
288 }
289
290// The currently-being-processed stack entry, which is always the top.
291#define CurrentBinaryOperator BinaryOperatorsStack.top()
292
293 // In a sequence of binary logical operators, if the new operator is different
294 // from the previous one, then the cognitive complexity is increased.
295 bool TraverseBinaryOperator(BinaryOperator *Op) {
296 if (!Op || !Op->isLogicalOp())
297 return Base::TraverseBinaryOperator(Op);
298
299 // Make sure that there is always at least one frame in the stack.
300 if (BinaryOperatorsStack.empty())
301 BinaryOperatorsStack.emplace();
302
303 // If this is the first binary operator that we are processing, or the
304 // previous binary operator was different, there is an increment.
305 if (!CurrentBinaryOperator || Op->getOpcode() != CurrentBinaryOperator)
306 CC.account(Op->getOperatorLoc(), CurrentNestingLevel,
307 CognitiveComplexity::Criteria::Increment);
308
309 // We might encounter a function call, which starts a new sequence, thus
310 // we need to save the current previous binary operator.
311 const std::optional<BinaryOperator::Opcode> BinOpCopy(
313
314 // Record the operator that we are currently processing and traverse it.
315 CurrentBinaryOperator = Op->getOpcode();
316 const bool ShouldContinue = Base::TraverseBinaryOperator(Op);
317
318 // And restore the previous binary operator, which might be nonexistent.
319 CurrentBinaryOperator = BinOpCopy;
320
321 return ShouldContinue;
322 }
323
324 // It would make sense for the function call to start the new binary
325 // operator sequence, thus let's make sure that it creates a new stack frame.
326 bool TraverseCallExpr(CallExpr *Node) {
327 // If we are not currently processing any binary operator sequence, then
328 // no Node-handling is needed.
329 if (!Node || BinaryOperatorsStack.empty() || !CurrentBinaryOperator)
330 return Base::TraverseCallExpr(Node);
331
332 // Else, do add [uninitialized] frame to the stack, and traverse call.
333 BinaryOperatorsStack.emplace();
334 const bool ShouldContinue = Base::TraverseCallExpr(Node);
335 // And remove the top frame.
336 BinaryOperatorsStack.pop();
337
338 return ShouldContinue;
339 }
340
341#undef CurrentBinaryOperator
342
343 bool TraverseStmt(Stmt *Node) {
344 if (!Node)
345 return Base::TraverseStmt(Node);
346
347 if (IgnoreMacros && Node->getBeginLoc().isMacroID())
348 return true;
349
350 // Three following switch()'es have huge duplication, but it is better to
351 // keep them separate, to simplify comparing them with the Specification.
352
353 CognitiveComplexity::Criteria Reasons = CognitiveComplexity::Criteria::None;
354 SourceLocation Location = Node->getBeginLoc();
355
356 // B1. Increments
357 // There is an increment for each of the following:
358 switch (Node->getStmtClass()) {
359 // if, else if, else are handled in TraverseIfStmt(),
360 // FIXME: "each method in a recursion cycle" Increment is not implemented.
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;
371 break;
372 default:
373 // break LABEL, continue LABEL increase cognitive complexity,
374 // but they are not supported in C++ or C.
375 // Regular break/continue do not increase cognitive complexity.
376 break;
377 }
378
379 // B2. Nesting level
380 // The following structures increment the nesting level:
381 switch (Node->getStmtClass()) {
382 // if, else if, else are handled in TraverseIfStmt(),
383 // Nested methods and such are handled in TraverseDecl.
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;
394 break;
395 default:
396 break;
397 }
398
399 // B3. Nesting increments
400 // The following structures receive a nesting increment
401 // commensurate with their nested depth inside B2 structures:
402 switch (Node->getStmtClass()) {
403 // if, else if, else are handled in TraverseIfStmt().
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;
412 break;
413 default:
414 break;
415 }
416
417 if (Node->getStmtClass() == Stmt::ConditionalOperatorClass) {
418 // A little beautification.
419 // For conditional operator "cond ? true : false" point at the "?"
420 // symbol.
421 Location = cast<ConditionalOperator>(Node)->getQuestionLoc();
422 }
423
424 // If we have found any reasons, let's account it.
425 if (Reasons & CognitiveComplexity::Criteria::All)
426 CC.account(Location, CurrentNestingLevel, Reasons);
427
428 // Did we decide that the nesting level should be increased?
429 if (!(Reasons & CognitiveComplexity::Criteria::IncrementNesting))
430 return Base::TraverseStmt(Node);
431
432 return traverseStmtWithIncreasedNestingLevel(Node);
433 }
434
435 // The parameter MainAnalyzedFunction is needed to differentiate between the
436 // cases where TraverseDecl() is the entry point from
437 // FunctionCognitiveComplexityCheck::check() and the cases where it was called
438 // from the FunctionASTVisitor itself. Explanation: if we get a function
439 // definition (e.g. constructor, destructor, method), the Cognitive Complexity
440 // specification states that the Nesting level shall be increased. But if this
441 // function is the entry point, then the Nesting level should not be
442 // increased. Thus that parameter is there and is used to fall-through
443 // directly to traversing if this is the main function that is being analyzed.
444 bool TraverseDecl(Decl *Node, bool MainAnalyzedFunction = false) {
445 if (!Node || MainAnalyzedFunction)
446 return Base::TraverseDecl(Node);
447
448 // B2. Nesting level
449 // The following structures increment the nesting level:
450 switch (Node->getKind()) {
451 case Decl::Function:
452 case Decl::CXXMethod:
453 case Decl::CXXConstructor:
454 case Decl::CXXDestructor:
455 case Decl::Block:
456 break;
457 default:
458 // If this is something else, we use early return!
459 return Base::TraverseDecl(Node);
460 break;
461 }
462
463 CC.account(Node->getBeginLoc(), CurrentNestingLevel,
464 CognitiveComplexity::Criteria::IncrementNesting);
465
466 return traverseDeclWithIncreasedNestingLevel(Node);
467 }
468
469 CognitiveComplexity CC;
470};
471
472} // namespace
473
475 StringRef Name, ClangTidyContext *Context)
476 : ClangTidyCheck(Name, Context),
477 Threshold(Options.get("Threshold", CognitiveComplexity::DefaultLimit)),
478 DescribeBasicIncrements(Options.get("DescribeBasicIncrements", true)),
479 IgnoreMacros(Options.get("IgnoreMacros", false)) {}
480
483 Options.store(Opts, "Threshold", Threshold);
484 Options.store(Opts, "DescribeBasicIncrements", DescribeBasicIncrements);
485 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
486}
487
489 Finder->addMatcher(
490 functionDecl(isDefinition(),
491 unless(anyOf(isDefaulted(), isDeleted(), isWeak())))
492 .bind("func"),
493 this);
494 Finder->addMatcher(lambdaExpr().bind("lambda"), this);
495}
496
498 const MatchFinder::MatchResult &Result) {
499 FunctionASTVisitor Visitor(IgnoreMacros);
500 SourceLocation Loc;
501
502 const auto *TheDecl = Result.Nodes.getNodeAs<FunctionDecl>("func");
503 const auto *TheLambdaExpr = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
504 if (TheDecl) {
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);
510 } else {
511 Loc = TheLambdaExpr->getBeginLoc();
512 Visitor.TraverseLambdaExpr(const_cast<LambdaExpr *>(TheLambdaExpr));
513 }
514
515 if (Visitor.CC.Total <= Threshold)
516 return;
517
518 if (TheDecl)
519 diag(Loc, "function %0 has cognitive complexity of %1 (threshold %2)")
520 << TheDecl << Visitor.CC.Total << Threshold;
521 else
522 diag(Loc, "lambda has cognitive complexity of %0 (threshold %1)")
523 << Visitor.CC.Total << Threshold;
524
525 if (!DescribeBasicIncrements)
526 return;
527
528 // Output all the basic increments of complexity.
529 for (const auto &Detail : Visitor.CC.Details) {
530 unsigned MsgId = 0; // The id of the message to output.
531 unsigned short Increase = 0; // How much of an increment?
532 std::tie(MsgId, Increase) = Detail.process();
533 assert(MsgId < Msgs.size() && "MsgId should always be valid");
534 // Increase, on the other hand, can be 0.
535
536 diag(Detail.Loc, Msgs[MsgId], DiagnosticIDs::Note)
537 << Increase << Detail.Nesting << 1 + Detail.Nesting;
538 }
539}
540
541} // namespace clang::tidy::readability
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static constexpr std::array< StringRef, 4 > Msgs
llvm::StringMap< ClangTidyValue > OptionMap