1*a563ced7SChris Cotter //===--- NoSuspendWithLockCheck.cpp - clang-tidy --------------------------===//
2*a563ced7SChris Cotter //
3*a563ced7SChris Cotter // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*a563ced7SChris Cotter // See https://llvm.org/LICENSE.txt for license information.
5*a563ced7SChris Cotter // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*a563ced7SChris Cotter //
7*a563ced7SChris Cotter //===----------------------------------------------------------------------===//
8*a563ced7SChris Cotter
9*a563ced7SChris Cotter #include "NoSuspendWithLockCheck.h"
10*a563ced7SChris Cotter #include "../utils/ExprSequence.h"
11*a563ced7SChris Cotter #include "../utils/Matchers.h"
12*a563ced7SChris Cotter #include "../utils/OptionsUtils.h"
13*a563ced7SChris Cotter #include "clang/AST/ASTContext.h"
14*a563ced7SChris Cotter #include "clang/ASTMatchers/ASTMatchFinder.h"
15*a563ced7SChris Cotter #include "clang/Analysis/CFG.h"
16*a563ced7SChris Cotter
17*a563ced7SChris Cotter using namespace clang::ast_matchers;
18*a563ced7SChris Cotter
19*a563ced7SChris Cotter namespace clang::tidy::cppcoreguidelines {
20*a563ced7SChris Cotter
storeOptions(ClangTidyOptions::OptionMap & Opts)21*a563ced7SChris Cotter void NoSuspendWithLockCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
22*a563ced7SChris Cotter Options.store(Opts, "LockGuards", LockGuards);
23*a563ced7SChris Cotter }
24*a563ced7SChris Cotter
registerMatchers(MatchFinder * Finder)25*a563ced7SChris Cotter void NoSuspendWithLockCheck::registerMatchers(MatchFinder *Finder) {
26*a563ced7SChris Cotter auto LockType = elaboratedType(namesType(templateSpecializationType(
27*a563ced7SChris Cotter hasDeclaration(namedDecl(matchers::matchesAnyListedName(
28*a563ced7SChris Cotter utils::options::parseStringList(LockGuards)))))));
29*a563ced7SChris Cotter
30*a563ced7SChris Cotter StatementMatcher Lock =
31*a563ced7SChris Cotter declStmt(has(varDecl(hasType(LockType)).bind("lock-decl")))
32*a563ced7SChris Cotter .bind("lock-decl-stmt");
33*a563ced7SChris Cotter Finder->addMatcher(
34*a563ced7SChris Cotter expr(anyOf(coawaitExpr(), coyieldExpr(), dependentCoawaitExpr()),
35*a563ced7SChris Cotter forCallable(functionDecl().bind("function")),
36*a563ced7SChris Cotter unless(isInTemplateInstantiation()),
37*a563ced7SChris Cotter hasAncestor(
38*a563ced7SChris Cotter compoundStmt(has(Lock), forCallable(equalsBoundNode("function")))
39*a563ced7SChris Cotter .bind("block")))
40*a563ced7SChris Cotter .bind("suspend"),
41*a563ced7SChris Cotter this);
42*a563ced7SChris Cotter }
43*a563ced7SChris Cotter
check(const MatchFinder::MatchResult & Result)44*a563ced7SChris Cotter void NoSuspendWithLockCheck::check(const MatchFinder::MatchResult &Result) {
45*a563ced7SChris Cotter const auto *Block = Result.Nodes.getNodeAs<CompoundStmt>("block");
46*a563ced7SChris Cotter const auto *Suspend = Result.Nodes.getNodeAs<Expr>("suspend");
47*a563ced7SChris Cotter const auto *LockDecl = Result.Nodes.getNodeAs<VarDecl>("lock-decl");
48*a563ced7SChris Cotter const auto *LockStmt = Result.Nodes.getNodeAs<Stmt>("lock-decl-stmt");
49*a563ced7SChris Cotter
50*a563ced7SChris Cotter if (!Block || !Suspend || !LockDecl || !LockStmt)
51*a563ced7SChris Cotter return;
52*a563ced7SChris Cotter
53*a563ced7SChris Cotter ASTContext &Context = *Result.Context;
54*a563ced7SChris Cotter CFG::BuildOptions Options;
55*a563ced7SChris Cotter Options.AddImplicitDtors = true;
56*a563ced7SChris Cotter Options.AddTemporaryDtors = true;
57*a563ced7SChris Cotter
58*a563ced7SChris Cotter std::unique_ptr<CFG> TheCFG = CFG::buildCFG(
59*a563ced7SChris Cotter nullptr, const_cast<clang::CompoundStmt *>(Block), &Context, Options);
60*a563ced7SChris Cotter if (!TheCFG)
61*a563ced7SChris Cotter return;
62*a563ced7SChris Cotter
63*a563ced7SChris Cotter utils::ExprSequence Sequence(TheCFG.get(), Block, &Context);
64*a563ced7SChris Cotter const Stmt *LastBlockStmt = Block->body_back();
65*a563ced7SChris Cotter if (Sequence.inSequence(LockStmt, Suspend) &&
66*a563ced7SChris Cotter (Suspend == LastBlockStmt ||
67*a563ced7SChris Cotter Sequence.inSequence(Suspend, LastBlockStmt))) {
68*a563ced7SChris Cotter diag(Suspend->getBeginLoc(), "coroutine suspended with lock %0 held")
69*a563ced7SChris Cotter << LockDecl;
70*a563ced7SChris Cotter }
71*a563ced7SChris Cotter }
72*a563ced7SChris Cotter
73*a563ced7SChris Cotter } // namespace clang::tidy::cppcoreguidelines
74