126 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//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 22using namespace clang::ast_matchers;23namespace clang::tidy::misc {24namespace {25using clang::ast_matchers::internal::BoundNodesTreeBuilder;26 27AST_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(*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 across41 // this suspension.42 if (Sibling == Child)43 break;44 // In case of a match, add the bindings as a separate match. Also don't45 // 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);49 IsHostile = true;50 }51 }52 }53 return IsHostile;54}55 56// Matches the expression awaited by the `co_await`.57AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>,58 InnerMatcher) {59 if (const Expr *E = Node.getOperand())60 return InnerMatcher.matches(*E, Finder, Builder);61 return false;62}63} // namespace64 65static auto typeWithNameIn(const std::vector<StringRef> &Names) {66 return hasType(67 hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(Names)))));68}69 70static auto functionWithNameIn(const std::vector<StringRef> &Names) {71 auto Call = callExpr(callee(functionDecl(hasAnyName(Names))));72 return anyOf(expr(cxxBindTemporaryExpr(has(Call))), expr(Call));73}74 75CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name,76 ClangTidyContext *Context)77 : ClangTidyCheck(Name, Context),78 RAIITypesList(utils::options::parseStringList(79 Options.get("RAIITypesList", "std::lock_guard;std::scoped_lock"))),80 AllowedAwaitablesList(utils::options::parseStringList(81 Options.get("AllowedAwaitablesList", ""))),82 AllowedCallees(83 utils::options::parseStringList(Options.get("AllowedCallees", ""))) {}84 85void CoroutineHostileRAIICheck::registerMatchers(MatchFinder *Finder) {86 // A suspension happens with co_await or co_yield.87 auto ScopedLockable = varDecl(hasType(hasCanonicalType(hasDeclaration(88 hasAttr(attr::Kind::ScopedLockable)))))89 .bind("scoped-lockable");90 auto OtherRAII = varDecl(typeWithNameIn(RAIITypesList)).bind("raii");91 auto AllowedSuspend = awaitable(anyOf(typeWithNameIn(AllowedAwaitablesList),92 functionWithNameIn(AllowedCallees)));93 Finder->addMatcher(94 expr(anyOf(coawaitExpr(unless(AllowedSuspend)), coyieldExpr()),95 forEachPrevStmt(96 declStmt(forEach(varDecl(anyOf(ScopedLockable, OtherRAII))))))97 .bind("suspension"),98 this);99}100 101void CoroutineHostileRAIICheck::check(const MatchFinder::MatchResult &Result) {102 if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("scoped-lockable"))103 diag(VD->getLocation(),104 "%0 holds a lock across a suspension point of coroutine and could be "105 "unlocked by a different thread")106 << VD;107 if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("raii"))108 diag(VD->getLocation(),109 "%0 persists across a suspension point of coroutine")110 << VD;111 if (const auto *Suspension = Result.Nodes.getNodeAs<Expr>("suspension"))112 diag(Suspension->getBeginLoc(), "suspension point is here",113 DiagnosticIDs::Note);114}115 116void CoroutineHostileRAIICheck::storeOptions(117 ClangTidyOptions::OptionMap &Opts) {118 Options.store(Opts, "RAIITypesList",119 utils::options::serializeStringList(RAIITypesList));120 Options.store(Opts, "AllowedAwaitablesList",121 utils::options::serializeStringList(AllowedAwaitablesList));122 Options.store(Opts, "AllowedCallees",123 utils::options::serializeStringList(AllowedCallees));124}125} // namespace clang::tidy::misc126