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
10 changes: 8 additions & 2 deletions clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name,
RAIITypesList(utils::options::parseStringList(
Options.get("RAIITypesList", "std::lock_guard;std::scoped_lock"))),
AllowedAwaitablesList(utils::options::parseStringList(
Options.get("AllowedAwaitablesList", ""))) {}
Options.get("AllowedAwaitablesList", ""))),
AllowedCallees(
utils::options::parseStringList(Options.get("AllowedCallees", ""))) {}

void CoroutineHostileRAIICheck::registerMatchers(MatchFinder *Finder) {
// A suspension happens with co_await or co_yield.
auto ScopedLockable = varDecl(hasType(hasCanonicalType(hasDeclaration(
hasAttr(attr::Kind::ScopedLockable)))))
.bind("scoped-lockable");
auto OtherRAII = varDecl(typeWithNameIn(RAIITypesList)).bind("raii");
auto AllowedSuspend = awaitable(typeWithNameIn(AllowedAwaitablesList));
auto AllowedSuspend = awaitable(
anyOf(typeWithNameIn(AllowedAwaitablesList),
callExpr(callee(functionDecl(hasAnyName(AllowedCallees))))));
Finder->addMatcher(
expr(anyOf(coawaitExpr(unless(AllowedSuspend)), coyieldExpr()),
forEachPrevStmt(
Expand Down Expand Up @@ -111,5 +115,7 @@ void CoroutineHostileRAIICheck::storeOptions(
utils::options::serializeStringList(RAIITypesList));
Options.store(Opts, "SafeAwaitableList",
utils::options::serializeStringList(AllowedAwaitablesList));
Options.store(Opts, "SafeCallees",
utils::options::serializeStringList(AllowedCallees));
}
} // namespace clang::tidy::misc
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class CoroutineHostileRAIICheck : public ClangTidyCheck {
// List of fully qualified awaitable types which are considered safe to
// co_await.
std::vector<StringRef> AllowedAwaitablesList;
// List of callees whose return values are considered safe to directly
// co_await.
std::vector<StringRef> AllowedCallees;
};

} // namespace clang::tidy::misc
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ Changes in existing checks
positives on return of non-const pointer and fix false positives on
pointer-to-member operator.

- Improved :doc:`misc-coroutine-hostile-raii
<clang-tidy/checks/misc/coroutine-hostile-raii>` check by adding the option
`AllowedCallees`, that allows exempting safely awaitable callees from the
check.

- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,23 @@ Options
Eg: `my::safe::awaitable;other::awaitable`
Default is an empty string.

.. option:: AllowedCallees

A semicolon-separated list of callee function names which can
be safely awaited while having hostile RAII objects in scope.
Example usage:

.. code-block:: c++

// Consider option AllowedCallees = "noop"
task noop() { co_return; }

task coro() {
// This persists across the co_await but is not flagged
// because the awaitable is considered safe to await on.
const std::lock_guard l(&mu_);
co_await noop();
}

Eg: `my::safe::await;other::await`
Default is an empty string.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %check_clang_tidy -std=c++20 %s misc-coroutine-hostile-raii %t \
// 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.AllowedAwaitablesList: 'safe::awaitable; ::transformable::awaitable', \
// RUN: misc-coroutine-hostile-raii.AllowedCallees: 'safe::AwaitFunc; ::safe::Obj::AwaitMethod' \
// RUN: }}"

namespace std {
Expand Down Expand Up @@ -145,12 +146,18 @@ namespace safe {
void await_suspend(std::coroutine_handle<>) noexcept {}
void await_resume() noexcept {}
};
std::suspend_always AwaitFunc();
struct Obj {
std::suspend_always AwaitMethod();
};
} // namespace safe
ReturnObject RAIISafeSuspendTest() {
absl::Mutex a;
co_await safe::awaitable{};
using other = safe::awaitable;
co_await other{};
co_await safe::AwaitFunc();
co_await safe::Obj().AwaitMethod();
}

// ================================================================================
Expand Down
Loading