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"
25using clang::ast_matchers::internal::BoundNodesTreeBuilder;
27AST_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()) {
46 BoundNodesTreeBuilder SiblingBuilder;
47 if (InnerMatcher.matches(*Sibling, Finder, &SiblingBuilder)) {
48 Builder->addMatch(SiblingBuilder);
57AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>,
59 if (Expr *
E =
Node.getOperand())
60 return InnerMatcher.matches(*
E, Finder,
Builder);
64auto typeWithNameIn(
const std::vector<StringRef> &Names) {
66 hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(Names)))));
73 RAIITypesList(utils::options::parseStringList(
74 Options.get(
"RAIITypesList",
"std::lock_guard;std::scoped_lock"))),
75 AllowedAwaitablesList(utils::options::parseStringList(
76 Options.get(
"AllowedAwaitablesList",
""))) {}
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))))))
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);
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
CoroutineHostileRAIICheck(llvm::StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap