1 //===--- CoroutineHostileRAII.cpp - clang-tidy ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
22 using namespace clang::ast_matchers
;
23 namespace clang::tidy::misc
{
25 using clang::ast_matchers::internal::BoundNodesTreeBuilder
;
27 AST_MATCHER_P(Stmt
, forEachPrevStmt
, ast_matchers::internal::Matcher
<Stmt
>,
30 bool IsHostile
= false;
31 for (const Stmt
*Child
= &Node
; Child
; Child
= P
.get
<Stmt
>()) {
32 auto Parents
= Finder
->getASTContext().getParents(*Child
);
36 auto *PCS
= P
.get
<CompoundStmt
>();
39 for (const auto &Sibling
: PCS
->children()) {
40 // Child contains suspension. Siblings after Child do not persist across
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(*Sibling
, Finder
, &SiblingBuilder
)) {
48 Builder
->addMatch(SiblingBuilder
);
56 // Matches the expression awaited by the `co_await`.
57 AST_MATCHER_P(CoawaitExpr
, awaitable
, ast_matchers::internal::Matcher
<Expr
>,
59 if (Expr
*E
= Node
.getOperand())
60 return InnerMatcher
.matches(*E
, Finder
, Builder
);
64 auto typeWithNameIn(const std::vector
<StringRef
> &Names
) {
66 hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(Names
)))));
70 CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name
,
71 ClangTidyContext
*Context
)
72 : ClangTidyCheck(Name
, Context
),
73 RAIITypesList(utils::options::parseStringList(
74 Options
.get("RAIITypesList", "std::lock_guard;std::scoped_lock"))),
75 AllowedAwaitablesList(utils::options::parseStringList(
76 Options
.get("AllowedAwaitablesList", ""))) {}
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(RAIITypesList
)).bind("raii");
84 auto AllowedSuspend
= awaitable(typeWithNameIn(AllowedAwaitablesList
));
86 expr(anyOf(coawaitExpr(unless(AllowedSuspend
)), coyieldExpr()),
88 declStmt(forEach(varDecl(anyOf(ScopedLockable
, OtherRAII
))))))
93 void CoroutineHostileRAIICheck::check(const MatchFinder::MatchResult
&Result
) {
94 if (const auto *VD
= Result
.Nodes
.getNodeAs
<VarDecl
>("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")
99 if (const auto *VD
= Result
.Nodes
.getNodeAs
<VarDecl
>("raii"))
100 diag(VD
->getLocation(),
101 "%0 persists across a suspension point of coroutine")
103 if (const auto *Suspension
= Result
.Nodes
.getNodeAs
<Expr
>("suspension"))
104 diag(Suspension
->getBeginLoc(), "suspension point is here",
105 DiagnosticIDs::Note
);
108 void CoroutineHostileRAIICheck::storeOptions(
109 ClangTidyOptions::OptionMap
&Opts
) {
110 Options
.store(Opts
, "RAIITypesList",
111 utils::options::serializeStringList(RAIITypesList
));
112 Options
.store(Opts
, "SafeAwaitableList",
113 utils::options::serializeStringList(AllowedAwaitablesList
));
115 } // namespace clang::tidy::misc