Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ static auto typeWithNameIn(const std::vector<StringRef> &Names) {
hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(Names)))));
}

static auto functionWithNameIn(const std::vector<StringRef> &Names) {
auto Call = callExpr(callee(functionDecl(hasAnyName(Names))));
return anyOf(expr(cxxBindTemporaryExpr(has(Call))), expr(Call));
}

CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
Expand All @@ -83,9 +88,8 @@ void CoroutineHostileRAIICheck::registerMatchers(MatchFinder *Finder) {
hasAttr(attr::Kind::ScopedLockable)))))
.bind("scoped-lockable");
auto OtherRAII = varDecl(typeWithNameIn(RAIITypesList)).bind("raii");
auto AllowedSuspend = awaitable(
anyOf(typeWithNameIn(AllowedAwaitablesList),
callExpr(callee(functionDecl(hasAnyName(AllowedCallees))))));
auto AllowedSuspend = awaitable(anyOf(typeWithNameIn(AllowedAwaitablesList),
functionWithNameIn(AllowedCallees)));
Finder->addMatcher(
expr(anyOf(coawaitExpr(unless(AllowedSuspend)), coyieldExpr()),
forEachPrevStmt(
Expand Down Expand Up @@ -113,9 +117,9 @@ void CoroutineHostileRAIICheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "RAIITypesList",
utils::options::serializeStringList(RAIITypesList));
Options.store(Opts, "SafeAwaitableList",
Options.store(Opts, "AllowedAwaitablesList",
utils::options::serializeStringList(AllowedAwaitablesList));
Options.store(Opts, "SafeCallees",
Options.store(Opts, "AllowedCallees",
Comment on lines +120 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be updated in the documentation as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names are already correct in the documentation -- they were just misnamed in this function, so that's why I fixed them:

https://github.com/search?q=repo%3Allvm%2Fllvm-project%20SafeAwaitableList&type=code

utils::options::serializeStringList(AllowedCallees));
}
} // namespace clang::tidy::misc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: -config="{CheckOptions: {\
// RUN: misc-coroutine-hostile-raii.RAIITypesList: 'my::Mutex; ::my::other::Mutex', \
// RUN: misc-coroutine-hostile-raii.AllowedAwaitablesList: 'safe::awaitable; ::transformable::awaitable', \
// RUN: misc-coroutine-hostile-raii.AllowedCallees: 'safe::AwaitFunc; ::safe::Obj::AwaitMethod' \
// RUN: misc-coroutine-hostile-raii.AllowedCallees: 'safe::AwaitFunc; ::safe::Obj::AwaitMethod; retExemptedAwaitable' \
// RUN: }}"

namespace std {
Expand Down Expand Up @@ -163,7 +163,10 @@ ReturnObject RAIISafeSuspendTest() {
// ================================================================================
// Safe transformable awaitable
// ================================================================================
struct transformable { struct awaitable{}; };
struct transformable {
struct awaitable{};
struct unsafe_awaitable{};
};
using alias_transformable_awaitable = transformable::awaitable;
struct UseTransformAwaitable {
struct promise_type {
Expand All @@ -172,13 +175,18 @@ struct UseTransformAwaitable {
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() {}
std::suspend_always await_transform(transformable::awaitable) { return {}; }
std::suspend_always await_transform(transformable::unsafe_awaitable) {
return {};
}
};
};

auto retAwaitable() { return transformable::awaitable{}; }
auto retExemptedAwaitable() { return transformable::unsafe_awaitable{}; }
UseTransformAwaitable RAIISafeSuspendTest2() {
absl::Mutex a;
co_await retAwaitable();
co_await retExemptedAwaitable();
co_await transformable::awaitable{};
co_await alias_transformable_awaitable{};
}
Expand Down
Loading