clang-tools 20.0.0git
NoSuspendWithLockCheck.cpp
Go to the documentation of this file.
1//===--- NoSuspendWithLockCheck.cpp - clang-tidy --------------------------===//
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
10#include "../utils/ExprSequence.h"
11#include "../utils/Matchers.h"
12#include "../utils/OptionsUtils.h"
13#include "clang/AST/ASTContext.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/Analysis/CFG.h"
16
17using namespace clang::ast_matchers;
18
20
22 Options.store(Opts, "LockGuards", LockGuards);
23}
24
26 auto LockType = elaboratedType(namesType(templateSpecializationType(
27 hasDeclaration(namedDecl(matchers::matchesAnyListedName(
28 utils::options::parseStringList(LockGuards)))))));
29
30 StatementMatcher Lock =
31 declStmt(has(varDecl(hasType(LockType)).bind("lock-decl")))
32 .bind("lock-decl-stmt");
33 Finder->addMatcher(
34 expr(anyOf(coawaitExpr(), coyieldExpr(), dependentCoawaitExpr()),
35 forCallable(functionDecl().bind("function")),
36 unless(isInTemplateInstantiation()),
37 hasAncestor(
38 compoundStmt(has(Lock), forCallable(equalsBoundNode("function")))
39 .bind("block")))
40 .bind("suspend"),
41 this);
42}
43
44void NoSuspendWithLockCheck::check(const MatchFinder::MatchResult &Result) {
45 const auto *Block = Result.Nodes.getNodeAs<CompoundStmt>("block");
46 const auto *Suspend = Result.Nodes.getNodeAs<Expr>("suspend");
47 const auto *LockDecl = Result.Nodes.getNodeAs<VarDecl>("lock-decl");
48 const auto *LockStmt = Result.Nodes.getNodeAs<Stmt>("lock-decl-stmt");
49
50 if (!Block || !Suspend || !LockDecl || !LockStmt)
51 return;
52
53 ASTContext &Context = *Result.Context;
54 CFG::BuildOptions Options;
55 Options.AddImplicitDtors = true;
56 Options.AddTemporaryDtors = true;
57
58 std::unique_ptr<CFG> TheCFG = CFG::buildCFG(
59 nullptr, const_cast<clang::CompoundStmt *>(Block), &Context, Options);
60 if (!TheCFG)
61 return;
62
63 utils::ExprSequence Sequence(TheCFG.get(), Block, &Context);
64 const Stmt *LastBlockStmt = Block->body_back();
65 if (Sequence.inSequence(LockStmt, Suspend) &&
66 (Suspend == LastBlockStmt ||
67 Sequence.inSequence(Suspend, LastBlockStmt))) {
68 diag(Suspend->getBeginLoc(), "coroutine suspended with lock %0 held")
69 << LockDecl;
70 }
71}
72
73} // namespace clang::tidy::cppcoreguidelines
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Provides information about the evaluation order of (sub-)expressions within a CFGBlock.
Definition: ExprSequence.h:66
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::vector< StringRef > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
llvm::StringMap< ClangTidyValue > OptionMap