| 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 |
|
| 9 |
#include "NoSuspendWithLockCheck.h" |
| 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 |
|
| 17 |
using namespace clang::ast_matchers; |
| 18 |
|
| 19 |
namespace clang::tidy::cppcoreguidelines { |
| 20 |
|
| 21 |
void NoSuspendWithLockCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 22 |
Options.store(Opts, "LockGuards", LockGuards); |
| 23 |
} |
| 24 |
|
| 25 |
void NoSuspendWithLockCheck::registerMatchers(MatchFinder *Finder) { |
| 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 |
|
| 44 |
void 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 |