Skip to content

Commit 7685d81

Browse files
0x1eafAaronBallman
authored andcommitted
Mark implicit coroutine variables as being implicit
This prevents the clang-tidy readability-identifier-naming check from triggering on implicit __coro_gro and __promise variables in coroutines.
1 parent 900d71a commit 7685d81

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
namespace std {
4+
namespace experimental {
5+
6+
template <typename ret_t, typename... args_t>
7+
struct coroutine_traits {
8+
using promise_type = typename ret_t::promise_type;
9+
};
10+
11+
template <class promise_t>
12+
struct coroutine_handle {
13+
static constexpr coroutine_handle from_address(void *addr) noexcept { return {}; };
14+
};
15+
16+
} // namespace experimental
17+
} // namespace std
18+
19+
struct never_suspend {
20+
bool await_ready() noexcept { return false; }
21+
template <typename coro_t>
22+
void await_suspend(coro_t handle) noexcept {}
23+
void await_resume() noexcept {}
24+
};
25+
26+
struct task {
27+
struct promise_type {
28+
task get_return_object() noexcept { return {}; }
29+
never_suspend initial_suspend() noexcept { return {}; }
30+
never_suspend final_suspend() noexcept { return {}; }
31+
void return_void() {}
32+
void unhandled_exception() {}
33+
};
34+
};

clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@
8181
// RUN: {key: readability-identifier-naming.LocalPointerPrefix, value: 'l_'}, \
8282
// RUN: {key: readability-identifier-naming.LocalConstantPointerCase, value: CamelCase}, \
8383
// RUN: {key: readability-identifier-naming.LocalConstantPointerPrefix, value: 'lc_'}, \
84-
// RUN: ]}' -- -fno-delayed-template-parsing -Dbad_macro \
84+
// RUN: ]}' -- -fno-delayed-template-parsing -Dbad_macro -std=c++17 -fcoroutines-ts \
8585
// RUN: -I%S/Inputs/readability-identifier-naming \
8686
// RUN: -isystem %S/Inputs/readability-identifier-naming/system
8787

8888
// clang-format off
8989

9090
#include <system-header.h>
91+
#include <coroutines.h>
9192
#include "user-header.h"
9293
// NO warnings or fixes expected from declarations within header files without
9394
// the -header-filter= option
@@ -287,7 +288,7 @@ class COverriding : public AOverridden {
287288
// Overriding a badly-named base isn't a new violation.
288289
void BadBaseMethod() override {}
289290
// CHECK-FIXES: {{^}} void v_Bad_Base_Method() override {}
290-
291+
291292
void foo() {
292293
BadBaseMethod();
293294
// CHECK-FIXES: {{^}} v_Bad_Base_Method();
@@ -614,3 +615,14 @@ template<typename type_t>
614615
auto GetRes(type_t& Param) -> decltype(Param.res());
615616
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for parameter 'Param'
616617
// CHECK-FIXES: auto GetRes(type_t& a_param) -> decltype(a_param.res());
618+
619+
// Check implicit declarations in coroutines
620+
621+
struct async_obj {
622+
public:
623+
never_suspend operator co_await() const noexcept;
624+
};
625+
626+
task ImplicitDeclTest(async_obj &a_object) {
627+
co_await a_object; // CHECK-MESSAGES-NOT: warning: invalid case style for local variable
628+
}

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) {
544544
auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(),
545545
&PP.getIdentifierTable().get("__promise"), T,
546546
Context.getTrivialTypeSourceInfo(T, Loc), SC_None);
547+
VD->setImplicit();
547548
CheckVariableDeclarationType(VD);
548549
if (VD->isInvalidDecl())
549550
return nullptr;
@@ -1577,6 +1578,7 @@ bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
15771578
S.Context, &FD, FD.getLocation(), FD.getLocation(),
15781579
&S.PP.getIdentifierTable().get("__coro_gro"), GroType,
15791580
S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None);
1581+
GroDecl->setImplicit();
15801582

15811583
S.CheckVariableDeclarationType(GroDecl);
15821584
if (GroDecl->isInvalidDecl())

0 commit comments

Comments
 (0)