[clang-tidy] Implement CppCoreGuideline CP.53
Implement CppCoreGuideline CP.53 to warn when a coroutine accepts references parameters. Although the guideline mentions that it is safe to access a reference parameter before suspension points, the guideline recommends flagging all coroutine parameter references. Reviewed By: carlosgalvezp Differential Revision: https://reviews.llvm.org/D140793
This commit is contained in:
committed by
Carlos Galvez
parent
2d9b4a50ca
commit
b06b248ad9
@@ -0,0 +1,38 @@
|
||||
//===--- AvoidReferenceCoroutineParametersCheck.cpp - clang-tidy ----------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "AvoidReferenceCoroutineParametersCheck.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace cppcoreguidelines {
|
||||
|
||||
void AvoidReferenceCoroutineParametersCheck::registerMatchers(
|
||||
MatchFinder *Finder) {
|
||||
auto IsCoroMatcher =
|
||||
hasDescendant(expr(anyOf(coyieldExpr(), coreturnStmt(), coawaitExpr())));
|
||||
Finder->addMatcher(parmVarDecl(hasType(type(referenceType())),
|
||||
hasAncestor(functionDecl(IsCoroMatcher)))
|
||||
.bind("param"),
|
||||
this);
|
||||
}
|
||||
|
||||
void AvoidReferenceCoroutineParametersCheck::check(
|
||||
const MatchFinder::MatchResult &Result) {
|
||||
if (const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param")) {
|
||||
diag(Param->getBeginLoc(), "coroutine parameters should not be references");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cppcoreguidelines
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
@@ -0,0 +1,40 @@
|
||||
//===--- AvoidReferenceCoroutineParametersCheck.h - clang-tidy --*- C++ -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDREFERENCECOROUTINEPARAMETERSCHECK_H
|
||||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDREFERENCECOROUTINEPARAMETERSCHECK_H
|
||||
|
||||
#include "../ClangTidyCheck.h"
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace cppcoreguidelines {
|
||||
|
||||
/// Warns on coroutines that accept reference parameters. Accessing a reference
|
||||
/// after a coroutine suspension point is not safe since the reference may no
|
||||
/// longer be valid. This implements CppCoreGuideline CP.53.
|
||||
///
|
||||
/// For the user-facing documentation see:
|
||||
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.html
|
||||
class AvoidReferenceCoroutineParametersCheck : public ClangTidyCheck {
|
||||
public:
|
||||
AvoidReferenceCoroutineParametersCheck(StringRef Name,
|
||||
ClangTidyContext *Context)
|
||||
: ClangTidyCheck(Name, Context) {}
|
||||
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
||||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
||||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
|
||||
return LangOpts.CPlusPlus20;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace cppcoreguidelines
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
|
||||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDREFERENCECOROUTINEPARAMETERSCHECK_H
|
||||
@@ -8,6 +8,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule
|
||||
AvoidDoWhileCheck.cpp
|
||||
AvoidGotoCheck.cpp
|
||||
AvoidNonConstGlobalVariablesCheck.cpp
|
||||
AvoidReferenceCoroutineParametersCheck.cpp
|
||||
CppCoreGuidelinesTidyModule.cpp
|
||||
InitVariablesCheck.cpp
|
||||
InterfacesGlobalInitCheck.cpp
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "AvoidDoWhileCheck.h"
|
||||
#include "AvoidGotoCheck.h"
|
||||
#include "AvoidNonConstGlobalVariablesCheck.h"
|
||||
#include "AvoidReferenceCoroutineParametersCheck.h"
|
||||
#include "InitVariablesCheck.h"
|
||||
#include "InterfacesGlobalInitCheck.h"
|
||||
#include "MacroUsageCheck.h"
|
||||
@@ -59,6 +60,8 @@ public:
|
||||
"cppcoreguidelines-avoid-magic-numbers");
|
||||
CheckFactories.registerCheck<AvoidNonConstGlobalVariablesCheck>(
|
||||
"cppcoreguidelines-avoid-non-const-global-variables");
|
||||
CheckFactories.registerCheck<AvoidReferenceCoroutineParametersCheck>(
|
||||
"cppcoreguidelines-avoid-reference-coroutine-parameters");
|
||||
CheckFactories.registerCheck<modernize::UseOverrideCheck>(
|
||||
"cppcoreguidelines-explicit-virtual-functions");
|
||||
CheckFactories.registerCheck<InitVariablesCheck>(
|
||||
|
||||
@@ -118,6 +118,11 @@ New checks
|
||||
|
||||
Warns when using ``do-while`` loops.
|
||||
|
||||
- New :doc:`cppcoreguidelines-avoid-reference-coroutine-parameters
|
||||
<clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters>` check.
|
||||
|
||||
Warns on coroutines that accept reference parameters.
|
||||
|
||||
- New :doc:`misc-use-anonymous-namespace
|
||||
<clang-tidy/checks/misc/use-anonymous-namespace>` check.
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
.. title:: clang-tidy - cppcoreguidelines-avoid-reference-coroutine-parameters
|
||||
|
||||
cppcoreguidelines-avoid-reference-coroutine-parameters
|
||||
======================================================
|
||||
|
||||
Warns when a coroutine accepts reference parameters. After a coroutine suspend point,
|
||||
references could be dangling and no longer valid. Instead, pass parameters as values.
|
||||
|
||||
Examples:
|
||||
|
||||
.. code-block:: c++
|
||||
|
||||
std::future<int> someCoroutine(int& val) {
|
||||
co_await ...;
|
||||
// When the coroutine is resumed, 'val' might no longer be valid.
|
||||
if (val) ...
|
||||
}
|
||||
|
||||
This check implements
|
||||
`CppCoreGuideline CP.53 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rcoro-reference-parameters>`_.
|
||||
@@ -182,6 +182,7 @@ Clang-Tidy Checks
|
||||
`cppcoreguidelines-avoid-do-while <cppcoreguidelines/avoid-do-while.html>`_,
|
||||
`cppcoreguidelines-avoid-goto <cppcoreguidelines/avoid-goto.html>`_,
|
||||
`cppcoreguidelines-avoid-non-const-global-variables <cppcoreguidelines/avoid-non-const-global-variables.html>`_,
|
||||
`cppcoreguidelines-avoid-reference-coroutine-parameters <cppcoreguidelines/avoid-reference-coroutine-parameters.html>`_,
|
||||
`cppcoreguidelines-init-variables <cppcoreguidelines/init-variables.html>`_, "Yes"
|
||||
`cppcoreguidelines-interfaces-global-init <cppcoreguidelines/interfaces-global-init.html>`_,
|
||||
`cppcoreguidelines-macro-usage <cppcoreguidelines/macro-usage.html>`_,
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// RUN: %check_clang_tidy -std=c++20 %s cppcoreguidelines-avoid-reference-coroutine-parameters %t
|
||||
|
||||
// NOLINTBEGIN
|
||||
namespace std {
|
||||
template <typename T, typename... Args>
|
||||
struct coroutine_traits {
|
||||
using promise_type = typename T::promise_type;
|
||||
};
|
||||
template <typename T = void>
|
||||
struct coroutine_handle;
|
||||
template <>
|
||||
struct coroutine_handle<void> {
|
||||
coroutine_handle() noexcept;
|
||||
coroutine_handle(decltype(nullptr)) noexcept;
|
||||
static constexpr coroutine_handle from_address(void*);
|
||||
};
|
||||
template <typename T>
|
||||
struct coroutine_handle {
|
||||
coroutine_handle() noexcept;
|
||||
coroutine_handle(decltype(nullptr)) noexcept;
|
||||
static constexpr coroutine_handle from_address(void*);
|
||||
operator coroutine_handle<>() const noexcept;
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
struct Awaiter {
|
||||
bool await_ready() noexcept;
|
||||
void await_suspend(std::coroutine_handle<>) noexcept;
|
||||
void await_resume() noexcept;
|
||||
};
|
||||
|
||||
struct Coro {
|
||||
struct promise_type {
|
||||
Awaiter initial_suspend();
|
||||
Awaiter final_suspend() noexcept;
|
||||
void return_void();
|
||||
Coro get_return_object();
|
||||
void unhandled_exception();
|
||||
};
|
||||
};
|
||||
// NOLINTEND
|
||||
|
||||
struct Obj {};
|
||||
|
||||
Coro no_args() {
|
||||
co_return;
|
||||
}
|
||||
|
||||
Coro no_references(int x, int* y, Obj z, const Obj w) {
|
||||
co_return;
|
||||
}
|
||||
|
||||
Coro accepts_references(int& x, const int &y) {
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
|
||||
// CHECK-MESSAGES: :[[@LINE-2]]:33: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
|
||||
co_return;
|
||||
}
|
||||
|
||||
Coro accepts_references_and_non_references(int& x, int y) {
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
|
||||
co_return;
|
||||
}
|
||||
|
||||
Coro accepts_references_to_objects(Obj& x) {
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
|
||||
co_return;
|
||||
}
|
||||
|
||||
Coro non_coro_accepts_references(int& x) {
|
||||
if (x);
|
||||
return Coro{};
|
||||
}
|
||||
|
||||
void defines_a_lambda() {
|
||||
auto NoArgs = [](int x) -> Coro { co_return; };
|
||||
|
||||
auto NoReferences = [](int x) -> Coro { co_return; };
|
||||
|
||||
auto WithReferences = [](int& x) -> Coro { co_return; };
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
|
||||
|
||||
auto WithReferences2 = [](int&) -> Coro { co_return; };
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
|
||||
}
|
||||
Reference in New Issue
Block a user