| 1 | //===--- CoroutineHostileRAII.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 "CoroutineHostileRAIICheck.h" |
| 10 | #include "../utils/OptionsUtils.h" |
| 11 | #include "clang/AST/Attr.h" |
| 12 | #include "clang/AST/Decl.h" |
| 13 | #include "clang/AST/ExprCXX.h" |
| 14 | #include "clang/AST/Stmt.h" |
| 15 | #include "clang/AST/Type.h" |
| 16 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 17 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 18 | #include "clang/ASTMatchers/ASTMatchersInternal.h" |
| 19 | #include "clang/Basic/AttrKinds.h" |
| 20 | #include "clang/Basic/DiagnosticIDs.h" |
| 21 | |
| 22 | using namespace clang::ast_matchers; |
| 23 | namespace clang::tidy::misc { |
| 24 | namespace { |
| 25 | using clang::ast_matchers::internal::BoundNodesTreeBuilder; |
| 26 | |
| 27 | AST_MATCHER_P(Stmt, forEachPrevStmt, ast_matchers::internal::Matcher<Stmt>, |
| 28 | InnerMatcher) { |
| 29 | DynTypedNode P; |
| 30 | bool IsHostile = false; |
| 31 | for (const Stmt *Child = &Node; Child; Child = P.get<Stmt>()) { |
| 32 | auto Parents = Finder->getASTContext().getParents(Node: *Child); |
| 33 | if (Parents.empty()) |
| 34 | break; |
| 35 | P = *Parents.begin(); |
| 36 | auto *PCS = P.get<CompoundStmt>(); |
| 37 | if (!PCS) |
| 38 | continue; |
| 39 | for (const auto &Sibling : PCS->children()) { |
| 40 | // Child contains suspension. Siblings after Child do not persist across |
| 41 | // this suspension. |
| 42 | if (Sibling == Child) |
| 43 | break; |
| 44 | // In case of a match, add the bindings as a separate match. Also don't |
| 45 | // clear the bindings if a match is not found (unlike Matcher::matches). |
| 46 | BoundNodesTreeBuilder SiblingBuilder; |
| 47 | if (InnerMatcher.matches(Node: *Sibling, Finder, Builder: &SiblingBuilder)) { |
| 48 | Builder->addMatch(Bindings: SiblingBuilder); |
| 49 | IsHostile = true; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return IsHostile; |
| 54 | } |
| 55 | |
| 56 | // Matches the expression awaited by the `co_await`. |
| 57 | AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>, |
| 58 | InnerMatcher) { |
| 59 | if (Expr *E = Node.getOperand()) |
| 60 | return InnerMatcher.matches(Node: *E, Finder, Builder); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | auto typeWithNameIn(const std::vector<StringRef> &Names) { |
| 65 | return hasType( |
| 66 | InnerMatcher: hasCanonicalType(InnerMatcher: hasDeclaration(InnerMatcher: namedDecl(hasAnyName(Names))))); |
| 67 | } |
| 68 | } // namespace |
| 69 | |
| 70 | CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name, |
| 71 | ClangTidyContext *Context) |
| 72 | : ClangTidyCheck(Name, Context), |
| 73 | RAIITypesList(utils::options::parseStringList( |
| 74 | Option: Options.get(LocalName: "RAIITypesList" , Default: "std::lock_guard;std::scoped_lock" ))), |
| 75 | AllowedAwaitablesList(utils::options::parseStringList( |
| 76 | Option: Options.get(LocalName: "AllowedAwaitablesList" , Default: "" ))) {} |
| 77 | |
| 78 | void CoroutineHostileRAIICheck::registerMatchers(MatchFinder *Finder) { |
| 79 | // A suspension happens with co_await or co_yield. |
| 80 | auto ScopedLockable = varDecl(hasType(hasCanonicalType(hasDeclaration( |
| 81 | hasAttr(attr::Kind::ScopedLockable))))) |
| 82 | .bind("scoped-lockable" ); |
| 83 | auto OtherRAII = varDecl(typeWithNameIn(Names: RAIITypesList)).bind(ID: "raii" ); |
| 84 | auto AllowedSuspend = awaitable(InnerMatcher: typeWithNameIn(Names: AllowedAwaitablesList)); |
| 85 | Finder->addMatcher( |
| 86 | expr(anyOf(coawaitExpr(unless(AllowedSuspend)), coyieldExpr()), |
| 87 | forEachPrevStmt( |
| 88 | declStmt(forEach(varDecl(anyOf(ScopedLockable, OtherRAII)))))) |
| 89 | .bind("suspension" ), |
| 90 | this); |
| 91 | } |
| 92 | |
| 93 | void CoroutineHostileRAIICheck::check(const MatchFinder::MatchResult &Result) { |
| 94 | if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>(ID: "scoped-lockable" )) |
| 95 | diag(VD->getLocation(), |
| 96 | "%0 holds a lock across a suspension point of coroutine and could be " |
| 97 | "unlocked by a different thread" ) |
| 98 | << VD; |
| 99 | if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>(ID: "raii" )) |
| 100 | diag(VD->getLocation(), |
| 101 | "%0 persists across a suspension point of coroutine" ) |
| 102 | << VD; |
| 103 | if (const auto *Suspension = Result.Nodes.getNodeAs<Expr>(ID: "suspension" )) |
| 104 | diag(Suspension->getBeginLoc(), "suspension point is here" , |
| 105 | DiagnosticIDs::Note); |
| 106 | } |
| 107 | |
| 108 | void CoroutineHostileRAIICheck::storeOptions( |
| 109 | ClangTidyOptions::OptionMap &Opts) { |
| 110 | Options.store(Options&: Opts, LocalName: "RAIITypesList" , |
| 111 | Value: utils::options::serializeStringList(Strings: RAIITypesList)); |
| 112 | Options.store(Options&: Opts, LocalName: "SafeAwaitableList" , |
| 113 | Value: utils::options::serializeStringList(Strings: AllowedAwaitablesList)); |
| 114 | } |
| 115 | } // namespace clang::tidy::misc |
| 116 | |