| 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(Options&: Opts, LocalName: "LockGuards" , Value: LockGuards); |
| 23 | } |
| 24 | |
| 25 | void NoSuspendWithLockCheck::registerMatchers(MatchFinder *Finder) { |
| 26 | auto LockType = elaboratedType(namesType(InnerMatcher: templateSpecializationType( |
| 27 | hasDeclaration(InnerMatcher: namedDecl(matchers::matchesAnyListedName( |
| 28 | NameList: utils::options::parseStringList(Option: LockGuards))))))); |
| 29 | |
| 30 | StatementMatcher Lock = |
| 31 | declStmt(has(varDecl(hasType(InnerMatcher: LockType)).bind(ID: "lock-decl" ))) |
| 32 | .bind(ID: "lock-decl-stmt" ); |
| 33 | Finder->addMatcher( |
| 34 | NodeMatch: expr(anyOf(coawaitExpr(), coyieldExpr(), dependentCoawaitExpr()), |
| 35 | forCallable(InnerMatcher: functionDecl().bind(ID: "function" )), |
| 36 | unless(isInTemplateInstantiation()), |
| 37 | hasAncestor( |
| 38 | compoundStmt(has(Lock), forCallable(InnerMatcher: equalsBoundNode(ID: "function" ))) |
| 39 | .bind(ID: "block" ))) |
| 40 | .bind(ID: "suspend" ), |
| 41 | Action: this); |
| 42 | } |
| 43 | |
| 44 | void NoSuspendWithLockCheck::check(const MatchFinder::MatchResult &Result) { |
| 45 | const auto *Block = Result.Nodes.getNodeAs<CompoundStmt>(ID: "block" ); |
| 46 | const auto *Suspend = Result.Nodes.getNodeAs<Expr>(ID: "suspend" ); |
| 47 | const auto *LockDecl = Result.Nodes.getNodeAs<VarDecl>(ID: "lock-decl" ); |
| 48 | const auto *LockStmt = Result.Nodes.getNodeAs<Stmt>(ID: "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 |
| 74 | |