From 7685d818ef329cd3f6ef121af1208be409eb59db Mon Sep 17 00:00:00 2001 From: Emma Blink Date: Wed, 16 Dec 2020 14:42:07 -0500 Subject: [PATCH] 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. --- .../system/coroutines.h | 34 ++++++++++++++++++++++ .../checkers/readability-identifier-naming.cpp | 16 ++++++++-- clang/lib/Sema/SemaCoroutine.cpp | 2 ++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/Inputs/readability-identifier-naming/system/coroutines.h diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/readability-identifier-naming/system/coroutines.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/readability-identifier-naming/system/coroutines.h new file mode 100644 index 0000000..b38da99 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/readability-identifier-naming/system/coroutines.h @@ -0,0 +1,34 @@ +#pragma once + +namespace std { +namespace experimental { + +template +struct coroutine_traits { + using promise_type = typename ret_t::promise_type; +}; + +template +struct coroutine_handle { + static constexpr coroutine_handle from_address(void *addr) noexcept { return {}; }; +}; + +} // namespace experimental +} // namespace std + +struct never_suspend { + bool await_ready() noexcept { return false; } + template + void await_suspend(coro_t handle) noexcept {} + void await_resume() noexcept {} +}; + +struct task { + struct promise_type { + task get_return_object() noexcept { return {}; } + never_suspend initial_suspend() noexcept { return {}; } + never_suspend final_suspend() noexcept { return {}; } + void return_void() {} + void unhandled_exception() {} + }; +}; diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp index 66b6009..f66202e 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming.cpp @@ -81,13 +81,14 @@ // RUN: {key: readability-identifier-naming.LocalPointerPrefix, value: 'l_'}, \ // RUN: {key: readability-identifier-naming.LocalConstantPointerCase, value: CamelCase}, \ // RUN: {key: readability-identifier-naming.LocalConstantPointerPrefix, value: 'lc_'}, \ -// RUN: ]}' -- -fno-delayed-template-parsing -Dbad_macro \ +// RUN: ]}' -- -fno-delayed-template-parsing -Dbad_macro -std=c++17 -fcoroutines-ts \ // RUN: -I%S/Inputs/readability-identifier-naming \ // RUN: -isystem %S/Inputs/readability-identifier-naming/system // clang-format off #include +#include #include "user-header.h" // NO warnings or fixes expected from declarations within header files without // the -header-filter= option @@ -287,7 +288,7 @@ public: // Overriding a badly-named base isn't a new violation. void BadBaseMethod() override {} // CHECK-FIXES: {{^}} void v_Bad_Base_Method() override {} - + void foo() { BadBaseMethod(); // CHECK-FIXES: {{^}} v_Bad_Base_Method(); @@ -614,3 +615,14 @@ template auto GetRes(type_t& Param) -> decltype(Param.res()); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for parameter 'Param' // CHECK-FIXES: auto GetRes(type_t& a_param) -> decltype(a_param.res()); + +// Check implicit declarations in coroutines + +struct async_obj { +public: + never_suspend operator co_await() const noexcept; +}; + +task ImplicitDeclTest(async_obj &a_object) { + co_await a_object; // CHECK-MESSAGES-NOT: warning: invalid case style for local variable +} diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp index 7682061..7a48bfa 100644 --- a/clang/lib/Sema/SemaCoroutine.cpp +++ b/clang/lib/Sema/SemaCoroutine.cpp @@ -544,6 +544,7 @@ VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) { auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(), &PP.getIdentifierTable().get("__promise"), T, Context.getTrivialTypeSourceInfo(T, Loc), SC_None); + VD->setImplicit(); CheckVariableDeclarationType(VD); if (VD->isInvalidDecl()) return nullptr; @@ -1577,6 +1578,7 @@ bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() { S.Context, &FD, FD.getLocation(), FD.getLocation(), &S.PP.getIdentifierTable().get("__coro_gro"), GroType, S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None); + GroDecl->setImplicit(); S.CheckVariableDeclarationType(GroDecl); if (GroDecl->isInvalidDecl()) -- 2.7.4