-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Fix bugs in misc-coroutine-hostile-raii check #167947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-clang-tidy Author: None (higher-performance) Changes
Full diff: https://github.com/llvm/llvm-project/pull/167947.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp b/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
index a7b74944690b4..c7001d4eebfed 100644
--- a/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
@@ -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),
@@ -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(
@@ -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",
utils::options::serializeStringList(AllowedCallees));
}
} // namespace clang::tidy::misc
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp
index ec6ddec56e1f2..dff73aeb7a5ee 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp
@@ -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 {
@@ -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 {
@@ -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{};
}
|
|
@llvm/pr-subscribers-clang-tools-extra Author: None (higher-performance) Changes
Full diff: https://github.com/llvm/llvm-project/pull/167947.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp b/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
index a7b74944690b4..c7001d4eebfed 100644
--- a/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp
@@ -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),
@@ -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(
@@ -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",
utils::options::serializeStringList(AllowedCallees));
}
} // namespace clang::tidy::misc
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp
index ec6ddec56e1f2..dff73aeb7a5ee 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp
@@ -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 {
@@ -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 {
@@ -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{};
}
|
|
✅ With the latest revision this PR passed the C/C++ code linter. |
9d1c14f to
8b0ab09
Compare
Handle transformed awaitables for
AllowedCallees, which generate temporaries and weren't being handled by Add the ability to exempt callees from the misc-coroutine-hostile-raii clang-tidy check #167778.Fix name mismatches in
storeOptions.